backend.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523
  1. /*
  2. Copyright (c) 2021-2022, b4rt-dev
  3. Copyright (c) 2012-2015, Alexey Frunze
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are met:
  7. 1. Redistributions of source code must retain the above copyright notice, this
  8. list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright notice,
  10. this list of conditions and the following disclaimer in the documentation
  11. and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  13. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  14. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  16. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  18. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  19. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  21. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. /*****************************************************************************/
  24. /* */
  25. /* BCC (B32P C Compiler) */
  26. /* */
  27. /* C compiler for B32P */
  28. /* Modified version intended to run on FPGC */
  29. /* */
  30. /* Based on SmallerC: */
  31. /* A simple and small single-pass C compiler */
  32. /* */
  33. /* B32P code generator */
  34. /* Modified from the MIPS code generator */
  35. /* */
  36. /*****************************************************************************/
  37. // NOTE: Pretty inefficient generated code, because of the direct-ish translation from MIPS to B32P
  38. /* MAIN TODOs:
  39. - Improve using new B32P instructions
  40. - Remove all MIPS code leftovers
  41. - Do optimizations (like size stuff)
  42. */
  43. /* WORDSIZE issue tags (to look into if I will ever try to fix it)
  44. //WORDSIZE
  45. //CurFxnLocalOfs (gcc.c)
  46. */
  47. void GenInit(void)
  48. {
  49. // initialization of target-specific code generator
  50. // Assembler should move all .data and .rdata parts away from the code
  51. SizeOfWord = 4;
  52. OutputFormat = FormatSegmented;
  53. CodeHeaderFooter[0] = ".code";
  54. DataHeaderFooter[0] = ".data";
  55. RoDataHeaderFooter[0] = ".rdata";
  56. BssHeaderFooter[0] = ".bss"; // object data
  57. UseLeadingUnderscores = 0;
  58. }
  59. void GenInitFinalize(void)
  60. {
  61. // finalization of initialization of target-specific code generator
  62. // Put all C specific wrapper code (start) here
  63. if (compileUserBDOS)
  64. {
  65. printf2(
  66. ".code\n"
  67. "Main:\n"
  68. " load32 0 r14\n"
  69. " load32 0x73FFFF r13\n"
  70. " addr2reg Return_BDOS r1\n"
  71. " or r0 r1 r15\n"
  72. " jump main\n"
  73. " halt\n"
  74. "Return_BDOS:\n"
  75. " load32 0x100300 r1\n"
  76. " write 0 r1 r0\n"
  77. " pop r1\n"
  78. " jumpr 3 r1\n"
  79. " halt\n"
  80. );
  81. }
  82. else
  83. {
  84. printf2(
  85. ".code\n"
  86. "Main:\n"
  87. " load32 0 r14\n"
  88. " load32 0x77FFFF r13\n"
  89. " addr2reg Return_UART r1\n"
  90. " or r0 r1 r15\n"
  91. " jump main\n"
  92. " halt\n"
  93. "Return_UART:\n"
  94. " load32 0xC02723 r1\n"
  95. " write 0 r1 r2\n"
  96. " halt\n"
  97. );
  98. }
  99. }
  100. void GenStartCommentLine(void)
  101. {
  102. printf2(" ; ");
  103. }
  104. // No alignment needed on B32P
  105. void GenWordAlignment(word bss)
  106. {
  107. (void)bss;
  108. if (doAnnotations)
  109. {
  110. printf2("; .align 2\n");
  111. }
  112. }
  113. void GenLabel(char* Label, word Static)
  114. {
  115. {
  116. if (!Static && GenExterns && doAnnotations)
  117. {
  118. printf2("; .globl ");
  119. printf2(Label);
  120. printf2("\n");
  121. }
  122. printf2(Label);
  123. printf2(":\n");
  124. }
  125. }
  126. void GenPrintLabel(char* Label)
  127. {
  128. {
  129. if (isdigit(*Label))
  130. {
  131. printf2("Label_");
  132. printf2(Label);
  133. }
  134. else
  135. {
  136. printf2(Label);
  137. }
  138. }
  139. }
  140. void GenNumLabel(word Label)
  141. {
  142. printf2("Label_");
  143. printd2(Label);
  144. printf2(":\n");
  145. }
  146. void GenPrintNumLabel(word label)
  147. {
  148. printf2("Label_");
  149. printd2(label);
  150. }
  151. void GenZeroData(unsigned Size, word bss)
  152. {
  153. (void)bss;
  154. if (doAnnotations)
  155. {
  156. printf2("; .space ");
  157. printd2(truncUint(Size));
  158. printf2("\n");
  159. }
  160. // B32P implementation of .space:
  161. if (Size > 0)
  162. {
  163. word i;
  164. for (i = 0; i < Size; i++)
  165. {
  166. if (MATH_modU(i, 100) == 0)
  167. {
  168. if (i == 0)
  169. {
  170. printf2(".dw");
  171. }
  172. else
  173. {
  174. printf2("\n.dw");
  175. }
  176. }
  177. printf2(" 0");
  178. }
  179. printf2("\n");
  180. }
  181. }
  182. void GenIntData(word Size, word Val)
  183. {
  184. Val = truncInt(Val);
  185. // Print multiple times, since the compiler does not know yet B32P is word addressable
  186. if (Size == 1)
  187. {
  188. printf2(" .dw ");
  189. printd2(Val);
  190. printf2("\n");
  191. }
  192. else if (Size == 2)
  193. {
  194. printf2(" .dw ");
  195. printd2(Val);
  196. printf2(" ");
  197. printd2(Val);
  198. printf2("\n");
  199. }
  200. else if (Size == 4)
  201. {
  202. printf2(" .dw ");
  203. printd2(Val);
  204. printf2(" ");
  205. printd2(Val);
  206. printf2(" ");
  207. printd2(Val);
  208. printf2(" ");
  209. printd2(Val);
  210. printf2("\n");
  211. }
  212. }
  213. void GenStartAsciiString(void)
  214. {
  215. printf2(".dw "); // String should be converted into 1 character per word
  216. }
  217. void GenAddrData(word Size, char* Label, word ofs)
  218. {
  219. ofs = truncInt(ofs);
  220. word i;
  221. for (i = 0; i < 4; i++) // label is 4 "bytes", hotfix since the compiler does not know yet B32P is word addressable
  222. {
  223. printf2(".dl ");
  224. GenPrintLabel(Label);
  225. // Still not sure if this ever gets called (and probably will not work until an Assembler update)
  226. if (ofs)
  227. {
  228. printf2(" +");
  229. printd2(ofs);
  230. }
  231. puts2("");
  232. }
  233. }
  234. word GenFxnSizeNeeded(void)
  235. {
  236. return 0;
  237. }
  238. void GenRecordFxnSize(char* startLabelName, word endLabelNo)
  239. {
  240. (void)startLabelName;
  241. (void)endLabelNo;
  242. }
  243. #define B32PInstrHalt 0x30
  244. #define B32PInstrRead 0x31
  245. #define B32PInstrWrite 0x32
  246. #define B32PInstrIntID 0x33
  247. #define B32PInstrPush 0x34
  248. #define B32PInstrPop 0x35
  249. #define B32PInstrJump 0x36
  250. #define B32PInstrJumpo 0x37
  251. #define B32PInstrJumpr 0x38
  252. #define B32PInstrJumpro 0x39
  253. #define B32PInstrBEQ 0x3A
  254. #define B32PInstrBGT 0x3B
  255. #define B32PInstrBGTS 0x3C
  256. #define B32PInstrBGE 0x3D
  257. #define B32PInstrBGES 0x3E
  258. #define B32PInstrBNE 0x3F
  259. #define B32PInstrBLT 0x40
  260. #define B32PInstrBLTS 0x41
  261. #define B32PInstrBLE 0x42
  262. #define B32PInstrBLES 0x43
  263. #define B32PInstrSavPC 0x44
  264. #define B32PInstrReti 0x45
  265. #define B32PInstrOR 0x46
  266. #define B32PInstrAND 0x47
  267. #define B32PInstrXOR 0x48
  268. #define B32PInstrADD 0x49
  269. #define B32PInstrSUB 0x4A
  270. #define B32PInstrSHIFTL 0x4B
  271. #define B32PInstrSHIFTR 0x4C
  272. #define B32PInstrNOT 0x4D
  273. #define B32PInstrMULTS 0x4E
  274. #define B32PInstrMULTU 0x4F
  275. #define B32PInstrSLT 0x50
  276. #define B32PInstrSLTU 0x51
  277. #define B32PInstrLoad 0x52
  278. #define B32PInstrLoadHi 0x53
  279. #define B32PInstrAddr2reg 0x54
  280. #define B32PInstrLoad32 0x55
  281. #define B32PInstrNOP 0x56
  282. void GenPrintInstr(word instr, word val)
  283. {
  284. char* p = "";
  285. (void)val;
  286. switch (instr)
  287. {
  288. case B32PInstrHalt : p = "halt"; break;
  289. case B32PInstrRead : p = "read"; break;
  290. case B32PInstrWrite : p = "write"; break;
  291. case B32PInstrIntID : p = "readintid"; break;
  292. case B32PInstrPush : p = "push"; break;
  293. case B32PInstrPop : p = "pop"; break;
  294. case B32PInstrJump : p = "jump"; break;
  295. case B32PInstrJumpo : p = "jumpo"; break;
  296. case B32PInstrJumpr : p = "jumpr"; break;
  297. case B32PInstrJumpro : p = "jumpro"; break;
  298. case B32PInstrBEQ : p = "beq"; break;
  299. case B32PInstrBGT : p = "bgts"; break; // HACK: Default signed comparison, because of MIPS
  300. case B32PInstrBGTS : p = "bgts"; break;
  301. case B32PInstrBGE : p = "bges"; break; // HACK: Default signed comparison, because of MIPS
  302. case B32PInstrBGES : p = "bges"; break;
  303. case B32PInstrBNE : p = "bne"; break;
  304. case B32PInstrBLT : p = "blts"; break; // HACK: Default signed comparison, because of MIPS
  305. case B32PInstrBLTS : p = "blts"; break;
  306. case B32PInstrBLE : p = "bles"; break; // HACK: Default signed comparison, because of MIPS
  307. case B32PInstrBLES : p = "bles"; break;
  308. case B32PInstrSavPC : p = "savpc"; break;
  309. case B32PInstrReti : p = "reti"; break;
  310. case B32PInstrOR : p = "or"; break;
  311. case B32PInstrAND : p = "and"; break;
  312. case B32PInstrXOR : p = "xor"; break;
  313. case B32PInstrADD : p = "add"; break;
  314. case B32PInstrSUB : p = "sub"; break;
  315. case B32PInstrSHIFTL : p = "shiftl"; break;
  316. case B32PInstrSHIFTR : p = "shiftr"; break;
  317. case B32PInstrNOT : p = "not"; break;
  318. case B32PInstrMULTS : p = "mults"; break;
  319. case B32PInstrMULTU : p = "multu"; break;
  320. case B32PInstrSLT : p = "slt"; break;
  321. case B32PInstrSLTU : p = "sltu"; break;
  322. case B32PInstrLoad : p = "load32"; break;
  323. case B32PInstrLoadHi : p = "loadhi"; break;
  324. case B32PInstrAddr2reg : p = "addr2reg"; break;
  325. case B32PInstrLoad32 : p = "load32"; break;
  326. }
  327. printf2(" ");
  328. printf2(p);
  329. printf2(" ");
  330. }
  331. #define B32POpRegZero 0x00 //0 0
  332. #define B32POpRegAt 0x01 //1 at
  333. #define B32POpRegV0 0x02 //2 ret0
  334. #define B32POpRegV1 0x03 //3 ret1
  335. #define B32POpRegA0 0x04 //4 arg0
  336. #define B32POpRegA1 0x05 //5 arg1
  337. #define B32POpRegA2 0x06 //6 arg2
  338. #define B32POpRegA3 0x07 //7 arg3
  339. #define B32POpRegT0 0x08 //8 gp0
  340. #define B32POpRegT1 0x09 //9 gp1
  341. #define B32POpRegT2 0x0A //10 gp2
  342. #define B32POpRegT8 0x0B //11 tempa
  343. #define B32POpRegT9 0x0C //12 tempb
  344. #define B32POpRegSp 0x0D //13 sp
  345. #define B32POpRegFp 0x0E //14 fp
  346. #define B32POpRegRa 0x0F //15 retaddr
  347. #define B32POpIndRegZero 0x20
  348. #define B32POpIndRegAt 0x21
  349. #define B32POpIndRegV0 0x22
  350. #define B32POpIndRegV1 0x23
  351. #define B32POpIndRegA0 0x24
  352. #define B32POpIndRegA1 0x25
  353. #define B32POpIndRegA2 0x26
  354. #define B32POpIndRegA3 0x27
  355. #define B32POpIndRegT0 0x28
  356. #define B32POpIndRegT1 0x29
  357. #define B32POpIndRegSp 0x2D
  358. #define B32POpIndRegFp 0x2E
  359. #define B32POpIndRegRa 0x2F
  360. #define B32POpConst 0x80
  361. #define B32POpLabel 0x81
  362. #define B32POpNumLabel 0x82
  363. #define B32POpLabelLo 0x83
  364. #define B32POpIndLocal B32POpIndRegFp
  365. #define MAX_TEMP_REGS 3 // this many temp registers used beginning with T0 to hold subexpression results
  366. #define TEMP_REG_A B32POpRegT8 // two temporary registers used for momentary operations, similarly to the AT register
  367. #define TEMP_REG_B B32POpRegT9
  368. void GenPrintOperand(word op, word val)
  369. {
  370. if (op >= B32POpRegZero && op <= B32POpRegRa)
  371. {
  372. printf2("r");
  373. printd2(op);
  374. }
  375. else if (op >= B32POpIndRegZero && op <= B32POpIndRegRa)
  376. {
  377. printd2(truncInt(val));
  378. printf2(" r");
  379. printd2(op - B32POpIndRegZero);
  380. }
  381. else
  382. {
  383. switch (op)
  384. {
  385. case B32POpConst: printd2(truncInt(val)); break;
  386. case B32POpLabelLo:
  387. // should not be called anymore
  388. printf("LABELLO WHOOPS!\n");
  389. /*
  390. printf2("%%lo(");
  391. GenPrintLabel(IdentTable + val);
  392. printf2(")(r1)");
  393. */
  394. break;
  395. case B32POpLabel: GenPrintLabel(IdentTable + val); break;
  396. case B32POpNumLabel: GenPrintNumLabel(val); break;
  397. default:
  398. errorInternal(100);
  399. break;
  400. }
  401. }
  402. }
  403. void GenPrintOperandSeparator(void)
  404. {
  405. printf2(" ");
  406. }
  407. void GenPrintNewLine(void)
  408. {
  409. puts2("");
  410. }
  411. void GenPrintInstr1Operand(word instr, word instrval, word operand, word operandval)
  412. {
  413. GenPrintInstr(instr, instrval);
  414. GenPrintOperand(operand, operandval);
  415. GenPrintNewLine();
  416. }
  417. void GenPrintInstr2Operands(word instr, word instrval, word operand1, word operand1val, word operand2, word operand2val)
  418. {
  419. // TODO: figure out if this ever happens because ADD and SUB need 3 args
  420. if (operand2 == B32POpConst && operand2val == 0 &&
  421. (instr == B32PInstrADD || instr == B32PInstrSUB))
  422. return;
  423. GenPrintInstr(instr, instrval);
  424. GenPrintOperand(operand1, operand1val);
  425. GenPrintOperandSeparator();
  426. GenPrintOperand(operand2, operand2val);
  427. GenPrintNewLine();
  428. }
  429. void GenPrintInstr3Operands(word instr, word instrval,
  430. word operand1, word operand1val,
  431. word operand2, word operand2val,
  432. word operand3, word operand3val)
  433. {
  434. if (operand3 == B32POpConst && operand3val == 0 &&
  435. (instr == B32PInstrADD || instr == B32PInstrSUB) &&
  436. operand1 == operand2)
  437. return;
  438. // If constant is negative, swap B32PInstrADD for B32PInstrSUB and vice versa
  439. // and flip sign of constant
  440. if (operand2 == B32POpConst && operand2val < 0)
  441. {
  442. if (instr == B32PInstrADD)
  443. {
  444. instr = B32PInstrSUB;
  445. operand2val = -operand2val;
  446. }
  447. else if (instr == B32PInstrSUB)
  448. {
  449. instr = B32PInstrADD;
  450. operand2val = -operand2val;
  451. }
  452. }
  453. GenPrintInstr(instr, instrval);
  454. GenPrintOperand(operand1, operand1val);
  455. GenPrintOperandSeparator();
  456. GenPrintOperand(operand2, operand2val);
  457. GenPrintOperandSeparator();
  458. GenPrintOperand(operand3, operand3val);
  459. GenPrintNewLine();
  460. }
  461. // Currently we do not want to "extend" any reg
  462. void GenExtendRegIfNeeded(word reg, word opSz)
  463. {
  464. }
  465. void GenJumpUncond(word label)
  466. {
  467. GenPrintInstr1Operand(B32PInstrJump, 0,
  468. B32POpNumLabel, label);
  469. }
  470. extern word GenWreg; // GenWreg is defined below
  471. void GenJumpIfEqual(word val, word label)
  472. {
  473. GenPrintInstr2Operands(B32PInstrLoad, 0,
  474. B32POpConst, val,
  475. TEMP_REG_B, 0);
  476. /*
  477. GenPrintInstr3Operands(MipsInstrBEQ, 0,
  478. GenWreg, 0,
  479. TEMP_REG_B, 0,
  480. B32POpNumLabel, label);
  481. */
  482. GenPrintInstr3Operands(B32PInstrBNE, 0,
  483. GenWreg, 0,
  484. TEMP_REG_B, 0,
  485. B32POpConst, 2);
  486. GenPrintInstr1Operand(B32PInstrJump, 0,
  487. B32POpNumLabel, label);
  488. }
  489. void GenJumpIfZero(word label)
  490. {
  491. if (doAnnotations)
  492. {
  493. printf2(" ; JumpIfZero\n");
  494. }
  495. /* if Wreg == 0, jump to label
  496. GenPrintInstr3Operands(MipsInstrBEQ, 0,
  497. GenWreg, 0,
  498. B32POpRegZero, 0,
  499. B32POpNumLabel, label);
  500. */
  501. GenPrintInstr3Operands(B32PInstrBNE, 0,
  502. GenWreg, 0,
  503. B32POpRegZero, 0,
  504. B32POpConst, 2);
  505. GenPrintInstr1Operand(B32PInstrJump, 0,
  506. B32POpNumLabel, label);
  507. }
  508. void GenJumpIfNotZero(word label)
  509. {
  510. if (doAnnotations)
  511. {
  512. printf2(" ; JumpIfNotZero\n");
  513. }
  514. /* if Wreg != 0, jump to label
  515. GenPrintInstr3Operands(MipsInstrBNE, 0,
  516. GenWreg, 0,
  517. B32POpRegZero, 0,
  518. B32POpNumLabel, label);
  519. */
  520. GenPrintInstr3Operands(B32PInstrBEQ, 0,
  521. GenWreg, 0,
  522. B32POpRegZero, 0,
  523. B32POpConst, 2);
  524. GenPrintInstr1Operand(B32PInstrJump, 0,
  525. B32POpNumLabel, label);
  526. }
  527. word GenPrologPos = 0;
  528. word GenLeaf;
  529. void GenWriteFrameSize(void) //WORDSIZE
  530. {
  531. unsigned size = 8/*RA + FP*/ - CurFxnMinLocalOfs;
  532. //printf2(" subu r13, r13, %10u\n", size); // 10 chars are enough for 32-bit unsigned ints
  533. printf2(" sub r13 "); // r13 = r13 - size
  534. printd2(size); // r13 = r13 - size
  535. printf2(" r13\n"); // r13 = r13 - size
  536. //printf2(" sw r14, %10u r13\n", size - 8);
  537. printf2(" write "); // write r14 to memory[r13+(size-8)]
  538. printd2(size - 8); // write r14 to memory[r13+(size-8)]
  539. printf2(" r13 r14\n"); // write r14 to memory[r13+(size-8)]
  540. //printf2(" addu r14, r13, %10u\n", size - 8);
  541. printf2(" add r13 "); // r14 = r13 + (size-8)
  542. printd2(size - 8); // r14 = r13 + (size-8)
  543. printf2(" r14\n"); // r14 = r13 + (size-8)
  544. //printf2(" %csw r15, 4 r14\n", GenLeaf ? ';' : ' ');
  545. if (GenLeaf)
  546. {
  547. printf2(" ");
  548. }
  549. else
  550. {
  551. printf2(" write 4 r14 r15\n"); // write r15 to memory[r14+4]
  552. }
  553. }
  554. void GenUpdateFrameSize(void)
  555. {
  556. word curpos = 0;
  557. curpos = fgetpos(OutFile);
  558. //printf("cur: ");
  559. //printd(curpos);
  560. //printf("\ngoto: ");
  561. //printd(GenPrologPos);
  562. //printf("\n");
  563. fsetpos(OutFile, GenPrologPos);
  564. GenWriteFrameSize();
  565. //printf("back to cur: ");
  566. //printd(curpos);
  567. //printf("\n");
  568. //printf("\n");
  569. fsetpos(OutFile, curpos);
  570. }
  571. void GenFxnProlog(void)
  572. {
  573. if (CurFxnParamCntMin && CurFxnParamCntMax)
  574. {
  575. word i, cnt = CurFxnParamCntMax;
  576. if (cnt > 4)
  577. cnt = 4; //WORDSIZE?
  578. // TBD!!! for structure passing use the cumulative parameter size
  579. // instead of the number of parameters. Currently this bug is masked
  580. // by the subroutine that pushes structures on the stack (it copies
  581. // all words except the first to the stack). But passing structures
  582. // in registers from assembly code won't always work.
  583. for (i = 0; i < cnt; i++)
  584. GenPrintInstr2Operands(B32PInstrWrite, 0,
  585. B32POpIndRegSp, 4 * i, //WORDSIZE
  586. B32POpRegA0 + i, 0);
  587. }
  588. GenLeaf = 1; // will be reset to 0 if a call is generated
  589. GenPrologPos = fgetpos(OutFile);
  590. // write an empty space for the frame size
  591. word x;
  592. for(x = 0; x < 100; x++)
  593. {
  594. printf2(" ");
  595. }
  596. printf2("\n");
  597. }
  598. void GenGrowStack(word size) //WORDSIZE
  599. {
  600. if (!size)
  601. return;
  602. if (size > 0)
  603. {
  604. GenPrintInstr3Operands(B32PInstrSUB, 0,
  605. B32POpRegSp, 0,
  606. B32POpConst, size,
  607. B32POpRegSp, 0);
  608. }
  609. else
  610. {
  611. GenPrintInstr3Operands(B32PInstrADD, 0,
  612. B32POpRegSp, 0,
  613. B32POpConst, -size,
  614. B32POpRegSp, 0);
  615. }
  616. }
  617. void GenFxnEpilog(void)
  618. {
  619. //printf("DONE with function\n");
  620. GenUpdateFrameSize();
  621. //printf("BackToEnd\n");
  622. if (!GenLeaf)
  623. GenPrintInstr2Operands(B32PInstrRead, 0,
  624. B32POpIndRegFp, 4, //WORDSIZE
  625. B32POpRegRa, 0);
  626. GenPrintInstr2Operands(B32PInstrRead, 0,
  627. B32POpIndRegFp, 0,
  628. B32POpRegFp, 0);
  629. GenPrintInstr3Operands(B32PInstrADD, 0,
  630. B32POpRegSp, 0,
  631. B32POpConst, 8/*RA + FP*/ - CurFxnMinLocalOfs, //WORDSIZE
  632. B32POpRegSp, 0);
  633. GenPrintInstr2Operands(B32PInstrJumpr, 0,
  634. B32POpConst, 0,
  635. B32POpRegRa, 0);
  636. }
  637. word GenMaxLocalsSize(void)
  638. {
  639. return 0x7FFFFFFF;
  640. }
  641. word GenGetBinaryOperatorInstr(word tok)
  642. {
  643. switch (tok)
  644. {
  645. case tokPostAdd:
  646. case tokAssignAdd:
  647. case '+':
  648. return B32PInstrADD;
  649. case tokPostSub:
  650. case tokAssignSub:
  651. case '-':
  652. return B32PInstrSUB;
  653. case '&':
  654. case tokAssignAnd:
  655. return B32PInstrAND;
  656. case '^':
  657. case tokAssignXor:
  658. return B32PInstrXOR;
  659. case '|':
  660. case tokAssignOr:
  661. return B32PInstrOR;
  662. case '<':
  663. case '>':
  664. case tokLEQ:
  665. case tokGEQ:
  666. case tokEQ:
  667. case tokNEQ:
  668. case tokULess:
  669. case tokUGreater:
  670. case tokULEQ:
  671. case tokUGEQ:
  672. return B32PInstrNOP;
  673. case '*':
  674. case tokAssignMul:
  675. return B32PInstrMULTS;
  676. case '/':
  677. case '%':
  678. case tokAssignDiv:
  679. case tokAssignMod:
  680. printf("DIVISION/MOD is not supported!\n");
  681. return B32PInstrHalt;
  682. case tokUDiv:
  683. case tokUMod:
  684. case tokAssignUDiv:
  685. case tokAssignUMod:
  686. printf("DIVISION/MOD is not supported!\n");
  687. return B32PInstrHalt;
  688. case tokLShift:
  689. case tokAssignLSh:
  690. return B32PInstrSHIFTL;
  691. case tokRShift:
  692. case tokAssignRSh:
  693. return B32PInstrSHIFTR; // B32P does not know about signed, so normal shiftr is done instead
  694. case tokURShift:
  695. case tokAssignURSh:
  696. return B32PInstrSHIFTR;
  697. default:
  698. //error("Error: Invalid operator\n");
  699. errorInternal(101);
  700. return 0;
  701. }
  702. }
  703. void GenReadIdent(word regDst, word opSz, word label)
  704. {
  705. GenPrintInstr2Operands(B32PInstrAddr2reg, 0,
  706. B32POpLabel, label,
  707. B32POpRegAt, 0);
  708. GenPrintInstr3Operands(B32PInstrRead, 0,
  709. B32POpConst, 0,
  710. B32POpRegAt, 0,
  711. regDst, 0);
  712. }
  713. void GenReadLocal(word regDst, word opSz, word ofs)
  714. {
  715. word instr = B32PInstrRead;
  716. GenPrintInstr2Operands(instr, 0,
  717. B32POpIndRegFp, ofs,
  718. regDst, 0);
  719. }
  720. void GenReadIndirect(word regDst, word regSrc, word opSz)
  721. {
  722. word instr = B32PInstrRead;
  723. GenPrintInstr2Operands(instr, 0,
  724. regSrc + B32POpIndRegZero, 0,
  725. regDst, 0);
  726. }
  727. void GenWriteIdent(word regSrc, word opSz, word label)
  728. {
  729. GenPrintInstr2Operands(B32PInstrAddr2reg, 0,
  730. B32POpLabel, label,
  731. B32POpRegAt, 0);
  732. GenPrintInstr3Operands(B32PInstrWrite, 0,
  733. B32POpConst, 0,
  734. B32POpRegAt, 0,
  735. regSrc, 0);
  736. }
  737. void GenWriteLocal(word regSrc, word opSz, word ofs)
  738. {
  739. word instr = B32PInstrWrite;
  740. GenPrintInstr2Operands(instr, 0,
  741. B32POpIndRegFp, ofs,
  742. regSrc, 0);
  743. }
  744. void GenWriteIndirect(word regDst, word regSrc, word opSz)
  745. {
  746. word instr = B32PInstrWrite;
  747. GenPrintInstr2Operands(instr, 0,
  748. regDst + B32POpIndRegZero, 0,
  749. regSrc, 0);
  750. }
  751. void GenIncDecIdent(word regDst, word opSz, word label, word tok)
  752. {
  753. word instr = B32PInstrADD;
  754. if (tok != tokInc)
  755. instr = B32PInstrSUB;
  756. GenReadIdent(regDst, opSz, label);
  757. GenPrintInstr3Operands(instr, 0,
  758. regDst, 0,
  759. B32POpConst, 1,
  760. regDst, 0);
  761. GenWriteIdent(regDst, opSz, label);
  762. GenExtendRegIfNeeded(regDst, opSz);
  763. }
  764. void GenIncDecLocal(word regDst, word opSz, word ofs, word tok)
  765. {
  766. word instr = B32PInstrADD;
  767. if (tok != tokInc)
  768. instr = B32PInstrSUB;
  769. GenReadLocal(regDst, opSz, ofs);
  770. GenPrintInstr3Operands(instr, 0,
  771. regDst, 0,
  772. B32POpConst, 1,
  773. regDst, 0);
  774. GenWriteLocal(regDst, opSz, ofs);
  775. GenExtendRegIfNeeded(regDst, opSz);
  776. }
  777. void GenIncDecIndirect(word regDst, word regSrc, word opSz, word tok)
  778. {
  779. word instr = B32PInstrADD;
  780. if (tok != tokInc)
  781. instr = B32PInstrSUB;
  782. GenReadIndirect(regDst, regSrc, opSz);
  783. GenPrintInstr3Operands(instr, 0,
  784. regDst, 0,
  785. B32POpConst, 1,
  786. regDst, 0);
  787. GenWriteIndirect(regSrc, regDst, opSz);
  788. GenExtendRegIfNeeded(regDst, opSz);
  789. }
  790. void GenPostIncDecIdent(word regDst, word opSz, word label, word tok)
  791. {
  792. word instr = B32PInstrADD;
  793. if (tok != tokPostInc)
  794. instr = B32PInstrSUB;
  795. GenReadIdent(regDst, opSz, label);
  796. GenPrintInstr3Operands(instr, 0,
  797. regDst, 0,
  798. B32POpConst, 1,
  799. regDst, 0);
  800. GenWriteIdent(regDst, opSz, label);
  801. GenPrintInstr3Operands(instr, 0,
  802. regDst, 0,
  803. B32POpConst, -1,
  804. regDst, 0);
  805. GenExtendRegIfNeeded(regDst, opSz);
  806. }
  807. void GenPostIncDecLocal(word regDst, word opSz, word ofs, word tok)
  808. {
  809. word instr = B32PInstrADD;
  810. if (tok != tokPostInc)
  811. instr = B32PInstrSUB;
  812. GenReadLocal(regDst, opSz, ofs);
  813. GenPrintInstr3Operands(instr, 0,
  814. regDst, 0,
  815. B32POpConst, 1,
  816. regDst, 0);
  817. GenWriteLocal(regDst, opSz, ofs);
  818. GenPrintInstr3Operands(instr, 0,
  819. regDst, 0,
  820. B32POpConst, -1,
  821. regDst, 0);
  822. GenExtendRegIfNeeded(regDst, opSz);
  823. }
  824. void GenPostIncDecIndirect(word regDst, word regSrc, word opSz, word tok)
  825. {
  826. word instr = B32PInstrADD;
  827. if (tok != tokPostInc)
  828. instr = B32PInstrSUB;
  829. GenReadIndirect(regDst, regSrc, opSz);
  830. GenPrintInstr3Operands(instr, 0,
  831. regDst, 0,
  832. B32POpConst, 1,
  833. regDst, 0);
  834. GenWriteIndirect(regSrc, regDst, opSz);
  835. GenPrintInstr3Operands(instr, 0,
  836. regDst, 0,
  837. B32POpConst, -1,
  838. regDst, 0);
  839. GenExtendRegIfNeeded(regDst, opSz);
  840. }
  841. word CanUseTempRegs;
  842. word TempsUsed;
  843. word GenWreg = B32POpRegV0; // current working register (V0 or Tn or An)
  844. word GenLreg, GenRreg; // left operand register and right operand register after GenPopReg()
  845. /*
  846. General idea behind GenWreg, GenLreg, GenRreg:
  847. - In expressions w/o function calls:
  848. Subexpressions are evaluated in V0, T0, T1, ..., T<MAX_TEMP_REGS-1>. If those registers
  849. aren't enough, the stack is used additionally.
  850. The expression result ends up in V0, which is handy for returning from
  851. functions.
  852. In the process, GenWreg is the current working register and is one of: V0, T0, T1, ... .
  853. All unary operators are evaluated in the current working register.
  854. GenPushReg() and GenPopReg() advance GenWreg as needed when handling binary operators.
  855. GenPopReg() sets GenWreg, GenLreg and GenRreg. GenLreg and GenRreg are the registers
  856. where the left and right operands of a binary operator are.
  857. When the exression runs out of the temporary registers, the stack is used. While it is being
  858. used, GenWreg remains equal to the last temporary register, and GenPopReg() sets GenLreg = TEMP_REG_A.
  859. Hence, after GenPopReg() the operands of the binary operator are always in registers and can be
  860. directly manipulated with.
  861. Following GenPopReg(), binary operator evaluation must take the left and right operands from
  862. GenLreg and GenRreg and write the evaluated result into GenWreg. Care must be taken as GenWreg
  863. will be the same as either GenLreg (when the popped operand comes from T0-T<MAX_TEMP_REGS-1>)
  864. or GenRreg (when the popped operand comes from the stack in TEMP_REG_A).
  865. - In expressions with function calls:
  866. GenWreg is always V0 in subexpressions that aren't function parameters. These subexpressions
  867. get automatically pushed onto the stack as necessary.
  868. GenWreg is always V0 in expressions, where return values from function calls are used as parameters
  869. into other called functions. IOW, this is the case when the function call depth is greater than 1.
  870. Subexpressions in such expressions get automatically pushed onto the stack as necessary.
  871. GenWreg is A0-A3 in subexpressions that are function parameters when the function call depth is 1.
  872. Basically, while a function parameter is evaluated, it's evaluated in the register from where
  873. the called function will take it. This avoids some of unnecessary register copies and stack
  874. manipulations in the most simple and very common cases of function calls.
  875. */
  876. void GenWregInc(word inc)
  877. {
  878. if (inc > 0)
  879. {
  880. // Advance the current working register to the next available temporary register
  881. if (GenWreg == B32POpRegV0)
  882. GenWreg = B32POpRegT0;
  883. else
  884. GenWreg++;
  885. }
  886. else
  887. {
  888. // Return to the previous current working register
  889. if (GenWreg == B32POpRegT0)
  890. GenWreg = B32POpRegV0;
  891. else
  892. GenWreg--;
  893. }
  894. }
  895. void GenPushReg(void)
  896. {
  897. if (CanUseTempRegs && TempsUsed < MAX_TEMP_REGS)
  898. {
  899. GenWregInc(1);
  900. TempsUsed++;
  901. return;
  902. }
  903. GenPrintInstr3Operands(B32PInstrSUB, 0,
  904. B32POpRegSp, 0,
  905. B32POpConst, 4, //WORDSIZE
  906. B32POpRegSp, 0);
  907. GenPrintInstr2Operands(B32PInstrWrite, 0,
  908. B32POpIndRegSp, 0,
  909. GenWreg, 0);
  910. TempsUsed++;
  911. }
  912. void GenPopReg(void)
  913. {
  914. TempsUsed--;
  915. if (CanUseTempRegs && TempsUsed < MAX_TEMP_REGS)
  916. {
  917. GenRreg = GenWreg;
  918. GenWregInc(-1);
  919. GenLreg = GenWreg;
  920. return;
  921. }
  922. GenPrintInstr2Operands(B32PInstrRead, 0,
  923. B32POpIndRegSp, 0,
  924. TEMP_REG_A, 0);
  925. GenPrintInstr3Operands(B32PInstrADD, 0,
  926. B32POpRegSp, 0,
  927. B32POpConst, 4, //WORDSIZE
  928. B32POpRegSp, 0);
  929. GenLreg = TEMP_REG_A;
  930. GenRreg = GenWreg;
  931. }
  932. #define tokRevIdent 0x100
  933. #define tokRevLocalOfs 0x101
  934. #define tokAssign0 0x102
  935. #define tokNum0 0x103
  936. void GenPrep(word* idx)
  937. {
  938. word tok;
  939. word oldIdxRight, oldIdxLeft, t0, t1;
  940. if (*idx < 0)
  941. //error("GenFuse(): idx < 0\n");
  942. errorInternal(100);
  943. tok = stack[*idx][0];
  944. oldIdxRight = --*idx;
  945. switch (tok)
  946. {
  947. case tokUDiv:
  948. case tokUMod:
  949. case tokAssignUDiv:
  950. case tokAssignUMod:
  951. if (stack[oldIdxRight][0] == tokNumInt || stack[oldIdxRight][0] == tokNumUint)
  952. {
  953. // Change unsigned division to right shift and unsigned modulo to bitwise and
  954. unsigned m = truncUint(stack[oldIdxRight][1]);
  955. if (m && !(m & (m - 1)))
  956. {
  957. if (tok == tokUMod || tok == tokAssignUMod)
  958. {
  959. stack[oldIdxRight][1] = (word)(m - 1);
  960. tok = (tok == tokUMod) ? '&' : tokAssignAnd;
  961. }
  962. else
  963. {
  964. t1 = 0;
  965. while (m >>= 1) t1++;
  966. stack[oldIdxRight][1] = t1;
  967. tok = (tok == tokUDiv) ? tokURShift : tokAssignURSh;
  968. }
  969. stack[oldIdxRight + 1][0] = tok;
  970. }
  971. }
  972. }
  973. switch (tok)
  974. {
  975. case tokNumUint:
  976. stack[oldIdxRight + 1][0] = tokNumInt; // reduce the number of cases since tokNumInt and tokNumUint are handled the same way
  977. // fallthrough
  978. case tokNumInt:
  979. case tokNum0:
  980. case tokIdent:
  981. case tokLocalOfs:
  982. break;
  983. case tokPostAdd:
  984. case tokPostSub:
  985. case '-':
  986. case '/':
  987. case '%':
  988. case tokUDiv:
  989. case tokUMod:
  990. case tokLShift:
  991. case tokRShift:
  992. case tokURShift:
  993. case tokLogAnd:
  994. case tokLogOr:
  995. case tokComma:
  996. GenPrep(idx);
  997. // fallthrough
  998. case tokShortCirc:
  999. case tokGoto:
  1000. case tokUnaryStar:
  1001. case tokInc:
  1002. case tokDec:
  1003. case tokPostInc:
  1004. case tokPostDec:
  1005. case '~':
  1006. case tokUnaryPlus:
  1007. case tokUnaryMinus:
  1008. case tok_Bool:
  1009. case tokVoid:
  1010. case tokUChar:
  1011. case tokSChar:
  1012. case tokShort:
  1013. case tokUShort:
  1014. GenPrep(idx);
  1015. break;
  1016. case '=':
  1017. if (oldIdxRight + 1 == sp - 1 &&
  1018. (stack[oldIdxRight][0] == tokNumInt || stack[oldIdxRight][0] == tokNumUint) &&
  1019. truncUint(stack[oldIdxRight][1]) == 0)
  1020. {
  1021. // Special case for assigning 0 while throwing away the expression result value
  1022. // TBD??? ,
  1023. stack[oldIdxRight][0] = tokNum0; // this zero constant will not be loaded into a register
  1024. stack[oldIdxRight + 1][0] = tokAssign0; // change '=' to tokAssign0
  1025. }
  1026. // fallthrough
  1027. case tokAssignAdd:
  1028. case tokAssignSub:
  1029. case tokAssignMul:
  1030. case tokAssignDiv:
  1031. case tokAssignUDiv:
  1032. case tokAssignMod:
  1033. case tokAssignUMod:
  1034. case tokAssignLSh:
  1035. case tokAssignRSh:
  1036. case tokAssignURSh:
  1037. case tokAssignAnd:
  1038. case tokAssignXor:
  1039. case tokAssignOr:
  1040. GenPrep(idx);
  1041. oldIdxLeft = *idx;
  1042. GenPrep(idx);
  1043. // If the left operand is an identifier (with static or auto storage), swap it with the right operand
  1044. // and mark it specially, so it can be used directly
  1045. if ((t0 = stack[oldIdxLeft][0]) == tokIdent || t0 == tokLocalOfs)
  1046. {
  1047. t1 = stack[oldIdxLeft][1];
  1048. memmove(stack[oldIdxLeft], stack[oldIdxLeft + 1], (oldIdxRight - oldIdxLeft) * sizeof(stack[0]));
  1049. stack[oldIdxRight][0] = (t0 == tokIdent) ? tokRevIdent : tokRevLocalOfs;
  1050. stack[oldIdxRight][1] = t1;
  1051. }
  1052. break;
  1053. case '+':
  1054. case '*':
  1055. case '&':
  1056. case '^':
  1057. case '|':
  1058. case tokEQ:
  1059. case tokNEQ:
  1060. case '<':
  1061. case '>':
  1062. case tokLEQ:
  1063. case tokGEQ:
  1064. case tokULess:
  1065. case tokUGreater:
  1066. case tokULEQ:
  1067. case tokUGEQ:
  1068. GenPrep(idx);
  1069. oldIdxLeft = *idx;
  1070. GenPrep(idx);
  1071. // If the right operand isn't a constant, but the left operand is, swap the operands
  1072. // so the constant can become an immediate right operand in the instruction
  1073. t1 = stack[oldIdxRight][0];
  1074. t0 = stack[oldIdxLeft][0];
  1075. if (t1 != tokNumInt && t0 == tokNumInt)
  1076. {
  1077. word xor;
  1078. t1 = stack[oldIdxLeft][1];
  1079. memmove(stack[oldIdxLeft], stack[oldIdxLeft + 1], (oldIdxRight - oldIdxLeft) * sizeof(stack[0]));
  1080. stack[oldIdxRight][0] = t0;
  1081. stack[oldIdxRight][1] = t1;
  1082. switch (tok)
  1083. {
  1084. case '<':
  1085. case '>':
  1086. xor = '<' ^ '>'; break;
  1087. case tokLEQ:
  1088. case tokGEQ:
  1089. xor = tokLEQ ^ tokGEQ; break;
  1090. case tokULess:
  1091. case tokUGreater:
  1092. xor = tokULess ^ tokUGreater; break;
  1093. case tokULEQ:
  1094. case tokUGEQ:
  1095. xor = tokULEQ ^ tokUGEQ; break;
  1096. default:
  1097. xor = 0; break;
  1098. }
  1099. tok ^= xor;
  1100. }
  1101. // Handle a few special cases and transform the instruction
  1102. if (stack[oldIdxRight][0] == tokNumInt)
  1103. {
  1104. unsigned m = truncUint(stack[oldIdxRight][1]);
  1105. switch (tok)
  1106. {
  1107. case '*':
  1108. // Change multiplication to left shift, this helps indexing arrays of ints/pointers/etc
  1109. if (m && !(m & (m - 1)))
  1110. {
  1111. t1 = 0;
  1112. while (m >>= 1) t1++;
  1113. stack[oldIdxRight][1] = t1;
  1114. tok = tokLShift;
  1115. }
  1116. break;
  1117. case tokLEQ:
  1118. // left <= const will later change to left < const+1, but const+1 must be <=0x7FFFFFFF
  1119. if (m == 0x7FFFFFFF)
  1120. {
  1121. // left <= 0x7FFFFFFF is always true, change to the equivalent left >= 0u
  1122. stack[oldIdxRight][1] = 0;
  1123. tok = tokUGEQ;
  1124. }
  1125. break;
  1126. case tokULEQ:
  1127. // left <= const will later change to left < const+1, but const+1 must be <=0xFFFFFFFFu
  1128. if (m == 0xFFFFFFFF)
  1129. {
  1130. // left <= 0xFFFFFFFFu is always true, change to the equivalent left >= 0u
  1131. stack[oldIdxRight][1] = 0;
  1132. tok = tokUGEQ;
  1133. }
  1134. break;
  1135. case '>':
  1136. // left > const will later change to !(left < const+1), but const+1 must be <=0x7FFFFFFF
  1137. if (m == 0x7FFFFFFF)
  1138. {
  1139. // left > 0x7FFFFFFF is always false, change to the equivalent left & 0
  1140. stack[oldIdxRight][1] = 0;
  1141. tok = '&';
  1142. }
  1143. break;
  1144. case tokUGreater:
  1145. // left > const will later change to !(left < const+1), but const+1 must be <=0xFFFFFFFFu
  1146. if (m == 0xFFFFFFFF)
  1147. {
  1148. // left > 0xFFFFFFFFu is always false, change to the equivalent left & 0
  1149. stack[oldIdxRight][1] = 0;
  1150. tok = '&';
  1151. }
  1152. break;
  1153. }
  1154. }
  1155. stack[oldIdxRight + 1][0] = tok;
  1156. break;
  1157. case ')':
  1158. while (stack[*idx][0] != '(')
  1159. {
  1160. GenPrep(idx);
  1161. if (stack[*idx][0] == ',')
  1162. --*idx;
  1163. }
  1164. --*idx;
  1165. break;
  1166. default:
  1167. //error("GenPrep: unexpected token %s\n", GetTokenName(tok));
  1168. errorInternal(101);
  1169. }
  1170. }
  1171. /*
  1172. ; l <[u] 0 // slt[u] w, w, 0 "k"
  1173. l <[u] const // slt[u] w, w, const "m"
  1174. l <[u] r // slt[u] w, l, r "i"
  1175. * if (l < 0) // bgez w, Lskip "f"
  1176. if (l <[u] const) // slt[u] w, w, const; beq w, r0, Lskip "mc"
  1177. if (l <[u] r) // slt[u] w, l, r; beq w, r0, Lskip "ic"
  1178. ; l <=[u] 0 // slt[u] w, w, 1 "l"
  1179. l <=[u] const // slt[u] w, w, const + 1 "n"
  1180. l <=[u] r // slt[u] w, r, l; xor w, w, 1 "js"
  1181. * if (l <= 0) // bgtz w, Lskip "g"
  1182. if (l <=[u] const) // slt[u] w, w, const + 1; beq w, r0, Lskip "nc"
  1183. if (l <=[u] r) // slt[u] w, r, l; bne w, r0, Lskip "jd"
  1184. l >[u] 0 // slt[u] w, r0, w "o"
  1185. l >[u] const // slt[u] w, w, const + 1; xor w, w, 1 "ns"
  1186. l >[u] r // slt[u] w, r, l "j"
  1187. * if (l > 0) // blez w, Lskip "h"
  1188. **if (l >u 0) // beq w, r0, Lskip
  1189. if (l >[u] const) // slt[u] w, w, const + 1; bne w, r0, Lskip "nd"
  1190. if (l >[u] r) // slt[u] w, r, l; beq w, r0, Lskip "jc"
  1191. ; l >=[u] 0 // slt[u] w, w, 0; xor w, w, 1 "ks"
  1192. l >=[u] const // slt[u] w, w, const; xor w, w, 1 "ms"
  1193. l >=[u] r // slt[u] w, l, r; xor w, w, 1 "is"
  1194. * if (l >= 0) // bltz w, Lskip "e"
  1195. if (l >=[u] const) // slt[u] w, w, const; bne w, r0, Lskip "md"
  1196. if (l >=[u] r) // slt[u] w, l, r; bne w, r0, Lskip "id"
  1197. l == 0 // sltu w, w, 1 "q"
  1198. l == const // xor w, w, const; sltu w, w, 1 "tq"
  1199. l == r // xor w, l, r; sltu w, w, 1 "rq"
  1200. if (l == 0) // bne w, r0, Lskip "d"
  1201. if (l == const) // xor w, w, const; bne w, r0, Lskip "td"
  1202. if (l == r) // bne l, r, Lskip "b"
  1203. l != 0 // sltu w, r0, w "p"
  1204. l != const // xor w, w, const; sltu w, r0, w "tp"
  1205. l != r // xor w, l, r; sltu w, r0, w "rp"
  1206. if (l != 0) // beq w, r0, Lskip "c"
  1207. if (l != const) // xor w, w, const; beq w, r0, Lskip "tc"
  1208. if (l != r) // beq l, r, Lskip "a"
  1209. */
  1210. char CmpBlocks[6/*op*/][2/*condbranch*/][3/*constness*/][2] =
  1211. {
  1212. {
  1213. { "k", "m", "i" },
  1214. { "f", "mc", "ic" }
  1215. },
  1216. {
  1217. { "l", "n", "js" },
  1218. { "g", "nc", "jd" }
  1219. },
  1220. {
  1221. { "o", "ns", "j" },
  1222. { "h", "nd", "jc" }
  1223. },
  1224. {
  1225. { "ks", "ms", "is" },
  1226. { "e", "md", "id" }
  1227. },
  1228. {
  1229. { "q", "tq", "rq" },
  1230. { "d", "td", "b" }
  1231. },
  1232. {
  1233. { "p", "tp", "rp" },
  1234. { "c", "tc", "a" }
  1235. }
  1236. };
  1237. void GenCmp(word* idx, word op)
  1238. {
  1239. // TODO: direct conversion from MIPS to B32P is very inefficient, so optimize this!
  1240. /*
  1241. MIPS:
  1242. slt reg, s < t (reg := 1, else reg := 0)
  1243. B32P equivalent:
  1244. bge s >= t 2
  1245. load 1 reg
  1246. load 0 reg
  1247. */
  1248. /*
  1249. Inverses:
  1250. BEQ a b BNE a b
  1251. BNE a b BEQ a b
  1252. BLTZ a (a < 0) BGE a r0 (a >= 0)
  1253. BGEZ a (a >=0) BGT r0 a (a < 0) == (0 > a)
  1254. BGTZ a (a > 0) BGE r0 a (a <= 0) == (0 >= a)
  1255. BLEZ a (a <=0) BGT a r0 (a > 0)
  1256. */
  1257. // constness: 0 = zero const, 1 = non-zero const, 2 = non-const
  1258. word constness = (stack[*idx - 1][0] == tokNumInt) ? (stack[*idx - 1][1] != 0) : 2;
  1259. word constval = (constness == 1) ? truncInt(stack[*idx - 1][1]) : 0;
  1260. // condbranch: 0 = no conditional branch, 1 = branch if true, 2 = branch if false
  1261. word condbranch = (*idx + 1 < sp) ? (stack[*idx + 1][0] == tokIf) + (stack[*idx + 1][0] == tokIfNot) * 2 : 0;
  1262. word unsign = op >> 4;
  1263. int slt = unsign ? B32PInstrSLTU : B32PInstrSLT;
  1264. word label = condbranch ? stack[*idx + 1][1] : 0;
  1265. char* p;
  1266. word i;
  1267. op &= 0xF;
  1268. if (constness == 2)
  1269. GenPopReg();
  1270. // bltz, blez, bgez, bgtz are for signed comparison with 0 only,
  1271. // so for conditional branches on <0u, <=0u, >0u, >=0u use the general method instead
  1272. if (condbranch && op < 4 && constness == 0 && unsign)
  1273. {
  1274. // Except, >0u is more optimal as !=0
  1275. if (op == 2)
  1276. op = 5;
  1277. else
  1278. constness = 1;
  1279. }
  1280. p = CmpBlocks[op][condbranch != 0][constness];
  1281. for (i = 0; i < 2; i++)
  1282. {
  1283. switch (p[i])
  1284. {
  1285. case 'a':
  1286. condbranch ^= 3;
  1287. // fallthrough
  1288. case 'b':
  1289. GenPrintInstr3Operands((condbranch == 1) ? B32PInstrBNE : B32PInstrBEQ, 0,
  1290. GenLreg, 0,
  1291. GenRreg, 0,
  1292. B32POpConst, 2);
  1293. GenPrintInstr1Operand(B32PInstrJump, 0,
  1294. B32POpNumLabel, label);
  1295. break;
  1296. case 'c':
  1297. condbranch ^= 3;
  1298. // fallthrough
  1299. case 'd':
  1300. GenPrintInstr3Operands((condbranch == 1) ? B32PInstrBNE : B32PInstrBEQ, 0,
  1301. GenWreg, 0,
  1302. B32POpRegZero, 0,
  1303. B32POpConst, 2);
  1304. GenPrintInstr1Operand(B32PInstrJump, 0,
  1305. B32POpNumLabel, label);
  1306. break;
  1307. case 'e':
  1308. condbranch ^= 3;
  1309. // fallthrough
  1310. case 'f':
  1311. /*
  1312. BLTZ a (a < 0) BGE a r0 (a >= 0)
  1313. BGEZ a (a >=0) BGT r0 a (a < 0) == (0 > a)
  1314. */
  1315. if (condbranch == 1)
  1316. {
  1317. GenPrintInstr3Operands(B32PInstrBGE, 0,
  1318. GenWreg, 0,
  1319. B32POpRegZero, 0,
  1320. B32POpConst, 2);
  1321. }
  1322. else
  1323. {
  1324. GenPrintInstr3Operands(B32PInstrBGT, 0,
  1325. B32POpRegZero, 0,
  1326. GenWreg, 0,
  1327. B32POpConst, 2);
  1328. }
  1329. GenPrintInstr1Operand(B32PInstrJump, 0,
  1330. B32POpNumLabel, label);
  1331. break;
  1332. case 'g':
  1333. condbranch ^= 3;
  1334. // fallthrough
  1335. case 'h':
  1336. /*
  1337. BGTZ a (a > 0) BGE r0 a (a <= 0) == (0 >= a)
  1338. BLEZ a (a <=0) BGT a r0 (a > 0)
  1339. */
  1340. if (condbranch == 1)
  1341. {
  1342. GenPrintInstr3Operands(B32PInstrBGE, 0,
  1343. B32POpRegZero, 0,
  1344. GenWreg, 0,
  1345. B32POpConst, 2);
  1346. }
  1347. else
  1348. {
  1349. GenPrintInstr3Operands(B32PInstrBGT, 0,
  1350. GenWreg, 0,
  1351. B32POpRegZero, 0,
  1352. B32POpConst, 2);
  1353. }
  1354. GenPrintInstr1Operand(B32PInstrJump, 0,
  1355. B32POpNumLabel, label);
  1356. break;
  1357. case 'i':
  1358. GenPrintInstr3Operands(slt, 0,
  1359. GenLreg, 0,
  1360. GenRreg, 0,
  1361. GenWreg, 0);
  1362. break;
  1363. case 'j':
  1364. GenPrintInstr3Operands(slt, 0,
  1365. GenRreg, 0,
  1366. GenLreg, 0,
  1367. GenWreg, 0);
  1368. break;
  1369. case 'k':
  1370. GenPrintInstr3Operands(slt, 0,
  1371. GenWreg, 0,
  1372. B32POpRegZero, 0,
  1373. GenWreg, 0);
  1374. break;
  1375. case 'l':
  1376. GenPrintInstr3Operands(slt, 0,
  1377. GenWreg, 0,
  1378. B32POpConst, 1,
  1379. GenWreg, 0);
  1380. break;
  1381. case 'n':
  1382. constval++;
  1383. // fallthrough
  1384. case 'm':
  1385. if (constval < 0x8000)
  1386. {
  1387. GenPrintInstr3Operands(slt, 0,
  1388. GenWreg, 0,
  1389. B32POpConst, constval,
  1390. GenWreg, 0);
  1391. }
  1392. else
  1393. {
  1394. GenPrintInstr2Operands(B32PInstrLoad, 0,
  1395. B32POpConst, constval,
  1396. TEMP_REG_A, 0);
  1397. GenPrintInstr3Operands(slt, 0,
  1398. GenWreg, 0,
  1399. TEMP_REG_A, 0,
  1400. GenWreg, 0);
  1401. }
  1402. break;
  1403. case 'o':
  1404. GenPrintInstr3Operands(slt, 0,
  1405. B32POpRegZero, 0,
  1406. GenWreg, 0,
  1407. GenWreg, 0);
  1408. break;
  1409. case 'p':
  1410. GenPrintInstr3Operands(B32PInstrSLTU, 0,
  1411. B32POpRegZero, 0,
  1412. GenWreg, 0,
  1413. GenWreg, 0);
  1414. break;
  1415. case 'q':
  1416. GenPrintInstr3Operands(B32PInstrSLTU, 0,
  1417. GenWreg, 0,
  1418. B32POpConst, 1,
  1419. GenWreg, 0);
  1420. break;
  1421. case 'r':
  1422. GenPrintInstr3Operands(B32PInstrXOR, 0,
  1423. GenLreg, 0,
  1424. GenRreg, 0,
  1425. GenWreg, 0);
  1426. break;
  1427. case 's':
  1428. GenPrintInstr3Operands(B32PInstrXOR, 0,
  1429. GenWreg, 0,
  1430. B32POpConst, 1,
  1431. GenWreg, 0);
  1432. break;
  1433. case 't':
  1434. if (constval < 0x8000)
  1435. {
  1436. GenPrintInstr3Operands(B32PInstrXOR, 0,
  1437. GenWreg, 0,
  1438. B32POpConst, constval,
  1439. GenWreg, 0);
  1440. }
  1441. else
  1442. {
  1443. GenPrintInstr2Operands(B32PInstrLoad, 0,
  1444. B32POpConst, constval,
  1445. TEMP_REG_A, 0);
  1446. GenPrintInstr3Operands(B32PInstrXOR, 0,
  1447. GenWreg, 0,
  1448. TEMP_REG_A, 0,
  1449. GenWreg, 0);
  1450. }
  1451. break;
  1452. }
  1453. }
  1454. *idx += condbranch != 0;
  1455. }
  1456. word GenIsCmp(word t)
  1457. {
  1458. return
  1459. t == '<' ||
  1460. t == '>' ||
  1461. t == tokGEQ ||
  1462. t == tokLEQ ||
  1463. t == tokULess ||
  1464. t == tokUGreater ||
  1465. t == tokUGEQ ||
  1466. t == tokULEQ ||
  1467. t == tokEQ ||
  1468. t == tokNEQ;
  1469. }
  1470. // Improved register/stack-based code generator
  1471. // DONE: test 32-bit code generation
  1472. void GenExpr0(void)
  1473. {
  1474. word i;
  1475. word gotUnary = 0;
  1476. word maxCallDepth = 0;
  1477. word callDepth = 0;
  1478. word paramOfs = 0;
  1479. word t = sp - 1;
  1480. if (stack[t][0] == tokIf || stack[t][0] == tokIfNot || stack[t][0] == tokReturn)
  1481. t--;
  1482. GenPrep(&t);
  1483. for (i = 0; i < sp; i++)
  1484. if (stack[i][0] == '(')
  1485. {
  1486. if (++callDepth > maxCallDepth)
  1487. maxCallDepth = callDepth;
  1488. }
  1489. else if (stack[i][0] == ')')
  1490. {
  1491. callDepth--;
  1492. }
  1493. CanUseTempRegs = maxCallDepth == 0;
  1494. TempsUsed = 0;
  1495. if (GenWreg != B32POpRegV0)
  1496. errorInternal(102);
  1497. for (i = 0; i < sp; i++)
  1498. {
  1499. word tok = stack[i][0];
  1500. word v = stack[i][1];
  1501. if (doAnnotations)
  1502. {
  1503. switch (tok)
  1504. {
  1505. case tokNumInt: printf2(" ; "); printd2(truncInt(v)); printf2("\n"); break;
  1506. //case tokNumUint: printf2(" ; %uu\n", truncUint(v)); break;
  1507. case tokIdent: case tokRevIdent: printf2(" ; "); printf2(IdentTable + v); printf2("\n"); break;
  1508. case tokLocalOfs: case tokRevLocalOfs: printf2(" ; local ofs\n"); break;
  1509. case ')': printf2(" ; ) fxn call\n"); break;
  1510. case tokUnaryStar: printf2(" ; * (read dereference)\n"); break;
  1511. case '=': printf2(" ; = (write dereference)\n"); break;
  1512. case tokShortCirc: printf2(" ; short-circuit "); break;
  1513. case tokGoto: printf2(" ; sh-circ-goto "); break;
  1514. case tokLogAnd: printf2(" ; short-circuit && target\n"); break;
  1515. case tokLogOr: printf2(" ; short-circuit || target\n"); break;
  1516. case tokIf: case tokIfNot: case tokReturn: break;
  1517. case tokNum0: printf2(" ; 0\n"); break;
  1518. case tokAssign0: printf2(" ; =\n"); break;
  1519. default: printf2(" ; "); printf2(GetTokenName(tok)); printf2("\n"); break;
  1520. }
  1521. }
  1522. switch (tok)
  1523. {
  1524. case tokNumInt:
  1525. if (!(i + 1 < sp && ((t = stack[i + 1][0]) == '+' ||
  1526. t == '-' ||
  1527. t == '&' ||
  1528. t == '^' ||
  1529. t == '|' ||
  1530. t == tokLShift ||
  1531. t == tokRShift ||
  1532. t == tokURShift ||
  1533. GenIsCmp(t))))
  1534. {
  1535. if (gotUnary)
  1536. GenPushReg();
  1537. GenPrintInstr2Operands(B32PInstrLoad, 0,
  1538. B32POpConst, v,
  1539. GenWreg, 0);
  1540. }
  1541. gotUnary = 1;
  1542. break;
  1543. case tokIdent:
  1544. if (gotUnary)
  1545. GenPushReg();
  1546. if (!(i + 1 < sp && ((t = stack[i + 1][0]) == ')' ||
  1547. t == tokUnaryStar ||
  1548. t == tokInc ||
  1549. t == tokDec ||
  1550. t == tokPostInc ||
  1551. t == tokPostDec)))
  1552. {
  1553. GenPrintInstr2Operands(B32PInstrAddr2reg, 0,
  1554. B32POpLabel, v,
  1555. GenWreg, 0);
  1556. }
  1557. gotUnary = 1;
  1558. break;
  1559. case tokLocalOfs:
  1560. if (gotUnary)
  1561. GenPushReg();
  1562. if (!(i + 1 < sp && ((t = stack[i + 1][0]) == tokUnaryStar ||
  1563. t == tokInc ||
  1564. t == tokDec ||
  1565. t == tokPostInc ||
  1566. t == tokPostDec)))
  1567. {
  1568. GenPrintInstr3Operands(B32PInstrADD, 0,
  1569. B32POpRegFp, 0,
  1570. B32POpConst, v,
  1571. GenWreg, 0);
  1572. }
  1573. gotUnary = 1;
  1574. break;
  1575. case '(':
  1576. if (gotUnary)
  1577. GenPushReg();
  1578. gotUnary = 0;
  1579. if (maxCallDepth != 1 && v < 16)
  1580. GenGrowStack(16 - v);
  1581. paramOfs = v - 4;
  1582. if (maxCallDepth == 1 && paramOfs >= 0 && paramOfs <= 12)
  1583. {
  1584. // Work directly in A0-A3 instead of working in V0 and avoid copying V0 to A0-A3
  1585. GenWreg = B32POpRegA0 + division(paramOfs, 4); //(paramOfs >> 2); //division(paramOfs, 4);
  1586. }
  1587. break;
  1588. case ',':
  1589. if (maxCallDepth == 1)
  1590. {
  1591. if (paramOfs == 16)
  1592. {
  1593. // Got the last on-stack parameter, the rest will go in A0-A3
  1594. GenPushReg();
  1595. gotUnary = 0;
  1596. GenWreg = B32POpRegA3;
  1597. }
  1598. if (paramOfs >= 0 && paramOfs <= 12)
  1599. {
  1600. // Advance to the next An reg or revert to V0
  1601. if (paramOfs)
  1602. GenWreg--;
  1603. else
  1604. GenWreg = B32POpRegV0;
  1605. gotUnary = 0;
  1606. }
  1607. paramOfs -= 4;
  1608. }
  1609. break;
  1610. case ')':
  1611. GenLeaf = 0;
  1612. if (maxCallDepth != 1)
  1613. {
  1614. if (v >= 4)
  1615. GenPrintInstr2Operands(B32PInstrRead, 0,
  1616. B32POpIndRegSp, 0,
  1617. B32POpRegA0, 0);
  1618. if (v >= 8)
  1619. GenPrintInstr2Operands(B32PInstrRead, 0,
  1620. B32POpIndRegSp, 4,
  1621. B32POpRegA1, 0);
  1622. if (v >= 12)
  1623. GenPrintInstr2Operands(B32PInstrRead, 0,
  1624. B32POpIndRegSp, 8,
  1625. B32POpRegA2, 0);
  1626. if (v >= 16)
  1627. GenPrintInstr2Operands(B32PInstrRead, 0,
  1628. B32POpIndRegSp, 12,
  1629. B32POpRegA3, 0);
  1630. }
  1631. else
  1632. {
  1633. GenGrowStack(16);
  1634. }
  1635. if (stack[i - 1][0] == tokIdent)
  1636. {
  1637. GenPrintInstr1Operand(B32PInstrSavPC, 0,
  1638. B32POpRegRa, 0);
  1639. GenPrintInstr3Operands(B32PInstrADD, 0,
  1640. B32POpRegRa, 0,
  1641. B32POpConst, 3,
  1642. B32POpRegRa, 0);
  1643. GenPrintInstr1Operand(B32PInstrJump, 0,
  1644. B32POpLabel, stack[i - 1][1]);
  1645. }
  1646. else
  1647. {
  1648. GenPrintInstr1Operand(B32PInstrSavPC, 0,
  1649. B32POpRegRa, 0);
  1650. GenPrintInstr3Operands(B32PInstrADD, 0,
  1651. B32POpRegRa, 0,
  1652. B32POpConst, 3,
  1653. B32POpRegRa, 0);
  1654. GenPrintInstr2Operands(B32PInstrJumpr, 0,
  1655. B32POpConst, 0,
  1656. GenWreg, 0);
  1657. }
  1658. if (v < 16)
  1659. v = 16;
  1660. GenGrowStack(-v);
  1661. break;
  1662. case tokUnaryStar:
  1663. if (stack[i - 1][0] == tokIdent)
  1664. GenReadIdent(GenWreg, v, stack[i - 1][1]);
  1665. else if (stack[i - 1][0] == tokLocalOfs)
  1666. GenReadLocal(GenWreg, v, stack[i - 1][1]);
  1667. else
  1668. GenReadIndirect(GenWreg, GenWreg, v);
  1669. break;
  1670. case tokUnaryPlus:
  1671. break;
  1672. case '~': //nor
  1673. GenPrintInstr3Operands(B32PInstrOR, 0,
  1674. GenWreg, 0,
  1675. GenWreg, 0,
  1676. GenWreg, 0);
  1677. GenPrintInstr2Operands(B32PInstrNOT, 0,
  1678. GenWreg, 0,
  1679. GenWreg, 0);
  1680. break;
  1681. case tokUnaryMinus:
  1682. GenPrintInstr3Operands(B32PInstrSUB, 0,
  1683. B32POpRegZero, 0,
  1684. GenWreg, 0,
  1685. GenWreg, 0);
  1686. break;
  1687. case '+':
  1688. case '-':
  1689. case '*':
  1690. case '&':
  1691. case '^':
  1692. case '|':
  1693. case tokLShift:
  1694. case tokRShift:
  1695. case tokURShift:
  1696. if (stack[i - 1][0] == tokNumInt && tok != '*')
  1697. {
  1698. word instr = GenGetBinaryOperatorInstr(tok);
  1699. GenPrintInstr3Operands(instr, 0,
  1700. GenWreg, 0,
  1701. B32POpConst, stack[i - 1][1],
  1702. GenWreg, 0);
  1703. }
  1704. else
  1705. {
  1706. word instr = GenGetBinaryOperatorInstr(tok);
  1707. GenPopReg();
  1708. GenPrintInstr3Operands(instr, 0,
  1709. GenLreg, 0,
  1710. GenRreg, 0,
  1711. GenWreg, 0);
  1712. }
  1713. break;
  1714. case '/':
  1715. case tokUDiv:
  1716. case '%':
  1717. case tokUMod:
  1718. {
  1719. printf("DIVISION/MOD is not supported!\n");
  1720. /*
  1721. GenPopReg();
  1722. if (tok == '/' || tok == '%')
  1723. GenPrintInstr3Operands(MipsInstrDiv, 0,
  1724. B32POpRegZero, 0,
  1725. GenLreg, 0,
  1726. GenRreg, 0);
  1727. else
  1728. GenPrintInstr3Operands(MipsInstrDivU, 0,
  1729. B32POpRegZero, 0,
  1730. GenLreg, 0,
  1731. GenRreg, 0);
  1732. if (tok == '%' || tok == tokUMod)
  1733. GenPrintInstr1Operand(MipsInstrMfHi, 0,
  1734. GenWreg, 0);
  1735. else
  1736. GenPrintInstr1Operand(MipsInstrMfLo, 0,
  1737. GenWreg, 0);
  1738. */
  1739. }
  1740. break;
  1741. case tokInc:
  1742. case tokDec:
  1743. if (stack[i - 1][0] == tokIdent)
  1744. {
  1745. GenIncDecIdent(GenWreg, v, stack[i - 1][1], tok);
  1746. }
  1747. else if (stack[i - 1][0] == tokLocalOfs)
  1748. {
  1749. GenIncDecLocal(GenWreg, v, stack[i - 1][1], tok);
  1750. }
  1751. else
  1752. {
  1753. GenPrintInstr3Operands(B32PInstrOR, 0,
  1754. B32POpRegZero, 0,
  1755. GenWreg, 0,
  1756. TEMP_REG_A, 0);
  1757. GenIncDecIndirect(GenWreg, TEMP_REG_A, v, tok);
  1758. }
  1759. break;
  1760. case tokPostInc:
  1761. case tokPostDec:
  1762. if (stack[i - 1][0] == tokIdent)
  1763. {
  1764. GenPostIncDecIdent(GenWreg, v, stack[i - 1][1], tok);
  1765. }
  1766. else if (stack[i - 1][0] == tokLocalOfs)
  1767. {
  1768. GenPostIncDecLocal(GenWreg, v, stack[i - 1][1], tok);
  1769. }
  1770. else
  1771. {
  1772. GenPrintInstr3Operands(B32PInstrOR, 0,
  1773. B32POpRegZero, 0,
  1774. GenWreg, 0,
  1775. TEMP_REG_A, 0);
  1776. GenPostIncDecIndirect(GenWreg, TEMP_REG_A, v, tok);
  1777. }
  1778. break;
  1779. case tokPostAdd:
  1780. case tokPostSub:
  1781. {
  1782. word instr = GenGetBinaryOperatorInstr(tok);
  1783. GenPopReg();
  1784. if (GenWreg == GenLreg)
  1785. {
  1786. GenPrintInstr3Operands(B32PInstrOR, 0,
  1787. B32POpRegZero, 0,
  1788. GenLreg, 0,
  1789. TEMP_REG_B, 0);
  1790. GenReadIndirect(GenWreg, TEMP_REG_B, v);
  1791. GenPrintInstr3Operands(instr, 0,
  1792. GenWreg, 0,
  1793. GenRreg, 0,
  1794. TEMP_REG_A, 0);
  1795. GenWriteIndirect(TEMP_REG_B, TEMP_REG_A, v);
  1796. }
  1797. else
  1798. {
  1799. // GenWreg == GenRreg here
  1800. GenPrintInstr3Operands(B32PInstrOR, 0,
  1801. B32POpRegZero, 0,
  1802. GenRreg, 0,
  1803. TEMP_REG_B, 0);
  1804. GenReadIndirect(GenWreg, GenLreg, v);
  1805. GenPrintInstr3Operands(instr, 0,
  1806. GenWreg, 0,
  1807. TEMP_REG_B, 0,
  1808. TEMP_REG_B, 0);
  1809. GenWriteIndirect(GenLreg, TEMP_REG_B, v);
  1810. }
  1811. }
  1812. break;
  1813. case tokAssignAdd:
  1814. case tokAssignSub:
  1815. case tokAssignMul:
  1816. case tokAssignAnd:
  1817. case tokAssignXor:
  1818. case tokAssignOr:
  1819. case tokAssignLSh:
  1820. case tokAssignRSh:
  1821. case tokAssignURSh:
  1822. if (stack[i - 1][0] == tokRevLocalOfs || stack[i - 1][0] == tokRevIdent)
  1823. {
  1824. word instr = GenGetBinaryOperatorInstr(tok);
  1825. if (stack[i - 1][0] == tokRevLocalOfs)
  1826. GenReadLocal(TEMP_REG_B, v, stack[i - 1][1]);
  1827. else
  1828. GenReadIdent(TEMP_REG_B, v, stack[i - 1][1]);
  1829. GenPrintInstr3Operands(instr, 0,
  1830. TEMP_REG_B, 0,
  1831. GenWreg, 0,
  1832. GenWreg, 0);
  1833. if (stack[i - 1][0] == tokRevLocalOfs)
  1834. GenWriteLocal(GenWreg, v, stack[i - 1][1]);
  1835. else
  1836. GenWriteIdent(GenWreg, v, stack[i - 1][1]);
  1837. }
  1838. else
  1839. {
  1840. word instr = GenGetBinaryOperatorInstr(tok);
  1841. word lsaved, rsaved;
  1842. GenPopReg();
  1843. if (GenWreg == GenLreg)
  1844. {
  1845. GenPrintInstr3Operands(B32PInstrOR, 0,
  1846. B32POpRegZero, 0,
  1847. GenLreg, 0,
  1848. TEMP_REG_B, 0);
  1849. lsaved = TEMP_REG_B;
  1850. rsaved = GenRreg;
  1851. }
  1852. else
  1853. {
  1854. // GenWreg == GenRreg here
  1855. GenPrintInstr3Operands(B32PInstrOR, 0,
  1856. B32POpRegZero, 0,
  1857. GenRreg, 0,
  1858. TEMP_REG_B, 0);
  1859. rsaved = TEMP_REG_B;
  1860. lsaved = GenLreg;
  1861. }
  1862. GenReadIndirect(GenWreg, GenLreg, v); // destroys either GenLreg or GenRreg because GenWreg coincides with one of them
  1863. GenPrintInstr3Operands(instr, 0,
  1864. GenWreg, 0,
  1865. rsaved, 0,
  1866. GenWreg, 0);
  1867. GenWriteIndirect(lsaved, GenWreg, v);
  1868. }
  1869. GenExtendRegIfNeeded(GenWreg, v);
  1870. break;
  1871. case tokAssignDiv:
  1872. case tokAssignUDiv:
  1873. case tokAssignMod:
  1874. case tokAssignUMod:
  1875. if (stack[i - 1][0] == tokRevLocalOfs || stack[i - 1][0] == tokRevIdent)
  1876. {
  1877. if (stack[i - 1][0] == tokRevLocalOfs)
  1878. GenReadLocal(TEMP_REG_B, v, stack[i - 1][1]);
  1879. else
  1880. GenReadIdent(TEMP_REG_B, v, stack[i - 1][1]);
  1881. if (tok == tokAssignDiv || tok == tokAssignMod)
  1882. {
  1883. printf("DIVISION/MOD is not supported!\n");
  1884. /*
  1885. GenPrintInstr3Operands(MipsInstrDiv, 0,
  1886. B32POpRegZero, 0,
  1887. TEMP_REG_B, 0,
  1888. GenWreg, 0);*/
  1889. }
  1890. else
  1891. {
  1892. printf("DIVISION/MOD is not supported!\n");
  1893. /*
  1894. GenPrintInstr3Operands(MipsInstrDivU, 0,
  1895. B32POpRegZero, 0,
  1896. TEMP_REG_B, 0,
  1897. GenWreg, 0);*/
  1898. }
  1899. if (tok == tokAssignMod || tok == tokAssignUMod)
  1900. {
  1901. printf("DIVISION/MOD is not supported!\n");
  1902. /*
  1903. GenPrintInstr1Operand(MipsInstrMfHi, 0,
  1904. GenWreg, 0);*/
  1905. }
  1906. else
  1907. {
  1908. printf("DIVISION/MOD is not supported!\n");
  1909. /*
  1910. GenPrintInstr1Operand(MipsInstrMfLo, 0,
  1911. GenWreg, 0);*/
  1912. }
  1913. if (stack[i - 1][0] == tokRevLocalOfs)
  1914. GenWriteLocal(GenWreg, v, stack[i - 1][1]);
  1915. else
  1916. GenWriteIdent(GenWreg, v, stack[i - 1][1]);
  1917. }
  1918. else
  1919. {
  1920. word lsaved, rsaved;
  1921. GenPopReg();
  1922. if (GenWreg == GenLreg)
  1923. {
  1924. GenPrintInstr3Operands(B32PInstrOR, 0,
  1925. B32POpRegZero, 0,
  1926. GenLreg, 0,
  1927. TEMP_REG_B, 0);
  1928. lsaved = TEMP_REG_B;
  1929. rsaved = GenRreg;
  1930. }
  1931. else
  1932. {
  1933. // GenWreg == GenRreg here
  1934. GenPrintInstr3Operands(B32PInstrOR, 0,
  1935. B32POpRegZero, 0,
  1936. GenRreg, 0,
  1937. TEMP_REG_B, 0);
  1938. rsaved = TEMP_REG_B;
  1939. lsaved = GenLreg;
  1940. }
  1941. GenReadIndirect(GenWreg, GenLreg, v); // destroys either GenLreg or GenRreg because GenWreg coincides with one of them
  1942. if (tok == tokAssignDiv || tok == tokAssignMod)
  1943. {
  1944. printf("DIVISION/MOD is not supported!\n");
  1945. /*
  1946. GenPrintInstr3Operands(MipsInstrDiv, 0,
  1947. B32POpRegZero, 0,
  1948. GenWreg, 0,
  1949. rsaved, 0);*/
  1950. }
  1951. else
  1952. {
  1953. printf("DIVISION/MOD is not supported!\n");
  1954. /*
  1955. GenPrintInstr3Operands(MipsInstrDivU, 0,
  1956. B32POpRegZero, 0,
  1957. GenWreg, 0,
  1958. rsaved, 0);*/
  1959. }
  1960. if (tok == tokAssignMod || tok == tokAssignUMod)
  1961. {
  1962. printf("DIVISION/MOD is not supported!\n");
  1963. /*
  1964. GenPrintInstr1Operand(MipsInstrMfHi, 0,
  1965. GenWreg, 0);*/
  1966. }
  1967. else
  1968. {
  1969. printf("DIVISION/MOD is not supported!\n");
  1970. /*
  1971. GenPrintInstr1Operand(MipsInstrMfLo, 0,
  1972. GenWreg, 0);*/
  1973. }
  1974. GenWriteIndirect(lsaved, GenWreg, v);
  1975. }
  1976. GenExtendRegIfNeeded(GenWreg, v);
  1977. break;
  1978. case '=':
  1979. if (stack[i - 1][0] == tokRevLocalOfs)
  1980. {
  1981. GenWriteLocal(GenWreg, v, stack[i - 1][1]);
  1982. }
  1983. else if (stack[i - 1][0] == tokRevIdent)
  1984. {
  1985. GenWriteIdent(GenWreg, v, stack[i - 1][1]);
  1986. }
  1987. else
  1988. {
  1989. GenPopReg();
  1990. GenWriteIndirect(GenLreg, GenRreg, v);
  1991. if (GenWreg != GenRreg)
  1992. GenPrintInstr3Operands(B32PInstrOR, 0,
  1993. B32POpRegZero, 0,
  1994. GenRreg, 0,
  1995. GenWreg, 0);
  1996. }
  1997. GenExtendRegIfNeeded(GenWreg, v);
  1998. break;
  1999. case tokAssign0: // assignment of 0, while throwing away the expression result value
  2000. if (stack[i - 1][0] == tokRevLocalOfs)
  2001. {
  2002. GenWriteLocal(B32POpRegZero, v, stack[i - 1][1]);
  2003. }
  2004. else if (stack[i - 1][0] == tokRevIdent)
  2005. {
  2006. GenWriteIdent(B32POpRegZero, v, stack[i - 1][1]);
  2007. }
  2008. else
  2009. {
  2010. GenWriteIndirect(GenWreg, B32POpRegZero, v);
  2011. }
  2012. break;
  2013. case '<': GenCmp(&i, 0x00); break;
  2014. case tokLEQ: GenCmp(&i, 0x01); break;
  2015. case '>': GenCmp(&i, 0x02); break;
  2016. case tokGEQ: GenCmp(&i, 0x03); break;
  2017. case tokULess: GenCmp(&i, 0x10); break;
  2018. case tokULEQ: GenCmp(&i, 0x11); break;
  2019. case tokUGreater: GenCmp(&i, 0x12); break;
  2020. case tokUGEQ: GenCmp(&i, 0x13); break;
  2021. case tokEQ: GenCmp(&i, 0x04); break;
  2022. case tokNEQ: GenCmp(&i, 0x05); break;
  2023. case tok_Bool:
  2024. /* if 0 < wreg (if wreg > 0)
  2025. wreg = 1
  2026. else
  2027. wreg = 0
  2028. GenPrintInstr3Operands(MipsInstrSLTU, 0,
  2029. GenWreg, 0,
  2030. B32POpRegZero, 0,
  2031. GenWreg, 0);
  2032. */
  2033. GenPrintInstr3Operands(B32PInstrSLTU, 0,
  2034. B32POpRegZero, 0,
  2035. GenWreg, 0,
  2036. GenWreg, 0);
  2037. break;
  2038. case tokSChar:
  2039. /* just use as an int for now
  2040. GenPrintInstr3Operands(B32PInstrSHIFTL, 0,
  2041. GenWreg, 0,
  2042. B32POpConst, 24,
  2043. GenWreg, 0);
  2044. GenPrintInstr3Operands(B32PInstrSHIFTR, 0, //TODO this should have also replicated the sign bit (SRA)
  2045. GenWreg, 0,
  2046. B32POpConst, 24,
  2047. GenWreg, 0);
  2048. */
  2049. break;
  2050. case tokUChar:
  2051. /* just use as an int for now
  2052. GenPrintInstr3Operands(B32PInstrAND, 0,
  2053. GenWreg, 0,
  2054. B32POpConst, 0xFF,
  2055. GenWreg, 0);
  2056. */
  2057. break;
  2058. case tokShort:
  2059. /* just use as an int for now
  2060. GenPrintInstr3Operands(B32PInstrSHIFTL, 0,
  2061. GenWreg, 0,
  2062. B32POpConst, 16,
  2063. GenWreg, 0);
  2064. GenPrintInstr3Operands(B32PInstrSHIFTR, 0, //TODO this should have also replicated the sign bit (SRA)
  2065. GenWreg, 0,
  2066. B32POpConst, 16,
  2067. GenWreg, 0);
  2068. */
  2069. break;
  2070. case tokUShort:
  2071. /*
  2072. GenPrintInstr3Operands(MipsInstrAnd, 0,
  2073. GenWreg, 0,
  2074. GenWreg, 0,
  2075. B32POpConst, 0xFFFF);*/
  2076. /* just use as an int for now
  2077. GenPrintInstr3Operands(B32PInstrSHIFTL, 0,
  2078. GenWreg, 0,
  2079. B32POpConst, 16,
  2080. GenWreg, 0);
  2081. GenPrintInstr3Operands(B32PInstrSHIFTR, 0,
  2082. GenWreg, 0,
  2083. B32POpConst, 16,
  2084. GenWreg, 0);
  2085. */
  2086. break;
  2087. case tokShortCirc:
  2088. if (doAnnotations)
  2089. {
  2090. if (v >= 0)
  2091. printf2("&&\n");
  2092. else
  2093. printf2("||\n");
  2094. }
  2095. if (v >= 0)
  2096. GenJumpIfZero(v); // &&
  2097. else
  2098. GenJumpIfNotZero(-v); // ||
  2099. gotUnary = 0;
  2100. break;
  2101. case tokGoto:
  2102. if (doAnnotations)
  2103. {
  2104. printf2("goto\n");
  2105. }
  2106. GenJumpUncond(v);
  2107. gotUnary = 0;
  2108. break;
  2109. case tokLogAnd:
  2110. case tokLogOr:
  2111. GenNumLabel(v);
  2112. break;
  2113. case tokVoid:
  2114. gotUnary = 0;
  2115. break;
  2116. case tokRevIdent:
  2117. case tokRevLocalOfs:
  2118. case tokComma:
  2119. case tokReturn:
  2120. case tokNum0:
  2121. break;
  2122. case tokIf:
  2123. GenJumpIfNotZero(stack[i][1]);
  2124. break;
  2125. case tokIfNot:
  2126. GenJumpIfZero(stack[i][1]);
  2127. break;
  2128. default:
  2129. //error("Error: Internal Error: GenExpr0(): unexpected token %s\n", GetTokenName(tok));
  2130. errorInternal(103);
  2131. break;
  2132. }
  2133. }
  2134. if (GenWreg != B32POpRegV0)
  2135. errorInternal(104);
  2136. }
  2137. void GenDumpChar(word ch)
  2138. {
  2139. if (ch < 0)
  2140. {
  2141. if (TokenStringLen)
  2142. printf2("\n");
  2143. return;
  2144. }
  2145. if (TokenStringLen == 0)
  2146. {
  2147. GenStartAsciiString();
  2148. //printf2("\"");
  2149. }
  2150. // Just print as ascii value
  2151. printd2(ch);
  2152. printf2(" ");
  2153. }
  2154. void GenExpr(void)
  2155. {
  2156. GenExpr0();
  2157. }
  2158. void GenFin(void)
  2159. {
  2160. // No idea what this does (something with structs??), so I just literally converted it to B32P asm
  2161. if (StructCpyLabel)
  2162. {
  2163. word lbl = LabelCnt++;
  2164. puts2(CodeHeaderFooter[0]);
  2165. GenNumLabel(StructCpyLabel);
  2166. //puts2(" move r2, r6\n" //r2 := r6
  2167. // " move r3, r6"); //r3 := r3
  2168. puts2(" or r0 r6 r2\n"
  2169. " or r0 r6 r3");
  2170. GenNumLabel(lbl);
  2171. //puts2(" lbu r6, 0 r5\n" // r6:=mem[r5]
  2172. // " addiu r5, r5, 1\n" // r5:= r5+1
  2173. // " addiu r4, r4, -1\n" // r4:= r4-1
  2174. // " sb r6, 0 r3\n" // mem[r3]:=r6
  2175. // " addiu r3, r3, 1"); // r3:= r3+1
  2176. puts2(" read 0 r5 r6\n"
  2177. " add r5 1 r5\n"
  2178. " sub r4 1 r4\n"
  2179. " write 0 r3 r6\n"
  2180. " add r3 1 r3");
  2181. //printf2(" bne r4, r0, "); GenPrintNumLabel(lbl); // if r4 != 0, jump to lbl
  2182. printf2("beq r4 r0 2\n");
  2183. printf2("jump ");GenPrintNumLabel(lbl);
  2184. puts2("");
  2185. puts2(" jumpr 0 r15");
  2186. puts2(CodeHeaderFooter[1]);
  2187. }
  2188. // Put all ending C specific wrapper code here
  2189. if (compileUserBDOS)
  2190. {
  2191. printf2(
  2192. ".code\n"
  2193. "Int:\n"
  2194. " load32 0x7BFFFF r13\n"
  2195. " load32 0 r14\n"
  2196. " addr2reg Return_Interrupt r1\n"
  2197. " or r0 r1 r15\n"
  2198. " jump interrupt\n"
  2199. " halt\n"
  2200. "Return_Interrupt:\n"
  2201. " pop r1\n"
  2202. " jumpr 3 r1\n"
  2203. " halt\n"
  2204. );
  2205. }
  2206. else
  2207. {
  2208. printf2(
  2209. ".code\n"
  2210. "Int:\n"
  2211. " push r1\n"
  2212. " push r2\n"
  2213. " push r3\n"
  2214. " push r4\n"
  2215. " push r5\n"
  2216. " push r6\n"
  2217. " push r7\n"
  2218. " push r8\n"
  2219. " push r9\n"
  2220. " push r10\n"
  2221. " push r11\n"
  2222. " push r12\n"
  2223. " push r13\n"
  2224. " push r14\n"
  2225. " push r15\n"
  2226. " load32 0x7FFFFF r13\n"
  2227. " load32 0 r14\n"
  2228. " addr2reg Return_Interrupt r1\n"
  2229. " or r0 r1 r15\n"
  2230. " jump int1\n"
  2231. " halt\n"
  2232. "Return_Interrupt:\n"
  2233. " pop r15\n"
  2234. " pop r14\n"
  2235. " pop r13\n"
  2236. " pop r12\n"
  2237. " pop r11\n"
  2238. " pop r10\n"
  2239. " pop r9\n"
  2240. " pop r8\n"
  2241. " pop r7\n"
  2242. " pop r6\n"
  2243. " pop r5\n"
  2244. " pop r4\n"
  2245. " pop r3\n"
  2246. " pop r2\n"
  2247. " pop r1\n"
  2248. " reti\n"
  2249. " halt\n");
  2250. }
  2251. if (compileOS)
  2252. {
  2253. printf2(
  2254. ".code\n"
  2255. "Syscall:\n"
  2256. " load32 0x3FFFFF r13\n"
  2257. " load32 0 r14\n"
  2258. " addr2reg Return_Syscall r1\n"
  2259. " or r0 r1 r15\n"
  2260. " jump syscall\n"
  2261. " halt\n"
  2262. "Return_Syscall:\n"
  2263. " pop r1\n"
  2264. " jumpr 3 r1\n"
  2265. " halt\n"
  2266. );
  2267. }
  2268. }