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