compile-i386.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396
  1. /*
  2. * sparse/compile-i386.c
  3. *
  4. * Copyright (C) 2003 Transmeta Corp.
  5. * 2003 Linus Torvalds
  6. * Copyright 2003 Jeff Garzik
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. * x86 backend
  27. *
  28. * TODO list:
  29. * in general, any non-32bit SYM_BASETYPE is unlikely to work.
  30. * complex initializers
  31. * bitfields
  32. * global struct/union variables
  33. * addressing structures, and members of structures (as opposed to
  34. * scalars) on the stack. Requires smarter stack frame allocation.
  35. * labels / goto
  36. * any function argument that isn't 32 bits (or promoted to such)
  37. * inline asm
  38. * floating point
  39. *
  40. */
  41. #include <stdarg.h>
  42. #include <stdlib.h>
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include <ctype.h>
  46. #include <unistd.h>
  47. #include <fcntl.h>
  48. #include <assert.h>
  49. #include "lib.h"
  50. #include "allocate.h"
  51. #include "token.h"
  52. #include "parse.h"
  53. #include "symbol.h"
  54. #include "scope.h"
  55. #include "expression.h"
  56. #include "target.h"
  57. #include "compile.h"
  58. #include "bitmap.h"
  59. #include "version.h"
  60. struct textbuf {
  61. unsigned int len; /* does NOT include terminating null */
  62. char *text;
  63. struct textbuf *next;
  64. struct textbuf *prev;
  65. };
  66. struct loop_stack {
  67. int continue_lbl;
  68. int loop_bottom_lbl;
  69. struct loop_stack *next;
  70. };
  71. struct atom;
  72. struct storage;
  73. DECLARE_PTR_LIST(str_list, struct atom);
  74. DECLARE_PTR_LIST(atom_list, struct atom);
  75. DECLARE_PTR_LIST(storage_list, struct storage);
  76. struct function {
  77. int stack_size;
  78. int pseudo_nr;
  79. struct storage_list *pseudo_list;
  80. struct atom_list *atom_list;
  81. struct str_list *str_list;
  82. struct loop_stack *loop_stack;
  83. struct symbol **argv;
  84. unsigned int argc;
  85. int ret_target;
  86. };
  87. enum storage_type {
  88. STOR_PSEUDO, /* variable stored on the stack */
  89. STOR_ARG, /* function argument */
  90. STOR_SYM, /* a symbol we can directly ref in the asm */
  91. STOR_REG, /* scratch register */
  92. STOR_VALUE, /* integer constant */
  93. STOR_LABEL, /* label / jump target */
  94. STOR_LABELSYM, /* label generated from symbol's pointer value */
  95. };
  96. struct reg_info {
  97. const char *name;
  98. struct storage *contains;
  99. const unsigned char aliases[12];
  100. #define own_regno aliases[0]
  101. };
  102. struct storage {
  103. enum storage_type type;
  104. unsigned long flags;
  105. /* STOR_REG */
  106. struct reg_info *reg;
  107. struct symbol *ctype;
  108. union {
  109. /* STOR_PSEUDO */
  110. struct {
  111. int pseudo;
  112. int offset;
  113. int size;
  114. };
  115. /* STOR_ARG */
  116. struct {
  117. int idx;
  118. };
  119. /* STOR_SYM */
  120. struct {
  121. struct symbol *sym;
  122. };
  123. /* STOR_VALUE */
  124. struct {
  125. long long value;
  126. };
  127. /* STOR_LABEL */
  128. struct {
  129. int label;
  130. };
  131. /* STOR_LABELSYM */
  132. struct {
  133. struct symbol *labelsym;
  134. };
  135. };
  136. };
  137. enum {
  138. STOR_LABEL_VAL = (1 << 0),
  139. STOR_WANTS_FREE = (1 << 1),
  140. };
  141. struct symbol_private {
  142. struct storage *addr;
  143. };
  144. enum atom_type {
  145. ATOM_TEXT,
  146. ATOM_INSN,
  147. ATOM_CSTR,
  148. };
  149. struct atom {
  150. enum atom_type type;
  151. union {
  152. /* stuff for text */
  153. struct {
  154. char *text;
  155. unsigned int text_len; /* w/o terminating null */
  156. };
  157. /* stuff for insns */
  158. struct {
  159. char insn[32];
  160. char comment[40];
  161. struct storage *op1;
  162. struct storage *op2;
  163. };
  164. /* stuff for C strings */
  165. struct {
  166. struct string *string;
  167. int label;
  168. };
  169. };
  170. };
  171. static struct function *current_func = NULL;
  172. static struct textbuf *unit_post_text = NULL;
  173. static const char *current_section;
  174. static void emit_comment(const char * fmt, ...) FORMAT_ATTR(1);
  175. static void emit_move(struct storage *src, struct storage *dest,
  176. struct symbol *ctype, const char *comment);
  177. static struct storage *x86_address_gen(struct expression *expr);
  178. static struct storage *x86_symbol_expr(struct symbol *sym);
  179. static void x86_symbol(struct symbol *sym);
  180. static struct storage *x86_statement(struct statement *stmt);
  181. static struct storage *x86_expression(struct expression *expr);
  182. enum registers {
  183. NOREG,
  184. AL, DL, CL, BL, AH, DH, CH, BH, // 8-bit
  185. AX, DX, CX, BX, SI, DI, BP, SP, // 16-bit
  186. EAX, EDX, ECX, EBX, ESI, EDI, EBP, ESP, // 32-bit
  187. EAX_EDX, ECX_EBX, ESI_EDI, // 64-bit
  188. };
  189. /* This works on regno's, reg_info's and hardreg_storage's */
  190. #define byte_reg(reg) ((reg) - 16)
  191. #define highbyte_reg(reg) ((reg)-12)
  192. #define word_reg(reg) ((reg)-8)
  193. #define REGINFO(nr, str, conflicts...) [nr] = { .name = str, .aliases = { nr , conflicts } }
  194. static struct reg_info reg_info_table[] = {
  195. REGINFO( AL, "%al", AX, EAX, EAX_EDX),
  196. REGINFO( DL, "%dl", DX, EDX, EAX_EDX),
  197. REGINFO( CL, "%cl", CX, ECX, ECX_EBX),
  198. REGINFO( BL, "%bl", BX, EBX, ECX_EBX),
  199. REGINFO( AH, "%ah", AX, EAX, EAX_EDX),
  200. REGINFO( DH, "%dh", DX, EDX, EAX_EDX),
  201. REGINFO( CH, "%ch", CX, ECX, ECX_EBX),
  202. REGINFO( BH, "%bh", BX, EBX, ECX_EBX),
  203. REGINFO( AX, "%ax", AL, AH, EAX, EAX_EDX),
  204. REGINFO( DX, "%dx", DL, DH, EDX, EAX_EDX),
  205. REGINFO( CX, "%cx", CL, CH, ECX, ECX_EBX),
  206. REGINFO( BX, "%bx", BL, BH, EBX, ECX_EBX),
  207. REGINFO( SI, "%si", ESI, ESI_EDI),
  208. REGINFO( DI, "%di", EDI, ESI_EDI),
  209. REGINFO( BP, "%bp", EBP),
  210. REGINFO( SP, "%sp", ESP),
  211. REGINFO(EAX, "%eax", AL, AH, AX, EAX_EDX),
  212. REGINFO(EDX, "%edx", DL, DH, DX, EAX_EDX),
  213. REGINFO(ECX, "%ecx", CL, CH, CX, ECX_EBX),
  214. REGINFO(EBX, "%ebx", BL, BH, BX, ECX_EBX),
  215. REGINFO(ESI, "%esi", SI, ESI_EDI),
  216. REGINFO(EDI, "%edi", DI, ESI_EDI),
  217. REGINFO(EBP, "%ebp", BP),
  218. REGINFO(ESP, "%esp", SP),
  219. REGINFO(EAX_EDX, "%eax:%edx", AL, AH, AX, EAX, DL, DH, DX, EDX),
  220. REGINFO(ECX_EBX, "%ecx:%ebx", CL, CH, CX, ECX, BL, BH, BX, EBX),
  221. REGINFO(ESI_EDI, "%esi:%edi", SI, ESI, DI, EDI),
  222. };
  223. #define REGSTORAGE(nr) [nr] = { .type = STOR_REG, .reg = reg_info_table + (nr) }
  224. static struct storage hardreg_storage_table[] = {
  225. REGSTORAGE(AL), REGSTORAGE(DL), REGSTORAGE(CL), REGSTORAGE(BL),
  226. REGSTORAGE(AH), REGSTORAGE(DH), REGSTORAGE(CH), REGSTORAGE(BH),
  227. REGSTORAGE(AX), REGSTORAGE(DX), REGSTORAGE(CX), REGSTORAGE(BX),
  228. REGSTORAGE(SI), REGSTORAGE(DI), REGSTORAGE(BP), REGSTORAGE(SP),
  229. REGSTORAGE(EAX), REGSTORAGE(EDX), REGSTORAGE(ECX), REGSTORAGE(EBX),
  230. REGSTORAGE(ESI), REGSTORAGE(EDI), REGSTORAGE(EBP), REGSTORAGE(ESP),
  231. REGSTORAGE(EAX_EDX), REGSTORAGE(ECX_EBX), REGSTORAGE(ESI_EDI),
  232. };
  233. #define REG_EAX (&hardreg_storage_table[EAX])
  234. #define REG_ECX (&hardreg_storage_table[ECX])
  235. #define REG_EDX (&hardreg_storage_table[EDX])
  236. #define REG_ESP (&hardreg_storage_table[ESP])
  237. #define REG_DL (&hardreg_storage_table[DL])
  238. #define REG_DX (&hardreg_storage_table[DX])
  239. #define REG_AL (&hardreg_storage_table[AL])
  240. #define REG_AX (&hardreg_storage_table[AX])
  241. static DECLARE_BITMAP(regs_in_use, 256);
  242. static inline struct storage * reginfo_reg(struct reg_info *info)
  243. {
  244. return hardreg_storage_table + info->own_regno;
  245. }
  246. static struct storage * get_hardreg(struct storage *reg, int clear)
  247. {
  248. struct reg_info *info = reg->reg;
  249. const unsigned char *aliases;
  250. int regno;
  251. aliases = info->aliases;
  252. while ((regno = *aliases++) != NOREG) {
  253. if (test_bit(regno, regs_in_use))
  254. goto busy;
  255. if (clear)
  256. reg_info_table[regno].contains = NULL;
  257. }
  258. set_bit(info->own_regno, regs_in_use);
  259. return reg;
  260. busy:
  261. fprintf(stderr, "register %s is busy\n", info->name);
  262. if (regno + reg_info_table != info)
  263. fprintf(stderr, " conflicts with %s\n", reg_info_table[regno].name);
  264. exit(1);
  265. }
  266. static void put_reg(struct storage *reg)
  267. {
  268. struct reg_info *info = reg->reg;
  269. int regno = info->own_regno;
  270. if (test_and_clear_bit(regno, regs_in_use))
  271. return;
  272. fprintf(stderr, "freeing already free'd register %s\n", reg_info_table[regno].name);
  273. }
  274. struct regclass {
  275. const char *name;
  276. const unsigned char regs[30];
  277. };
  278. static struct regclass regclass_8 = { "8-bit", { AL, DL, CL, BL, AH, DH, CH, BH }};
  279. static struct regclass regclass_16 = { "16-bit", { AX, DX, CX, BX, SI, DI, BP }};
  280. static struct regclass regclass_32 = { "32-bit", { EAX, EDX, ECX, EBX, ESI, EDI, EBP }};
  281. static struct regclass regclass_64 = { "64-bit", { EAX_EDX, ECX_EBX, ESI_EDI }};
  282. static struct regclass regclass_32_8 = { "32-bit bytes", { EAX, EDX, ECX, EBX }};
  283. static struct regclass *get_regclass_bits(int bits)
  284. {
  285. switch (bits) {
  286. case 8: return &regclass_8;
  287. case 16: return &regclass_16;
  288. case 64: return &regclass_64;
  289. default: return &regclass_32;
  290. }
  291. }
  292. static struct regclass *get_regclass(struct expression *expr)
  293. {
  294. return get_regclass_bits(expr->ctype->bit_size);
  295. }
  296. static int register_busy(int regno)
  297. {
  298. if (!test_bit(regno, regs_in_use)) {
  299. struct reg_info *info = reg_info_table + regno;
  300. const unsigned char *regs = info->aliases+1;
  301. while ((regno = *regs) != NOREG) {
  302. regs++;
  303. if (test_bit(regno, regs_in_use))
  304. goto busy;
  305. }
  306. return 0;
  307. }
  308. busy:
  309. return 1;
  310. }
  311. static struct storage *get_reg(struct regclass *class)
  312. {
  313. const unsigned char *regs = class->regs;
  314. int regno;
  315. while ((regno = *regs) != NOREG) {
  316. regs++;
  317. if (register_busy(regno))
  318. continue;
  319. return get_hardreg(hardreg_storage_table + regno, 1);
  320. }
  321. fprintf(stderr, "Ran out of %s registers\n", class->name);
  322. exit(1);
  323. }
  324. static struct storage *get_reg_value(struct storage *value, struct regclass *class)
  325. {
  326. struct reg_info *info;
  327. struct storage *reg;
  328. /* Do we already have it somewhere */
  329. info = value->reg;
  330. if (info && info->contains == value) {
  331. emit_comment("already have register %s", info->name);
  332. return get_hardreg(hardreg_storage_table + info->own_regno, 0);
  333. }
  334. reg = get_reg(class);
  335. emit_move(value, reg, value->ctype, "reload register");
  336. info = reg->reg;
  337. info->contains = value;
  338. value->reg = info;
  339. return reg;
  340. }
  341. static struct storage *temp_from_bits(unsigned int bit_size)
  342. {
  343. return get_reg(get_regclass_bits(bit_size));
  344. }
  345. static inline unsigned int pseudo_offset(struct storage *s)
  346. {
  347. if (s->type != STOR_PSEUDO)
  348. return 123456; /* intentionally bogus value */
  349. return s->offset;
  350. }
  351. static inline unsigned int arg_offset(struct storage *s)
  352. {
  353. if (s->type != STOR_ARG)
  354. return 123456; /* intentionally bogus value */
  355. /* FIXME: this is wrong wrong wrong */
  356. return current_func->stack_size + ((1 + s->idx) * 4);
  357. }
  358. static const char *pretty_offset(int ofs)
  359. {
  360. static char esp_buf[64];
  361. if (ofs)
  362. sprintf(esp_buf, "%d(%%esp)", ofs);
  363. else
  364. strcpy(esp_buf, "(%esp)");
  365. return esp_buf;
  366. }
  367. static void stor_sym_init(struct symbol *sym)
  368. {
  369. struct storage *stor;
  370. struct symbol_private *priv;
  371. priv = calloc(1, sizeof(*priv) + sizeof(*stor));
  372. if (!priv)
  373. die("OOM in stor_sym_init");
  374. stor = (struct storage *) (priv + 1);
  375. priv->addr = stor;
  376. stor->type = STOR_SYM;
  377. stor->sym = sym;
  378. }
  379. static const char *stor_op_name(struct storage *s)
  380. {
  381. static char name[32];
  382. switch (s->type) {
  383. case STOR_PSEUDO:
  384. strcpy(name, pretty_offset((int) pseudo_offset(s)));
  385. break;
  386. case STOR_ARG:
  387. strcpy(name, pretty_offset((int) arg_offset(s)));
  388. break;
  389. case STOR_SYM:
  390. strcpy(name, show_ident(s->sym->ident));
  391. break;
  392. case STOR_REG:
  393. strcpy(name, s->reg->name);
  394. break;
  395. case STOR_VALUE:
  396. sprintf(name, "$%lld", s->value);
  397. break;
  398. case STOR_LABEL:
  399. sprintf(name, "%s.L%d", s->flags & STOR_LABEL_VAL ? "$" : "",
  400. s->label);
  401. break;
  402. case STOR_LABELSYM:
  403. sprintf(name, "%s.LS%p", s->flags & STOR_LABEL_VAL ? "$" : "",
  404. s->labelsym);
  405. break;
  406. }
  407. return name;
  408. }
  409. static struct atom *new_atom(enum atom_type type)
  410. {
  411. struct atom *atom;
  412. atom = calloc(1, sizeof(*atom)); /* TODO: chunked alloc */
  413. if (!atom)
  414. die("nuclear OOM");
  415. atom->type = type;
  416. return atom;
  417. }
  418. static inline void push_cstring(struct function *f, struct string *str,
  419. int label)
  420. {
  421. struct atom *atom;
  422. atom = new_atom(ATOM_CSTR);
  423. atom->string = str;
  424. atom->label = label;
  425. add_ptr_list(&f->str_list, atom); /* note: _not_ atom_list */
  426. }
  427. static inline void push_atom(struct function *f, struct atom *atom)
  428. {
  429. add_ptr_list(&f->atom_list, atom);
  430. }
  431. static void push_text_atom(struct function *f, const char *text)
  432. {
  433. struct atom *atom = new_atom(ATOM_TEXT);
  434. atom->text = strdup(text);
  435. atom->text_len = strlen(text);
  436. push_atom(f, atom);
  437. }
  438. static struct storage *new_storage(enum storage_type type)
  439. {
  440. struct storage *stor;
  441. stor = calloc(1, sizeof(*stor));
  442. if (!stor)
  443. die("OOM in new_storage");
  444. stor->type = type;
  445. return stor;
  446. }
  447. static struct storage *stack_alloc(int n_bytes)
  448. {
  449. struct function *f = current_func;
  450. struct storage *stor;
  451. assert(f != NULL);
  452. stor = new_storage(STOR_PSEUDO);
  453. stor->type = STOR_PSEUDO;
  454. stor->pseudo = f->pseudo_nr;
  455. stor->offset = f->stack_size; /* FIXME: stack req. natural align */
  456. stor->size = n_bytes;
  457. f->stack_size += n_bytes;
  458. f->pseudo_nr++;
  459. add_ptr_list(&f->pseudo_list, stor);
  460. return stor;
  461. }
  462. static struct storage *new_labelsym(struct symbol *sym)
  463. {
  464. struct storage *stor;
  465. stor = new_storage(STOR_LABELSYM);
  466. if (stor) {
  467. stor->flags |= STOR_WANTS_FREE;
  468. stor->labelsym = sym;
  469. }
  470. return stor;
  471. }
  472. static struct storage *new_val(long long value)
  473. {
  474. struct storage *stor;
  475. stor = new_storage(STOR_VALUE);
  476. if (stor) {
  477. stor->flags |= STOR_WANTS_FREE;
  478. stor->value = value;
  479. }
  480. return stor;
  481. }
  482. static int new_label(void)
  483. {
  484. static int label = 0;
  485. return ++label;
  486. }
  487. static void textbuf_push(struct textbuf **buf_p, const char *text)
  488. {
  489. struct textbuf *tmp, *list = *buf_p;
  490. unsigned int text_len = strlen(text);
  491. unsigned int alloc_len = text_len + 1 + sizeof(*list);
  492. tmp = calloc(1, alloc_len);
  493. if (!tmp)
  494. die("OOM on textbuf alloc");
  495. tmp->text = ((void *) tmp) + sizeof(*tmp);
  496. memcpy(tmp->text, text, text_len + 1);
  497. tmp->len = text_len;
  498. /* add to end of list */
  499. if (!list) {
  500. list = tmp;
  501. tmp->prev = tmp;
  502. } else {
  503. tmp->prev = list->prev;
  504. tmp->prev->next = tmp;
  505. list->prev = tmp;
  506. }
  507. tmp->next = list;
  508. *buf_p = list;
  509. }
  510. static void textbuf_emit(struct textbuf **buf_p)
  511. {
  512. struct textbuf *tmp, *list = *buf_p;
  513. while (list) {
  514. tmp = list;
  515. if (tmp->next == tmp)
  516. list = NULL;
  517. else {
  518. tmp->prev->next = tmp->next;
  519. tmp->next->prev = tmp->prev;
  520. list = tmp->next;
  521. }
  522. fputs(tmp->text, stdout);
  523. free(tmp);
  524. }
  525. *buf_p = list;
  526. }
  527. static void insn(const char *insn, struct storage *op1, struct storage *op2,
  528. const char *comment_in)
  529. {
  530. struct function *f = current_func;
  531. struct atom *atom = new_atom(ATOM_INSN);
  532. assert(insn != NULL);
  533. strcpy(atom->insn, insn);
  534. if (comment_in && (*comment_in))
  535. strncpy(atom->comment, comment_in,
  536. sizeof(atom->comment) - 1);
  537. atom->op1 = op1;
  538. atom->op2 = op2;
  539. push_atom(f, atom);
  540. }
  541. static void emit_comment(const char *fmt, ...)
  542. {
  543. struct function *f = current_func;
  544. static char tmpbuf[100] = "\t# ";
  545. va_list args;
  546. int i;
  547. va_start(args, fmt);
  548. i = vsnprintf(tmpbuf+3, sizeof(tmpbuf)-4, fmt, args);
  549. va_end(args);
  550. tmpbuf[i+3] = '\n';
  551. tmpbuf[i+4] = '\0';
  552. push_text_atom(f, tmpbuf);
  553. }
  554. static void emit_label (int label, const char *comment)
  555. {
  556. struct function *f = current_func;
  557. char s[64];
  558. if (!comment)
  559. sprintf(s, ".L%d:\n", label);
  560. else
  561. sprintf(s, ".L%d:\t\t\t\t\t# %s\n", label, comment);
  562. push_text_atom(f, s);
  563. }
  564. static void emit_labelsym (struct symbol *sym, const char *comment)
  565. {
  566. struct function *f = current_func;
  567. char s[64];
  568. if (!comment)
  569. sprintf(s, ".LS%p:\n", sym);
  570. else
  571. sprintf(s, ".LS%p:\t\t\t\t# %s\n", sym, comment);
  572. push_text_atom(f, s);
  573. }
  574. void emit_unit_begin(const char *basename)
  575. {
  576. printf("\t.file\t\"%s\"\n", basename);
  577. }
  578. void emit_unit_end(void)
  579. {
  580. textbuf_emit(&unit_post_text);
  581. printf("\t.ident\t\"sparse silly x86 backend (version %s)\"\n", SPARSE_VERSION);
  582. }
  583. /* conditionally switch sections */
  584. static void emit_section(const char *s)
  585. {
  586. if (s == current_section)
  587. return;
  588. if (current_section && (!strcmp(s, current_section)))
  589. return;
  590. printf("\t%s\n", s);
  591. current_section = s;
  592. }
  593. static void emit_insn_atom(struct function *f, struct atom *atom)
  594. {
  595. char s[128];
  596. char comment[64];
  597. struct storage *op1 = atom->op1;
  598. struct storage *op2 = atom->op2;
  599. if (atom->comment[0])
  600. sprintf(comment, "\t\t# %s", atom->comment);
  601. else
  602. comment[0] = 0;
  603. if (atom->op2) {
  604. char tmp[16];
  605. strcpy(tmp, stor_op_name(op1));
  606. sprintf(s, "\t%s\t%s, %s%s\n",
  607. atom->insn, tmp, stor_op_name(op2), comment);
  608. } else if (atom->op1)
  609. sprintf(s, "\t%s\t%s%s%s\n",
  610. atom->insn, stor_op_name(op1),
  611. comment[0] ? "\t" : "", comment);
  612. else
  613. sprintf(s, "\t%s\t%s%s\n",
  614. atom->insn,
  615. comment[0] ? "\t\t" : "", comment);
  616. if (write(STDOUT_FILENO, s, strlen(s)) < 0)
  617. die("can't write to stdout");
  618. }
  619. static void emit_atom_list(struct function *f)
  620. {
  621. struct atom *atom;
  622. FOR_EACH_PTR(f->atom_list, atom) {
  623. switch (atom->type) {
  624. case ATOM_TEXT: {
  625. if (write(STDOUT_FILENO, atom->text, atom->text_len) < 0)
  626. die("can't write to stdout");
  627. break;
  628. }
  629. case ATOM_INSN:
  630. emit_insn_atom(f, atom);
  631. break;
  632. case ATOM_CSTR:
  633. assert(0);
  634. break;
  635. }
  636. } END_FOR_EACH_PTR(atom);
  637. }
  638. static void emit_string_list(struct function *f)
  639. {
  640. struct atom *atom;
  641. emit_section(".section\t.rodata");
  642. FOR_EACH_PTR(f->str_list, atom) {
  643. /* FIXME: escape " in string */
  644. printf(".L%d:\n", atom->label);
  645. printf("\t.string\t%s\n", show_string(atom->string));
  646. free(atom);
  647. } END_FOR_EACH_PTR(atom);
  648. }
  649. static void func_cleanup(struct function *f)
  650. {
  651. struct storage *stor;
  652. struct atom *atom;
  653. FOR_EACH_PTR(f->atom_list, atom) {
  654. if ((atom->type == ATOM_TEXT) && (atom->text))
  655. free(atom->text);
  656. if (atom->op1 && (atom->op1->flags & STOR_WANTS_FREE))
  657. free(atom->op1);
  658. if (atom->op2 && (atom->op2->flags & STOR_WANTS_FREE))
  659. free(atom->op2);
  660. free(atom);
  661. } END_FOR_EACH_PTR(atom);
  662. FOR_EACH_PTR(f->pseudo_list, stor) {
  663. free(stor);
  664. } END_FOR_EACH_PTR(stor);
  665. free_ptr_list(&f->pseudo_list);
  666. free(f);
  667. }
  668. /* function prologue */
  669. static void emit_func_pre(struct symbol *sym)
  670. {
  671. struct function *f;
  672. struct symbol *arg;
  673. unsigned int i, argc = 0, alloc_len;
  674. unsigned char *mem;
  675. struct symbol_private *privbase;
  676. struct storage *storage_base;
  677. struct symbol *base_type = sym->ctype.base_type;
  678. FOR_EACH_PTR(base_type->arguments, arg) {
  679. argc++;
  680. } END_FOR_EACH_PTR(arg);
  681. alloc_len =
  682. sizeof(*f) +
  683. (argc * sizeof(struct symbol *)) +
  684. (argc * sizeof(struct symbol_private)) +
  685. (argc * sizeof(struct storage));
  686. mem = calloc(1, alloc_len);
  687. if (!mem)
  688. die("OOM on func info");
  689. f = (struct function *) mem;
  690. mem += sizeof(*f);
  691. f->argv = (struct symbol **) mem;
  692. mem += (argc * sizeof(struct symbol *));
  693. privbase = (struct symbol_private *) mem;
  694. mem += (argc * sizeof(struct symbol_private));
  695. storage_base = (struct storage *) mem;
  696. f->argc = argc;
  697. f->ret_target = new_label();
  698. i = 0;
  699. FOR_EACH_PTR(base_type->arguments, arg) {
  700. f->argv[i] = arg;
  701. arg->aux = &privbase[i];
  702. storage_base[i].type = STOR_ARG;
  703. storage_base[i].idx = i;
  704. privbase[i].addr = &storage_base[i];
  705. i++;
  706. } END_FOR_EACH_PTR(arg);
  707. assert(current_func == NULL);
  708. current_func = f;
  709. }
  710. /* function epilogue */
  711. static void emit_func_post(struct symbol *sym)
  712. {
  713. const char *name = show_ident(sym->ident);
  714. struct function *f = current_func;
  715. int stack_size = f->stack_size;
  716. if (f->str_list)
  717. emit_string_list(f);
  718. /* function prologue */
  719. emit_section(".text");
  720. if ((sym->ctype.modifiers & MOD_STATIC) == 0)
  721. printf(".globl %s\n", name);
  722. printf("\t.type\t%s, @function\n", name);
  723. printf("%s:\n", name);
  724. if (stack_size) {
  725. char pseudo_const[16];
  726. sprintf(pseudo_const, "$%d", stack_size);
  727. printf("\tsubl\t%s, %%esp\n", pseudo_const);
  728. }
  729. /* function epilogue */
  730. /* jump target for 'return' statements */
  731. emit_label(f->ret_target, NULL);
  732. if (stack_size) {
  733. struct storage *val;
  734. val = new_storage(STOR_VALUE);
  735. val->value = (long long) (stack_size);
  736. val->flags = STOR_WANTS_FREE;
  737. insn("addl", val, REG_ESP, NULL);
  738. }
  739. insn("ret", NULL, NULL, NULL);
  740. /* output everything to stdout */
  741. fflush(stdout); /* paranoia; needed? */
  742. emit_atom_list(f);
  743. /* function footer */
  744. name = show_ident(sym->ident);
  745. printf("\t.size\t%s, .-%s\n", name, name);
  746. func_cleanup(f);
  747. current_func = NULL;
  748. }
  749. /* emit object (a.k.a. variable, a.k.a. data) prologue */
  750. static void emit_object_pre(const char *name, unsigned long modifiers,
  751. unsigned long alignment, unsigned int byte_size)
  752. {
  753. if ((modifiers & MOD_STATIC) == 0)
  754. printf(".globl %s\n", name);
  755. emit_section(".data");
  756. if (alignment)
  757. printf("\t.align %lu\n", alignment);
  758. printf("\t.type\t%s, @object\n", name);
  759. printf("\t.size\t%s, %d\n", name, byte_size);
  760. printf("%s:\n", name);
  761. }
  762. /* emit value (only) for an initializer scalar */
  763. static void emit_scalar(struct expression *expr, unsigned int bit_size)
  764. {
  765. const char *type;
  766. long long ll;
  767. assert(expr->type == EXPR_VALUE);
  768. if (expr->value == 0ULL) {
  769. printf("\t.zero\t%d\n", bit_size / 8);
  770. return;
  771. }
  772. ll = (long long) expr->value;
  773. switch (bit_size) {
  774. case 8: type = "byte"; ll = (char) ll; break;
  775. case 16: type = "value"; ll = (short) ll; break;
  776. case 32: type = "long"; ll = (int) ll; break;
  777. case 64: type = "quad"; break;
  778. default: type = NULL; break;
  779. }
  780. assert(type != NULL);
  781. printf("\t.%s\t%lld\n", type, ll);
  782. }
  783. static void emit_global_noinit(const char *name, unsigned long modifiers,
  784. unsigned long alignment, unsigned int byte_size)
  785. {
  786. char s[64];
  787. if (modifiers & MOD_STATIC) {
  788. sprintf(s, "\t.local\t%s\n", name);
  789. textbuf_push(&unit_post_text, s);
  790. }
  791. if (alignment)
  792. sprintf(s, "\t.comm\t%s,%d,%lu\n", name, byte_size, alignment);
  793. else
  794. sprintf(s, "\t.comm\t%s,%d\n", name, byte_size);
  795. textbuf_push(&unit_post_text, s);
  796. }
  797. static int ea_current, ea_last;
  798. static void emit_initializer(struct symbol *sym,
  799. struct expression *expr)
  800. {
  801. int distance = ea_current - ea_last - 1;
  802. if (distance > 0)
  803. printf("\t.zero\t%d\n", (sym->bit_size / 8) * distance);
  804. if (expr->type == EXPR_VALUE) {
  805. struct symbol *base_type = sym->ctype.base_type;
  806. assert(base_type != NULL);
  807. emit_scalar(expr, sym->bit_size / get_expression_value(base_type->array_size));
  808. return;
  809. }
  810. if (expr->type != EXPR_INITIALIZER)
  811. return;
  812. assert(0); /* FIXME */
  813. }
  814. static int sort_array_cmp(const struct expression *a,
  815. const struct expression *b)
  816. {
  817. int a_ofs = 0, b_ofs = 0;
  818. if (a->type == EXPR_POS)
  819. a_ofs = (int) a->init_offset;
  820. if (b->type == EXPR_POS)
  821. b_ofs = (int) b->init_offset;
  822. return a_ofs - b_ofs;
  823. }
  824. /* move to front-end? */
  825. static void sort_array(struct expression *expr)
  826. {
  827. struct expression *entry, **list;
  828. unsigned int elem, sorted, i;
  829. elem = expression_list_size(expr->expr_list);
  830. if (!elem)
  831. return;
  832. list = malloc(sizeof(entry) * elem);
  833. if (!list)
  834. die("OOM in sort_array");
  835. /* this code is no doubt evil and ignores EXPR_INDEX possibly
  836. * to its detriment and other nasty things. improvements
  837. * welcome.
  838. */
  839. i = 0;
  840. sorted = 0;
  841. FOR_EACH_PTR(expr->expr_list, entry) {
  842. if ((entry->type == EXPR_POS) || (entry->type == EXPR_VALUE)) {
  843. /* add entry to list[], in sorted order */
  844. if (sorted == 0) {
  845. list[0] = entry;
  846. sorted = 1;
  847. } else {
  848. for (i = 0; i < sorted; i++)
  849. if (sort_array_cmp(entry, list[i]) <= 0)
  850. break;
  851. /* If inserting into the middle of list[]
  852. * instead of appending, we memmove.
  853. * This is ugly, but thankfully
  854. * uncommon. Input data with tons of
  855. * entries very rarely have explicit
  856. * offsets. convert to qsort eventually...
  857. */
  858. if (i != sorted)
  859. memmove(&list[i + 1], &list[i],
  860. (sorted - i) * sizeof(entry));
  861. list[i] = entry;
  862. sorted++;
  863. }
  864. }
  865. } END_FOR_EACH_PTR(entry);
  866. i = 0;
  867. FOR_EACH_PTR(expr->expr_list, entry) {
  868. if ((entry->type == EXPR_POS) || (entry->type == EXPR_VALUE))
  869. *THIS_ADDRESS(entry) = list[i++];
  870. } END_FOR_EACH_PTR(entry);
  871. free(list);
  872. }
  873. static void emit_array(struct symbol *sym)
  874. {
  875. struct symbol *base_type = sym->ctype.base_type;
  876. struct expression *expr = sym->initializer;
  877. struct expression *entry;
  878. assert(base_type != NULL);
  879. stor_sym_init(sym);
  880. ea_last = -1;
  881. emit_object_pre(show_ident(sym->ident), sym->ctype.modifiers,
  882. sym->ctype.alignment,
  883. sym->bit_size / 8);
  884. sort_array(expr);
  885. FOR_EACH_PTR(expr->expr_list, entry) {
  886. if (entry->type == EXPR_VALUE) {
  887. ea_current = 0;
  888. emit_initializer(sym, entry);
  889. ea_last = ea_current;
  890. } else if (entry->type == EXPR_POS) {
  891. ea_current =
  892. entry->init_offset / (base_type->bit_size / 8);
  893. emit_initializer(sym, entry->init_expr);
  894. ea_last = ea_current;
  895. }
  896. } END_FOR_EACH_PTR(entry);
  897. }
  898. void emit_one_symbol(struct symbol *sym)
  899. {
  900. x86_symbol(sym);
  901. }
  902. static void emit_copy(struct storage *dest, struct storage *src,
  903. struct symbol *ctype)
  904. {
  905. struct storage *reg = NULL;
  906. unsigned int bit_size;
  907. /* FIXME: Bitfield copy! */
  908. bit_size = src->size * 8;
  909. if (!bit_size)
  910. bit_size = 32;
  911. if ((src->type == STOR_ARG) && (bit_size < 32))
  912. bit_size = 32;
  913. reg = temp_from_bits(bit_size);
  914. emit_move(src, reg, ctype, "begin copy ..");
  915. bit_size = dest->size * 8;
  916. if (!bit_size)
  917. bit_size = 32;
  918. if ((dest->type == STOR_ARG) && (bit_size < 32))
  919. bit_size = 32;
  920. emit_move(reg, dest, ctype, ".... end copy");
  921. put_reg(reg);
  922. }
  923. static void emit_store(struct expression *dest_expr, struct storage *dest,
  924. struct storage *src, int bits)
  925. {
  926. /* FIXME: Bitfield store! */
  927. printf("\tst.%d\t\tv%d,[v%d]\n", bits, src->pseudo, dest->pseudo);
  928. }
  929. static void emit_scalar_noinit(struct symbol *sym)
  930. {
  931. emit_global_noinit(show_ident(sym->ident),
  932. sym->ctype.modifiers, sym->ctype.alignment,
  933. sym->bit_size / 8);
  934. stor_sym_init(sym);
  935. }
  936. static void emit_array_noinit(struct symbol *sym)
  937. {
  938. emit_global_noinit(show_ident(sym->ident),
  939. sym->ctype.modifiers, sym->ctype.alignment,
  940. get_expression_value(sym->array_size) * (sym->bit_size / 8));
  941. stor_sym_init(sym);
  942. }
  943. static const char *opbits(const char *insn, unsigned int bits)
  944. {
  945. static char opbits_str[32];
  946. char c;
  947. switch (bits) {
  948. case 8: c = 'b'; break;
  949. case 16: c = 'w'; break;
  950. case 32: c = 'l'; break;
  951. case 64: c = 'q'; break;
  952. default: abort(); break;
  953. }
  954. sprintf(opbits_str, "%s%c", insn, c);
  955. return opbits_str;
  956. }
  957. static void emit_move(struct storage *src, struct storage *dest,
  958. struct symbol *ctype, const char *comment)
  959. {
  960. unsigned int bits;
  961. unsigned int is_signed;
  962. unsigned int is_dest = (src->type == STOR_REG);
  963. const char *opname;
  964. if (ctype) {
  965. bits = ctype->bit_size;
  966. is_signed = is_signed_type(ctype);
  967. } else {
  968. bits = 32;
  969. is_signed = 0;
  970. }
  971. /*
  972. * Are we moving from a register to a register?
  973. * Make the new reg to be the "cache".
  974. */
  975. if ((dest->type == STOR_REG) && (src->type == STOR_REG)) {
  976. struct storage *backing;
  977. reg_reg_move:
  978. if (dest == src)
  979. return;
  980. backing = src->reg->contains;
  981. if (backing) {
  982. /* Is it still valid? */
  983. if (backing->reg != src->reg)
  984. backing = NULL;
  985. else
  986. backing->reg = dest->reg;
  987. }
  988. dest->reg->contains = backing;
  989. insn("mov", src, dest, NULL);
  990. return;
  991. }
  992. /*
  993. * Are we moving to a register from a non-reg?
  994. *
  995. * See if we have the non-reg source already cached
  996. * in a register..
  997. */
  998. if (dest->type == STOR_REG) {
  999. if (src->reg) {
  1000. struct reg_info *info = src->reg;
  1001. if (info->contains == src) {
  1002. src = reginfo_reg(info);
  1003. goto reg_reg_move;
  1004. }
  1005. }
  1006. dest->reg->contains = src;
  1007. src->reg = dest->reg;
  1008. }
  1009. if (src->type == STOR_REG) {
  1010. /* We could just mark the register dirty here and do lazy store.. */
  1011. src->reg->contains = dest;
  1012. dest->reg = src->reg;
  1013. }
  1014. if ((bits == 8) || (bits == 16)) {
  1015. if (is_dest)
  1016. opname = "mov";
  1017. else
  1018. opname = is_signed ? "movsx" : "movzx";
  1019. } else
  1020. opname = "mov";
  1021. insn(opbits(opname, bits), src, dest, comment);
  1022. }
  1023. static struct storage *emit_compare(struct expression *expr)
  1024. {
  1025. struct storage *left = x86_expression(expr->left);
  1026. struct storage *right = x86_expression(expr->right);
  1027. struct storage *reg1, *reg2;
  1028. struct storage *new, *val;
  1029. const char *opname = NULL;
  1030. unsigned int right_bits = expr->right->ctype->bit_size;
  1031. switch(expr->op) {
  1032. case '<': opname = "setl"; break;
  1033. case '>': opname = "setg"; break;
  1034. case SPECIAL_LTE:
  1035. opname = "setle"; break;
  1036. case SPECIAL_GTE:
  1037. opname = "setge"; break;
  1038. case SPECIAL_EQUAL: opname = "sete"; break;
  1039. case SPECIAL_NOTEQUAL: opname = "setne"; break;
  1040. case SPECIAL_UNSIGNED_LT:
  1041. opname = "setb"; break;
  1042. case SPECIAL_UNSIGNED_GT:
  1043. opname = "seta"; break;
  1044. case SPECIAL_UNSIGNED_LTE:
  1045. opname = "setb"; break;
  1046. case SPECIAL_UNSIGNED_GTE:
  1047. opname = "setae"; break;
  1048. default:
  1049. assert(0);
  1050. break;
  1051. }
  1052. /* init EDX to 0 */
  1053. val = new_storage(STOR_VALUE);
  1054. val->flags = STOR_WANTS_FREE;
  1055. reg1 = get_reg(&regclass_32_8);
  1056. emit_move(val, reg1, NULL, NULL);
  1057. /* move op1 into EAX */
  1058. reg2 = get_reg_value(left, get_regclass(expr->left));
  1059. /* perform comparison, RHS (op1, right) and LHS (op2, EAX) */
  1060. insn(opbits("cmp", right_bits), right, reg2, NULL);
  1061. put_reg(reg2);
  1062. /* store result of operation, 0 or 1, in DL using SETcc */
  1063. insn(opname, byte_reg(reg1), NULL, NULL);
  1064. /* finally, store the result (DL) in a new pseudo / stack slot */
  1065. new = stack_alloc(4);
  1066. emit_move(reg1, new, NULL, "end EXPR_COMPARE");
  1067. put_reg(reg1);
  1068. return new;
  1069. }
  1070. static struct storage *emit_value(struct expression *expr)
  1071. {
  1072. #if 0 /* old and slow way */
  1073. struct storage *new = stack_alloc(4);
  1074. struct storage *val;
  1075. val = new_storage(STOR_VALUE);
  1076. val->value = (long long) expr->value;
  1077. val->flags = STOR_WANTS_FREE;
  1078. insn("movl", val, new, NULL);
  1079. return new;
  1080. #else
  1081. struct storage *val;
  1082. val = new_storage(STOR_VALUE);
  1083. val->value = (long long) expr->value;
  1084. return val; /* FIXME: memory leak */
  1085. #endif
  1086. }
  1087. static struct storage *emit_divide(struct expression *expr, struct storage *left, struct storage *right)
  1088. {
  1089. struct storage *eax_edx;
  1090. struct storage *reg, *new;
  1091. struct storage *val = new_storage(STOR_VALUE);
  1092. emit_comment("begin DIVIDE");
  1093. eax_edx = get_hardreg(hardreg_storage_table + EAX_EDX, 1);
  1094. /* init EDX to 0 */
  1095. val->flags = STOR_WANTS_FREE;
  1096. emit_move(val, REG_EDX, NULL, NULL);
  1097. new = stack_alloc(expr->ctype->bit_size / 8);
  1098. /* EAX is dividend */
  1099. emit_move(left, REG_EAX, NULL, NULL);
  1100. reg = get_reg_value(right, &regclass_32);
  1101. /* perform binop */
  1102. insn("div", reg, REG_EAX, NULL);
  1103. put_reg(reg);
  1104. reg = REG_EAX;
  1105. if (expr->op == '%')
  1106. reg = REG_EDX;
  1107. emit_move(reg, new, NULL, NULL);
  1108. put_reg(eax_edx);
  1109. emit_comment("end DIVIDE");
  1110. return new;
  1111. }
  1112. static struct storage *emit_binop(struct expression *expr)
  1113. {
  1114. struct storage *left = x86_expression(expr->left);
  1115. struct storage *right = x86_expression(expr->right);
  1116. struct storage *new;
  1117. struct storage *dest, *src;
  1118. const char *opname = NULL;
  1119. const char *suffix = NULL;
  1120. char opstr[16];
  1121. int is_signed;
  1122. /* Divides have special register constraints */
  1123. if ((expr->op == '/') || (expr->op == '%'))
  1124. return emit_divide(expr, left, right);
  1125. is_signed = is_signed_type(expr->ctype);
  1126. switch (expr->op) {
  1127. case '+':
  1128. opname = "add";
  1129. break;
  1130. case '-':
  1131. opname = "sub";
  1132. break;
  1133. case '&':
  1134. opname = "and";
  1135. break;
  1136. case '|':
  1137. opname = "or";
  1138. break;
  1139. case '^':
  1140. opname = "xor";
  1141. break;
  1142. case SPECIAL_LEFTSHIFT:
  1143. opname = "shl";
  1144. break;
  1145. case SPECIAL_RIGHTSHIFT:
  1146. if (is_signed)
  1147. opname = "sar";
  1148. else
  1149. opname = "shr";
  1150. break;
  1151. case '*':
  1152. if (is_signed)
  1153. opname = "imul";
  1154. else
  1155. opname = "mul";
  1156. break;
  1157. case SPECIAL_LOGICAL_AND:
  1158. warning(expr->pos, "bogus bitwise and for logical op (should use '2*setne + and' or something)");
  1159. opname = "and";
  1160. break;
  1161. case SPECIAL_LOGICAL_OR:
  1162. warning(expr->pos, "bogus bitwise or for logical op (should use 'or + setne' or something)");
  1163. opname = "or";
  1164. break;
  1165. default:
  1166. error_die(expr->pos, "unhandled binop '%s'\n", show_special(expr->op));
  1167. break;
  1168. }
  1169. dest = get_reg_value(right, &regclass_32);
  1170. src = get_reg_value(left, &regclass_32);
  1171. switch (expr->ctype->bit_size) {
  1172. case 8:
  1173. suffix = "b";
  1174. break;
  1175. case 16:
  1176. suffix = "w";
  1177. break;
  1178. case 32:
  1179. suffix = "l";
  1180. break;
  1181. case 64:
  1182. suffix = "q"; /* FIXME */
  1183. break;
  1184. default:
  1185. assert(0);
  1186. break;
  1187. }
  1188. snprintf(opstr, sizeof(opstr), "%s%s", opname, suffix);
  1189. /* perform binop */
  1190. insn(opstr, src, dest, NULL);
  1191. put_reg(src);
  1192. /* store result in new pseudo / stack slot */
  1193. new = stack_alloc(expr->ctype->bit_size / 8);
  1194. emit_move(dest, new, NULL, "end EXPR_BINOP");
  1195. put_reg(dest);
  1196. return new;
  1197. }
  1198. static int emit_conditional_test(struct storage *val)
  1199. {
  1200. struct storage *reg;
  1201. struct storage *target_val;
  1202. int target_false;
  1203. /* load result into EAX */
  1204. emit_comment("begin if/conditional");
  1205. reg = get_reg_value(val, &regclass_32);
  1206. /* compare result with zero */
  1207. insn("test", reg, reg, NULL);
  1208. put_reg(reg);
  1209. /* create conditional-failed label to jump to */
  1210. target_false = new_label();
  1211. target_val = new_storage(STOR_LABEL);
  1212. target_val->label = target_false;
  1213. target_val->flags = STOR_WANTS_FREE;
  1214. insn("jz", target_val, NULL, NULL);
  1215. return target_false;
  1216. }
  1217. static int emit_conditional_end(int target_false)
  1218. {
  1219. struct storage *cond_end_st;
  1220. int cond_end;
  1221. /* finished generating code for if-true statement.
  1222. * add a jump-to-end jump to avoid falling through
  1223. * to the if-false statement code.
  1224. */
  1225. cond_end = new_label();
  1226. cond_end_st = new_storage(STOR_LABEL);
  1227. cond_end_st->label = cond_end;
  1228. cond_end_st->flags = STOR_WANTS_FREE;
  1229. insn("jmp", cond_end_st, NULL, NULL);
  1230. /* if we have both if-true and if-false statements,
  1231. * the failed-conditional case will fall through to here
  1232. */
  1233. emit_label(target_false, NULL);
  1234. return cond_end;
  1235. }
  1236. static void emit_if_conditional(struct statement *stmt)
  1237. {
  1238. struct storage *val;
  1239. int cond_end;
  1240. /* emit test portion of conditional */
  1241. val = x86_expression(stmt->if_conditional);
  1242. cond_end = emit_conditional_test(val);
  1243. /* emit if-true statement */
  1244. x86_statement(stmt->if_true);
  1245. /* emit if-false statement, if present */
  1246. if (stmt->if_false) {
  1247. cond_end = emit_conditional_end(cond_end);
  1248. x86_statement(stmt->if_false);
  1249. }
  1250. /* end of conditional; jump target for if-true branch */
  1251. emit_label(cond_end, "end if");
  1252. }
  1253. static struct storage *emit_inc_dec(struct expression *expr, int postop)
  1254. {
  1255. struct storage *addr = x86_address_gen(expr->unop);
  1256. struct storage *retval;
  1257. char opname[16];
  1258. strcpy(opname, opbits(expr->op == SPECIAL_INCREMENT ? "inc" : "dec",
  1259. expr->ctype->bit_size));
  1260. if (postop) {
  1261. struct storage *new = stack_alloc(4);
  1262. emit_copy(new, addr, expr->unop->ctype);
  1263. retval = new;
  1264. } else
  1265. retval = addr;
  1266. insn(opname, addr, NULL, NULL);
  1267. return retval;
  1268. }
  1269. static struct storage *emit_postop(struct expression *expr)
  1270. {
  1271. return emit_inc_dec(expr, 1);
  1272. }
  1273. static struct storage *emit_return_stmt(struct statement *stmt)
  1274. {
  1275. struct function *f = current_func;
  1276. struct expression *expr = stmt->ret_value;
  1277. struct storage *val = NULL, *jmplbl;
  1278. if (expr && expr->ctype) {
  1279. val = x86_expression(expr);
  1280. assert(val != NULL);
  1281. emit_move(val, REG_EAX, expr->ctype, "return");
  1282. }
  1283. jmplbl = new_storage(STOR_LABEL);
  1284. jmplbl->flags |= STOR_WANTS_FREE;
  1285. jmplbl->label = f->ret_target;
  1286. insn("jmp", jmplbl, NULL, NULL);
  1287. return val;
  1288. }
  1289. static struct storage *emit_conditional_expr(struct expression *expr)
  1290. {
  1291. struct storage *cond, *stot = NULL, *stof = NULL;
  1292. struct storage *new = stack_alloc(expr->ctype->bit_size / 8);
  1293. int target_false, cond_end;
  1294. /* evaluate conditional */
  1295. cond = x86_expression(expr->conditional);
  1296. target_false = emit_conditional_test(cond);
  1297. /* handle if-true part of the expression */
  1298. stot = x86_expression(expr->cond_true);
  1299. emit_copy(new, stot, expr->ctype);
  1300. cond_end = emit_conditional_end(target_false);
  1301. /* handle if-false part of the expression */
  1302. stof = x86_expression(expr->cond_false);
  1303. emit_copy(new, stof, expr->ctype);
  1304. /* end of conditional; jump target for if-true branch */
  1305. emit_label(cond_end, "end conditional");
  1306. return new;
  1307. }
  1308. static struct storage *emit_select_expr(struct expression *expr)
  1309. {
  1310. struct storage *cond = x86_expression(expr->conditional);
  1311. struct storage *stot = x86_expression(expr->cond_true);
  1312. struct storage *stof = x86_expression(expr->cond_false);
  1313. struct storage *reg_cond, *reg_true, *reg_false;
  1314. struct storage *new = stack_alloc(4);
  1315. emit_comment("begin SELECT");
  1316. reg_cond = get_reg_value(cond, get_regclass(expr->conditional));
  1317. reg_true = get_reg_value(stot, get_regclass(expr));
  1318. reg_false = get_reg_value(stof, get_regclass(expr));
  1319. /*
  1320. * Do the actual select: check the conditional for zero,
  1321. * move false over true if zero
  1322. */
  1323. insn("test", reg_cond, reg_cond, NULL);
  1324. insn("cmovz", reg_false, reg_true, NULL);
  1325. /* Store it back */
  1326. emit_move(reg_true, new, expr->ctype, NULL);
  1327. put_reg(reg_cond);
  1328. put_reg(reg_true);
  1329. put_reg(reg_false);
  1330. emit_comment("end SELECT");
  1331. return new;
  1332. }
  1333. static struct storage *emit_symbol_expr_init(struct symbol *sym)
  1334. {
  1335. struct expression *expr = sym->initializer;
  1336. struct symbol_private *priv = sym->aux;
  1337. if (priv == NULL) {
  1338. priv = calloc(1, sizeof(*priv));
  1339. sym->aux = priv;
  1340. if (expr == NULL) {
  1341. struct storage *new = stack_alloc(4);
  1342. fprintf(stderr, "FIXME! no value for symbol %s. creating pseudo %d (stack offset %d)\n",
  1343. show_ident(sym->ident),
  1344. new->pseudo, new->pseudo * 4);
  1345. priv->addr = new;
  1346. } else {
  1347. priv->addr = x86_expression(expr);
  1348. }
  1349. }
  1350. return priv->addr;
  1351. }
  1352. static struct storage *emit_string_expr(struct expression *expr)
  1353. {
  1354. struct function *f = current_func;
  1355. int label = new_label();
  1356. struct storage *new;
  1357. push_cstring(f, expr->string, label);
  1358. new = new_storage(STOR_LABEL);
  1359. new->label = label;
  1360. new->flags = STOR_LABEL_VAL | STOR_WANTS_FREE;
  1361. return new;
  1362. }
  1363. static struct storage *emit_cast_expr(struct expression *expr)
  1364. {
  1365. struct symbol *old_type, *new_type;
  1366. struct storage *op = x86_expression(expr->cast_expression);
  1367. int oldbits, newbits;
  1368. struct storage *new;
  1369. old_type = expr->cast_expression->ctype;
  1370. new_type = expr->cast_type;
  1371. oldbits = old_type->bit_size;
  1372. newbits = new_type->bit_size;
  1373. if (oldbits >= newbits)
  1374. return op;
  1375. emit_move(op, REG_EAX, old_type, "begin cast ..");
  1376. new = stack_alloc(newbits / 8);
  1377. emit_move(REG_EAX, new, new_type, ".... end cast");
  1378. return new;
  1379. }
  1380. static struct storage *emit_regular_preop(struct expression *expr)
  1381. {
  1382. struct storage *target = x86_expression(expr->unop);
  1383. struct storage *val, *new = stack_alloc(4);
  1384. const char *opname = NULL;
  1385. switch (expr->op) {
  1386. case '!':
  1387. val = new_storage(STOR_VALUE);
  1388. val->flags = STOR_WANTS_FREE;
  1389. emit_move(val, REG_EDX, NULL, NULL);
  1390. emit_move(target, REG_EAX, expr->unop->ctype, NULL);
  1391. insn("test", REG_EAX, REG_EAX, NULL);
  1392. insn("setz", REG_DL, NULL, NULL);
  1393. emit_move(REG_EDX, new, expr->unop->ctype, NULL);
  1394. break;
  1395. case '~':
  1396. opname = "not";
  1397. case '-':
  1398. if (!opname)
  1399. opname = "neg";
  1400. emit_move(target, REG_EAX, expr->unop->ctype, NULL);
  1401. insn(opname, REG_EAX, NULL, NULL);
  1402. emit_move(REG_EAX, new, expr->unop->ctype, NULL);
  1403. break;
  1404. default:
  1405. assert(0);
  1406. break;
  1407. }
  1408. return new;
  1409. }
  1410. static void emit_case_statement(struct statement *stmt)
  1411. {
  1412. emit_labelsym(stmt->case_label, NULL);
  1413. x86_statement(stmt->case_statement);
  1414. }
  1415. static void emit_switch_statement(struct statement *stmt)
  1416. {
  1417. struct storage *val = x86_expression(stmt->switch_expression);
  1418. struct symbol *sym, *default_sym = NULL;
  1419. struct storage *labelsym, *label;
  1420. int switch_end = 0;
  1421. emit_move(val, REG_EAX, stmt->switch_expression->ctype, "begin case");
  1422. /*
  1423. * This is where a _real_ back-end would go through the
  1424. * cases to decide whether to use a lookup table or a
  1425. * series of comparisons etc
  1426. */
  1427. FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
  1428. struct statement *case_stmt = sym->stmt;
  1429. struct expression *expr = case_stmt->case_expression;
  1430. struct expression *to = case_stmt->case_to;
  1431. /* default: */
  1432. if (!expr)
  1433. default_sym = sym;
  1434. /* case NNN: */
  1435. else {
  1436. struct storage *case_val = new_val(expr->value);
  1437. assert (expr->type == EXPR_VALUE);
  1438. insn("cmpl", case_val, REG_EAX, NULL);
  1439. if (!to) {
  1440. labelsym = new_labelsym(sym);
  1441. insn("je", labelsym, NULL, NULL);
  1442. } else {
  1443. int next_test;
  1444. label = new_storage(STOR_LABEL);
  1445. label->flags |= STOR_WANTS_FREE;
  1446. label->label = next_test = new_label();
  1447. /* FIXME: signed/unsigned */
  1448. insn("jl", label, NULL, NULL);
  1449. case_val = new_val(to->value);
  1450. insn("cmpl", case_val, REG_EAX, NULL);
  1451. /* TODO: implement and use refcounting... */
  1452. label = new_storage(STOR_LABEL);
  1453. label->flags |= STOR_WANTS_FREE;
  1454. label->label = next_test;
  1455. /* FIXME: signed/unsigned */
  1456. insn("jg", label, NULL, NULL);
  1457. labelsym = new_labelsym(sym);
  1458. insn("jmp", labelsym, NULL, NULL);
  1459. emit_label(next_test, NULL);
  1460. }
  1461. }
  1462. } END_FOR_EACH_PTR(sym);
  1463. if (default_sym) {
  1464. labelsym = new_labelsym(default_sym);
  1465. insn("jmp", labelsym, NULL, "default");
  1466. } else {
  1467. label = new_storage(STOR_LABEL);
  1468. label->flags |= STOR_WANTS_FREE;
  1469. label->label = switch_end = new_label();
  1470. insn("jmp", label, NULL, "goto end of switch");
  1471. }
  1472. x86_statement(stmt->switch_statement);
  1473. if (stmt->switch_break->used)
  1474. emit_labelsym(stmt->switch_break, NULL);
  1475. if (switch_end)
  1476. emit_label(switch_end, NULL);
  1477. }
  1478. static void x86_struct_member(struct symbol *sym)
  1479. {
  1480. printf("\t%s:%d:%ld at offset %ld.%d", show_ident(sym->ident), sym->bit_size, sym->ctype.alignment, sym->offset, sym->bit_offset);
  1481. printf("\n");
  1482. }
  1483. static void x86_symbol(struct symbol *sym)
  1484. {
  1485. struct symbol *type;
  1486. if (!sym)
  1487. return;
  1488. type = sym->ctype.base_type;
  1489. if (!type)
  1490. return;
  1491. /*
  1492. * Show actual implementation information
  1493. */
  1494. switch (type->type) {
  1495. case SYM_ARRAY:
  1496. if (sym->initializer)
  1497. emit_array(sym);
  1498. else
  1499. emit_array_noinit(sym);
  1500. break;
  1501. case SYM_BASETYPE:
  1502. if (sym->initializer) {
  1503. emit_object_pre(show_ident(sym->ident),
  1504. sym->ctype.modifiers,
  1505. sym->ctype.alignment,
  1506. sym->bit_size / 8);
  1507. emit_scalar(sym->initializer, sym->bit_size);
  1508. stor_sym_init(sym);
  1509. } else
  1510. emit_scalar_noinit(sym);
  1511. break;
  1512. case SYM_STRUCT:
  1513. case SYM_UNION: {
  1514. struct symbol *member;
  1515. printf(" {\n");
  1516. FOR_EACH_PTR(type->symbol_list, member) {
  1517. x86_struct_member(member);
  1518. } END_FOR_EACH_PTR(member);
  1519. printf("}\n");
  1520. break;
  1521. }
  1522. case SYM_FN: {
  1523. struct statement *stmt = type->stmt;
  1524. if (stmt) {
  1525. emit_func_pre(sym);
  1526. x86_statement(stmt);
  1527. emit_func_post(sym);
  1528. }
  1529. break;
  1530. }
  1531. default:
  1532. break;
  1533. }
  1534. if (sym->initializer && (type->type != SYM_BASETYPE) &&
  1535. (type->type != SYM_ARRAY)) {
  1536. printf(" = \n");
  1537. x86_expression(sym->initializer);
  1538. }
  1539. }
  1540. static void x86_symbol_init(struct symbol *sym);
  1541. static void x86_symbol_decl(struct symbol_list *syms)
  1542. {
  1543. struct symbol *sym;
  1544. FOR_EACH_PTR(syms, sym) {
  1545. x86_symbol_init(sym);
  1546. } END_FOR_EACH_PTR(sym);
  1547. }
  1548. static void loopstk_push(int cont_lbl, int loop_bottom_lbl)
  1549. {
  1550. struct function *f = current_func;
  1551. struct loop_stack *ls;
  1552. ls = malloc(sizeof(*ls));
  1553. ls->continue_lbl = cont_lbl;
  1554. ls->loop_bottom_lbl = loop_bottom_lbl;
  1555. ls->next = f->loop_stack;
  1556. f->loop_stack = ls;
  1557. }
  1558. static void loopstk_pop(void)
  1559. {
  1560. struct function *f = current_func;
  1561. struct loop_stack *ls;
  1562. assert(f->loop_stack != NULL);
  1563. ls = f->loop_stack;
  1564. f->loop_stack = f->loop_stack->next;
  1565. free(ls);
  1566. }
  1567. static int loopstk_break(void)
  1568. {
  1569. return current_func->loop_stack->loop_bottom_lbl;
  1570. }
  1571. static int loopstk_continue(void)
  1572. {
  1573. return current_func->loop_stack->continue_lbl;
  1574. }
  1575. static void emit_loop(struct statement *stmt)
  1576. {
  1577. struct statement *pre_statement = stmt->iterator_pre_statement;
  1578. struct expression *pre_condition = stmt->iterator_pre_condition;
  1579. struct statement *statement = stmt->iterator_statement;
  1580. struct statement *post_statement = stmt->iterator_post_statement;
  1581. struct expression *post_condition = stmt->iterator_post_condition;
  1582. int loop_top = 0, loop_bottom, loop_continue;
  1583. int have_bottom = 0;
  1584. struct storage *val;
  1585. loop_bottom = new_label();
  1586. loop_continue = new_label();
  1587. loopstk_push(loop_continue, loop_bottom);
  1588. x86_symbol_decl(stmt->iterator_syms);
  1589. x86_statement(pre_statement);
  1590. if (!post_condition || post_condition->type != EXPR_VALUE || post_condition->value) {
  1591. loop_top = new_label();
  1592. emit_label(loop_top, "loop top");
  1593. }
  1594. if (pre_condition) {
  1595. if (pre_condition->type == EXPR_VALUE) {
  1596. if (!pre_condition->value) {
  1597. struct storage *lbv;
  1598. lbv = new_storage(STOR_LABEL);
  1599. lbv->label = loop_bottom;
  1600. lbv->flags = STOR_WANTS_FREE;
  1601. insn("jmp", lbv, NULL, "go to loop bottom");
  1602. have_bottom = 1;
  1603. }
  1604. } else {
  1605. struct storage *lbv = new_storage(STOR_LABEL);
  1606. lbv->label = loop_bottom;
  1607. lbv->flags = STOR_WANTS_FREE;
  1608. have_bottom = 1;
  1609. val = x86_expression(pre_condition);
  1610. emit_move(val, REG_EAX, NULL, "loop pre condition");
  1611. insn("test", REG_EAX, REG_EAX, NULL);
  1612. insn("jz", lbv, NULL, NULL);
  1613. }
  1614. }
  1615. x86_statement(statement);
  1616. if (stmt->iterator_continue->used)
  1617. emit_label(loop_continue, "'continue' iterator");
  1618. x86_statement(post_statement);
  1619. if (!post_condition) {
  1620. struct storage *lbv = new_storage(STOR_LABEL);
  1621. lbv->label = loop_top;
  1622. lbv->flags = STOR_WANTS_FREE;
  1623. insn("jmp", lbv, NULL, "go to loop top");
  1624. } else if (post_condition->type == EXPR_VALUE) {
  1625. if (post_condition->value) {
  1626. struct storage *lbv = new_storage(STOR_LABEL);
  1627. lbv->label = loop_top;
  1628. lbv->flags = STOR_WANTS_FREE;
  1629. insn("jmp", lbv, NULL, "go to loop top");
  1630. }
  1631. } else {
  1632. struct storage *lbv = new_storage(STOR_LABEL);
  1633. lbv->label = loop_top;
  1634. lbv->flags = STOR_WANTS_FREE;
  1635. val = x86_expression(post_condition);
  1636. emit_move(val, REG_EAX, NULL, "loop post condition");
  1637. insn("test", REG_EAX, REG_EAX, NULL);
  1638. insn("jnz", lbv, NULL, NULL);
  1639. }
  1640. if (have_bottom || stmt->iterator_break->used)
  1641. emit_label(loop_bottom, "loop bottom");
  1642. loopstk_pop();
  1643. }
  1644. /*
  1645. * Print out a statement
  1646. */
  1647. static struct storage *x86_statement(struct statement *stmt)
  1648. {
  1649. if (!stmt)
  1650. return NULL;
  1651. switch (stmt->type) {
  1652. default:
  1653. return NULL;
  1654. case STMT_RETURN:
  1655. return emit_return_stmt(stmt);
  1656. case STMT_DECLARATION:
  1657. x86_symbol_decl(stmt->declaration);
  1658. break;
  1659. case STMT_COMPOUND: {
  1660. struct statement *s;
  1661. struct storage *last = NULL;
  1662. FOR_EACH_PTR(stmt->stmts, s) {
  1663. last = x86_statement(s);
  1664. } END_FOR_EACH_PTR(s);
  1665. return last;
  1666. }
  1667. case STMT_EXPRESSION:
  1668. return x86_expression(stmt->expression);
  1669. case STMT_IF:
  1670. emit_if_conditional(stmt);
  1671. return NULL;
  1672. case STMT_CASE:
  1673. emit_case_statement(stmt);
  1674. break;
  1675. case STMT_SWITCH:
  1676. emit_switch_statement(stmt);
  1677. break;
  1678. case STMT_ITERATOR:
  1679. emit_loop(stmt);
  1680. break;
  1681. case STMT_NONE:
  1682. break;
  1683. case STMT_LABEL:
  1684. printf(".L%p:\n", stmt->label_identifier);
  1685. x86_statement(stmt->label_statement);
  1686. break;
  1687. case STMT_GOTO:
  1688. if (stmt->goto_expression) {
  1689. struct storage *val = x86_expression(stmt->goto_expression);
  1690. printf("\tgoto *v%d\n", val->pseudo);
  1691. } else if (!strcmp("break", show_ident(stmt->goto_label->ident))) {
  1692. struct storage *lbv = new_storage(STOR_LABEL);
  1693. lbv->label = loopstk_break();
  1694. lbv->flags = STOR_WANTS_FREE;
  1695. insn("jmp", lbv, NULL, "'break'; go to loop bottom");
  1696. } else if (!strcmp("continue", show_ident(stmt->goto_label->ident))) {
  1697. struct storage *lbv = new_storage(STOR_LABEL);
  1698. lbv->label = loopstk_continue();
  1699. lbv->flags = STOR_WANTS_FREE;
  1700. insn("jmp", lbv, NULL, "'continue'; go to loop top");
  1701. } else {
  1702. struct storage *labelsym = new_labelsym(stmt->goto_label);
  1703. insn("jmp", labelsym, NULL, NULL);
  1704. }
  1705. break;
  1706. case STMT_ASM:
  1707. printf("\tasm( .... )\n");
  1708. break;
  1709. }
  1710. return NULL;
  1711. }
  1712. static struct storage *x86_call_expression(struct expression *expr)
  1713. {
  1714. struct function *f = current_func;
  1715. struct symbol *direct;
  1716. struct expression *arg, *fn;
  1717. struct storage *retval, *fncall;
  1718. int framesize;
  1719. char s[64];
  1720. if (!expr->ctype) {
  1721. warning(expr->pos, "\tcall with no type!");
  1722. return NULL;
  1723. }
  1724. framesize = 0;
  1725. FOR_EACH_PTR_REVERSE(expr->args, arg) {
  1726. struct storage *new = x86_expression(arg);
  1727. int size = arg->ctype->bit_size;
  1728. /*
  1729. * FIXME: i386 SysV ABI dictates that values
  1730. * smaller than 32 bits should be placed onto
  1731. * the stack as 32-bit objects. We should not
  1732. * blindly do a 32-bit push on objects smaller
  1733. * than 32 bits.
  1734. */
  1735. if (size < 32)
  1736. size = 32;
  1737. insn("pushl", new, NULL,
  1738. !framesize ? "begin function call" : NULL);
  1739. framesize += bits_to_bytes(size);
  1740. } END_FOR_EACH_PTR_REVERSE(arg);
  1741. fn = expr->fn;
  1742. /* Remove dereference, if any */
  1743. direct = NULL;
  1744. if (fn->type == EXPR_PREOP) {
  1745. if (fn->unop->type == EXPR_SYMBOL) {
  1746. struct symbol *sym = fn->unop->symbol;
  1747. if (sym->ctype.base_type->type == SYM_FN)
  1748. direct = sym;
  1749. }
  1750. }
  1751. if (direct) {
  1752. struct storage *direct_stor = new_storage(STOR_SYM);
  1753. direct_stor->flags |= STOR_WANTS_FREE;
  1754. direct_stor->sym = direct;
  1755. insn("call", direct_stor, NULL, NULL);
  1756. } else {
  1757. fncall = x86_expression(fn);
  1758. emit_move(fncall, REG_EAX, fn->ctype, NULL);
  1759. strcpy(s, "\tcall\t*%eax\n");
  1760. push_text_atom(f, s);
  1761. }
  1762. /* FIXME: pay attention to BITS_IN_POINTER */
  1763. if (framesize) {
  1764. struct storage *val = new_storage(STOR_VALUE);
  1765. val->value = (long long) framesize;
  1766. val->flags = STOR_WANTS_FREE;
  1767. insn("addl", val, REG_ESP, NULL);
  1768. }
  1769. retval = stack_alloc(4);
  1770. emit_move(REG_EAX, retval, NULL, "end function call");
  1771. return retval;
  1772. }
  1773. static struct storage *x86_address_gen(struct expression *expr)
  1774. {
  1775. struct function *f = current_func;
  1776. struct storage *addr;
  1777. struct storage *new;
  1778. char s[32];
  1779. addr = x86_expression(expr->unop);
  1780. if (expr->unop->type == EXPR_SYMBOL)
  1781. return addr;
  1782. emit_move(addr, REG_EAX, NULL, "begin deref ..");
  1783. /* FIXME: operand size */
  1784. strcpy(s, "\tmovl\t(%eax), %ecx\n");
  1785. push_text_atom(f, s);
  1786. new = stack_alloc(4);
  1787. emit_move(REG_ECX, new, NULL, ".... end deref");
  1788. return new;
  1789. }
  1790. static struct storage *x86_assignment(struct expression *expr)
  1791. {
  1792. struct expression *target = expr->left;
  1793. struct storage *val, *addr;
  1794. if (!expr->ctype)
  1795. return NULL;
  1796. val = x86_expression(expr->right);
  1797. addr = x86_address_gen(target);
  1798. switch (val->type) {
  1799. /* copy, where both operands are memory */
  1800. case STOR_PSEUDO:
  1801. case STOR_ARG:
  1802. emit_copy(addr, val, expr->ctype);
  1803. break;
  1804. /* copy, one or zero operands are memory */
  1805. case STOR_REG:
  1806. case STOR_SYM:
  1807. case STOR_VALUE:
  1808. case STOR_LABEL:
  1809. emit_move(val, addr, expr->left->ctype, NULL);
  1810. break;
  1811. case STOR_LABELSYM:
  1812. assert(0);
  1813. break;
  1814. }
  1815. return val;
  1816. }
  1817. static int x86_initialization(struct symbol *sym, struct expression *expr)
  1818. {
  1819. struct storage *val, *addr;
  1820. int bits;
  1821. if (!expr->ctype)
  1822. return 0;
  1823. bits = expr->ctype->bit_size;
  1824. val = x86_expression(expr);
  1825. addr = x86_symbol_expr(sym);
  1826. // FIXME! The "target" expression is for bitfield store information.
  1827. // Leave it NULL, which works fine.
  1828. emit_store(NULL, addr, val, bits);
  1829. return 0;
  1830. }
  1831. static struct storage *x86_access(struct expression *expr)
  1832. {
  1833. return x86_address_gen(expr);
  1834. }
  1835. static struct storage *x86_preop(struct expression *expr)
  1836. {
  1837. /*
  1838. * '*' is an lvalue access, and is fundamentally different
  1839. * from an arithmetic operation. Maybe it should have an
  1840. * expression type of its own..
  1841. */
  1842. if (expr->op == '*')
  1843. return x86_access(expr);
  1844. if (expr->op == SPECIAL_INCREMENT || expr->op == SPECIAL_DECREMENT)
  1845. return emit_inc_dec(expr, 0);
  1846. return emit_regular_preop(expr);
  1847. }
  1848. static struct storage *x86_symbol_expr(struct symbol *sym)
  1849. {
  1850. struct storage *new = stack_alloc(4);
  1851. if (sym->ctype.modifiers & (MOD_TOPLEVEL | MOD_EXTERN | MOD_STATIC)) {
  1852. printf("\tmovi.%d\t\tv%d,$%s\n", bits_in_pointer, new->pseudo, show_ident(sym->ident));
  1853. return new;
  1854. }
  1855. if (sym->ctype.modifiers & MOD_ADDRESSABLE) {
  1856. printf("\taddi.%d\t\tv%d,vFP,$%lld\n", bits_in_pointer, new->pseudo, 0LL);
  1857. return new;
  1858. }
  1859. printf("\taddi.%d\t\tv%d,vFP,$offsetof(%s:%p)\n", bits_in_pointer, new->pseudo, show_ident(sym->ident), sym);
  1860. return new;
  1861. }
  1862. static void x86_symbol_init(struct symbol *sym)
  1863. {
  1864. struct symbol_private *priv = sym->aux;
  1865. struct expression *expr = sym->initializer;
  1866. struct storage *new;
  1867. if (expr)
  1868. new = x86_expression(expr);
  1869. else
  1870. new = stack_alloc(sym->bit_size / 8);
  1871. if (!priv) {
  1872. priv = calloc(1, sizeof(*priv));
  1873. sym->aux = priv;
  1874. /* FIXME: leak! we don't free... */
  1875. /* (well, we don't free symbols either) */
  1876. }
  1877. priv->addr = new;
  1878. }
  1879. static struct storage *x86_label_expr(struct expression *expr)
  1880. {
  1881. struct storage *new = stack_alloc(4);
  1882. printf("\tmovi.%d\t\tv%d,.L%p\n", bits_in_pointer, new->pseudo, expr->label_symbol);
  1883. return new;
  1884. }
  1885. static struct storage *x86_statement_expr(struct expression *expr)
  1886. {
  1887. return x86_statement(expr->statement);
  1888. }
  1889. static int x86_position_expr(struct expression *expr, struct symbol *base)
  1890. {
  1891. struct storage *new = x86_expression(expr->init_expr);
  1892. struct symbol *ctype = expr->init_expr->ctype;
  1893. printf("\tinsert v%d at [%d:%d] of %s\n", new->pseudo,
  1894. expr->init_offset, ctype->bit_offset,
  1895. show_ident(base->ident));
  1896. return 0;
  1897. }
  1898. static void x86_initializer_expr(struct expression *expr, struct symbol *ctype)
  1899. {
  1900. struct expression *entry;
  1901. FOR_EACH_PTR(expr->expr_list, entry) {
  1902. // Nested initializers have their positions already
  1903. // recursively calculated - just output them too
  1904. if (entry->type == EXPR_INITIALIZER) {
  1905. x86_initializer_expr(entry, ctype);
  1906. continue;
  1907. }
  1908. // Ignore initializer indexes and identifiers - the
  1909. // evaluator has taken them into account
  1910. if (entry->type == EXPR_IDENTIFIER || entry->type == EXPR_INDEX)
  1911. continue;
  1912. if (entry->type == EXPR_POS) {
  1913. x86_position_expr(entry, ctype);
  1914. continue;
  1915. }
  1916. x86_initialization(ctype, entry);
  1917. } END_FOR_EACH_PTR(entry);
  1918. }
  1919. /*
  1920. * Print out an expression. Return the pseudo that contains the
  1921. * variable.
  1922. */
  1923. static struct storage *x86_expression(struct expression *expr)
  1924. {
  1925. if (!expr)
  1926. return NULL;
  1927. if (!expr->ctype) {
  1928. struct position *pos = &expr->pos;
  1929. printf("\tno type at %s:%d:%d\n",
  1930. stream_name(pos->stream),
  1931. pos->line, pos->pos);
  1932. return NULL;
  1933. }
  1934. switch (expr->type) {
  1935. default:
  1936. return NULL;
  1937. case EXPR_CALL:
  1938. return x86_call_expression(expr);
  1939. case EXPR_ASSIGNMENT:
  1940. return x86_assignment(expr);
  1941. case EXPR_COMPARE:
  1942. return emit_compare(expr);
  1943. case EXPR_BINOP:
  1944. case EXPR_COMMA:
  1945. case EXPR_LOGICAL:
  1946. return emit_binop(expr);
  1947. case EXPR_PREOP:
  1948. return x86_preop(expr);
  1949. case EXPR_POSTOP:
  1950. return emit_postop(expr);
  1951. case EXPR_SYMBOL:
  1952. return emit_symbol_expr_init(expr->symbol);
  1953. case EXPR_DEREF:
  1954. case EXPR_SIZEOF:
  1955. case EXPR_ALIGNOF:
  1956. warning(expr->pos, "invalid expression after evaluation");
  1957. return NULL;
  1958. case EXPR_CAST:
  1959. case EXPR_FORCE_CAST:
  1960. case EXPR_IMPLIED_CAST:
  1961. return emit_cast_expr(expr);
  1962. case EXPR_VALUE:
  1963. return emit_value(expr);
  1964. case EXPR_STRING:
  1965. return emit_string_expr(expr);
  1966. case EXPR_INITIALIZER:
  1967. x86_initializer_expr(expr, expr->ctype);
  1968. return NULL;
  1969. case EXPR_SELECT:
  1970. return emit_select_expr(expr);
  1971. case EXPR_CONDITIONAL:
  1972. return emit_conditional_expr(expr);
  1973. case EXPR_STATEMENT:
  1974. return x86_statement_expr(expr);
  1975. case EXPR_LABEL:
  1976. return x86_label_expr(expr);
  1977. // None of these should exist as direct expressions: they are only
  1978. // valid as sub-expressions of initializers.
  1979. case EXPR_POS:
  1980. warning(expr->pos, "unable to show plain initializer position expression");
  1981. return NULL;
  1982. case EXPR_IDENTIFIER:
  1983. warning(expr->pos, "unable to show identifier expression");
  1984. return NULL;
  1985. case EXPR_INDEX:
  1986. warning(expr->pos, "unable to show index expression");
  1987. return NULL;
  1988. case EXPR_TYPE:
  1989. warning(expr->pos, "unable to show type expression");
  1990. return NULL;
  1991. case EXPR_FVALUE:
  1992. warning(expr->pos, "floating point support is not implemented");
  1993. return NULL;
  1994. }
  1995. return NULL;
  1996. }