BenchCookie  0.0.1
A simple, intuitive open source language.
x86_compiler.cpp
Go to the documentation of this file.
1 // allocate enough space for drew-sized strings.
2 #ifndef X86_COMPILER_SOURCE
3 #define X86_COMPILER_SOURCE
4 #define MAX_STRING_SIZE 100
5 void Command::doBSS(std::ostream & file)
6 {
7  file<<"\t.bss"<<std::endl<<std::endl;
8  for (int x = 0; x < m_string_vars.size(); ++x) {
9  file<<"\t.lcomm IS"<<x<<", "<<MAX_STRING_SIZE<<std::endl;
10  }
11 
14  file<<"\t.lcomm locals, 400";
15  file<<std::endl;
16 }
17 
18 void Command::doData(std::ostream & file)
19 {
20  file<<"\t.data"<<std::endl<<std::endl;
21 
24  for (int x = 0; x < m_literals.size(); ++x) {
25  file<<"S"<<x<<":\t.string \""<<m_literals[x]<<"\\0\""<<std::endl;
26  }
27 
28  std::cout<<std::endl;
29 
32  for (int x = 0; x < m_int_vars.size() - m_int_declarations.size(); ++x) {
33  file<<"I"<<x<<":\t.word 0"<<std::endl;
34  }
35 
36  file<<std::endl<<".string_fmt:\t.string \"%s\\0\""<<std::endl;
37  file<<".num_fmt:\t.string \"%d\\0\""<<std::endl;
38  file<<".TRUE:\t.string \"true\\0\""<<std::endl;
39  file<<".FALSE:\t.string \"false\\0\""<<std::endl;
40 }
41 #define INTGR 67
42 #define STRNG 68
43 
44 void Command::evaluate_expression(std::ostream & file)
45 {
46  static ssize_t nszcount = 0;
47  static ssize_t divcount = 0;
48  static ssize_t exprdex = 0;
49 
50  unsigned int t1, t2;
51 
53  std::stack<math_expression> * expr = m_evaluations[exprdex++];
54  std::stack<unsigned short> arg_types;
55  ssize_t stack_depth = 0;
56 
57  std::stack<math_expression> eval;// = *expr;
58  for(; !expr->empty(); eval.push(expr->top()), expr->pop(), 1);
59  std::stack<math_expression> curr;
60 
61  for (; eval.size();) {
62  math_expression a = eval.top(); eval.pop();
63  Command::exp_type type = static_cast<Command::exp_type>(a.expr_type);
64  exp_type aExpType = static_cast<Command::exp_type>(a.expr_type);
65  if (aExpType == VAR) {
66  push_variable(a.pirate_name, arg_types, file);
67  stack_depth += 4;
68  continue;
69  } else if (aExpType == AN_INT) {
70  file<<"\tpush "<<a.int_arg<<std::endl;
71  stack_depth += 4;
72  arg_types.push(INTGR);
73  continue;
74  } else if (aExpType == LITERAL) {
75  arg_types.push(STRNG);
76  ssize_t idx = m_exp_literals.front();
77  m_exp_literals.pop();
78  file<<"\tpush S"<<idx<<std::endl;
79  // file<<"\tpush {%r1}"<<std::endl;
80  stack_depth += 4;
81  continue;
82  } else if (aExpType == PTRDEREF) {
83  file<<"\tpop ebx"<<std::endl;
84  file<<"\tmov ebx, [ebx]"<<std::endl;
85  file<<"\tpush ebx"<<std::endl;
86  continue;
87  } else if (aExpType == LOGNOT) {
88  file<<"\tpop %{r1}"<<std::endl;
89  file<<"\tmov %r0, $1"<<std::endl;
90  file<<"\tcmp %r1, $0"<<std::endl;
91  file<<"\tbeq NSZ"<<nszcount<<std::endl;
92  file<<"\tmov %r0, $0"<<nszcount<<std::endl;
93  file<<"NSZ"<<nszcount<<":"<<std::endl;
94  file<<"\tpush {%r0}"<<std::endl;
95  ++nszcount;
96  continue;
97  }
98 
99  if (stack_depth >= 8) {
100  file<<"\tpop {%r1, %r2}"<<std::endl;
101  stack_depth -= 8;
102  } else continue;
103  switch (type) {
104  case ADD:
110  file<<"\tadd %r0, %r1, %r2"<<std::endl;
111  goto do_default;
112  case SUB:
113  file<<"\tsub %r0, %r2, %r1"<<std::endl;
114  goto do_default;
115  case MUL:
116  file<<"\tmul %r0, %r1, %r2"<<std::endl;
117  goto do_default;
118  case DIV:
119  /* @todo case divide by zero */
120  file<<"\tmov %r0, $0"<<std::endl<<std::endl;
121  file<<"\tcmp %r1, $0"<<std::endl;
122  file<<"\tmov %r5, $1"<<std::endl;
123  file<<"\tbge NLZ1"<<divcount<<std::endl;
124  file<<"\tmov %r3, #-1"<<std::endl;
125  file<<"\tmul %r5, %r5, %r3"<<std::endl;
126  file<<"\tmul %r1, %r1, %r3"<<std::endl;
127  file<<"NLZ1"<<divcount<<":"<<std::endl;
128  file<<"\tcmp %r2, $0"<<std::endl;
129  file<<"\tbge NLZ2"<<divcount<<std::endl;
130  file<<"\tmov %r3, #-1"<<std::endl;
131  file<<"\tmul %r5, %r3, %r5"<<std::endl;
132  file<<"\tmul %r2, %r3, %r2"<<std::endl;
133  file<<"NLZ2"<<divcount<<":"<<std::endl;
134  file<<"DIVIDE"<<divcount++<<": cmp %r1, %r2"<<std::endl;
135  file<<"\t bgt DONEDIVIDE"<<divcount-1<<std::endl;
136  file<<"\t sub %r2, %r2, %r1"<<std::endl;
137  file<<"\t add %r0, $1"<<std::endl;
138  file<<"\t b DIVIDE"<<divcount-1<<std::endl;
139  file<<"DONEDIVIDE"<<divcount-1<<":"<<std::endl;
140  file<<"\tmul %r0, %r0, %r5"<<std::endl;
141  goto do_default;
142  case MOD:
143  file<<"\tudiv %r0, %r2, %r1"<<std::endl;
144  file<<"\tmls %r2, %r1, %r0, %r2"<<std::endl;
145  goto do_default;
146  case GT:
147  t1 = arg_types.top(); arg_types.pop();
148  t2 = arg_types.top(); arg_types.pop();
149  if (t1 == STRNG && t2 == STRNG) {
150  file<<"\tmov %r0, %r1"<<std::endl;
151  file<<"\tmov %r1, %r2"<<std::endl;
152  file<<"\tbl strcmp"<<std::endl;
153  file<<"\tpush {%r0}"<<std::endl;
154  file<<"\tmov %r0, $1"<<std::endl;
155  file<<"\tpop {%r1}"<<std::endl;
156  file<<"\tmov %r2, #-1"<<std::endl;
157  file<<"\tcmp %r2, %r1"<<std::endl;
158  file<<"\tbeq NOSETZERO"<<nszcount<<std::endl;
159  file<<"\tmov %r0, $0"<<std::endl;
160  file<<"NOSETZERO"<<nszcount<<": @ label used to set as true"<<std::endl;
161  } else {
162  file<<"\tmov %r0, $1"<<std::endl;
163  file<<"\tcmp %r2, %r1"<<std::endl;
164  file<<"\tbgt NOSETZERO"<<nszcount<<std::endl;
165  file<<"\tmov %r0, $0"<<std::endl;
166  file<<"NOSETZERO"<<nszcount<<": @ label used to set as true"<<std::endl;
167  }
168  nszcount++;
169  goto do_default;
170  case GEQ:
171  t1 = arg_types.top(); arg_types.pop();
172  t2 = arg_types.top(); arg_types.pop();
173  if (t1 == STRNG && t2 == STRNG) {
174  file<<"\tmov %r0, %r1"<<std::endl;
175  file<<"\tmov %r1, %r2"<<std::endl;
176  file<<"\tbl strcmp"<<std::endl;
177  file<<"\tpush {%r0}"<<std::endl;
178  file<<"\tmov %r0, $1"<<std::endl;
179  file<<"\tpop {%r1}"<<std::endl;
180  file<<"\tmov %r2, #-1"<<std::endl;
181  file<<"\tcmp %r2, %r1"<<std::endl;
182  file<<"\tbeq NOSETZERO"<<nszcount<<std::endl;
183  file<<"\tmov %r0, $0"<<std::endl;
184  file<<"NOSETZERO"<<nszcount<<": @ label used to set as true"<<std::endl;
185  } else {
186  file<<"\tmov %r0, $1"<<std::endl;
187  file<<"\tcmp %r2, %r1"<<std::endl;
188  file<<"\tbge NOSETZERO"<<nszcount<<std::endl;
189  file<<"\tmov %r0, $0"<<std::endl;
190  file<<"NOSETZERO"<<nszcount<<": @ label used to set as true"<<std::endl;
191  }
192  nszcount++;
193  goto do_default;
194  case LT:
195  t1 = arg_types.top(); arg_types.pop();
196  t2 = arg_types.top(); arg_types.pop();
197  if (t1 == STRNG && t2 == STRNG) {
198  file<<"\tmov %r0, %r1"<<std::endl;
199  file<<"\tmov %r1, %r2"<<std::endl;
200  file<<"\tbl strcmp"<<std::endl;
201  file<<"\tpush {%r0}"<<std::endl;
202  file<<"\tmov %r0, $1"<<std::endl;
203  file<<"\tpop {%r1}"<<std::endl;
204  file<<"\tmov %r2, #-1"<<std::endl;
205  file<<"\tcmp %r2, %r1"<<std::endl;
206  file<<"\tbeq NOSETZERO"<<nszcount<<std::endl;
207  file<<"\tmov %r0, $0"<<std::endl;
208  file<<"NOSETZERO"<<nszcount<<": @ label used to set as true"<<std::endl;
209  } else {
210  file<<"\tmov %r0, $1"<<std::endl;
211  file<<"\tcmp %r2, %r1"<<std::endl;
212  file<<"\tblt NOSETZERO"<<nszcount<<std::endl;
213  file<<"\tmov %r0, $0"<<std::endl;
214  file<<"NOSETZERO"<<nszcount<<": @ label used to set as true"<<std::endl;
215  }
216  nszcount++;
217  goto do_default;
218  case LEQ:
219  t1 = arg_types.top(); arg_types.pop();
220  t2 = arg_types.top(); arg_types.pop();
221  if (t1 == STRNG && t2 == STRNG) {
222  file<<"\tmov %r0, %r1"<<std::endl;
223  file<<"\tmov %r1, %r2"<<std::endl;
224  file<<"\tbl strcmp"<<std::endl;
225  file<<"\tpush {%r0}"<<std::endl;
226  file<<"\tmov %r0, $1"<<std::endl;
227  file<<"\tpop {%r1}"<<std::endl;
228  file<<"\tmov %r2, #-1"<<std::endl;
229  file<<"\tcmp %r2, %r1"<<std::endl;
230  file<<"\tbeq NOSETZERO"<<nszcount<<std::endl;
231  file<<"\tmov %r2, %r1"<<std::endl;
232  file<<"\tbeq NOSETZERO"<<nszcount<<std::endl;
233  file<<"\tmov %r0, $0"<<std::endl;
234  file<<"NOSETZERO"<<nszcount<<": @ label used to set as true"<<std::endl;
235  } else {
236  file<<"\tmov %r0, $1"<<std::endl;
237  file<<"\tcmp %r2, %r1"<<std::endl;
238  file<<"\tble NOSETZERO"<<nszcount<<std::endl;
239  file<<"\tmov %r0, $0"<<std::endl;
240  file<<"NOSETZERO"<<nszcount<<": @ label used to set as true"<<std::endl;
241  }
242  nszcount++;
243  goto do_default;
244  case EQ:
245  t1 = arg_types.top(); arg_types.pop();
246  t2 = STRNG;// arg_types.top(); arg_types.pop();
247  if (t1 == STRNG && t2 == STRNG) {
248  file<<"\tmov %r0, %r1"<<std::endl;
249  file<<"\tmov %r1, %r2"<<std::endl;
250  file<<"\tbl strcmp"<<std::endl;
251  file<<"\tpush {%r0}"<<std::endl;
252  file<<"\tmov %r0, $1"<<std::endl;
253  file<<"\tpop {%r1}"<<std::endl;
254  file<<"\tmov %r2, $0"<<std::endl;
255  file<<"\tcmp %r2, %r1"<<std::endl;
256  file<<"\tbeq NOSETZERO"<<nszcount<<std::endl;
257  file<<"\tmov %r0, $0"<<std::endl;
258  file<<"NOSETZERO"<<nszcount<<": @ label used to set as true"<<std::endl;
259  } else {
260  file<<"\tmov %r0, $1"<<std::endl;
261  file<<"\tcmp %r2, %r1"<<std::endl;
262  file<<"\tbeq NOSETZERO"<<nszcount<<std::endl;
263  file<<"\tmov %r0, $0"<<std::endl;
264  file<<"NOSETZERO"<<nszcount<<": @ label used to set as true"<<std::endl;
265  }
266  nszcount++;
267  goto do_default;
268  case NEQ:
269  t1 = arg_types.top(); arg_types.pop();
270  t2 = STRNG;// arg_types.top(); arg_types.pop();
271  if (t1 == STRNG && t2 == STRNG) {
272  file<<"\tmov %r0, %r1"<<std::endl;
273  file<<"\tmov %r1, %r2"<<std::endl;
274  file<<"\tbl strcmp"<<std::endl;
275  file<<"\tpush {%r0}"<<std::endl;
276  file<<"\tmov %r0, $1"<<std::endl;
277  file<<"\tpop {%r1}"<<std::endl;
278  file<<"\tmov %r2, $0"<<std::endl;
279  file<<"\tcmp %r2, %r1"<<std::endl;
280  file<<"\tbne NOSETZERO"<<nszcount<<std::endl;
281  file<<"\tmov %r0, $0"<<std::endl;
282  file<<"NOSETZERO"<<nszcount<<": @ label used to set as true"<<std::endl;
283  } else {
284  file<<"\tmov %r0, $1"<<std::endl;
285  file<<"\tcmp %r2, %r1"<<std::endl;
286  file<<"\tbne NOSETZERO"<<nszcount<<std::endl;
287  file<<"\tmov %r0, $0"<<std::endl;
288  file<<"NOSETZERO"<<nszcount<<": @ label used to set as true"<<std::endl;
289  }
290  nszcount++;
291  goto do_default;
292  case LOGAND:
293  file<<"\tmul %r3, %r1, %r2"<<std::endl;
294  file<<"\tmov %r0, $1"<<std::endl;
295  file<<"\tmov %r4, $0"<<std::endl;
296  file<<"\tcmp %r3, %r4"<<std::endl;
297  file<<"\tbne NSZ"<<nszcount<<std::endl;
298  file<<"\tmov %r0, $0"<<std::endl;
299  file<<"NSZ"<<nszcount<<": @ label used to set as true"<<std::endl;
300  nszcount++;
301  goto do_default;
302  case LOGOR:
303  file<<"\tadd %r1, %r1, %r2"<<std::endl;
304  file<<"\tmov %r0, $1"<<std::endl;
305  file<<"\tcmp %r1, $0"<<std::endl;
306  file<<"\tbne NSZ"<<nszcount<<std::endl;
307  file<<"\tmov %r0, $0"<<std::endl;
308  file<<"NSZ"<<nszcount<<": @ label used to set as true"<<std::endl;
309  nszcount++;
310  goto do_default;
311  case LOGXOR:
312  file<<"\tmul %r3, %r1, %r2"<<std::endl;
313  file<<"\tadd %r4, %r1, %r2"<<std::endl;
314  file<<"\tmov %r1, $1"<<std::endl;
315  file<<"\tmov %r2, $1"<<std::endl;
316  file<<"\tcmp %r3, $0"<<std::endl;
317  file<<"\tbne NSZ"<<nszcount<<std::endl;
318  file<<"\tmov %r1, $0"<<std::endl;
319  file<<"NSZ"<<nszcount++<<": @ r1 <-- a && b"<<std::endl;
320  file<<"\tcmp %r4, $0"<<std::endl;
321  file<<"\tbne NSZ"<<nszcount<<std::endl;
322  file<<"\tmov %r2, $0";
323  file<<"NSZ"<<nszcount<<": @ r1 <-- a || b"<<std::endl;
324  file<<"\teor %r0, %r1, %r2"<<std::endl;
325  nszcount++;
326  goto do_default;
327  default:
328  std::cerr<<"Welp. You did it. You tried it."<<std::endl;
329  std::cerr<<"This is what happens. You get mad."<<std::endl;
330  std::cerr<<"Don't use things I didn't implement yet."<<std::endl;
331  exit(101);
332  do_default:
333  // if (eval.size()) {
334  file<<"\tpush {%r0}"<<std::endl;
335  stack_depth += 4;
336  // }
337  // else ;
338  }
339  }
340 }
341 
342 void Command::push_variable(std::string var_name, std::stack<unsigned short> & vartype,
343  std::ostream & file)
344 {
345  std::string varname = var_name;
346  for (int x = 0; x < m_int_declarations.size(); ++x) {
347  if (m_int_declarations[x] == varname) {
348  file<<"\tmov %r1, %r9"<<std::endl;
349  file<<"\tldr %r1, [%r9, #"<<4 * x<<"]"<<std::endl;
350  file<<"\tldr %r1, [%r1]"<<std::endl;
351  file<<"\tpush {%r1}"<<std::endl;
352  vartype.push(INTGR);
353  return;
354  }
355  }
356 
357  for (int x = 0; x < m_int_vars.size(); ++x) {
358  if (m_int_vars[x] == varname) {
359  file<<"\tldr %r1, =I"<<x<<std::endl;
360  file<<"\tldr %r1, [%r1]"<<std::endl;
361  file<<"\tpush {%r1}"<<std::endl;
362  vartype.push(INTGR);
363  return;
364  }
365  }
366 
367  for (int x = 0; x < m_string_vars.size(); ++x) {
368  if (m_string_vars[x] == varname) {
369  file<<"\tldr %r1, =IS"<<x<<std::endl;
370  file<<"\tpush {%r1}"<<std::endl;
371  vartype.push(STRNG);
372  return;
373  }
374  }
375 
376  std::cerr<<"Error: could not resolve variable \"";
377  std::cerr<<varname<<"\"."<<std::endl;
378  exit(6);
379 }
380 
381 void Command::doMain(std::ostream & file)
382 {
383  int y;
384  const size_t local_count = 0;// Idk, yo. Just... somewhere.
385  if (m_is_c_callable) file<<m_function_name<<":"<<std::endl;
386  else file<<"main:"<<std::endl;
387 
388  file<<"\tldr %r9, =locals"<<std::endl;
389  file<<"\t@ Make space for locals"<<std::endl;
394  for (int x = 0, intassdex = 0, stringdex = 0, exprdex = 0,
395  intdex = 0, pintdex = 0, pstringdex = 0, litdex = 0,
396  pbooldex = 0, sints = 0, ifndex = 0, forndex = 0, nszcount = 0;
397  x < m_execOrder.size(); ++x) {
398 
399  if (m_execOrder[x] == cmd_type::READ_STRING) {
400  file<<"\tldr %r0, =string_fmt"<<std::endl;
401  file<<"\tldr %r1, =IS"<<stringdex<<std::endl;
402  file<<"\tbl scanf"<<std::endl;
403  file<<std::endl;
404 
405  ++stringdex;
406  } else if (m_execOrder[x] == cmd_type::READ_LINE) {
411  file<<"\tldr %r0, =IS"<<stringdex<<std::endl;
412  file<<"\tbl gets"<<std::endl;
413  file<<std::endl;
414 
415  ++stringdex;
416  } else if (m_execOrder[x] == cmd_type::DECL_INT) {
417  file<<"\tmov %r0, $4"<<std::endl;
418  file<<"\tbl malloc"<<std::endl;
419  file<<"\tstr %r0, [%r9, #"<<sints * 4<<"]"<<std::endl;
420 
421  file<<std::endl;
422  m_int_vars.push_back(m_int_declarations[sints++]);
423  } else if (m_execOrder[x] == cmd_type::BEGIN_IF) {
424  // find the variable we depend on!
425  evaluate_expression(file);
426  file<<"\tpop {%r0}"<<std::endl;
427  file<<"IF"<<ifndex<<":\tcmp %r0, $0"<<std::endl;
428  file<<"\tbeq END_IF"<<ifndex<<std::endl;
429  ++ifndex;
430  } else if (m_execOrder[x] == cmd_type::BEGIN_FOR) {
431  std::string var_name = m_for_deps[forndex];
432  for (y = 0; y < m_int_vars.size(); ++y) {
433  if (m_int_vars[y] == var_name) break;
434  } int z = 0;
435  for (; z < m_int_declarations.size(); ++z) {
436  if (m_int_declarations[z] == var_name) break;
437  } if (z < m_int_declarations.size()) {
438  file<<"FOR"<<forndex<<":";
439  file<<"\tmov %r8, %r9"<<std::endl;
440  file<<"\tldr %r8, [%r8, #"<<4 * z<<"]"<<std::endl;
441  file<<"\tldr %r8, [%r8]"<<std::endl;
442  } else if (y < m_int_vars.size()) {
443  file<<"FOR"<<forndex<<":";
444  file<<"\tmov %r8, =I"<<y<<std::endl;
445  file<<"\tldr %r8, [%r8]"<<std::endl;
446  } else {
447  std::cerr<<"Error: for-dependent variable "<<var_name;
448  std::cerr<<" not yet declared!"<<std::endl;
449  exit(7);
450  }
451 
452  file<<"\tcmp %r8, $0"<<std::endl;
453  file<<"\tbeq END_FOR"<<forndex<<std::endl;
454  } else if (m_execOrder[x] == cmd_type::END_FOR) {
455  file<<"b FOR"<<forndex<<std::endl;
456  file<<"END_FOR"<<forndex++<<":"<<std::endl;
457  } else if (m_execOrder[x] == cmd_type::END_IF) {
458  file<<"END_IF"<<ifndex-1<<":"<<std::endl;
459  } else if (m_execOrder[x] == cmd_type::READ_INT) {
460  file<<"\tldr %r0, =num_fmt"<<std::endl;
461  file<<"\tldr %r1, =I"<<intdex<<std::endl;
462  file<<"\tbl scanf"<<std::endl;
463  file<<std::endl;
464 
465  ++intdex;
466  } else if (m_execOrder[x] == cmd_type::NOPRINT) {
467  ++litdex;
468  } else if (m_execOrder[x] == cmd_type::PRINT) {
469  file<<"\tldr %r0, =S"<<litdex++<<std::endl;
470  file<<"\tbl printf"<<std::endl<<std::endl;
471  } else if (m_execOrder[x] == cmd_type::PRINT_STR) {
472  file<<"\tldr %r0, =string_fmt"<<std::endl;
473  for (y = 0; y < m_string_vars.size(); ++y) {
474  if (m_string_vars[y] == m_print_strings[pstringdex]) break;
475  } if (y == m_string_vars.size()) {
476  std::cerr<<"Error: variable "<<m_string_vars[y]<<" was not declared!"<<std::endl;
477  exit(3);
478  }
480  file<<"\tldr %r1, =IS"<<y<<std::endl;
481  file<<"\tbl printf"<<std::endl;
482  ++pstringdex;
483  } else if (m_execOrder[x] == cmd_type::PRINT_NUM) {
484  file<<"\tldr %r0, =num_fmt"<<std::endl;
485  for (y = 0; y < m_int_vars.size(); ++y) {
486  if (m_int_vars[y] == m_print_ints[pintdex]) break;
487  } if (y == m_int_vars.size()) {
488  std::cerr<<"Error: variable "<<m_int_vars[y]<<" was not declared!"<<std::endl;
489  exit(3);
490  } int z;
491  for (z = 0; z < m_int_declarations.size(); ++z) {
492  if (m_print_ints[pintdex] == m_int_declarations[z]) break;
493  } if (z != m_int_declarations.size()) {
494  file<<"\tldr %r1, =locals"<<std::endl;
495  file<<"\tldr %r1, [%r1, #"<<4 * z<<"]"<<std::endl;
496  file<<"\tldr %r1, [%r1]"<<std::endl;
497  } else {
498  file<<"\tldr %r1, =I"<<y<<std::endl;
499  file<<"\tldr %r1, [%r1]"<<std::endl;
500  } file<<"\tbl printf"<<std::endl;
501  ++pintdex;
502  } else if (m_execOrder[x] == cmd_type::PRINT_BOOL) {
503  for (y = 0; y < m_bool_vars.size(); ++y) {
504  if (m_bool_vars[y] == m_print_bools[pbooldex]) break;
505  } if (y == m_bool_vars.size()) {
506  std::cerr<<"Error: variable "<<m_bool_vars[y]<<" was not declared!"<<std::endl;
507  }
508  file<<"\tldr %r1, =B"<<y<<std::endl;
509  file<<"\tldr %r1, [%r1]"<<std::endl;
510  file<<"\tcmp %r1, $0"<<std::endl;
511  file<<"\tbne LIES"<<nszcount++<<std::endl;
512  file<<"\tldr %r0, =TRUE_STORY"<<std::endl;
513  file<<"\tb NOISTRUE"<<nszcount-1<<std::endl;
514  file<<"LIES"<<nszcount-1<<":"<<std::endl;
515  file<<"\tldr %r0, =FALSE"<<std::endl;
516  file<<"NOISTRUE"<<nszcount-1<<":"<<std::endl;
517  file<<"\tbl printf"<<std::endl;
518  ++pbooldex;
519  } else if (m_execOrder[x] == cmd_type::INTGETS) {
520  std::string int_gets = m_int_assigns[intassdex];
521  ssize_t offset;
522  for (y = 0; y < m_int_vars.size(); ++y) {
523  if (m_int_vars[y] == m_int_assigns[intassdex]) break;
524  } int z; bool on_stack = false; int * last_z = new int;
525  for (z = 0; z < m_int_declarations.size(); ++z) {
526  if (m_int_vars[y] == m_int_declarations[z]) { on_stack = true; break; }
527  } offset = (z != m_int_declarations.size()) ? 4 * z : y;
528  if (y == m_int_vars.size()) {
529  std::cerr<<"Error: variable "<<m_int_vars[y];
530  std::cerr<<" was not declared!"<<std::endl;
531  exit(3);
532  } *last_z = z;
533 
534  evaluate_expression(file);
535 
536  file<<"\tpop {%r0}"<<std::endl;
537 
538  if (!on_stack) {
539  int x;
540  for (x = 0; x < m_int_vars.size(); ++x) {
541  if (m_int_vars[x] == int_gets) break;
542  }
543  file<<"\tldr %r3, =I"<<x<<std::endl;
544  } else {
545  file<<"\tmov %r3, %r9"<<std::endl;
546  file<<"\tldr %r3, [%r3, #"<<*last_z * 4<<"]"<<std::endl;
547  delete last_z;
548  }
549  file<<"\tstr %r0, [%r3]"<<std::endl;
550  file<<std::endl; intassdex++;
551  }
552  }
553 }
554 
556 {
557  std::ofstream file;
558  file.open(m_filename);
559  if (!file.is_open()) {
560  std::cerr<<"Error: unable to open output file!";
561  exit(2);
562  }
563  file <<"/**"<<std::endl;
564  file <<" * "<<m_filename<<std::endl;
565  file <<" *"<<std::endl;
566  file <<" * Generated by Bench Cookie Compiler!"<<std::endl;
567  file <<" * Bench Cookie is... Experimental. Don't be mad."<<std::endl;
568  file <<" */"<<std::endl;
569 
570  doBSS(file);
571 
572  file<<std::endl;
573 
574  doData(file);
575 
576  file<<std::endl<<std::endl;
577  file<<"\t.text"<<std::endl;
578  if (!m_is_c_callable) file<<"\t.global main"<<std::endl;
579  file<<"\t.global printf"<<std::endl;
580  file<<"\t.global scanf"<<std::endl;
581  file<<"\t.global malloc"<<std::endl;
582  if (m_is_c_callable) file<<"\texport "<<m_function_name<<std::endl;
583  doMain(file);
584 
592  file<<"\t@ Free local vars"<<std::endl;
593  for (int x = 0; x < m_int_declarations.size(); ++x) {
594  file<<"\tldr %r0, =locals"<<std::endl;
595  file<<"\tldr %r0, [%r0, #"<<4 * x<<"]"<<std::endl;
596  file<<"\tbl free"<<std::endl<<std::endl;
597  }
598 
599  file<<"\t@ locals are free"<<std::endl<<std::endl;
600 
601  file<<std::endl;
602  file<<"\tmov %r0, $0"<<std::endl;
606  file<<"\tbl fflush"<<std::endl;
607 
610  if (!m_is_c_callable) {
611  file<<"\tmov %r7, $1"<<std::endl;
612 
613  file<<"\tswi $0"<<std::endl;
614  file<<"\t.end"<<std::endl;
615  file.close();
616  } else {
617  if (m_current_returns.size() > 3) {
619  } else {
623  file<<"\tmov %r9, =locals"<<std::endl;
624  for (int x = 0; x < m_current_returns.size(); ++x) {
625  int y, z;
626  for (y = 0; y < m_int_vars.size(); ++y) {
627  if (m_int_vars[y] == m_current_returns[x]) break;
628  } if (y == m_int_vars.size()) {
629  std::cerr<<"Error: returned variable "<<m_current_returns[x]
630  <<" was not declared!"<<std::endl;
631  exit(9);
632  } for (z = 0; z < m_int_declarations.size(); ++z) {
633  if (m_int_declarations[z] == m_int_vars[y]) break;
634  } if (z != m_int_declarations.size()) {
635  file<<"\tmov %r6, %r9"<<std::endl;
636  file<<"\tldr %r6, [%r9, #"<<4 * z<<"]"<<std::endl;
637  file<<"\tldr %r6, [%r6]"<<std::endl;
638  } else {
639  file<<"\tldr %r6, =I"<<y<<std::endl;
640  file<<"\tldr %r6, [%r6]"<<std::endl;
641  }
642  file<<"\tmov %r"<<x<<", %r6"<<std::endl;
643  }
644  }
645  file<<"\tbx lr"<<std::endl;
646  file<<"\t.end"<<std::endl;
647  }
648 }// allocate enough space for drew-sized strings.
649 #endif
std::string pirate_name
Definition: command.h:45
#define STRNG
#define MAX_STRING_SIZE
Definition: x86_compiler.cpp:4
#define INTGR
void writeAssembly()
exp_type
Definition: command.h:65