cc_core.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * Copyright (C) 2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  3. * Copyright (C) 2020 deesix <deesix@tuta.io>
  4. * This file is part of M2-Planet.
  5. *
  6. * M2-Planet is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * M2-Planet is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "cc.h"
  20. #include "gcc_req.h"
  21. #include <stdint.h>
  22. /* Global lists */
  23. struct token_list* global_symbol_list;
  24. struct token_list* global_function_list;
  25. struct token_list* global_constant_list;
  26. /* Core lists for this file */
  27. struct token_list* function;
  28. /* What we are currently working on */
  29. struct type* current_target;
  30. char* break_target_head;
  31. char* break_target_func;
  32. char* break_target_num;
  33. char* continue_target_head;
  34. struct token_list* break_frame;
  35. int current_count;
  36. int Address_of;
  37. /* Imported functions */
  38. char* number_to_hex(int a, int bytes);
  39. char* numerate_number(int a);
  40. char* parse_string(char* string);
  41. int escape_lookup(char* c);
  42. int numerate_string(char *a);
  43. void require(int bool, char* error);
  44. struct token_list* reverse_list(struct token_list* head);
  45. struct type* mirror_type(struct type* source, char* name);
  46. struct type* add_primitive(struct type* a);
  47. /* Host touchy function will need custom on 64bit systems*/
  48. int fixup_int32(int a);
  49. struct token_list* emit(char *s, struct token_list* head)
  50. {
  51. struct token_list* t = calloc(1, sizeof(struct token_list));
  52. require(NULL != t, "Exhusted memory while generating token to emit\n");
  53. t->next = head;
  54. t->s = s;
  55. return t;
  56. }
  57. void emit_out(char* s)
  58. {
  59. output_list = emit(s, output_list);
  60. }
  61. struct token_list* uniqueID(char* s, struct token_list* l, char* num)
  62. {
  63. l = emit("\n", emit(num, emit("_", emit(s, l))));
  64. return l;
  65. }
  66. void uniqueID_out(char* s, char* num)
  67. {
  68. output_list = uniqueID(s, output_list, num);
  69. }
  70. struct token_list* sym_declare(char *s, struct type* t, struct token_list* list)
  71. {
  72. struct token_list* a = calloc(1, sizeof(struct token_list));
  73. require(NULL != a, "Exhusted memory while attempting to declare a symbol\n");
  74. a->next = list;
  75. a->s = s;
  76. a->type = t;
  77. return a;
  78. }
  79. struct token_list* sym_lookup(char *s, struct token_list* symbol_list)
  80. {
  81. struct token_list* i;
  82. for(i = symbol_list; NULL != i; i = i->next)
  83. {
  84. if(match(i->s, s)) return i;
  85. }
  86. return NULL;
  87. }
  88. void line_error_token(struct token_list *token)
  89. {
  90. require(NULL != token, "EOF reached inside of line_error\n");
  91. file_print(token->filename, stderr);
  92. file_print(":", stderr);
  93. file_print(numerate_number(token->linenumber), stderr);
  94. file_print(":", stderr);
  95. }
  96. void line_error()
  97. {
  98. line_error_token(global_token);
  99. }
  100. void require_match(char* message, char* required)
  101. {
  102. require(NULL != global_token, "EOF reached inside of require match\n");
  103. if(!match(global_token->s, required))
  104. {
  105. line_error();
  106. file_print(message, stderr);
  107. exit(EXIT_FAILURE);
  108. }
  109. global_token = global_token->next;
  110. require(NULL != global_token, "EOF after require match occurred\n");
  111. }
  112. void maybe_bootstrap_error(char* feature)
  113. {
  114. if (BOOTSTRAP_MODE)
  115. {
  116. line_error();
  117. file_print(feature, stderr);
  118. file_print(" is not supported in --bootstrap-mode\n", stderr);
  119. exit(EXIT_FAILURE);
  120. }
  121. }
  122. void expression();
  123. void function_call(char* s, int bool)
  124. {
  125. require_match("ERROR in process_expression_list\nNo ( was found\n", "(");
  126. int passed = 0;
  127. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  128. {
  129. emit_out("PUSHR R13 R15\t# Prevent overwriting in recursion\n");
  130. emit_out("PUSHR R14 R15\t# Protect the old base pointer\n");
  131. emit_out("COPY R13 R15\t# Copy new base pointer\n");
  132. }
  133. else if(X86 == Architecture)
  134. {
  135. emit_out("PUSH_edi\t# Prevent overwriting in recursion\n");
  136. emit_out("PUSH_ebp\t# Protect the old base pointer\n");
  137. emit_out("COPY_esp_to_edi\t# Copy new base pointer\n");
  138. }
  139. else if(AMD64 == Architecture)
  140. {
  141. emit_out("PUSH_RDI\t# Prevent overwriting in recursion\n");
  142. emit_out("PUSH_RBP\t# Protect the old base pointer\n");
  143. emit_out("COPY_RSP_to_RDI\t# Copy new base pointer\n");
  144. }
  145. else if(ARMV7L == Architecture)
  146. {
  147. emit_out("{R11} PUSH_ALWAYS\t# Prevent overwriting in recursion\n");
  148. emit_out("{BP} PUSH_ALWAYS\t# Protect the old base pointer\n");
  149. emit_out("'0' SP R11 NO_SHIFT MOVE_ALWAYS\t# Copy new base pointer\n");
  150. }
  151. else if(AARCH64 == Architecture)
  152. {
  153. emit_out("PUSH_X16\t# Protect a tmp register we're going to use\n");
  154. emit_out("PUSH_LR\t# Protect the old return pointer (link)\n");
  155. emit_out("PUSH_BP\t# Protect the old base pointer\n");
  156. emit_out("SET_X16_FROM_SP\t# The base pointer to-be\n");
  157. }
  158. if(global_token->s[0] != ')')
  159. {
  160. expression();
  161. require(NULL != global_token, "incomplete function call, recieved EOF instead of )\n");
  162. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("PUSHR R0 R15\t#_process_expression1\n");
  163. else if(X86 == Architecture) emit_out("PUSH_eax\t#_process_expression1\n");
  164. else if(AMD64 == Architecture) emit_out("PUSH_RAX\t#_process_expression1\n");
  165. else if(ARMV7L == Architecture) emit_out("{R0} PUSH_ALWAYS\t#_process_expression1\n");
  166. else if(AARCH64 == Architecture) emit_out("PUSH_X0\t#_process_expression1\n");
  167. passed = 1;
  168. while(global_token->s[0] == ',')
  169. {
  170. global_token = global_token->next;
  171. require(NULL != global_token, "incomplete function call, recieved EOF instead of argument\n");
  172. expression();
  173. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("PUSHR R0 R15\t#_process_expression2\n");
  174. else if(X86 == Architecture) emit_out("PUSH_eax\t#_process_expression2\n");
  175. else if(AMD64 == Architecture) emit_out("PUSH_RAX\t#_process_expression2\n");
  176. else if(ARMV7L == Architecture) emit_out("{R0} PUSH_ALWAYS\t#_process_expression2\n");
  177. else if(AARCH64 == Architecture) emit_out("PUSH_X0\t#_process_expression2\n");
  178. passed = passed + 1;
  179. }
  180. }
  181. require_match("ERROR in process_expression_list\nNo ) was found\n", ")");
  182. if(TRUE == bool)
  183. {
  184. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  185. {
  186. emit_out("LOAD R0 R14 ");
  187. emit_out(s);
  188. emit_out("\nMOVE R14 R13\n");
  189. emit_out("CALL R0 R15\n");
  190. }
  191. else if(X86 == Architecture)
  192. {
  193. emit_out("LOAD_BASE_ADDRESS_eax %");
  194. emit_out(s);
  195. emit_out("\nLOAD_INTEGER\n");
  196. emit_out("COPY_edi_to_ebp\n");
  197. emit_out("CALL_eax\n");
  198. }
  199. else if(AMD64 == Architecture)
  200. {
  201. emit_out("LOAD_BASE_ADDRESS_rax %");
  202. emit_out(s);
  203. emit_out("\nLOAD_INTEGER\n");
  204. emit_out("COPY_rdi_to_rbp\n");
  205. emit_out("CALL_rax\n");
  206. }
  207. else if(ARMV7L == Architecture)
  208. {
  209. emit_out("!");
  210. emit_out(s);
  211. emit_out(" R0 SUB BP ARITH_ALWAYS\n");
  212. emit_out("!0 R0 LOAD32 R0 MEMORY\n");
  213. emit_out("{LR} PUSH_ALWAYS\t# Protect the old link register\n");
  214. emit_out("'0' R11 BP NO_SHIFT MOVE_ALWAYS\n");
  215. emit_out("'3' R0 CALL_REG_ALWAYS\n");
  216. emit_out("{LR} POP_ALWAYS\t# Prevent overwrite\n");
  217. }
  218. else if(AARCH64 == Architecture)
  219. {
  220. emit_out("SET_X0_FROM_BP\n");
  221. emit_out("LOAD_W1_AHEAD\nSKIP_32_DATA\n%");
  222. emit_out(s);
  223. emit_out("\nSUB_X0_X0_X1\n");
  224. emit_out("DEREF_X0\n");
  225. emit_out("SET_BP_FROM_X16\n");
  226. emit_out("SET_X16_FROM_X0\n");
  227. emit_out("BLR_X16\n");
  228. }
  229. }
  230. else
  231. {
  232. if((KNIGHT_NATIVE == Architecture) || (KNIGHT_POSIX == Architecture))
  233. {
  234. emit_out("MOVE R14 R13\n");
  235. emit_out("LOADR R0 4\nJUMP 4\n&FUNCTION_");
  236. emit_out(s);
  237. emit_out("\nCALL R0 R15\n");
  238. }
  239. else if(X86 == Architecture)
  240. {
  241. emit_out("COPY_edi_to_ebp\n");
  242. emit_out("CALL_IMMEDIATE %FUNCTION_");
  243. emit_out(s);
  244. emit_out("\n");
  245. }
  246. else if(AMD64 == Architecture)
  247. {
  248. emit_out("COPY_rdi_to_rbp\n");
  249. emit_out("CALL_IMMEDIATE %FUNCTION_");
  250. emit_out(s);
  251. emit_out("\n");
  252. }
  253. else if(ARMV7L == Architecture)
  254. {
  255. emit_out("{LR} PUSH_ALWAYS\t# Protect the old link register\n");
  256. emit_out("'0' R11 BP NO_SHIFT MOVE_ALWAYS\n");
  257. emit_out("^~FUNCTION_");
  258. emit_out(s);
  259. emit_out(" CALL_ALWAYS\n");
  260. emit_out("{LR} POP_ALWAYS\t# Restore the old link register\n");
  261. }
  262. else if(AARCH64 == Architecture)
  263. {
  264. emit_out("SET_BP_FROM_X16\n");
  265. emit_out("LOAD_W16_AHEAD\nSKIP_32_DATA\n&FUNCTION_");
  266. emit_out(s);
  267. emit_out("\n");
  268. emit_out("BLR_X16\n");
  269. }
  270. }
  271. for(; passed > 0; passed = passed - 1)
  272. {
  273. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("POPR R1 R15\t# _process_expression_locals\n");
  274. else if(X86 == Architecture) emit_out("POP_ebx\t# _process_expression_locals\n");
  275. else if(AMD64 == Architecture) emit_out("POP_RBX\t# _process_expression_locals\n");
  276. else if(ARMV7L == Architecture) emit_out("{R1} POP_ALWAYS\t# _process_expression_locals\n");
  277. else if(AARCH64 == Architecture) emit_out("POP_X1\t# _process_expression_locals\n");
  278. }
  279. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  280. {
  281. emit_out("POPR R14 R15\t# Restore old base pointer\n");
  282. emit_out("POPR R13 R15\t# Prevent overwrite\n");
  283. }
  284. else if(X86 == Architecture)
  285. {
  286. emit_out("POP_ebp\t# Restore old base pointer\n");
  287. emit_out("POP_edi\t# Prevent overwrite\n");
  288. }
  289. else if(AMD64 == Architecture)
  290. {
  291. emit_out("POP_RBP\t# Restore old base pointer\n");
  292. emit_out("POP_RDI\t# Prevent overwrite\n");
  293. }
  294. else if(ARMV7L == Architecture)
  295. {
  296. emit_out("{BP} POP_ALWAYS\t# Restore old base pointer\n");
  297. emit_out("{R11} POP_ALWAYS\t# Prevent overwrite\n");
  298. }
  299. else if(AARCH64 == Architecture)
  300. {
  301. emit_out("POP_BP\t# Restore the old base pointer\n");
  302. emit_out("POP_LR\t# Restore the old return pointer (link)\n");
  303. emit_out("POP_X16\t# Restore a register we used as tmp\n");
  304. }
  305. }
  306. void constant_load(struct token_list* a)
  307. {
  308. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("LOADI R0 ");
  309. else if(X86 == Architecture) emit_out("LOAD_IMMEDIATE_eax %");
  310. else if(AMD64 == Architecture) emit_out("LOAD_IMMEDIATE_rax %");
  311. else if(ARMV7L == Architecture) emit_out("!0 R0 LOAD32 R15 MEMORY\n~0 JUMP_ALWAYS\n%");
  312. else if(AARCH64 == Architecture) emit_out("LOAD_W0_AHEAD\nSKIP_32_DATA\n%");
  313. emit_out(a->arguments->s);
  314. emit_out("\n");
  315. }
  316. void variable_load(struct token_list* a)
  317. {
  318. require(NULL != global_token, "incomplete variable load recieved\n");
  319. if((match("FUNCTION", a->type->name) || match("FUNCTION*", a->type->name)) && match("(", global_token->s))
  320. {
  321. function_call(numerate_number(a->depth), TRUE);
  322. return;
  323. }
  324. current_target = a->type;
  325. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("ADDI R0 R14 ");
  326. else if(X86 == Architecture) emit_out("LOAD_BASE_ADDRESS_eax %");
  327. else if(AMD64 == Architecture) emit_out("LOAD_BASE_ADDRESS_rax %");
  328. else if(ARMV7L == Architecture) emit_out("!");
  329. else if(AARCH64 == Architecture) emit_out("SET_X0_FROM_BP\nLOAD_W1_AHEAD\nSKIP_32_DATA\n%");
  330. emit_out(numerate_number(a->depth));
  331. if(ARMV7L == Architecture) emit_out(" R0 SUB BP ARITH_ALWAYS");
  332. else if(AARCH64 == Architecture) emit_out("\nSUB_X0_X0_X1\n");
  333. emit_out("\n");
  334. if(TRUE == Address_of) return;
  335. if(match("=", global_token->s)) return;
  336. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("LOAD R0 R0 0\n");
  337. else if(X86 == Architecture) emit_out("LOAD_INTEGER\n");
  338. else if(AMD64 == Architecture) emit_out("LOAD_INTEGER\n");
  339. else if(ARMV7L == Architecture) emit_out("!0 R0 LOAD32 R0 MEMORY\n");
  340. else if(AARCH64 == Architecture) emit_out("DEREF_X0\n");
  341. }
  342. void function_load(struct token_list* a)
  343. {
  344. require(NULL != global_token, "incomplete function load\n");
  345. if(match("(", global_token->s))
  346. {
  347. function_call(a->s, FALSE);
  348. return;
  349. }
  350. if((KNIGHT_NATIVE == Architecture) || (KNIGHT_POSIX == Architecture)) emit_out("LOADR R0 4\nJUMP 4\n&FUNCTION_");
  351. else if(X86 == Architecture) emit_out("LOAD_IMMEDIATE_eax &FUNCTION_");
  352. else if(AMD64 == Architecture) emit_out("LOAD_IMMEDIATE_rax &FUNCTION_");
  353. else if(ARMV7L == Architecture) emit_out("!0 R0 LOAD32 R15 MEMORY\n~0 JUMP_ALWAYS\n&FUNCTION_");
  354. else if(AARCH64 == Architecture) emit_out("LOAD_W0_AHEAD\nSKIP_32_DATA\n&FUNCTION_");
  355. emit_out(a->s);
  356. emit_out("\n");
  357. }
  358. void global_load(struct token_list* a)
  359. {
  360. current_target = a->type;
  361. if((KNIGHT_NATIVE == Architecture) || (KNIGHT_POSIX == Architecture)) emit_out("LOADR R0 4\nJUMP 4\n&GLOBAL_");
  362. else if(X86 == Architecture) emit_out("LOAD_IMMEDIATE_eax &GLOBAL_");
  363. else if(AMD64 == Architecture) emit_out("LOAD_IMMEDIATE_rax &GLOBAL_");
  364. else if(ARMV7L == Architecture) emit_out("!0 R0 LOAD32 R15 MEMORY\n~0 JUMP_ALWAYS\n&GLOBAL_");
  365. else if(AARCH64 == Architecture) emit_out("LOAD_W0_AHEAD\nSKIP_32_DATA\n&GLOBAL_");
  366. emit_out(a->s);
  367. emit_out("\n");
  368. require(NULL != global_token, "unterminated global load\n");
  369. if(TRUE == Address_of) return;
  370. if(match("=", global_token->s)) return;
  371. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("LOAD R0 R0 0\n");
  372. else if(X86 == Architecture) emit_out("LOAD_INTEGER\n");
  373. else if(AMD64 == Architecture) emit_out("LOAD_INTEGER\n");
  374. else if(ARMV7L == Architecture) emit_out("!0 R0 LOAD32 R0 MEMORY\n");
  375. else if(AARCH64 == Architecture) emit_out("DEREF_X0\n");
  376. }
  377. /*
  378. * primary-expr:
  379. * FAILURE
  380. * "String"
  381. * 'Char'
  382. * [0-9]*
  383. * [a-z,A-Z]*
  384. * ( expression )
  385. */
  386. void primary_expr_failure()
  387. {
  388. require(NULL != global_token, "hit EOF when expecting primary expression\n");
  389. line_error();
  390. file_print("Received ", stderr);
  391. file_print(global_token->s, stderr);
  392. file_print(" in primary_expr\n", stderr);
  393. exit(EXIT_FAILURE);
  394. }
  395. void primary_expr_string()
  396. {
  397. char* number_string = numerate_number(current_count);
  398. current_count = current_count + 1;
  399. if((KNIGHT_NATIVE == Architecture) || (KNIGHT_POSIX == Architecture)) emit_out("LOADR R0 4\nJUMP 4\n&STRING_");
  400. else if(X86 == Architecture) emit_out("LOAD_IMMEDIATE_eax &STRING_");
  401. else if(AMD64 == Architecture) emit_out("LOAD_IMMEDIATE_rax &STRING_");
  402. else if(ARMV7L == Architecture) emit_out("!0 R0 LOAD32 R15 MEMORY\n~0 JUMP_ALWAYS\n&STRING_");
  403. else if(AARCH64 == Architecture) emit_out("LOAD_W0_AHEAD\nSKIP_32_DATA\n&STRING_");
  404. uniqueID_out(function->s, number_string);
  405. /* The target */
  406. strings_list = emit(":STRING_", strings_list);
  407. strings_list = uniqueID(function->s, strings_list, number_string);
  408. /* catch case of just "foo" from segfaulting */
  409. require(NULL != global_token->next, "a string by itself is not valid C\n");
  410. /* Parse the string */
  411. if('"' != global_token->next->s[0])
  412. {
  413. strings_list = emit(parse_string(global_token->s), strings_list);
  414. global_token = global_token->next;
  415. }
  416. else
  417. {
  418. char* s = calloc(MAX_STRING, sizeof(char));
  419. /* prefix leading string */
  420. s[0] = '"';
  421. int i = 1;
  422. int j;
  423. while('"' == global_token->s[0])
  424. {
  425. /* Step past the leading '"' */
  426. j = 1;
  427. /* Copy the rest of the string as is */
  428. while(0 != global_token->s[j])
  429. {
  430. require(i < MAX_STRING, "concat string exceeded max string length\n");
  431. s[i] = global_token->s[j];
  432. i = i + 1;
  433. j = j + 1;
  434. }
  435. /* Move on to the next token */
  436. global_token = global_token->next;
  437. require(NULL != global_token, "multi-string null is not valid C\n");
  438. }
  439. /* Now use it */
  440. strings_list = emit(parse_string(s), strings_list);
  441. }
  442. }
  443. void primary_expr_char()
  444. {
  445. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("LOADI R0 ");
  446. else if(X86 == Architecture) emit_out("LOAD_IMMEDIATE_eax %");
  447. else if(AMD64 == Architecture) emit_out("LOAD_IMMEDIATE_rax %");
  448. else if(ARMV7L == Architecture) emit_out("!");
  449. else if(AARCH64 == Architecture) emit_out("LOAD_W0_AHEAD\nSKIP_32_DATA\n%");
  450. emit_out(numerate_number(escape_lookup(global_token->s + 1)));
  451. if(ARMV7L == Architecture) emit_out(" R0 LOADI8_ALWAYS");
  452. emit_out("\n");
  453. global_token = global_token->next;
  454. }
  455. void primary_expr_number()
  456. {
  457. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  458. {
  459. int size = fixup_int32(numerate_string(global_token->s));
  460. if((32767 > size) && (size > -32768))
  461. {
  462. emit_out("LOADI R0 ");
  463. emit_out(global_token->s);
  464. }
  465. else
  466. {
  467. emit_out("LOADR R0 4\nJUMP 4\n'");
  468. emit_out(number_to_hex(size, register_size));
  469. emit_out("'");
  470. }
  471. }
  472. else if(X86 == Architecture)
  473. {
  474. emit_out("LOAD_IMMEDIATE_eax %");
  475. emit_out(global_token->s);
  476. }
  477. else if(AMD64 == Architecture)
  478. {
  479. emit_out("LOAD_IMMEDIATE_rax %");
  480. emit_out(global_token->s);
  481. }
  482. else if(ARMV7L == Architecture)
  483. {
  484. emit_out("!0 R0 LOAD32 R15 MEMORY\n~0 JUMP_ALWAYS\n%");
  485. emit_out(global_token->s);
  486. }
  487. else if(AARCH64 == Architecture)
  488. {
  489. emit_out("LOAD_W0_AHEAD\nSKIP_32_DATA\n%");
  490. emit_out(global_token->s);
  491. }
  492. emit_out("\n");
  493. global_token = global_token->next;
  494. }
  495. void primary_expr_variable()
  496. {
  497. char* s = global_token->s;
  498. global_token = global_token->next;
  499. struct token_list* a = sym_lookup(s, global_constant_list);
  500. if(NULL != a)
  501. {
  502. constant_load(a);
  503. return;
  504. }
  505. a= sym_lookup(s, function->locals);
  506. if(NULL != a)
  507. {
  508. variable_load(a);
  509. return;
  510. }
  511. a = sym_lookup(s, function->arguments);
  512. if(NULL != a)
  513. {
  514. variable_load(a);
  515. return;
  516. }
  517. a= sym_lookup(s, global_function_list);
  518. if(NULL != a)
  519. {
  520. function_load(a);
  521. return;
  522. }
  523. a = sym_lookup(s, global_symbol_list);
  524. if(NULL != a)
  525. {
  526. global_load(a);
  527. return;
  528. }
  529. line_error();
  530. file_print(s ,stderr);
  531. file_print(" is not a defined symbol\n", stderr);
  532. exit(EXIT_FAILURE);
  533. }
  534. void primary_expr();
  535. struct type* promote_type(struct type* a, struct type* b)
  536. {
  537. require(NULL != b, "impossible case 1 in promote_type\n");
  538. require(NULL != a, "impossible case 2 in promote_type\n");
  539. if(a == b) return a;
  540. struct type* i;
  541. for(i = global_types; NULL != i; i = i->next)
  542. {
  543. if(a->name == i->name) break;
  544. if(b->name == i->name) break;
  545. if(a->name == i->indirect->name) break;
  546. if(b->name == i->indirect->name) break;
  547. if(a->name == i->indirect->indirect->name) break;
  548. if(b->name == i->indirect->indirect->name) break;
  549. }
  550. require(NULL != i, "impossible case 3 in promote_type\n");
  551. return i;
  552. }
  553. void common_recursion(FUNCTION f)
  554. {
  555. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("PUSHR R0 R15\t#_common_recursion\n");
  556. else if(X86 == Architecture) emit_out("PUSH_eax\t#_common_recursion\n");
  557. else if(AMD64 == Architecture) emit_out("PUSH_RAX\t#_common_recursion\n");
  558. else if(ARMV7L == Architecture) emit_out("{R0} PUSH_ALWAYS\t#_common_recursion\n");
  559. else if(AARCH64 == Architecture) emit_out("PUSH_X0\t#_common_recursion\n");
  560. struct type* last_type = current_target;
  561. global_token = global_token->next;
  562. require(NULL != global_token, "Recieved EOF in common_recursion\n");
  563. f();
  564. current_target = promote_type(current_target, last_type);
  565. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("POPR R1 R15\t# _common_recursion\n");
  566. else if(X86 == Architecture) emit_out("POP_ebx\t# _common_recursion\n");
  567. else if(AMD64 == Architecture) emit_out("POP_RBX\t# _common_recursion\n");
  568. else if(ARMV7L == Architecture) emit_out("{R1} POP_ALWAYS\t# _common_recursion\n");
  569. else if(AARCH64 == Architecture) emit_out("POP_X1\t# _common_recursion\n");
  570. }
  571. void general_recursion(FUNCTION f, char* s, char* name, FUNCTION iterate)
  572. {
  573. require(NULL != global_token, "Recieved EOF in general_recursion\n");
  574. if(match(name, global_token->s))
  575. {
  576. common_recursion(f);
  577. emit_out(s);
  578. iterate();
  579. }
  580. }
  581. void arithmetic_recursion(FUNCTION f, char* s1, char* s2, char* name, FUNCTION iterate)
  582. {
  583. require(NULL != global_token, "Recieved EOF in arithmetic_recursion\n");
  584. if(match(name, global_token->s))
  585. {
  586. common_recursion(f);
  587. if(NULL == current_target)
  588. {
  589. emit_out(s1);
  590. }
  591. else if(current_target->is_signed)
  592. {
  593. emit_out(s1);
  594. }
  595. else
  596. {
  597. emit_out(s2);
  598. }
  599. iterate();
  600. }
  601. }
  602. int ceil_log2(int a)
  603. {
  604. int result = 0;
  605. if((a & (a - 1)) == 0)
  606. {
  607. result = -1;
  608. }
  609. while(a > 0)
  610. {
  611. result = result + 1;
  612. a = a >> 1;
  613. }
  614. if(ARMV7L == Architecture) return (result >> 1);
  615. return result;
  616. }
  617. /*
  618. * postfix-expr:
  619. * primary-expr
  620. * postfix-expr [ expression ]
  621. * postfix-expr ( expression-list-opt )
  622. * postfix-expr -> member
  623. */
  624. struct type* lookup_member(struct type* parent, char* name);
  625. void postfix_expr_arrow()
  626. {
  627. emit_out("# looking up offset\n");
  628. global_token = global_token->next;
  629. require(NULL != global_token, "naked -> not allowed\n");
  630. struct type* i = lookup_member(current_target, global_token->s);
  631. current_target = i->type;
  632. global_token = global_token->next;
  633. require(NULL != global_token, "Unterminated -> expression not allowed\n");
  634. if(0 != i->offset)
  635. {
  636. emit_out("# -> offset calculation\n");
  637. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  638. {
  639. emit_out("ADDUI R0 R0 ");
  640. emit_out(numerate_number(i->offset));
  641. emit_out("\n");
  642. }
  643. else if(X86 == Architecture)
  644. {
  645. emit_out("LOAD_IMMEDIATE_ebx %");
  646. emit_out(numerate_number(i->offset));
  647. emit_out("\nADD_ebx_to_eax\n");
  648. }
  649. else if(AMD64 == Architecture)
  650. {
  651. emit_out("LOAD_IMMEDIATE_rbx %");
  652. emit_out(numerate_number(i->offset));
  653. emit_out("\nADD_rbx_to_rax\n");
  654. }
  655. else if(ARMV7L == Architecture)
  656. {
  657. emit_out("!0 R1 LOAD32 R15 MEMORY\n~0 JUMP_ALWAYS\n%");
  658. emit_out(numerate_number(i->offset));
  659. emit_out("\n'0' R0 R0 ADD R1 ARITH2_ALWAYS\n");
  660. }
  661. else if(AARCH64 == Architecture)
  662. {
  663. emit_out("LOAD_W1_AHEAD\nSKIP_32_DATA\n%");
  664. emit_out(numerate_number(i->offset));
  665. emit_out("\nADD_X0_X1_X0\n");
  666. }
  667. }
  668. if((!match("=", global_token->s) && (register_size >= i->size)))
  669. {
  670. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("LOAD R0 R0 0\n");
  671. else if(X86 == Architecture) emit_out("LOAD_INTEGER\n");
  672. else if(AMD64 == Architecture) emit_out("LOAD_INTEGER\n");
  673. else if(ARMV7L == Architecture) emit_out("!0 R0 LOAD32 R0 MEMORY\n");
  674. else if(AARCH64 == Architecture) emit_out("DEREF_X0\n");
  675. }
  676. }
  677. void postfix_expr_array()
  678. {
  679. struct type* array = current_target;
  680. common_recursion(expression);
  681. current_target = array;
  682. require(NULL != current_target, "Arrays only apply to variables\n");
  683. char* assign;
  684. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) assign = "LOAD R0 R0 0\n";
  685. else if(X86 == Architecture) assign = "LOAD_INTEGER\n";
  686. else if(AMD64 == Architecture) assign = "LOAD_INTEGER\n";
  687. else if(ARMV7L == Architecture) assign = "!0 R0 LOAD32 R0 MEMORY\n";
  688. else if(AARCH64 == Architecture) assign = "DEREF_X0\n";
  689. /* Add support for Ints */
  690. if(match("char*", current_target->name))
  691. {
  692. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) assign = "LOAD8 R0 R0 0\n";
  693. else if(X86 == Architecture) assign = "LOAD_BYTE\n";
  694. else if(AMD64 == Architecture) assign = "LOAD_BYTE\n";
  695. else if(ARMV7L == Architecture) assign = "!0 R0 LOAD8 R0 MEMORY\n";
  696. else if(AARCH64 == Architecture) assign = "DEREF_X0_BYTE\n";
  697. }
  698. else
  699. {
  700. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("SALI R0 ");
  701. else if(X86 == Architecture) emit_out("SAL_eax_Immediate8 !");
  702. else if(AMD64 == Architecture) emit_out("SAL_rax_Immediate8 !");
  703. else if(ARMV7L == Architecture) emit_out("'0' R0 R0 '");
  704. else if(AARCH64 == Architecture) emit_out("LOAD_W2_AHEAD\nSKIP_32_DATA\n%");
  705. emit_out(numerate_number(ceil_log2(current_target->indirect->size)));
  706. if(ARMV7L == Architecture) emit_out("' MOVE_ALWAYS");
  707. else if(AARCH64 == Architecture) emit_out("\nLSHIFT_X0_X0_X2");
  708. emit_out("\n");
  709. }
  710. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("ADD R0 R0 R1\n");
  711. else if(X86 == Architecture) emit_out("ADD_ebx_to_eax\n");
  712. else if(AMD64 == Architecture) emit_out("ADD_rbx_to_rax\n");
  713. else if(ARMV7L == Architecture) emit_out("'0' R0 R0 ADD R1 ARITH2_ALWAYS\n");
  714. else if(AARCH64 == Architecture) emit_out("ADD_X0_X1_X0\n");
  715. require_match("ERROR in postfix_expr\nMissing ]\n", "]");
  716. if(match("=", global_token->s))
  717. {
  718. assign = "";
  719. }
  720. emit_out(assign);
  721. }
  722. /*
  723. * unary-expr:
  724. * postfix-expr
  725. * - postfix-expr
  726. * !postfix-expr
  727. * sizeof ( type )
  728. */
  729. struct type* type_name();
  730. void unary_expr_sizeof()
  731. {
  732. global_token = global_token->next;
  733. require(NULL != global_token, "Recieved EOF when starting sizeof\n");
  734. require_match("ERROR in unary_expr\nMissing (\n", "(");
  735. struct type* a = type_name();
  736. require_match("ERROR in unary_expr\nMissing )\n", ")");
  737. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("LOADUI R0 ");
  738. else if(X86 == Architecture) emit_out("LOAD_IMMEDIATE_eax %");
  739. else if(AMD64 == Architecture) emit_out("LOAD_IMMEDIATE_rax %");
  740. else if(ARMV7L == Architecture) emit_out("!");
  741. else if(AARCH64 == Architecture) emit_out("LOAD_W0_AHEAD\nSKIP_32_DATA\n%");
  742. emit_out(numerate_number(a->size));
  743. if(ARMV7L == Architecture) emit_out(" R0 LOADI8_ALWAYS");
  744. emit_out("\n");
  745. }
  746. void postfix_expr_stub()
  747. {
  748. require(NULL != global_token, "Unexpected EOF, improperly terminated primary expression\n");
  749. if(match("[", global_token->s))
  750. {
  751. postfix_expr_array();
  752. postfix_expr_stub();
  753. }
  754. if(match("->", global_token->s))
  755. {
  756. postfix_expr_arrow();
  757. postfix_expr_stub();
  758. }
  759. }
  760. void postfix_expr()
  761. {
  762. primary_expr();
  763. postfix_expr_stub();
  764. }
  765. /*
  766. * additive-expr:
  767. * postfix-expr
  768. * additive-expr * postfix-expr
  769. * additive-expr / postfix-expr
  770. * additive-expr % postfix-expr
  771. * additive-expr + postfix-expr
  772. * additive-expr - postfix-expr
  773. * additive-expr << postfix-expr
  774. * additive-expr >> postfix-expr
  775. */
  776. void additive_expr_stub()
  777. {
  778. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  779. {
  780. arithmetic_recursion(postfix_expr, "ADD R0 R1 R0\n", "ADDU R0 R1 R0\n", "+", additive_expr_stub);
  781. arithmetic_recursion(postfix_expr, "SUB R0 R1 R0\n", "SUBU R0 R1 R0\n", "-", additive_expr_stub);
  782. arithmetic_recursion(postfix_expr, "MUL R0 R1 R0\n", "MULU R0 R1 R0\n", "*", additive_expr_stub);
  783. arithmetic_recursion(postfix_expr, "DIV R0 R1 R0\n", "DIVU R0 R1 R0\n", "/", additive_expr_stub);
  784. arithmetic_recursion(postfix_expr, "MOD R0 R1 R0\n", "MODU R0 R1 R0\n", "%", additive_expr_stub);
  785. arithmetic_recursion(postfix_expr, "SAL R0 R1 R0\n", "SL0 R0 R1 R0\n", "<<", additive_expr_stub);
  786. arithmetic_recursion(postfix_expr, "SAR R0 R1 R0\n", "SR0 R0 R1 R0\n", ">>", additive_expr_stub);
  787. }
  788. else if(X86 == Architecture)
  789. {
  790. arithmetic_recursion(postfix_expr, "ADD_ebx_to_eax\n", "ADD_ebx_to_eax\n", "+", additive_expr_stub);
  791. arithmetic_recursion(postfix_expr, "SUBTRACT_eax_from_ebx_into_ebx\nMOVE_ebx_to_eax\n", "SUBTRACT_eax_from_ebx_into_ebx\nMOVE_ebx_to_eax\n", "-", additive_expr_stub);
  792. arithmetic_recursion(postfix_expr, "MULTIPLYS_eax_by_ebx_into_eax\n", "MULTIPLY_eax_by_ebx_into_eax\n", "*", additive_expr_stub);
  793. arithmetic_recursion(postfix_expr, "XCHG_eax_ebx\nCDTQ\nDIVIDES_eax_by_ebx_into_eax\n", "XCHG_eax_ebx\nLOAD_IMMEDIATE_edx %0\nDIVIDE_eax_by_ebx_into_eax\n", "/", additive_expr_stub);
  794. arithmetic_recursion(postfix_expr, "XCHG_eax_ebx\nCDTQ\nMODULUSS_eax_from_ebx_into_ebx\nMOVE_edx_to_eax\n", "XCHG_eax_ebx\nLOAD_IMMEDIATE_edx %0\nMODULUS_eax_from_ebx_into_ebx\nMOVE_edx_to_eax\n", "%", additive_expr_stub);
  795. arithmetic_recursion(postfix_expr, "COPY_eax_to_ecx\nCOPY_ebx_to_eax\nSAL_eax_cl\n", "COPY_eax_to_ecx\nCOPY_ebx_to_eax\nSHL_eax_cl\n", "<<", additive_expr_stub);
  796. arithmetic_recursion(postfix_expr, "COPY_eax_to_ecx\nCOPY_ebx_to_eax\nSAR_eax_cl\n", "COPY_eax_to_ecx\nCOPY_ebx_to_eax\nSHR_eax_cl\n", ">>", additive_expr_stub);
  797. }
  798. else if(AMD64 == Architecture)
  799. {
  800. arithmetic_recursion(postfix_expr, "ADD_rbx_to_rax\n", "ADD_rbx_to_rax\n", "+", additive_expr_stub);
  801. arithmetic_recursion(postfix_expr, "SUBTRACT_rax_from_rbx_into_rbx\nMOVE_rbx_to_rax\n", "SUBTRACT_rax_from_rbx_into_rbx\nMOVE_rbx_to_rax\n", "-", additive_expr_stub);
  802. arithmetic_recursion(postfix_expr, "MULTIPLYS_rax_by_rbx_into_rax\n", "MULTIPLY_rax_by_rbx_into_rax\n", "*", additive_expr_stub);
  803. arithmetic_recursion(postfix_expr, "XCHG_rax_rbx\nCQTO\nDIVIDES_rax_by_rbx_into_rax\n", "XCHG_rax_rbx\nLOAD_IMMEDIATE_rdx %0\nDIVIDE_rax_by_rbx_into_rax\n", "/", additive_expr_stub);
  804. arithmetic_recursion(postfix_expr, "XCHG_rax_rbx\nCQTO\nMODULUSS_rax_from_rbx_into_rbx\nMOVE_rdx_to_rax\n", "XCHG_rax_rbx\nLOAD_IMMEDIATE_rdx %0\nMODULUS_rax_from_rbx_into_rbx\nMOVE_rdx_to_rax\n", "%", additive_expr_stub);
  805. arithmetic_recursion(postfix_expr, "COPY_rax_to_rcx\nCOPY_rbx_to_rax\nSAL_rax_cl\n", "COPY_rax_to_rcx\nCOPY_rbx_to_rax\nSHL_rax_cl\n", "<<", additive_expr_stub);
  806. arithmetic_recursion(postfix_expr, "COPY_rax_to_rcx\nCOPY_rbx_to_rax\nSAR_rax_cl\n", "COPY_rax_to_rcx\nCOPY_rbx_to_rax\nSHR_rax_cl\n", ">>", additive_expr_stub);
  807. }
  808. else if(ARMV7L == Architecture)
  809. {
  810. arithmetic_recursion(postfix_expr, "'0' R0 R0 ADD R1 ARITH2_ALWAYS\n", "'0' R0 R0 ADD R1 ARITH2_ALWAYS\n", "+", additive_expr_stub);
  811. arithmetic_recursion(postfix_expr, "'0' R0 R0 SUB R1 ARITH2_ALWAYS\n", "'0' R0 R0 SUB R1 ARITH2_ALWAYS\n", "-", additive_expr_stub);
  812. arithmetic_recursion(postfix_expr, "'9' R0 '0' R1 MULS R0 ARITH2_ALWAYS\n", "'9' R0 '0' R1 MUL R0 ARITH2_ALWAYS\n", "*", additive_expr_stub);
  813. arithmetic_recursion(postfix_expr, "{LR} PUSH_ALWAYS\n^~divides CALL_ALWAYS\n{LR} POP_ALWAYS\n", "{LR} PUSH_ALWAYS\n^~divide CALL_ALWAYS\n{LR} POP_ALWAYS\n", "/", additive_expr_stub);
  814. arithmetic_recursion(postfix_expr, "{LR} PUSH_ALWAYS\n^~moduluss CALL_ALWAYS\n{LR} POP_ALWAYS\n", "{LR} PUSH_ALWAYS\n^~modulus CALL_ALWAYS\n{LR} POP_ALWAYS\n", "%", additive_expr_stub);
  815. arithmetic_recursion(postfix_expr, "LEFT R1 R0 R0 SHIFT AUX_ALWAYS\n", "LEFT R1 R0 R0 SHIFT AUX_ALWAYS\n", "<<", additive_expr_stub);
  816. arithmetic_recursion(postfix_expr, "ARITH_RIGHT R1 R0 R0 SHIFT AUX_ALWAYS\n", "RIGHT R1 R0 R0 SHIFT AUX_ALWAYS\n", ">>", additive_expr_stub);
  817. }
  818. else if(AARCH64 == Architecture)
  819. {
  820. general_recursion(postfix_expr, "ADD_X0_X1_X0\n", "+", additive_expr_stub);
  821. general_recursion(postfix_expr, "SUB_X0_X1_X0\n", "-", additive_expr_stub);
  822. general_recursion(postfix_expr, "MUL_X0_X1_X0\n", "*", additive_expr_stub);
  823. arithmetic_recursion(postfix_expr, "SDIV_X0_X1_X0\n", "UDIV_X0_X1_X0\n", "/", additive_expr_stub);
  824. arithmetic_recursion(postfix_expr, "SDIV_X2_X1_X0\nMSUB_X0_X0_X2_X1\n", "UDIV_X2_X1_X0\nMSUB_X0_X0_X2_X1\n", "%", additive_expr_stub);
  825. general_recursion(postfix_expr, "LSHIFT_X0_X1_X0\n", "<<", additive_expr_stub);
  826. arithmetic_recursion(postfix_expr, "ARITH_RSHIFT_X0_X1_X0\n", "LOGICAL_RSHIFT_X0_X1_X0\n", ">>", additive_expr_stub);
  827. }
  828. }
  829. void additive_expr()
  830. {
  831. postfix_expr();
  832. additive_expr_stub();
  833. }
  834. /*
  835. * relational-expr:
  836. * additive_expr
  837. * relational-expr < additive_expr
  838. * relational-expr <= additive_expr
  839. * relational-expr >= additive_expr
  840. * relational-expr > additive_expr
  841. */
  842. void relational_expr_stub()
  843. {
  844. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  845. {
  846. arithmetic_recursion(additive_expr, "CMP R0 R1 R0\nSET.L R0 R0 1\n", "CMPU R0 R1 R0\nSET.L R0 R0 1\n", "<", relational_expr_stub);
  847. arithmetic_recursion(additive_expr, "CMP R0 R1 R0\nSET.LE R0 R0 1\n", "CMPU R0 R1 R0\nSET.LE R0 R0 1\n", "<=", relational_expr_stub);
  848. arithmetic_recursion(additive_expr, "CMP R0 R1 R0\nSET.GE R0 R0 1\n", "CMPU R0 R1 R0\nSET.GE R0 R0 1\n", ">=", relational_expr_stub);
  849. arithmetic_recursion(additive_expr, "CMP R0 R1 R0\nSET.G R0 R0 1\n", "CMPU R0 R1 R0\nSET.G R0 R0 1\n", ">", relational_expr_stub);
  850. arithmetic_recursion(additive_expr, "CMP R0 R1 R0\nSET.E R0 R0 1\n", "CMPU R0 R1 R0\nSET.E R0 R0 1\n", "==", relational_expr_stub);
  851. arithmetic_recursion(additive_expr, "CMP R0 R1 R0\nSET.NE R0 R0 1\n", "CMPU R0 R1 R0\nSET.NE R0 R0 1\n", "!=", relational_expr_stub);
  852. }
  853. else if(X86 == Architecture)
  854. {
  855. arithmetic_recursion(additive_expr, "CMP\nSETL\nMOVEZBL\n", "CMP\nSETB\nMOVEZBL\n", "<", relational_expr_stub);
  856. arithmetic_recursion(additive_expr, "CMP\nSETLE\nMOVEZBL\n", "CMP\nSETBE\nMOVEZBL\n", "<=", relational_expr_stub);
  857. arithmetic_recursion(additive_expr, "CMP\nSETGE\nMOVEZBL\n", "CMP\nSETAE\nMOVEZBL\n", ">=", relational_expr_stub);
  858. arithmetic_recursion(additive_expr, "CMP\nSETG\nMOVEZBL\n", "CMP\nSETA\nMOVEZBL\n", ">", relational_expr_stub);
  859. general_recursion(additive_expr, "CMP\nSETE\nMOVEZBL\n", "==", relational_expr_stub);
  860. general_recursion(additive_expr, "CMP\nSETNE\nMOVEZBL\n", "!=", relational_expr_stub);
  861. }
  862. else if(AMD64 == Architecture)
  863. {
  864. arithmetic_recursion(additive_expr, "CMP\nSETL\nMOVEZX\n", "CMP\nSETB\nMOVEZX\n", "<", relational_expr_stub);
  865. arithmetic_recursion(additive_expr, "CMP\nSETLE\nMOVEZX\n", "CMP\nSETBE\nMOVEZX\n", "<=", relational_expr_stub);
  866. arithmetic_recursion(additive_expr, "CMP\nSETGE\nMOVEZX\n", "CMP\nSETAE\nMOVEZX\n", ">=", relational_expr_stub);
  867. arithmetic_recursion(additive_expr, "CMP\nSETG\nMOVEZX\n", "CMP\nSETA\nMOVEZX\n", ">", relational_expr_stub);
  868. general_recursion(additive_expr, "CMP\nSETE\nMOVEZX\n", "==", relational_expr_stub);
  869. general_recursion(additive_expr, "CMP\nSETNE\nMOVEZX\n", "!=", relational_expr_stub);
  870. }
  871. else if(ARMV7L == Architecture)
  872. {
  873. arithmetic_recursion(additive_expr, "'0' R0 CMP R1 AUX_ALWAYS\n!0 R0 LOADI8_ALWAYS\n!1 R0 LOADI8_L\n", "'0' R0 CMP R1 AUX_ALWAYS\n!0 R0 LOADI8_ALWAYS\n!1 R0 LOADI8_LO\n", "<", relational_expr_stub);
  874. arithmetic_recursion(additive_expr, "'0' R0 CMP R1 AUX_ALWAYS\n!0 R0 LOADI8_ALWAYS\n!1 R0 LOADI8_LE\n", "'0' R0 CMP R1 AUX_ALWAYS\n!0 R0 LOADI8_ALWAYS\n!1 R0 LOADI8_LS\n", "<=", relational_expr_stub);
  875. arithmetic_recursion(additive_expr, "'0' R0 CMP R1 AUX_ALWAYS\n!0 R0 LOADI8_ALWAYS\n!1 R0 LOADI8_GE\n", "'0' R0 CMP R1 AUX_ALWAYS\n!0 R0 LOADI8_ALWAYS\n!1 R0 LOADI8_HS\n", ">=", relational_expr_stub);
  876. arithmetic_recursion(additive_expr, "'0' R0 CMP R1 AUX_ALWAYS\n!0 R0 LOADI8_ALWAYS\n!1 R0 LOADI8_G\n", "'0' R0 CMP R1 AUX_ALWAYS\n!0 R0 LOADI8_ALWAYS\n!1 R0 LOADI8_HI\n", ">", relational_expr_stub);
  877. general_recursion(additive_expr, "'0' R0 CMP R1 AUX_ALWAYS\n!0 R0 LOADI8_ALWAYS\n!1 R0 LOADI8_EQUAL\n", "==", relational_expr_stub);
  878. general_recursion(additive_expr, "'0' R0 CMP R1 AUX_ALWAYS\n!0 R0 LOADI8_ALWAYS\n!1 R0 LOADI8_NE\n", "!=", relational_expr_stub);
  879. }
  880. else if(AARCH64 == Architecture)
  881. {
  882. arithmetic_recursion(additive_expr, "CMP_X1_X0\nSET_X0_TO_1\nSKIP_INST_LT\nSET_X0_TO_0\n", "CMP_X1_X0\nSET_X0_TO_1\nSKIP_INST_LO\nSET_X0_TO_0\n", "<", relational_expr_stub);
  883. arithmetic_recursion(additive_expr, "CMP_X1_X0\nSET_X0_TO_1\nSKIP_INST_LE\nSET_X0_TO_0\n", "CMP_X1_X0\nSET_X0_TO_1\nSKIP_INST_LS\nSET_X0_TO_0\n", "<=", relational_expr_stub);
  884. arithmetic_recursion(additive_expr, "CMP_X1_X0\nSET_X0_TO_1\nSKIP_INST_GE\nSET_X0_TO_0\n", "CMP_X1_X0\nSET_X0_TO_1\nSKIP_INST_HS\nSET_X0_TO_0\n", ">=", relational_expr_stub);
  885. arithmetic_recursion(additive_expr, "CMP_X1_X0\nSET_X0_TO_1\nSKIP_INST_GT\nSET_X0_TO_0\n", "CMP_X1_X0\nSET_X0_TO_1\nSKIP_INST_HI\nSET_X0_TO_0\n", ">", relational_expr_stub);
  886. general_recursion(additive_expr, "CMP_X1_X0\nSET_X0_TO_1\nSKIP_INST_EQ\nSET_X0_TO_0\n", "==", relational_expr_stub);
  887. general_recursion(additive_expr, "CMP_X1_X0\nSET_X0_TO_1\nSKIP_INST_NE\nSET_X0_TO_0\n", "!=", relational_expr_stub);
  888. }
  889. }
  890. void relational_expr()
  891. {
  892. additive_expr();
  893. relational_expr_stub();
  894. }
  895. /*
  896. * bitwise-expr:
  897. * relational-expr
  898. * bitwise-expr & bitwise-expr
  899. * bitwise-expr && bitwise-expr
  900. * bitwise-expr | bitwise-expr
  901. * bitwise-expr || bitwise-expr
  902. * bitwise-expr ^ bitwise-expr
  903. */
  904. void bitwise_expr_stub()
  905. {
  906. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  907. {
  908. general_recursion(relational_expr, "AND R0 R0 R1\n", "&", bitwise_expr_stub);
  909. general_recursion(relational_expr, "AND R0 R0 R1\n", "&&", bitwise_expr_stub);
  910. general_recursion(relational_expr, "OR R0 R0 R1\n", "|", bitwise_expr_stub);
  911. general_recursion(relational_expr, "OR R0 R0 R1\n", "||", bitwise_expr_stub);
  912. general_recursion(relational_expr, "XOR R0 R0 R1\n", "^", bitwise_expr_stub);
  913. }
  914. else if(X86 == Architecture)
  915. {
  916. general_recursion(relational_expr, "AND_eax_ebx\n", "&", bitwise_expr_stub);
  917. general_recursion(relational_expr, "AND_eax_ebx\n", "&&", bitwise_expr_stub);
  918. general_recursion(relational_expr, "OR_eax_ebx\n", "|", bitwise_expr_stub);
  919. general_recursion(relational_expr, "OR_eax_ebx\n", "||", bitwise_expr_stub);
  920. general_recursion(relational_expr, "XOR_ebx_eax_into_eax\n", "^", bitwise_expr_stub);
  921. }
  922. else if(AMD64 == Architecture)
  923. {
  924. general_recursion(relational_expr, "AND_rax_rbx\n", "&", bitwise_expr_stub);
  925. general_recursion(relational_expr, "AND_rax_rbx\n", "&&", bitwise_expr_stub);
  926. general_recursion(relational_expr, "OR_rax_rbx\n", "|", bitwise_expr_stub);
  927. general_recursion(relational_expr, "OR_rax_rbx\n", "||", bitwise_expr_stub);
  928. general_recursion(relational_expr, "XOR_rbx_rax_into_rax\n", "^", bitwise_expr_stub);
  929. }
  930. else if(ARMV7L == Architecture)
  931. {
  932. general_recursion(relational_expr, "NO_SHIFT R0 R0 AND R1 ARITH2_ALWAYS\n", "&", bitwise_expr_stub);
  933. general_recursion(relational_expr, "NO_SHIFT R0 R0 AND R1 ARITH2_ALWAYS\n", "&&", bitwise_expr_stub);
  934. general_recursion(relational_expr, "NO_SHIFT R0 R0 OR R1 AUX_ALWAYS\n", "|", bitwise_expr_stub);
  935. general_recursion(relational_expr, "NO_SHIFT R0 R0 OR R1 AUX_ALWAYS\n", "||", bitwise_expr_stub);
  936. general_recursion(relational_expr, "'0' R0 R0 XOR R1 ARITH2_ALWAYS\n", "^", bitwise_expr_stub);
  937. }
  938. else if(AARCH64 == Architecture)
  939. {
  940. general_recursion(relational_expr, "AND_X0_X1_X0\n", "&", bitwise_expr_stub);
  941. general_recursion(relational_expr, "AND_X0_X1_X0\n", "&&", bitwise_expr_stub);
  942. general_recursion(relational_expr, "OR_X0_X1_X0\n", "|", bitwise_expr_stub);
  943. general_recursion(relational_expr, "OR_X0_X1_X0\n", "||", bitwise_expr_stub);
  944. general_recursion(relational_expr, "XOR_X0_X1_X0\n", "^", bitwise_expr_stub);
  945. }
  946. }
  947. void bitwise_expr()
  948. {
  949. relational_expr();
  950. bitwise_expr_stub();
  951. }
  952. /*
  953. * expression:
  954. * bitwise-or-expr
  955. * bitwise-or-expr = expression
  956. */
  957. void primary_expr()
  958. {
  959. require(NULL != global_token, "Recieved EOF where primary expression expected\n");
  960. if(match("&", global_token->s))
  961. {
  962. Address_of = TRUE;
  963. global_token = global_token->next;
  964. require(NULL != global_token, "Recieved EOF after & where primary expression expected\n");
  965. }
  966. else
  967. {
  968. Address_of = FALSE;
  969. }
  970. if(match("sizeof", global_token->s)) unary_expr_sizeof();
  971. else if('-' == global_token->s[0])
  972. {
  973. if(X86 == Architecture) emit_out("LOAD_IMMEDIATE_eax %0\n");
  974. else if(AMD64 == Architecture) emit_out("LOAD_IMMEDIATE_rax %0\n");
  975. else if(ARMV7L == Architecture) emit_out("!0 R0 LOADI8_ALWAYS\n");
  976. else if(AARCH64 == Architecture) emit_out("SET_X0_TO_0\n");
  977. common_recursion(primary_expr);
  978. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("NEG R0 R0\n");
  979. else if(X86 == Architecture) emit_out("SUBTRACT_eax_from_ebx_into_ebx\nMOVE_ebx_to_eax\n");
  980. else if(AMD64 == Architecture) emit_out("SUBTRACT_rax_from_rbx_into_rbx\nMOVE_rbx_to_rax\n");
  981. else if(ARMV7L == Architecture) emit_out("'0' R0 R0 SUB R1 ARITH2_ALWAYS\n");
  982. else if(AARCH64 == Architecture) emit_out("SUB_X0_X1_X0\n");
  983. }
  984. else if('!' == global_token->s[0])
  985. {
  986. if(X86 == Architecture) emit_out("LOAD_IMMEDIATE_eax %1\n");
  987. else if(AMD64 == Architecture) emit_out("LOAD_IMMEDIATE_rax %1\n");
  988. else if(ARMV7L == Architecture) emit_out("!1 R0 LOADI8_ALWAYS\n");
  989. else if(AARCH64 == Architecture) emit_out("SET_X0_TO_1\n");
  990. common_recursion(postfix_expr);
  991. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("XORI R0 R0 1\n");
  992. else if(X86 == Architecture) emit_out("XOR_ebx_eax_into_eax\n");
  993. else if(AMD64 == Architecture) emit_out("XOR_rbx_rax_into_rax\n");
  994. else if(ARMV7L == Architecture) emit_out("'0' R0 R0 XOR R1 ARITH2_ALWAYS\n");
  995. else if(AARCH64 == Architecture) emit_out("XOR_X0_X1_X0\n");
  996. }
  997. else if('~' == global_token->s[0])
  998. {
  999. common_recursion(postfix_expr);
  1000. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("NOT R0 R0\n");
  1001. else if(X86 == Architecture) emit_out("NOT_eax\n");
  1002. else if(AMD64 == Architecture) emit_out("NOT_rax\n");
  1003. else if(ARMV7L == Architecture) emit_out("'0' R0 R0 MVN_ALWAYS\n");
  1004. else if(AARCH64 == Architecture) emit_out("MVN_X0\n");
  1005. }
  1006. else if(global_token->s[0] == '(')
  1007. {
  1008. global_token = global_token->next;
  1009. expression();
  1010. require_match("Error in Primary expression\nDidn't get )\n", ")");
  1011. }
  1012. else if(global_token->s[0] == '\'') primary_expr_char();
  1013. else if(global_token->s[0] == '"') primary_expr_string();
  1014. else if(in_set(global_token->s[0], "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_")) primary_expr_variable();
  1015. else if(in_set(global_token->s[0], "0123456789")) primary_expr_number();
  1016. else primary_expr_failure();
  1017. }
  1018. void expression()
  1019. {
  1020. bitwise_expr();
  1021. if(match("=", global_token->s))
  1022. {
  1023. char* store = "";
  1024. if(!match("]", global_token->prev->s) || !match("char*", current_target->name))
  1025. {
  1026. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) store = "STORE R0 R1 0\n";
  1027. else if(X86 == Architecture) store = "STORE_INTEGER\n";
  1028. else if(AMD64 == Architecture) store = "STORE_INTEGER\n";
  1029. else if(ARMV7L == Architecture) store = "!0 R0 STORE32 R1 MEMORY\n";
  1030. else if(AARCH64 == Architecture) store = "STR_X0_[X1]\n";
  1031. }
  1032. else
  1033. {
  1034. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) store = "STORE8 R0 R1 0\n";
  1035. else if(X86 == Architecture) store = "STORE_CHAR\n";
  1036. else if(AMD64 == Architecture) store = "STORE_CHAR\n";
  1037. else if(ARMV7L == Architecture) store = "!0 R0 STORE8 R1 MEMORY\n";
  1038. else if(AARCH64 == Architecture) store = "STR_BYTE_W0_[X1]\n";
  1039. }
  1040. common_recursion(expression);
  1041. emit_out(store);
  1042. current_target = integer;
  1043. }
  1044. }
  1045. /* Process local variable */
  1046. void collect_local()
  1047. {
  1048. if(NULL != break_target_func)
  1049. {
  1050. file_print("Local variable initialized inside of loop in file: ", stderr);
  1051. line_error();
  1052. file_print("\nMove the variable outside of the loop to resolve\n", stderr);
  1053. file_print("Otherwise the binary will segfault while running\n", stderr);
  1054. exit(EXIT_FAILURE);
  1055. }
  1056. struct type* type_size = type_name();
  1057. struct token_list* a = sym_declare(global_token->s, type_size, function->locals);
  1058. if(match("main", function->s) && (NULL == function->locals))
  1059. {
  1060. if(KNIGHT_NATIVE == Architecture) a->depth = register_size;
  1061. else if(KNIGHT_POSIX == Architecture) a->depth = 20;
  1062. else if(X86 == Architecture) a->depth = -20;
  1063. else if(AMD64 == Architecture) a->depth = -40;
  1064. else if(ARMV7L == Architecture) a->depth = 16;
  1065. else if(AARCH64 == Architecture) a->depth = 32; /* argc, argv, envp and the local (8 bytes each) */
  1066. }
  1067. else if((NULL == function->arguments) && (NULL == function->locals))
  1068. {
  1069. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) a->depth = register_size;
  1070. else if(X86 == Architecture) a->depth = -8;
  1071. else if(AMD64 == Architecture) a->depth = -16;
  1072. else if(ARMV7L == Architecture) a->depth = 8;
  1073. else if(AARCH64 == Architecture) a->depth = register_size;
  1074. }
  1075. else if(NULL == function->locals)
  1076. {
  1077. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) a->depth = function->arguments->depth + 8;
  1078. else if(X86 == Architecture) a->depth = function->arguments->depth - 8;
  1079. else if(AMD64 == Architecture) a->depth = function->arguments->depth - 16;
  1080. else if(ARMV7L == Architecture) a->depth = function->arguments->depth + 8;
  1081. else if(AARCH64 == Architecture) a->depth = function->arguments->depth + register_size;
  1082. }
  1083. else
  1084. {
  1085. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) a->depth = function->locals->depth + register_size;
  1086. else if(X86 == Architecture) a->depth = function->locals->depth - register_size;
  1087. else if(AMD64 == Architecture) a->depth = function->locals->depth - register_size;
  1088. else if(ARMV7L == Architecture) a->depth = function->locals->depth + register_size;
  1089. else if(AARCH64 == Architecture) a->depth = function->locals->depth + register_size;
  1090. }
  1091. function->locals = a;
  1092. emit_out("# Defining local ");
  1093. emit_out(global_token->s);
  1094. emit_out("\n");
  1095. global_token = global_token->next;
  1096. require(NULL != global_token, "incomplete local missing name\n");
  1097. if(match("=", global_token->s))
  1098. {
  1099. global_token = global_token->next;
  1100. require(NULL != global_token, "incomplete local assignment\n");
  1101. expression();
  1102. }
  1103. require_match("ERROR in collect_local\nMissing ;\n", ";");
  1104. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("PUSHR R0 R15\t#");
  1105. else if(X86 == Architecture) emit_out("PUSH_eax\t#");
  1106. else if(AMD64 == Architecture) emit_out("PUSH_RAX\t#");
  1107. else if(ARMV7L == Architecture) emit_out("{R0} PUSH_ALWAYS\t#");
  1108. else if(AARCH64 == Architecture) emit_out("PUSH_X0\t#");
  1109. emit_out(a->s);
  1110. emit_out("\n");
  1111. }
  1112. void statement();
  1113. /* Evaluate if statements */
  1114. void process_if()
  1115. {
  1116. char* number_string = numerate_number(current_count);
  1117. current_count = current_count + 1;
  1118. emit_out("# IF_");
  1119. uniqueID_out(function->s, number_string);
  1120. global_token = global_token->next;
  1121. require_match("ERROR in process_if\nMISSING (\n", "(");
  1122. expression();
  1123. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("JUMP.Z R0 @ELSE_");
  1124. else if(X86 == Architecture) emit_out("TEST\nJUMP_EQ %ELSE_");
  1125. else if(AMD64 == Architecture) emit_out("TEST\nJUMP_EQ %ELSE_");
  1126. else if(ARMV7L == Architecture) emit_out("!0 CMPI8 R0 IMM_ALWAYS\n^~ELSE_");
  1127. else if(AARCH64 == Architecture) emit_out("CBNZ_X0_PAST_BR\nLOAD_W16_AHEAD\nSKIP_32_DATA\n&ELSE_");
  1128. uniqueID_out(function->s, number_string);
  1129. if(ARMV7L == Architecture) emit_out(" JUMP_EQUAL\n");
  1130. else if(AARCH64 == Architecture) emit_out("\nBR_X16\n");
  1131. require_match("ERROR in process_if\nMISSING )\n", ")");
  1132. statement();
  1133. require(NULL != global_token, "Reached EOF inside of function\n");
  1134. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("JUMP @_END_IF_");
  1135. else if(X86 == Architecture) emit_out("JUMP %_END_IF_");
  1136. else if(AMD64 == Architecture) emit_out("JUMP %_END_IF_");
  1137. else if(ARMV7L == Architecture) emit_out("^~_END_IF_");
  1138. else if(AARCH64 == Architecture) emit_out("LOAD_W16_AHEAD\nSKIP_32_DATA\n&_END_IF_");
  1139. uniqueID_out(function->s, number_string);
  1140. if(ARMV7L == Architecture) emit_out(" JUMP_ALWAYS\n");
  1141. else if(AARCH64 == Architecture) emit_out("\nBR_X16\n");
  1142. emit_out(":ELSE_");
  1143. uniqueID_out(function->s, number_string);
  1144. if(match("else", global_token->s))
  1145. {
  1146. global_token = global_token->next;
  1147. require(NULL != global_token, "Recieved EOF where an else statement expected\n");
  1148. statement();
  1149. require(NULL != global_token, "Reached EOF inside of function\n");
  1150. }
  1151. emit_out(":_END_IF_");
  1152. uniqueID_out(function->s, number_string);
  1153. }
  1154. void process_for()
  1155. {
  1156. struct token_list* nested_locals = break_frame;
  1157. char* nested_break_head = break_target_head;
  1158. char* nested_break_func = break_target_func;
  1159. char* nested_break_num = break_target_num;
  1160. char* nested_continue_head = continue_target_head;
  1161. char* number_string = numerate_number(current_count);
  1162. current_count = current_count + 1;
  1163. break_target_head = "FOR_END_";
  1164. continue_target_head = "FOR_ITER_";
  1165. break_target_num = number_string;
  1166. break_frame = function->locals;
  1167. break_target_func = function->s;
  1168. emit_out("# FOR_initialization_");
  1169. uniqueID_out(function->s, number_string);
  1170. global_token = global_token->next;
  1171. require_match("ERROR in process_for\nMISSING (\n", "(");
  1172. if(!match(";",global_token->s))
  1173. {
  1174. expression();
  1175. }
  1176. emit_out(":FOR_");
  1177. uniqueID_out(function->s, number_string);
  1178. require_match("ERROR in process_for\nMISSING ;1\n", ";");
  1179. expression();
  1180. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("JUMP.Z R0 @FOR_END_");
  1181. else if(X86 == Architecture) emit_out("TEST\nJUMP_EQ %FOR_END_");
  1182. else if(AMD64 == Architecture) emit_out("TEST\nJUMP_EQ %FOR_END_");
  1183. else if(ARMV7L == Architecture) emit_out("!0 CMPI8 R0 IMM_ALWAYS\n^~FOR_END_");
  1184. else if(AARCH64 == Architecture) emit_out("CBNZ_X0_PAST_BR\nLOAD_W16_AHEAD\nSKIP_32_DATA\n&FOR_END_");
  1185. uniqueID_out(function->s, number_string);
  1186. if(ARMV7L == Architecture) emit_out(" JUMP_EQUAL\n");
  1187. else if(AARCH64 == Architecture) emit_out("\nBR_X16\n");
  1188. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("JUMP @FOR_THEN_");
  1189. else if(X86 == Architecture) emit_out("JUMP %FOR_THEN_");
  1190. else if(AMD64 == Architecture) emit_out("JUMP %FOR_THEN_");
  1191. else if(ARMV7L == Architecture) emit_out("^~FOR_THEN_");
  1192. else if(AARCH64 == Architecture) emit_out("LOAD_W16_AHEAD\nSKIP_32_DATA\n&FOR_THEN_");
  1193. uniqueID_out(function->s, number_string);
  1194. if(ARMV7L == Architecture) emit_out(" JUMP_ALWAYS\n");
  1195. else if(AARCH64 == Architecture) emit_out("\nBR_X16\n");
  1196. emit_out(":FOR_ITER_");
  1197. uniqueID_out(function->s, number_string);
  1198. require_match("ERROR in process_for\nMISSING ;2\n", ";");
  1199. expression();
  1200. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("JUMP @FOR_");
  1201. else if(X86 == Architecture) emit_out("JUMP %FOR_");
  1202. else if(AMD64 == Architecture) emit_out("JUMP %FOR_");
  1203. else if(ARMV7L == Architecture) emit_out("^~FOR_");
  1204. else if(AARCH64 == Architecture) emit_out("LOAD_W16_AHEAD\nSKIP_32_DATA\n&FOR_");
  1205. uniqueID_out(function->s, number_string);
  1206. if(ARMV7L == Architecture) emit_out(" JUMP_ALWAYS\n");
  1207. else if(AARCH64 == Architecture) emit_out("\nBR_X16\n");
  1208. emit_out(":FOR_THEN_");
  1209. uniqueID_out(function->s, number_string);
  1210. require_match("ERROR in process_for\nMISSING )\n", ")");
  1211. statement();
  1212. require(NULL != global_token, "Reached EOF inside of function\n");
  1213. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("JUMP @FOR_ITER_");
  1214. else if(X86 == Architecture) emit_out("JUMP %FOR_ITER_");
  1215. else if(AMD64 == Architecture) emit_out("JUMP %FOR_ITER_");
  1216. else if(ARMV7L == Architecture) emit_out("^~FOR_ITER_");
  1217. else if(AARCH64 == Architecture) emit_out("LOAD_W16_AHEAD\nSKIP_32_DATA\n&FOR_ITER_");
  1218. uniqueID_out(function->s, number_string);
  1219. if(ARMV7L == Architecture) emit_out(" JUMP_ALWAYS\n");
  1220. else if(AARCH64 == Architecture) emit_out("\nBR_X16\n");
  1221. emit_out(":FOR_END_");
  1222. uniqueID_out(function->s, number_string);
  1223. break_target_head = nested_break_head;
  1224. break_target_func = nested_break_func;
  1225. break_target_num = nested_break_num;
  1226. continue_target_head = nested_continue_head;
  1227. break_frame = nested_locals;
  1228. }
  1229. /* Process Assembly statements */
  1230. void process_asm()
  1231. {
  1232. global_token = global_token->next;
  1233. require_match("ERROR in process_asm\nMISSING (\n", "(");
  1234. while('"' == global_token->s[0])
  1235. {
  1236. emit_out((global_token->s + 1));
  1237. emit_out("\n");
  1238. global_token = global_token->next;
  1239. require(NULL != global_token, "Recieved EOF inside asm statement\n");
  1240. }
  1241. require_match("ERROR in process_asm\nMISSING )\n", ")");
  1242. require_match("ERROR in process_asm\nMISSING ;\n", ";");
  1243. }
  1244. /* Process do while loops */
  1245. void process_do()
  1246. {
  1247. struct token_list* nested_locals = break_frame;
  1248. char* nested_break_head = break_target_head;
  1249. char* nested_break_func = break_target_func;
  1250. char* nested_break_num = break_target_num;
  1251. char* nested_continue_head = continue_target_head;
  1252. char* number_string = numerate_number(current_count);
  1253. current_count = current_count + 1;
  1254. break_target_head = "DO_END_";
  1255. continue_target_head = "DO_TEST_";
  1256. break_target_num = number_string;
  1257. break_frame = function->locals;
  1258. break_target_func = function->s;
  1259. emit_out(":DO_");
  1260. uniqueID_out(function->s, number_string);
  1261. global_token = global_token->next;
  1262. require(NULL != global_token, "Recieved EOF where do statement is expected\n");
  1263. statement();
  1264. require(NULL != global_token, "Reached EOF inside of function\n");
  1265. emit_out(":DO_TEST_");
  1266. uniqueID_out(function->s, number_string);
  1267. require_match("ERROR in process_do\nMISSING while\n", "while");
  1268. require_match("ERROR in process_do\nMISSING (\n", "(");
  1269. expression();
  1270. require_match("ERROR in process_do\nMISSING )\n", ")");
  1271. require_match("ERROR in process_do\nMISSING ;\n", ";");
  1272. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("JUMP.NZ R0 @DO_");
  1273. else if(X86 == Architecture) emit_out("TEST\nJUMP_NE %DO_");
  1274. else if(AMD64 == Architecture) emit_out("TEST\nJUMP_NE %DO_");
  1275. else if(ARMV7L == Architecture) emit_out("!0 CMPI8 R0 IMM_ALWAYS\n^~DO_");
  1276. else if(AARCH64 == Architecture) emit_out("CBZ_X0_PAST_BR\nLOAD_W16_AHEAD\nSKIP_32_DATA\n&DO_");
  1277. uniqueID_out(function->s, number_string);
  1278. if(ARMV7L == Architecture) emit_out(" JUMP_NE\n");
  1279. else if(AARCH64 == Architecture) emit_out("\nBR_X16\n");
  1280. emit_out(":DO_END_");
  1281. uniqueID_out(function->s, number_string);
  1282. break_frame = nested_locals;
  1283. break_target_head = nested_break_head;
  1284. break_target_func = nested_break_func;
  1285. break_target_num = nested_break_num;
  1286. continue_target_head = nested_continue_head;
  1287. }
  1288. /* Process while loops */
  1289. void process_while()
  1290. {
  1291. struct token_list* nested_locals = break_frame;
  1292. char* nested_break_head = break_target_head;
  1293. char* nested_break_func = break_target_func;
  1294. char* nested_break_num = break_target_num;
  1295. char* nested_continue_head = continue_target_head;
  1296. char* number_string = numerate_number(current_count);
  1297. current_count = current_count + 1;
  1298. break_target_head = "END_WHILE_";
  1299. continue_target_head = "WHILE_";
  1300. break_target_num = number_string;
  1301. break_frame = function->locals;
  1302. break_target_func = function->s;
  1303. emit_out(":WHILE_");
  1304. uniqueID_out(function->s, number_string);
  1305. global_token = global_token->next;
  1306. require_match("ERROR in process_while\nMISSING (\n", "(");
  1307. expression();
  1308. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("JUMP.Z R0 @END_WHILE_");
  1309. else if(X86 == Architecture) emit_out("TEST\nJUMP_EQ %END_WHILE_");
  1310. else if(AMD64 == Architecture) emit_out("TEST\nJUMP_EQ %END_WHILE_");
  1311. else if(ARMV7L == Architecture) emit_out("!0 CMPI8 R0 IMM_ALWAYS\n^~END_WHILE_");
  1312. else if(AARCH64 == Architecture) emit_out("CBNZ_X0_PAST_BR\nLOAD_W16_AHEAD\nSKIP_32_DATA\n&END_WHILE_");
  1313. uniqueID_out(function->s, number_string);
  1314. if(ARMV7L == Architecture) emit_out(" JUMP_EQUAL\t");
  1315. else if(AARCH64 == Architecture) emit_out("\nBR_X16\n");
  1316. emit_out("# THEN_while_");
  1317. uniqueID_out(function->s, number_string);
  1318. require_match("ERROR in process_while\nMISSING )\n", ")");
  1319. statement();
  1320. require(NULL != global_token, "Reached EOF inside of function\n");
  1321. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("JUMP @WHILE_");
  1322. else if(X86 == Architecture) emit_out("JUMP %WHILE_");
  1323. else if(AMD64 == Architecture) emit_out("JUMP %WHILE_");
  1324. else if(ARMV7L == Architecture) emit_out("^~WHILE_");
  1325. else if(AARCH64 == Architecture) emit_out("LOAD_W16_AHEAD\nSKIP_32_DATA\n&WHILE_");
  1326. uniqueID_out(function->s, number_string);
  1327. if(ARMV7L == Architecture) emit_out(" JUMP_ALWAYS\n");
  1328. else if(AARCH64 == Architecture) emit_out("\nBR_X16\n");
  1329. emit_out(":END_WHILE_");
  1330. uniqueID_out(function->s, number_string);
  1331. break_target_head = nested_break_head;
  1332. break_target_func = nested_break_func;
  1333. break_target_num = nested_break_num;
  1334. continue_target_head = nested_continue_head;
  1335. break_frame = nested_locals;
  1336. }
  1337. /* Ensure that functions return */
  1338. void return_result()
  1339. {
  1340. global_token = global_token->next;
  1341. require(NULL != global_token, "Incomplete return statement recieved\n");
  1342. if(global_token->s[0] != ';') expression();
  1343. require_match("ERROR in return_result\nMISSING ;\n", ";");
  1344. struct token_list* i;
  1345. for(i = function->locals; NULL != i; i = i->next)
  1346. {
  1347. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("POPR R1 R15\t# _return_result_locals\n");
  1348. else if(X86 == Architecture) emit_out("POP_ebx\t# _return_result_locals\n");
  1349. else if(AMD64 == Architecture) emit_out("POP_RBX\t# _return_result_locals\n");
  1350. else if(ARMV7L == Architecture) emit_out("{R1} POP_ALWAYS\t# _return_result_locals\n");
  1351. else if(AARCH64 == Architecture) emit_out("POP_X1\t# _return_result_locals\n");
  1352. }
  1353. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("RET R15\n");
  1354. else if(X86 == Architecture) emit_out("RETURN\n");
  1355. else if(AMD64 == Architecture) emit_out("RETURN\n");
  1356. else if(ARMV7L == Architecture) emit_out("'1' LR RETURN\n");
  1357. else if(AARCH64 == Architecture) emit_out("RETURN\n");
  1358. }
  1359. void process_break()
  1360. {
  1361. if(NULL == break_target_head)
  1362. {
  1363. line_error();
  1364. file_print("Not inside of a loop or case statement\n", stderr);
  1365. exit(EXIT_FAILURE);
  1366. }
  1367. struct token_list* i = function->locals;
  1368. while(i != break_frame)
  1369. {
  1370. if(NULL == i) break;
  1371. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("POPR R1 R15\t# break_cleanup_locals\n");
  1372. else if(X86 == Architecture) emit_out("POP_ebx\t# break_cleanup_locals\n");
  1373. else if(AMD64 == Architecture) emit_out("POP_RBX\t# break_cleanup_locals\n");
  1374. else if(ARMV7L == Architecture) emit_out("{R1} POP_ALWAYS\t# break_cleanup_locals\n");
  1375. else if(AARCH64 == Architecture) emit_out("POP_X1\t# break_cleanup_locals\n");
  1376. i = i->next;
  1377. }
  1378. global_token = global_token->next;
  1379. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("JUMP @");
  1380. else if(X86 == Architecture) emit_out("JUMP %");
  1381. else if(AMD64 == Architecture) emit_out("JUMP %");
  1382. else if(ARMV7L == Architecture) emit_out("^~");
  1383. else if(AARCH64 == Architecture) emit_out("LOAD_W16_AHEAD\nSKIP_32_DATA\n&");
  1384. emit_out(break_target_head);
  1385. emit_out(break_target_func);
  1386. emit_out("_");
  1387. emit_out(break_target_num);
  1388. if(ARMV7L == Architecture) emit_out(" JUMP_ALWAYS");
  1389. else if(AARCH64 == Architecture) emit_out("\nBR_X16");
  1390. emit_out("\n");
  1391. require_match("ERROR in break statement\nMissing ;\n", ";");
  1392. }
  1393. void process_contine()
  1394. {
  1395. if(NULL == continue_target_head)
  1396. {
  1397. line_error();
  1398. file_print("Not inside of a loop\n", stderr);
  1399. exit(EXIT_FAILURE);
  1400. }
  1401. global_token = global_token->next;
  1402. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("JUMP @");
  1403. else if(X86 == Architecture) emit_out("JUMP %");
  1404. else if(AMD64 == Architecture) emit_out("JUMP %");
  1405. else if(ARMV7L == Architecture) emit_out("^~");
  1406. else if(AARCH64 == Architecture) emit_out("LOAD_W16_AHEAD\nSKIP_32_DATA\n&");
  1407. emit_out(continue_target_head);
  1408. emit_out(break_target_func);
  1409. emit_out("_");
  1410. emit_out(break_target_num);
  1411. if(ARMV7L == Architecture) emit_out(" JUMP_ALWAYS");
  1412. else if(AARCH64 == Architecture) emit_out("\nBR_X16");
  1413. emit_out("\n");
  1414. require_match("ERROR in continue statement\nMissing ;\n", ";");
  1415. }
  1416. void recursive_statement()
  1417. {
  1418. global_token = global_token->next;
  1419. require(NULL != global_token, "Recieved EOF in recursive statement\n");
  1420. struct token_list* frame = function->locals;
  1421. while(!match("}", global_token->s))
  1422. {
  1423. statement();
  1424. require(NULL != global_token, "Recieved EOF in recursive statement prior to }\n");
  1425. }
  1426. global_token = global_token->next;
  1427. /* Clean up any locals added */
  1428. if(((X86 == Architecture) && !match("RETURN\n", output_list->s)) ||
  1429. ((AMD64 == Architecture) && !match("RETURN\n", output_list->s)) ||
  1430. (((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) && !match("RET R15\n", output_list->s)) ||
  1431. ((ARMV7L == Architecture) && !match("'1' LR RETURN\n", output_list->s)) ||
  1432. ((AARCH64 == Architecture) && !match("RETURN\n", output_list->s)))
  1433. {
  1434. struct token_list* i;
  1435. for(i = function->locals; frame != i; i = i->next)
  1436. {
  1437. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("POPR R1 R15\t# _recursive_statement_locals\n");
  1438. else if(X86 == Architecture) emit_out( "POP_ebx\t# _recursive_statement_locals\n");
  1439. else if(AMD64 == Architecture) emit_out("POP_RBX\t# _recursive_statement_locals\n");
  1440. else if(ARMV7L == Architecture) emit_out("{R1} POP_ALWAYS\t# _recursive_statement_locals\n");
  1441. else if(AARCH64 == Architecture) emit_out("POP_X1\t# _recursive_statement_locals\n");
  1442. }
  1443. }
  1444. function->locals = frame;
  1445. }
  1446. /*
  1447. * statement:
  1448. * { statement-list-opt }
  1449. * type-name identifier ;
  1450. * type-name identifier = expression;
  1451. * if ( expression ) statement
  1452. * if ( expression ) statement else statement
  1453. * do statement while ( expression ) ;
  1454. * while ( expression ) statement
  1455. * for ( expression ; expression ; expression ) statement
  1456. * asm ( "assembly" ... "assembly" ) ;
  1457. * goto label ;
  1458. * label:
  1459. * return ;
  1460. * break ;
  1461. * expr ;
  1462. */
  1463. struct type* lookup_type(char* s, struct type* start);
  1464. void statement()
  1465. {
  1466. /* Always an integer until told otherwise */
  1467. current_target = integer;
  1468. if(global_token->s[0] == '{')
  1469. {
  1470. recursive_statement();
  1471. }
  1472. else if(':' == global_token->s[0])
  1473. {
  1474. emit_out(global_token->s);
  1475. emit_out("\t#C goto label\n");
  1476. global_token = global_token->next;
  1477. }
  1478. else if((NULL != lookup_type(global_token->s, prim_types)) ||
  1479. match("struct", global_token->s))
  1480. {
  1481. collect_local();
  1482. }
  1483. else if(match("if", global_token->s))
  1484. {
  1485. process_if();
  1486. }
  1487. else if(match("do", global_token->s))
  1488. {
  1489. process_do();
  1490. }
  1491. else if(match("while", global_token->s))
  1492. {
  1493. process_while();
  1494. }
  1495. else if(match("for", global_token->s))
  1496. {
  1497. process_for();
  1498. }
  1499. else if(match("asm", global_token->s))
  1500. {
  1501. process_asm();
  1502. }
  1503. else if(match("goto", global_token->s))
  1504. {
  1505. global_token = global_token->next;
  1506. require(NULL != global_token, "naked goto is not supported\n");
  1507. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("JUMP @");
  1508. else if(X86 == Architecture) emit_out("JUMP %");
  1509. else if(AMD64 == Architecture) emit_out("JUMP %");
  1510. else if(ARMV7L == Architecture) emit_out("^~");
  1511. else if(AARCH64 == Architecture) emit_out("LOAD_W16_AHEAD\nSKIP_32_DATA\n&");
  1512. emit_out(global_token->s);
  1513. if(ARMV7L == Architecture) emit_out(" JUMP_ALWAYS");
  1514. else if(AARCH64 == Architecture) emit_out("\nBR_X16");
  1515. emit_out("\n");
  1516. global_token = global_token->next;
  1517. require_match("ERROR in statement\nMissing ;\n", ";");
  1518. }
  1519. else if(match("return", global_token->s))
  1520. {
  1521. return_result();
  1522. }
  1523. else if(match("break", global_token->s))
  1524. {
  1525. process_break();
  1526. }
  1527. else if(match("continue", global_token->s))
  1528. {
  1529. process_contine();
  1530. }
  1531. else
  1532. {
  1533. expression();
  1534. require_match("ERROR in statement\nMISSING ;\n", ";");
  1535. }
  1536. }
  1537. /* Collect function arguments */
  1538. void collect_arguments()
  1539. {
  1540. global_token = global_token->next;
  1541. require(NULL != global_token, "Recieved EOF when attempting to collect arguments\n");
  1542. struct type* type_size;
  1543. struct token_list* a;
  1544. while(!match(")", global_token->s))
  1545. {
  1546. type_size = type_name();
  1547. if(global_token->s[0] == ')')
  1548. {
  1549. /* foo(int,char,void) doesn't need anything done */
  1550. continue;
  1551. }
  1552. else if(global_token->s[0] != ',')
  1553. {
  1554. /* deal with foo(int a, char b) */
  1555. a = sym_declare(global_token->s, type_size, function->arguments);
  1556. if(NULL == function->arguments)
  1557. {
  1558. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) a->depth = 0;
  1559. else if(X86 == Architecture) a->depth = -4;
  1560. else if(AMD64 == Architecture) a->depth = -8;
  1561. else if(ARMV7L == Architecture) a->depth = 4;
  1562. else if(AARCH64 == Architecture) a->depth = register_size;
  1563. }
  1564. else
  1565. {
  1566. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) a->depth = function->arguments->depth + register_size;
  1567. else if(X86 == Architecture) a->depth = function->arguments->depth - register_size;
  1568. else if(AMD64 == Architecture) a->depth = function->arguments->depth - register_size;
  1569. else if(ARMV7L == Architecture) a->depth = function->arguments->depth + register_size;
  1570. else if(AARCH64 == Architecture) a->depth = function->arguments->depth + register_size;
  1571. }
  1572. global_token = global_token->next;
  1573. require(NULL != global_token, "Incomplete argument list\n");
  1574. function->arguments = a;
  1575. }
  1576. /* ignore trailing comma (needed for foo(bar(), 1); expressions*/
  1577. if(global_token->s[0] == ',')
  1578. {
  1579. global_token = global_token->next;
  1580. require(NULL != global_token, "naked comma in collect arguments\n");
  1581. }
  1582. require(NULL != global_token, "Argument list never completed\n");
  1583. }
  1584. global_token = global_token->next;
  1585. }
  1586. void declare_function()
  1587. {
  1588. current_count = 0;
  1589. function = sym_declare(global_token->prev->s, NULL, global_function_list);
  1590. /* allow previously defined functions to be looked up */
  1591. global_function_list = function;
  1592. if((KNIGHT_NATIVE == Architecture) && match("main", function->s))
  1593. {
  1594. require_match("Impossible error ( vanished\n", "(");
  1595. require_match("Reality ERROR (USING KNIGHT-NATIVE)\nHardware does not support arguments\nthus neither can main on this architecture\ntry tape_01 and tape_02 instead\n", ")");
  1596. }
  1597. else collect_arguments();
  1598. require(NULL != global_token, "Function definitions either need to be prototypes or full\n");
  1599. /* If just a prototype don't waste time */
  1600. if(global_token->s[0] == ';') global_token = global_token->next;
  1601. else
  1602. {
  1603. emit_out("# Defining function ");
  1604. emit_out(function->s);
  1605. emit_out("\n");
  1606. emit_out(":FUNCTION_");
  1607. emit_out(function->s);
  1608. emit_out("\n");
  1609. statement();
  1610. /* Prevent duplicate RETURNS */
  1611. if(((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) && !match("RET R15\n", output_list->s)) emit_out("RET R15\n");
  1612. else if((X86 == Architecture) && !match("RETURN\n", output_list->s)) emit_out("RETURN\n");
  1613. else if((AMD64 == Architecture) && !match("RETURN\n", output_list->s)) emit_out("RETURN\n");
  1614. else if((ARMV7L == Architecture) && !match("'1' LR RETURN\n", output_list->s)) emit_out("'1' LR RETURN\n");
  1615. else if((AARCH64 == Architecture) && !match("RETURN\n", output_list->s)) emit_out("RETURN\n");
  1616. }
  1617. }
  1618. /*
  1619. * program:
  1620. * declaration
  1621. * declaration program
  1622. *
  1623. * declaration:
  1624. * CONSTANT identifer value
  1625. * type-name identifier ;
  1626. * type-name identifier ( parameter-list ) ;
  1627. * type-name identifier ( parameter-list ) statement
  1628. *
  1629. * parameter-list:
  1630. * parameter-declaration
  1631. * parameter-list, parameter-declaration
  1632. *
  1633. * parameter-declaration:
  1634. * type-name identifier-opt
  1635. */
  1636. void program()
  1637. {
  1638. function = NULL;
  1639. Address_of = FALSE;
  1640. struct type* type_size;
  1641. int size;
  1642. new_type:
  1643. if (NULL == global_token) return;
  1644. if(match("CONSTANT", global_token->s))
  1645. {
  1646. global_token = global_token->next;
  1647. require(NULL != global_token, "CONSTANT lacks a name\n");
  1648. global_constant_list = sym_declare(global_token->s, NULL, global_constant_list);
  1649. require(NULL != global_token->next, "CONSTANT lacks a value\n");
  1650. if(match("sizeof", global_token->next->s))
  1651. {
  1652. global_token = global_token->next->next;
  1653. require_match("ERROR in CONSTANT with sizeof\nMissing (\n", "(");
  1654. struct type* a = type_name();
  1655. require_match("ERROR in CONSTANT with sizeof\nMissing )\n", ")");
  1656. global_token->prev->s = numerate_number(a->size);
  1657. global_constant_list->arguments = global_token->prev;
  1658. }
  1659. else
  1660. {
  1661. global_constant_list->arguments = global_token->next;
  1662. global_token = global_token->next->next;
  1663. }
  1664. }
  1665. else if(match("typedef", global_token->s))
  1666. {
  1667. /* typedef $TYPE $NAME; */
  1668. global_token = global_token->next;
  1669. type_size = type_name();
  1670. type_size = mirror_type(type_size, global_token->s);
  1671. add_primitive(type_size);
  1672. global_token = global_token->next;
  1673. require_match("ERROR in typedef statement\nMissing ;\n", ";");
  1674. }
  1675. else
  1676. {
  1677. require('#' != global_token->s[0], "unhandled macro directive\n");
  1678. require(!match("\n", global_token->s), "unexpected newline token\n");
  1679. type_size = type_name();
  1680. if(NULL == type_size)
  1681. {
  1682. goto new_type;
  1683. }
  1684. require(NULL != global_token->next, "Unterminated global\n");
  1685. if(match("[", global_token->next->s))
  1686. {
  1687. maybe_bootstrap_error("global array definitions");
  1688. globals_list = emit(":GLOBAL_", globals_list);
  1689. globals_list = emit(global_token->s, globals_list);
  1690. globals_list = emit("\n&GLOBAL_STORAGE_", globals_list);
  1691. globals_list = emit(global_token->s, globals_list);
  1692. if (AARCH64 == Architecture || AMD64 == Architecture)
  1693. {
  1694. globals_list = emit(" %0", globals_list);
  1695. }
  1696. globals_list = emit("\n:GLOBAL_STORAGE_", globals_list);
  1697. globals_list = emit(global_token->s, globals_list);
  1698. global_symbol_list = sym_declare(global_token->s, type_size->indirect, global_symbol_list);
  1699. require(NULL != global_token->next->next, "Unterminated global\n");
  1700. global_token = global_token->next->next;
  1701. /* length */
  1702. size = numerate_string(global_token->s);
  1703. globals_list = emit("\n'", globals_list);
  1704. while (0 != size)
  1705. {
  1706. globals_list = emit(" 00", globals_list);
  1707. size = size - 1;
  1708. }
  1709. globals_list = emit("'\n", globals_list);
  1710. global_token = global_token->next;
  1711. require_match("missing close bracket\n", "]");
  1712. require_match("missing ;\n", ";");
  1713. goto new_type;
  1714. }
  1715. /* Add to global symbol table */
  1716. global_symbol_list = sym_declare(global_token->s, type_size, global_symbol_list);
  1717. global_token = global_token->next;
  1718. if(match(";", global_token->s))
  1719. {
  1720. /* Ensure 4 bytes are allocated for the global */
  1721. globals_list = emit(":GLOBAL_", globals_list);
  1722. globals_list = emit(global_token->prev->s, globals_list);
  1723. globals_list = emit("\nNULL\n", globals_list);
  1724. global_token = global_token->next;
  1725. }
  1726. else if(match("(", global_token->s)) declare_function();
  1727. else if(match("=",global_token->s))
  1728. {
  1729. /* Store the global's value*/
  1730. globals_list = emit(":GLOBAL_", globals_list);
  1731. globals_list = emit(global_token->prev->s, globals_list);
  1732. globals_list = emit("\n", globals_list);
  1733. global_token = global_token->next;
  1734. require(NULL != global_token, "Global locals value in assignment\n");
  1735. if(in_set(global_token->s[0], "0123456789"))
  1736. { /* Assume Int */
  1737. globals_list = emit("%", globals_list);
  1738. globals_list = emit(global_token->s, globals_list);
  1739. globals_list = emit("\n", globals_list);
  1740. }
  1741. else if(('"' == global_token->s[0]))
  1742. { /* Assume a string*/
  1743. globals_list = emit("&GLOBAL_", globals_list);
  1744. globals_list = emit(global_token->prev->prev->s, globals_list);
  1745. globals_list = emit("_contents\n", globals_list);
  1746. globals_list = emit(":GLOBAL_", globals_list);
  1747. globals_list = emit(global_token->prev->prev->s, globals_list);
  1748. globals_list = emit("_contents\n", globals_list);
  1749. globals_list = emit(parse_string(global_token->s), globals_list);
  1750. }
  1751. else
  1752. {
  1753. line_error();
  1754. file_print("Received ", stderr);
  1755. file_print(global_token->s, stderr);
  1756. file_print(" in program\n", stderr);
  1757. exit(EXIT_FAILURE);
  1758. }
  1759. global_token = global_token->next;
  1760. require_match("ERROR in Program\nMissing ;\n", ";");
  1761. }
  1762. else
  1763. {
  1764. line_error();
  1765. file_print("Received ", stderr);
  1766. file_print(global_token->s, stderr);
  1767. file_print(" in program\n", stderr);
  1768. exit(EXIT_FAILURE);
  1769. }
  1770. }
  1771. goto new_type;
  1772. }
  1773. void recursive_output(struct token_list* head, FILE* out)
  1774. {
  1775. struct token_list* i = reverse_list(head);
  1776. while(NULL != i)
  1777. {
  1778. file_print(i->s, out);
  1779. i = i->next;
  1780. }
  1781. }
  1782. void output_tokens(struct token_list *i, FILE* out)
  1783. {
  1784. while(NULL != i)
  1785. {
  1786. file_print(i->s, out);
  1787. file_print(" ", out);
  1788. i = i->next;
  1789. }
  1790. }