linearize.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637
  1. /*
  2. * Linearize - walk the statement tree (but _not_ the expressions)
  3. * to generate a linear version of it and the basic blocks.
  4. *
  5. * NOTE! We're not interested in the actual sub-expressions yet,
  6. * even though they can generate conditional branches and
  7. * subroutine calls. That's all "local" behaviour.
  8. *
  9. * Copyright (C) 2004 Linus Torvalds
  10. * Copyright (C) 2004 Christopher Li
  11. */
  12. #include <string.h>
  13. #include <stdarg.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <assert.h>
  17. #include "parse.h"
  18. #include "expression.h"
  19. #include "linearize.h"
  20. #include "optimize.h"
  21. #include "flow.h"
  22. #include "target.h"
  23. static pseudo_t linearize_statement(struct entrypoint *ep, struct statement *stmt);
  24. static pseudo_t linearize_expression(struct entrypoint *ep, struct expression *expr);
  25. static pseudo_t add_cast(struct entrypoint *ep, struct symbol *to, struct symbol *from, int op, pseudo_t src);
  26. static pseudo_t add_binary_op(struct entrypoint *ep, struct symbol *ctype, int op, pseudo_t left, pseudo_t right);
  27. static pseudo_t add_setval(struct entrypoint *ep, struct symbol *ctype, struct expression *val);
  28. static pseudo_t linearize_one_symbol(struct entrypoint *ep, struct symbol *sym);
  29. struct access_data;
  30. static pseudo_t add_load(struct entrypoint *ep, struct access_data *);
  31. static pseudo_t linearize_initializer(struct entrypoint *ep, struct expression *initializer, struct access_data *);
  32. static pseudo_t cast_pseudo(struct entrypoint *ep, pseudo_t src, struct symbol *from, struct symbol *to);
  33. struct pseudo void_pseudo = {};
  34. static struct position current_pos;
  35. ALLOCATOR(pseudo_user, "pseudo_user");
  36. static struct instruction *alloc_instruction(int opcode, int size)
  37. {
  38. struct instruction * insn = __alloc_instruction(0);
  39. insn->opcode = opcode;
  40. insn->size = size;
  41. insn->pos = current_pos;
  42. return insn;
  43. }
  44. static inline int type_size(struct symbol *type)
  45. {
  46. return type ? type->bit_size > 0 ? type->bit_size : 0 : 0;
  47. }
  48. static struct instruction *alloc_typed_instruction(int opcode, struct symbol *type)
  49. {
  50. struct instruction *insn = alloc_instruction(opcode, type_size(type));
  51. insn->type = type;
  52. return insn;
  53. }
  54. static struct entrypoint *alloc_entrypoint(void)
  55. {
  56. return __alloc_entrypoint(0);
  57. }
  58. static struct basic_block *alloc_basic_block(struct entrypoint *ep, struct position pos)
  59. {
  60. static int nr;
  61. struct basic_block *bb = __alloc_basic_block(0);
  62. bb->pos = pos;
  63. bb->ep = ep;
  64. bb->nr = nr++;
  65. return bb;
  66. }
  67. static struct multijmp *alloc_multijmp(struct basic_block *target, long long begin, long long end)
  68. {
  69. struct multijmp *multijmp = __alloc_multijmp(0);
  70. multijmp->target = target;
  71. multijmp->begin = begin;
  72. multijmp->end = end;
  73. return multijmp;
  74. }
  75. const char *show_label(struct basic_block *bb)
  76. {
  77. static int n;
  78. static char buffer[4][16];
  79. char *buf = buffer[3 & ++n];
  80. if (!bb)
  81. return ".L???";
  82. snprintf(buf, 64, ".L%u", bb->nr);
  83. return buf;
  84. }
  85. const char *show_pseudo(pseudo_t pseudo)
  86. {
  87. static int n;
  88. static char buffer[4][64];
  89. char *buf;
  90. int i;
  91. if (!pseudo)
  92. return "no pseudo";
  93. if (pseudo == VOID)
  94. return "VOID";
  95. buf = buffer[3 & ++n];
  96. switch(pseudo->type) {
  97. case PSEUDO_SYM: {
  98. struct symbol *sym = pseudo->sym;
  99. struct expression *expr;
  100. if (!sym) {
  101. snprintf(buf, 64, "<bad symbol>");
  102. break;
  103. }
  104. if (sym->bb_target) {
  105. snprintf(buf, 64, "%s", show_label(sym->bb_target));
  106. break;
  107. }
  108. if (sym->ident) {
  109. snprintf(buf, 64, "%s", show_ident(sym->ident));
  110. break;
  111. }
  112. expr = sym->initializer;
  113. snprintf(buf, 64, "<anon symbol:%p>", verbose ? sym : NULL);
  114. if (expr) {
  115. switch (expr->type) {
  116. case EXPR_VALUE:
  117. snprintf(buf, 64, "<symbol value: %lld>", expr->value);
  118. break;
  119. case EXPR_STRING:
  120. return show_string(expr->string);
  121. default:
  122. break;
  123. }
  124. }
  125. break;
  126. }
  127. case PSEUDO_REG:
  128. i = snprintf(buf, 64, "%%r%d", pseudo->nr);
  129. if (pseudo->ident)
  130. sprintf(buf+i, "(%s)", show_ident(pseudo->ident));
  131. break;
  132. case PSEUDO_VAL: {
  133. long long value = pseudo->value;
  134. if (value > 1000 || value < -1000)
  135. snprintf(buf, 64, "$%#llx", value);
  136. else
  137. snprintf(buf, 64, "$%lld", value);
  138. break;
  139. }
  140. case PSEUDO_ARG:
  141. snprintf(buf, 64, "%%arg%d", pseudo->nr);
  142. break;
  143. case PSEUDO_PHI:
  144. i = snprintf(buf, 64, "%%phi%d", pseudo->nr);
  145. if (pseudo->ident)
  146. sprintf(buf+i, "(%s)", show_ident(pseudo->ident));
  147. break;
  148. case PSEUDO_UNDEF:
  149. return "UNDEF";
  150. default:
  151. snprintf(buf, 64, "<bad pseudo type %d>", pseudo->type);
  152. }
  153. return buf;
  154. }
  155. static const char *opcodes[] = {
  156. [OP_BADOP] = "bad_op",
  157. /* Fn entrypoint */
  158. [OP_ENTRY] = "<entry-point>",
  159. /* Terminator */
  160. [OP_RET] = "ret",
  161. [OP_BR] = "br",
  162. [OP_CBR] = "cbr",
  163. [OP_SWITCH] = "switch",
  164. [OP_UNREACH] = "unreachable",
  165. [OP_COMPUTEDGOTO] = "jmp *",
  166. /* Binary */
  167. [OP_ADD] = "add",
  168. [OP_SUB] = "sub",
  169. [OP_MUL] = "mul",
  170. [OP_DIVU] = "divu",
  171. [OP_DIVS] = "divs",
  172. [OP_MODU] = "modu",
  173. [OP_MODS] = "mods",
  174. [OP_SHL] = "shl",
  175. [OP_LSR] = "lsr",
  176. [OP_ASR] = "asr",
  177. /* Floating-point Binary */
  178. [OP_FADD] = "fadd",
  179. [OP_FSUB] = "fsub",
  180. [OP_FMUL] = "fmul",
  181. [OP_FDIV] = "fdiv",
  182. /* Logical */
  183. [OP_AND] = "and",
  184. [OP_OR] = "or",
  185. [OP_XOR] = "xor",
  186. /* Binary comparison */
  187. [OP_SET_EQ] = "seteq",
  188. [OP_SET_NE] = "setne",
  189. [OP_SET_LE] = "setle",
  190. [OP_SET_GE] = "setge",
  191. [OP_SET_LT] = "setlt",
  192. [OP_SET_GT] = "setgt",
  193. [OP_SET_B] = "setb",
  194. [OP_SET_A] = "seta",
  195. [OP_SET_BE] = "setbe",
  196. [OP_SET_AE] = "setae",
  197. /* floating-point comparison */
  198. [OP_FCMP_ORD] = "fcmpord",
  199. [OP_FCMP_OEQ] = "fcmpoeq",
  200. [OP_FCMP_ONE] = "fcmpone",
  201. [OP_FCMP_OLE] = "fcmpole",
  202. [OP_FCMP_OGE] = "fcmpoge",
  203. [OP_FCMP_OLT] = "fcmpolt",
  204. [OP_FCMP_OGT] = "fcmpogt",
  205. [OP_FCMP_UEQ] = "fcmpueq",
  206. [OP_FCMP_UNE] = "fcmpune",
  207. [OP_FCMP_ULE] = "fcmpule",
  208. [OP_FCMP_UGE] = "fcmpuge",
  209. [OP_FCMP_ULT] = "fcmpult",
  210. [OP_FCMP_UGT] = "fcmpugt",
  211. [OP_FCMP_UNO] = "fcmpuno",
  212. /* Uni */
  213. [OP_NOT] = "not",
  214. [OP_NEG] = "neg",
  215. [OP_FNEG] = "fneg",
  216. /* Special three-input */
  217. [OP_SEL] = "select",
  218. [OP_FMADD] = "fmadd",
  219. /* Memory */
  220. [OP_LOAD] = "load",
  221. [OP_STORE] = "store",
  222. [OP_SETVAL] = "set",
  223. [OP_SETFVAL] = "setfval",
  224. [OP_SYMADDR] = "symaddr",
  225. /* Other */
  226. [OP_PHI] = "phi",
  227. [OP_PHISOURCE] = "phisrc",
  228. [OP_SEXT] = "sext",
  229. [OP_ZEXT] = "zext",
  230. [OP_TRUNC] = "trunc",
  231. [OP_FCVTU] = "fcvtu",
  232. [OP_FCVTS] = "fcvts",
  233. [OP_UCVTF] = "ucvtf",
  234. [OP_SCVTF] = "scvtf",
  235. [OP_FCVTF] = "fcvtf",
  236. [OP_UTPTR] = "utptr",
  237. [OP_PTRTU] = "ptrtu",
  238. [OP_PTRCAST] = "ptrcast",
  239. [OP_INLINED_CALL] = "# call",
  240. [OP_CALL] = "call",
  241. [OP_SLICE] = "slice",
  242. [OP_NOP] = "nop",
  243. [OP_DEATHNOTE] = "dead",
  244. [OP_ASM] = "asm",
  245. /* Sparse tagging (line numbers, context, whatever) */
  246. [OP_CONTEXT] = "context",
  247. [OP_RANGE] = "range-check",
  248. [OP_COPY] = "copy",
  249. };
  250. static char *show_asm_constraints(char *buf, const char *sep, struct asm_constraint_list *list)
  251. {
  252. struct asm_constraint *entry;
  253. FOR_EACH_PTR(list, entry) {
  254. buf += sprintf(buf, "%s\"%s\"", sep, entry->constraint);
  255. if (entry->pseudo)
  256. buf += sprintf(buf, " (%s)", show_pseudo(entry->pseudo));
  257. if (entry->ident)
  258. buf += sprintf(buf, " [%s]", show_ident(entry->ident));
  259. sep = ", ";
  260. } END_FOR_EACH_PTR(entry);
  261. return buf;
  262. }
  263. static char *show_asm(char *buf, struct instruction *insn)
  264. {
  265. struct asm_rules *rules = insn->asm_rules;
  266. buf += sprintf(buf, "\"%s\"", insn->string);
  267. buf = show_asm_constraints(buf, "\n\t\tout: ", rules->outputs);
  268. buf = show_asm_constraints(buf, "\n\t\tin: ", rules->inputs);
  269. buf = show_asm_constraints(buf, "\n\t\tclobber: ", rules->clobbers);
  270. return buf;
  271. }
  272. const char *show_instruction(struct instruction *insn)
  273. {
  274. int opcode = insn->opcode;
  275. static char buffer[4096];
  276. char *buf;
  277. buf = buffer;
  278. if (!insn->bb)
  279. buf += sprintf(buf, "# ");
  280. if (opcode < ARRAY_SIZE(opcodes)) {
  281. const char *op = opcodes[opcode];
  282. if (!op)
  283. buf += sprintf(buf, "opcode:%d", opcode);
  284. else
  285. buf += sprintf(buf, "%s", op);
  286. if (insn->size)
  287. buf += sprintf(buf, ".%d", insn->size);
  288. memset(buf, ' ', 20);
  289. buf++;
  290. }
  291. if (buf < buffer + 12)
  292. buf = buffer + 12;
  293. switch (opcode) {
  294. case OP_RET:
  295. if (insn->src && insn->src != VOID)
  296. buf += sprintf(buf, "%s", show_pseudo(insn->src));
  297. break;
  298. case OP_CBR:
  299. buf += sprintf(buf, "%s, %s, %s", show_pseudo(insn->cond), show_label(insn->bb_true), show_label(insn->bb_false));
  300. break;
  301. case OP_BR:
  302. buf += sprintf(buf, "%s", show_label(insn->bb_true));
  303. break;
  304. case OP_SETVAL: {
  305. struct expression *expr = insn->val;
  306. buf += sprintf(buf, "%s <- ", show_pseudo(insn->target));
  307. if (!expr) {
  308. buf += sprintf(buf, "%s", "<none>");
  309. break;
  310. }
  311. switch (expr->type) {
  312. case EXPR_VALUE:
  313. buf += sprintf(buf, "%lld", expr->value);
  314. break;
  315. case EXPR_FVALUE:
  316. buf += sprintf(buf, "%Le", expr->fvalue);
  317. break;
  318. case EXPR_STRING:
  319. buf += sprintf(buf, "%.40s", show_string(expr->string));
  320. break;
  321. case EXPR_SYMBOL:
  322. buf += sprintf(buf, "%s", show_ident(expr->symbol->ident));
  323. break;
  324. case EXPR_LABEL:
  325. buf += sprintf(buf, "%s", show_label(expr->symbol->bb_target));
  326. break;
  327. default:
  328. buf += sprintf(buf, "SETVAL EXPR TYPE %d", expr->type);
  329. }
  330. break;
  331. }
  332. case OP_SETFVAL:
  333. buf += sprintf(buf, "%s <- ", show_pseudo(insn->target));
  334. buf += sprintf(buf, "%Le", insn->fvalue);
  335. break;
  336. case OP_SWITCH: {
  337. struct multijmp *jmp;
  338. buf += sprintf(buf, "%s", show_pseudo(insn->cond));
  339. FOR_EACH_PTR(insn->multijmp_list, jmp) {
  340. if (jmp->begin == jmp->end)
  341. buf += sprintf(buf, ", %lld -> %s", jmp->begin, show_label(jmp->target));
  342. else if (jmp->begin < jmp->end)
  343. buf += sprintf(buf, ", %lld ... %lld -> %s", jmp->begin, jmp->end, show_label(jmp->target));
  344. else
  345. buf += sprintf(buf, ", default -> %s", show_label(jmp->target));
  346. } END_FOR_EACH_PTR(jmp);
  347. break;
  348. }
  349. case OP_COMPUTEDGOTO: {
  350. struct multijmp *jmp;
  351. buf += sprintf(buf, "%s", show_pseudo(insn->src));
  352. FOR_EACH_PTR(insn->multijmp_list, jmp) {
  353. buf += sprintf(buf, ", %s", show_label(jmp->target));
  354. } END_FOR_EACH_PTR(jmp);
  355. break;
  356. }
  357. case OP_UNREACH:
  358. break;
  359. case OP_PHISOURCE: {
  360. struct instruction *phi;
  361. buf += sprintf(buf, "%s <- %s ", show_pseudo(insn->target), show_pseudo(insn->phi_src));
  362. FOR_EACH_PTR(insn->phi_users, phi) {
  363. buf += sprintf(buf, " (%s)", show_pseudo(phi->target));
  364. } END_FOR_EACH_PTR(phi);
  365. break;
  366. }
  367. case OP_PHI: {
  368. pseudo_t phi;
  369. const char *s = " <-";
  370. buf += sprintf(buf, "%s", show_pseudo(insn->target));
  371. FOR_EACH_PTR(insn->phi_list, phi) {
  372. if (phi == VOID && !verbose)
  373. continue;
  374. buf += sprintf(buf, "%s %s", s, show_pseudo(phi));
  375. s = ",";
  376. } END_FOR_EACH_PTR(phi);
  377. break;
  378. }
  379. case OP_LOAD:
  380. buf += sprintf(buf, "%s <- %d[%s]", show_pseudo(insn->target), insn->offset, show_pseudo(insn->src));
  381. break;
  382. case OP_STORE:
  383. buf += sprintf(buf, "%s -> %d[%s]", show_pseudo(insn->target), insn->offset, show_pseudo(insn->src));
  384. break;
  385. case OP_INLINED_CALL:
  386. case OP_CALL: {
  387. struct pseudo *arg;
  388. if (insn->target && insn->target != VOID)
  389. buf += sprintf(buf, "%s <- ", show_pseudo(insn->target));
  390. buf += sprintf(buf, "%s", show_pseudo(insn->func));
  391. FOR_EACH_PTR(insn->arguments, arg) {
  392. buf += sprintf(buf, ", %s", show_pseudo(arg));
  393. } END_FOR_EACH_PTR(arg);
  394. break;
  395. }
  396. case OP_SEXT: case OP_ZEXT:
  397. case OP_TRUNC:
  398. case OP_FCVTU: case OP_FCVTS:
  399. case OP_UCVTF: case OP_SCVTF:
  400. case OP_FCVTF:
  401. case OP_UTPTR:
  402. case OP_PTRTU:
  403. case OP_PTRCAST:
  404. buf += sprintf(buf, "%s <- (%d) %s",
  405. show_pseudo(insn->target),
  406. type_size(insn->orig_type),
  407. show_pseudo(insn->src));
  408. break;
  409. case OP_BINARY ... OP_BINARY_END:
  410. case OP_FPCMP ... OP_FPCMP_END:
  411. case OP_BINCMP ... OP_BINCMP_END:
  412. buf += sprintf(buf, "%s <- %s, %s", show_pseudo(insn->target), show_pseudo(insn->src1), show_pseudo(insn->src2));
  413. break;
  414. case OP_SEL:
  415. case OP_FMADD:
  416. buf += sprintf(buf, "%s <- %s, %s, %s", show_pseudo(insn->target),
  417. show_pseudo(insn->src1), show_pseudo(insn->src2), show_pseudo(insn->src3));
  418. break;
  419. case OP_SLICE:
  420. buf += sprintf(buf, "%s <- %s, %d, %d", show_pseudo(insn->target), show_pseudo(insn->base), insn->from, insn->len);
  421. break;
  422. case OP_NOT: case OP_NEG:
  423. case OP_FNEG:
  424. case OP_SYMADDR:
  425. buf += sprintf(buf, "%s <- %s", show_pseudo(insn->target), show_pseudo(insn->src1));
  426. break;
  427. case OP_CONTEXT:
  428. buf += sprintf(buf, "%s%d", insn->check ? "check: " : "", insn->increment);
  429. break;
  430. case OP_RANGE:
  431. buf += sprintf(buf, "%s between %s..%s", show_pseudo(insn->src1), show_pseudo(insn->src2), show_pseudo(insn->src3));
  432. break;
  433. case OP_NOP:
  434. buf += sprintf(buf, "%s <- %s", show_pseudo(insn->target), show_pseudo(insn->src1));
  435. break;
  436. case OP_DEATHNOTE:
  437. buf += sprintf(buf, "%s", show_pseudo(insn->target));
  438. break;
  439. case OP_ASM:
  440. buf = show_asm(buf, insn);
  441. break;
  442. case OP_COPY:
  443. buf += sprintf(buf, "%s <- %s", show_pseudo(insn->target), show_pseudo(insn->src));
  444. break;
  445. default:
  446. break;
  447. }
  448. if (buf >= buffer + sizeof(buffer))
  449. die("instruction buffer overflowed %td\n", buf - buffer);
  450. do { --buf; } while (*buf == ' ');
  451. *++buf = 0;
  452. return buffer;
  453. }
  454. void show_bb(struct basic_block *bb)
  455. {
  456. struct instruction *insn;
  457. printf("%s:\n", show_label(bb));
  458. if (verbose) {
  459. pseudo_t needs, defines;
  460. printf("%s:%d\n", stream_name(bb->pos.stream), bb->pos.line);
  461. FOR_EACH_PTR(bb->needs, needs) {
  462. struct instruction *def = needs->def;
  463. if (def->opcode != OP_PHI) {
  464. printf(" **uses %s (from %s)**\n", show_pseudo(needs), show_label(def->bb));
  465. } else {
  466. pseudo_t phi;
  467. const char *sep = " ";
  468. printf(" **uses %s (from", show_pseudo(needs));
  469. FOR_EACH_PTR(def->phi_list, phi) {
  470. if (phi == VOID)
  471. continue;
  472. printf("%s(%s:%s)", sep, show_pseudo(phi), show_label(phi->def->bb));
  473. sep = ", ";
  474. } END_FOR_EACH_PTR(phi);
  475. printf(")**\n");
  476. }
  477. } END_FOR_EACH_PTR(needs);
  478. FOR_EACH_PTR(bb->defines, defines) {
  479. printf(" **defines %s **\n", show_pseudo(defines));
  480. } END_FOR_EACH_PTR(defines);
  481. if (bb->parents) {
  482. struct basic_block *from;
  483. FOR_EACH_PTR(bb->parents, from) {
  484. printf(" **from %s (%s:%d:%d)**\n", show_label(from),
  485. stream_name(from->pos.stream), from->pos.line, from->pos.pos);
  486. } END_FOR_EACH_PTR(from);
  487. }
  488. if (bb->children) {
  489. struct basic_block *to;
  490. FOR_EACH_PTR(bb->children, to) {
  491. printf(" **to %s (%s:%d:%d)**\n", show_label(to),
  492. stream_name(to->pos.stream), to->pos.line, to->pos.pos);
  493. } END_FOR_EACH_PTR(to);
  494. }
  495. }
  496. FOR_EACH_PTR(bb->insns, insn) {
  497. if (!insn->bb && verbose < 2)
  498. continue;
  499. printf("\t%s\n", show_instruction(insn));
  500. } END_FOR_EACH_PTR(insn);
  501. if (!bb_terminated(bb))
  502. printf("\tEND\n");
  503. }
  504. static void show_symbol_usage(pseudo_t pseudo)
  505. {
  506. struct pseudo_user *pu;
  507. if (pseudo) {
  508. FOR_EACH_PTR(pseudo->users, pu) {
  509. printf("\t%s\n", show_instruction(pu->insn));
  510. } END_FOR_EACH_PTR(pu);
  511. }
  512. }
  513. void show_entry(struct entrypoint *ep)
  514. {
  515. struct symbol *sym;
  516. struct basic_block *bb;
  517. printf("%s:\n", show_ident(ep->name->ident));
  518. if (verbose) {
  519. printf("ep %p: %s\n", ep, show_ident(ep->name->ident));
  520. FOR_EACH_PTR(ep->syms, sym) {
  521. if (!sym->pseudo)
  522. continue;
  523. if (!sym->pseudo->users)
  524. continue;
  525. printf(" sym: %p %s\n", sym, show_ident(sym->ident));
  526. if (sym->ctype.modifiers & (MOD_EXTERN | MOD_STATIC | MOD_ADDRESSABLE))
  527. printf("\texternal visibility\n");
  528. show_symbol_usage(sym->pseudo);
  529. } END_FOR_EACH_PTR(sym);
  530. printf("\n");
  531. }
  532. FOR_EACH_PTR(ep->bbs, bb) {
  533. if (!bb)
  534. continue;
  535. if (!bb->parents && !bb->children && !bb->insns && verbose < 2)
  536. continue;
  537. show_bb(bb);
  538. printf("\n");
  539. } END_FOR_EACH_PTR(bb);
  540. printf("\n");
  541. }
  542. static void bind_label(struct symbol *label, struct basic_block *bb, struct position pos)
  543. {
  544. if (label->bb_target)
  545. warning(pos, "label '%s' already bound", show_ident(label->ident));
  546. label->bb_target = bb;
  547. }
  548. static struct basic_block * get_bound_block(struct entrypoint *ep, struct symbol *label)
  549. {
  550. struct basic_block *bb = label->bb_target;
  551. if (!bb) {
  552. bb = alloc_basic_block(ep, label->pos);
  553. label->bb_target = bb;
  554. }
  555. return bb;
  556. }
  557. static void finish_block(struct entrypoint *ep)
  558. {
  559. struct basic_block *src = ep->active;
  560. if (bb_reachable(src))
  561. ep->active = NULL;
  562. }
  563. static void add_goto(struct entrypoint *ep, struct basic_block *dst)
  564. {
  565. struct basic_block *src = ep->active;
  566. if (bb_reachable(src)) {
  567. struct instruction *br = alloc_instruction(OP_BR, 0);
  568. br->bb_true = dst;
  569. add_bb(&dst->parents, src);
  570. add_bb(&src->children, dst);
  571. br->bb = src;
  572. add_instruction(&src->insns, br);
  573. ep->active = NULL;
  574. }
  575. }
  576. static void add_one_insn(struct entrypoint *ep, struct instruction *insn)
  577. {
  578. struct basic_block *bb = ep->active;
  579. if (bb_reachable(bb)) {
  580. insn->bb = bb;
  581. add_instruction(&bb->insns, insn);
  582. }
  583. }
  584. static void add_unreachable(struct entrypoint *ep)
  585. {
  586. struct instruction *insn = alloc_instruction(OP_UNREACH, 0);
  587. add_one_insn(ep, insn);
  588. ep->active = NULL;
  589. }
  590. static void set_activeblock(struct entrypoint *ep, struct basic_block *bb)
  591. {
  592. if (!bb_terminated(ep->active))
  593. add_goto(ep, bb);
  594. ep->active = bb;
  595. if (bb_reachable(bb))
  596. add_bb(&ep->bbs, bb);
  597. }
  598. static void remove_parent(struct basic_block *child, struct basic_block *parent)
  599. {
  600. remove_bb_from_list(&child->parents, parent, 1);
  601. if (!child->parents)
  602. repeat_phase |= REPEAT_CFG_CLEANUP;
  603. }
  604. /* Change a "switch" or a conditional branch into a branch */
  605. void insert_branch(struct basic_block *bb, struct instruction *jmp, struct basic_block *target)
  606. {
  607. struct instruction *br, *old;
  608. struct basic_block *child;
  609. /* Remove the switch */
  610. old = delete_last_instruction(&bb->insns);
  611. assert(old == jmp);
  612. kill_instruction(old);
  613. br = alloc_instruction(OP_BR, 0);
  614. br->bb = bb;
  615. br->bb_true = target;
  616. add_instruction(&bb->insns, br);
  617. FOR_EACH_PTR(bb->children, child) {
  618. if (child == target) {
  619. target = NULL; /* Trigger just once */
  620. continue;
  621. }
  622. DELETE_CURRENT_PTR(child);
  623. remove_parent(child, bb);
  624. } END_FOR_EACH_PTR(child);
  625. PACK_PTR_LIST(&bb->children);
  626. }
  627. void insert_select(struct basic_block *bb, struct instruction *br, struct instruction *phi_node, pseudo_t if_true, pseudo_t if_false)
  628. {
  629. pseudo_t target;
  630. struct instruction *select;
  631. /* Remove the 'br' */
  632. delete_last_instruction(&bb->insns);
  633. select = alloc_typed_instruction(OP_SEL, phi_node->type);
  634. select->bb = bb;
  635. assert(br->cond);
  636. use_pseudo(select, br->cond, &select->src1);
  637. target = phi_node->target;
  638. assert(target->def == phi_node);
  639. select->target = target;
  640. target->def = select;
  641. use_pseudo(select, if_true, &select->src2);
  642. use_pseudo(select, if_false, &select->src3);
  643. add_instruction(&bb->insns, select);
  644. add_instruction(&bb->insns, br);
  645. }
  646. static inline int bb_empty(struct basic_block *bb)
  647. {
  648. return !bb->insns;
  649. }
  650. /* Add a label to the currently active block, return new active block */
  651. static struct basic_block * add_label(struct entrypoint *ep, struct symbol *label)
  652. {
  653. struct basic_block *bb = label->bb_target;
  654. if (bb) {
  655. set_activeblock(ep, bb);
  656. return bb;
  657. }
  658. bb = ep->active;
  659. if (!bb_reachable(bb) || !bb_empty(bb)) {
  660. bb = alloc_basic_block(ep, label->pos);
  661. set_activeblock(ep, bb);
  662. }
  663. label->bb_target = bb;
  664. return bb;
  665. }
  666. static void add_branch(struct entrypoint *ep, pseudo_t cond, struct basic_block *bb_true, struct basic_block *bb_false)
  667. {
  668. struct basic_block *bb = ep->active;
  669. struct instruction *br;
  670. if (bb_reachable(bb)) {
  671. br = alloc_instruction(OP_CBR, 0);
  672. use_pseudo(br, cond, &br->cond);
  673. br->bb_true = bb_true;
  674. br->bb_false = bb_false;
  675. add_bb(&bb_true->parents, bb);
  676. add_bb(&bb_false->parents, bb);
  677. add_bb(&bb->children, bb_true);
  678. add_bb(&bb->children, bb_false);
  679. add_one_insn(ep, br);
  680. }
  681. }
  682. pseudo_t alloc_pseudo(struct instruction *def)
  683. {
  684. static int nr = 0;
  685. struct pseudo * pseudo = __alloc_pseudo(0);
  686. pseudo->type = PSEUDO_REG;
  687. pseudo->nr = ++nr;
  688. pseudo->def = def;
  689. return pseudo;
  690. }
  691. static pseudo_t symbol_pseudo(struct entrypoint *ep, struct symbol *sym)
  692. {
  693. pseudo_t pseudo;
  694. if (!sym)
  695. return VOID;
  696. pseudo = sym->pseudo;
  697. if (!pseudo) {
  698. pseudo = __alloc_pseudo(0);
  699. pseudo->nr = -1;
  700. pseudo->type = PSEUDO_SYM;
  701. pseudo->sym = sym;
  702. pseudo->ident = sym->ident;
  703. sym->pseudo = pseudo;
  704. add_pseudo(&ep->accesses, pseudo);
  705. }
  706. /* Symbol pseudos have neither nr nor def */
  707. return pseudo;
  708. }
  709. pseudo_t value_pseudo(long long val)
  710. {
  711. #define MAX_VAL_HASH 64
  712. static struct pseudo_list *prev[MAX_VAL_HASH];
  713. int hash = val & (MAX_VAL_HASH-1);
  714. struct pseudo_list **list = prev + hash;
  715. pseudo_t pseudo;
  716. FOR_EACH_PTR(*list, pseudo) {
  717. if (pseudo->value == val)
  718. return pseudo;
  719. } END_FOR_EACH_PTR(pseudo);
  720. pseudo = __alloc_pseudo(0);
  721. pseudo->type = PSEUDO_VAL;
  722. pseudo->value = val;
  723. add_pseudo(list, pseudo);
  724. /* Value pseudos have neither nr, usage nor def */
  725. return pseudo;
  726. }
  727. pseudo_t undef_pseudo(void)
  728. {
  729. pseudo_t pseudo = __alloc_pseudo(0);
  730. pseudo->type = PSEUDO_UNDEF;
  731. return pseudo;
  732. }
  733. static pseudo_t argument_pseudo(struct entrypoint *ep, int nr)
  734. {
  735. pseudo_t pseudo = __alloc_pseudo(0);
  736. struct instruction *entry = ep->entry;
  737. pseudo->type = PSEUDO_ARG;
  738. pseudo->nr = nr;
  739. pseudo->def = entry;
  740. add_pseudo(&entry->arg_list, pseudo);
  741. /* Argument pseudos have neither usage nor def */
  742. return pseudo;
  743. }
  744. struct instruction *alloc_phisrc(pseudo_t pseudo, struct symbol *type)
  745. {
  746. struct instruction *insn = alloc_typed_instruction(OP_PHISOURCE, type);
  747. pseudo_t phi = __alloc_pseudo(0);
  748. static int nr = 0;
  749. phi->type = PSEUDO_PHI;
  750. phi->nr = ++nr;
  751. phi->def = insn;
  752. use_pseudo(insn, pseudo, &insn->phi_src);
  753. insn->target = phi;
  754. return insn;
  755. }
  756. pseudo_t alloc_phi(struct basic_block *source, pseudo_t pseudo, struct symbol *type)
  757. {
  758. struct instruction *insn;
  759. if (!source)
  760. return VOID;
  761. insn = alloc_phisrc(pseudo, type);
  762. insn->bb = source;
  763. add_instruction(&source->insns, insn);
  764. return insn->target;
  765. }
  766. struct instruction *alloc_phi_node(struct basic_block *bb, struct symbol *type, struct ident *ident)
  767. {
  768. struct instruction *phi_node = alloc_typed_instruction(OP_PHI, type);
  769. pseudo_t phi;
  770. phi = alloc_pseudo(phi_node);
  771. phi->ident = ident;
  772. phi->def = phi_node;
  773. phi_node->target = phi;
  774. phi_node->bb = bb;
  775. return phi_node;
  776. }
  777. void add_phi_node(struct basic_block *bb, struct instruction *phi_node)
  778. {
  779. struct instruction *insn;
  780. FOR_EACH_PTR(bb->insns, insn) {
  781. enum opcode op = insn->opcode;
  782. if (op == OP_PHI)
  783. continue;
  784. INSERT_CURRENT(phi_node, insn);
  785. return;
  786. } END_FOR_EACH_PTR(insn);
  787. // FIXME
  788. add_instruction(&bb->insns, phi_node);
  789. }
  790. struct instruction *insert_phi_node(struct basic_block *bb, struct symbol *var)
  791. {
  792. struct instruction *phi_node = alloc_phi_node(bb, var, var->ident);
  793. add_phi_node(bb, phi_node);
  794. return phi_node;
  795. }
  796. /*
  797. * We carry the "access_data" structure around for any accesses,
  798. * which simplifies things a lot. It contains all the access
  799. * information in one place.
  800. */
  801. struct access_data {
  802. struct symbol *type; // ctype
  803. struct symbol *btype; // base type of bitfields
  804. pseudo_t address; // pseudo containing address ..
  805. unsigned int offset; // byte offset
  806. };
  807. static int linearize_simple_address(struct entrypoint *ep,
  808. struct expression *addr,
  809. struct access_data *ad)
  810. {
  811. if (addr->type == EXPR_SYMBOL) {
  812. linearize_one_symbol(ep, addr->symbol);
  813. ad->address = symbol_pseudo(ep, addr->symbol);
  814. return 1;
  815. }
  816. if (addr->type == EXPR_BINOP) {
  817. if (addr->right->type == EXPR_VALUE) {
  818. if (addr->op == '+') {
  819. ad->offset += get_expression_value(addr->right);
  820. return linearize_simple_address(ep, addr->left, ad);
  821. }
  822. }
  823. }
  824. ad->address = linearize_expression(ep, addr);
  825. return 1;
  826. }
  827. static struct symbol *bitfield_base_type(struct symbol *sym)
  828. {
  829. struct symbol *base = sym;
  830. if (sym) {
  831. if (sym->type == SYM_NODE)
  832. base = base->ctype.base_type;
  833. if (base->type == SYM_BITFIELD)
  834. return base->ctype.base_type;
  835. }
  836. return sym;
  837. }
  838. static int linearize_address_gen(struct entrypoint *ep,
  839. struct expression *expr,
  840. struct access_data *ad)
  841. {
  842. struct symbol *ctype = expr->ctype;
  843. if (!ctype)
  844. return 0;
  845. ad->type = ctype;
  846. if (expr->type == EXPR_PREOP && expr->op == '*')
  847. return linearize_simple_address(ep, expr->unop, ad);
  848. warning(expr->pos, "generating address of non-lvalue (%d)", expr->type);
  849. return 0;
  850. }
  851. static pseudo_t add_load(struct entrypoint *ep, struct access_data *ad)
  852. {
  853. struct instruction *insn;
  854. pseudo_t new;
  855. if (!ep->active)
  856. return VOID;
  857. insn = alloc_typed_instruction(OP_LOAD, ad->btype);
  858. new = alloc_pseudo(insn);
  859. insn->target = new;
  860. insn->offset = ad->offset;
  861. insn->is_volatile = ad->type && (ad->type->ctype.modifiers & MOD_VOLATILE);
  862. use_pseudo(insn, ad->address, &insn->src);
  863. add_one_insn(ep, insn);
  864. return new;
  865. }
  866. static void add_store(struct entrypoint *ep, struct access_data *ad, pseudo_t value)
  867. {
  868. struct basic_block *bb = ep->active;
  869. struct instruction *store;
  870. if (!bb)
  871. return;
  872. store = alloc_typed_instruction(OP_STORE, ad->btype);
  873. store->offset = ad->offset;
  874. store->is_volatile = ad->type && (ad->type->ctype.modifiers & MOD_VOLATILE);
  875. use_pseudo(store, value, &store->target);
  876. use_pseudo(store, ad->address, &store->src);
  877. add_one_insn(ep, store);
  878. }
  879. static pseudo_t linearize_bitfield_insert(struct entrypoint *ep,
  880. pseudo_t ori, pseudo_t val, struct symbol *ctype, struct symbol *btype)
  881. {
  882. unsigned int shift = ctype->bit_offset;
  883. unsigned int size = ctype->bit_size;
  884. unsigned long long mask = ((1ULL << size) - 1);
  885. unsigned long long smask= bits_mask(btype->bit_size);
  886. val = add_cast(ep, btype, ctype, OP_ZEXT, val);
  887. if (shift) {
  888. val = add_binary_op(ep, btype, OP_SHL, val, value_pseudo(shift));
  889. mask <<= shift;
  890. }
  891. ori = add_binary_op(ep, btype, OP_AND, ori, value_pseudo(~mask & smask));
  892. val = add_binary_op(ep, btype, OP_OR, ori, val);
  893. return val;
  894. }
  895. static pseudo_t linearize_store_gen(struct entrypoint *ep,
  896. pseudo_t value,
  897. struct access_data *ad)
  898. {
  899. struct symbol *ctype = ad->type;
  900. struct symbol *btype;
  901. pseudo_t store = value;
  902. if (!ep->active)
  903. return VOID;
  904. btype = ad->btype = bitfield_base_type(ctype);
  905. if (type_size(btype) != type_size(ctype)) {
  906. pseudo_t orig = add_load(ep, ad);
  907. store = linearize_bitfield_insert(ep, orig, value, ctype, btype);
  908. }
  909. add_store(ep, ad, store);
  910. return value;
  911. }
  912. static void taint_undefined_behaviour(struct instruction *insn)
  913. {
  914. pseudo_t src2;
  915. switch (insn->opcode) {
  916. case OP_LSR:
  917. case OP_ASR:
  918. case OP_SHL:
  919. src2 = insn->src2;
  920. if (src2->type != PSEUDO_VAL)
  921. break;
  922. if ((unsigned long long)src2->value >= insn->size)
  923. insn->tainted = 1;
  924. break;
  925. }
  926. }
  927. static pseudo_t add_binary_op(struct entrypoint *ep, struct symbol *ctype, int op, pseudo_t left, pseudo_t right)
  928. {
  929. struct instruction *insn = alloc_typed_instruction(op, ctype);
  930. pseudo_t target = alloc_pseudo(insn);
  931. insn->target = target;
  932. use_pseudo(insn, left, &insn->src1);
  933. use_pseudo(insn, right, &insn->src2);
  934. add_one_insn(ep, insn);
  935. return target;
  936. }
  937. static pseudo_t add_setval(struct entrypoint *ep, struct symbol *ctype, struct expression *val)
  938. {
  939. struct instruction *insn = alloc_typed_instruction(OP_SETVAL, ctype);
  940. pseudo_t target = alloc_pseudo(insn);
  941. insn->target = target;
  942. insn->val = val;
  943. add_one_insn(ep, insn);
  944. return target;
  945. }
  946. static pseudo_t add_setfval(struct entrypoint *ep, struct symbol *ctype, long double fval)
  947. {
  948. struct instruction *insn = alloc_typed_instruction(OP_SETFVAL, ctype);
  949. pseudo_t target = alloc_pseudo(insn);
  950. insn->target = target;
  951. insn->fvalue = fval;
  952. add_one_insn(ep, insn);
  953. return target;
  954. }
  955. static pseudo_t add_symbol_address(struct entrypoint *ep, struct symbol *sym)
  956. {
  957. struct instruction *insn = alloc_instruction(OP_SYMADDR, bits_in_pointer);
  958. pseudo_t target = alloc_pseudo(insn);
  959. insn->target = target;
  960. use_pseudo(insn, symbol_pseudo(ep, sym), &insn->src);
  961. add_one_insn(ep, insn);
  962. return target;
  963. }
  964. static pseudo_t linearize_bitfield_extract(struct entrypoint *ep,
  965. pseudo_t val, struct symbol *ctype, struct symbol *btype)
  966. {
  967. unsigned int off = ctype->bit_offset;
  968. if (off) {
  969. pseudo_t shift = value_pseudo(off);
  970. val = add_binary_op(ep, btype, OP_LSR, val, shift);
  971. }
  972. val = cast_pseudo(ep, val, btype, ctype);
  973. return val;
  974. }
  975. static pseudo_t linearize_load_gen(struct entrypoint *ep, struct access_data *ad)
  976. {
  977. struct symbol *ctype = ad->type;
  978. struct symbol *btype;
  979. pseudo_t new;
  980. if (!ep->active)
  981. return VOID;
  982. btype = ad->btype = bitfield_base_type(ctype);
  983. new = add_load(ep, ad);
  984. if (ctype->bit_size != type_size(btype))
  985. new = linearize_bitfield_extract(ep, new, ctype, btype);
  986. return new;
  987. }
  988. static pseudo_t linearize_access(struct entrypoint *ep, struct expression *expr)
  989. {
  990. struct access_data ad = { NULL, };
  991. pseudo_t value;
  992. if (!linearize_address_gen(ep, expr, &ad))
  993. return VOID;
  994. value = linearize_load_gen(ep, &ad);
  995. return value;
  996. }
  997. static pseudo_t linearize_inc_dec(struct entrypoint *ep, struct expression *expr, int postop)
  998. {
  999. struct access_data ad = { NULL, };
  1000. pseudo_t old, new, one;
  1001. int op = expr->op == SPECIAL_INCREMENT ? OP_ADD : OP_SUB;
  1002. if (!linearize_address_gen(ep, expr->unop, &ad))
  1003. return VOID;
  1004. old = linearize_load_gen(ep, &ad);
  1005. op = opcode_float(op, expr->ctype);
  1006. if (is_float_type(expr->ctype))
  1007. one = add_setfval(ep, expr->ctype, expr->op_value);
  1008. else
  1009. one = value_pseudo(expr->op_value);
  1010. if (ad.btype != ad.type)
  1011. old = cast_pseudo(ep, old, ad.type, ad.btype);
  1012. new = add_binary_op(ep, ad.btype, op, old, one);
  1013. if (ad.btype != ad.type)
  1014. new = cast_pseudo(ep, new, ad.btype, ad.type);
  1015. linearize_store_gen(ep, new, &ad);
  1016. return postop ? old : new;
  1017. }
  1018. static pseudo_t add_unop(struct entrypoint *ep, struct symbol *ctype, int op, pseudo_t src)
  1019. {
  1020. struct instruction *insn = alloc_typed_instruction(op, ctype);
  1021. pseudo_t new = alloc_pseudo(insn);
  1022. insn->target = new;
  1023. use_pseudo(insn, src, &insn->src1);
  1024. add_one_insn(ep, insn);
  1025. return new;
  1026. }
  1027. static pseudo_t add_cast(struct entrypoint *ep, struct symbol *to,
  1028. struct symbol *from, int op, pseudo_t src)
  1029. {
  1030. pseudo_t new = add_unop(ep, to, op, src);
  1031. new->def->orig_type = from;
  1032. return new;
  1033. }
  1034. static pseudo_t linearize_slice(struct entrypoint *ep, struct expression *expr)
  1035. {
  1036. pseudo_t pre = linearize_expression(ep, expr->base);
  1037. struct instruction *insn = alloc_typed_instruction(OP_SLICE, expr->ctype);
  1038. pseudo_t new = alloc_pseudo(insn);
  1039. insn->target = new;
  1040. insn->from = expr->r_bitpos;
  1041. insn->len = expr->r_nrbits;
  1042. use_pseudo(insn, pre, &insn->base);
  1043. add_one_insn(ep, insn);
  1044. return new;
  1045. }
  1046. static pseudo_t linearize_regular_preop(struct entrypoint *ep, struct expression *expr)
  1047. {
  1048. pseudo_t pre = linearize_expression(ep, expr->unop);
  1049. struct symbol *ctype = expr->ctype;
  1050. switch (expr->op) {
  1051. case '+':
  1052. return pre;
  1053. case '!': {
  1054. pseudo_t zero = value_pseudo(0);
  1055. return add_binary_op(ep, ctype, OP_SET_EQ, pre, zero);
  1056. }
  1057. case '~':
  1058. return add_unop(ep, ctype, OP_NOT, pre);
  1059. case '-':
  1060. return add_unop(ep, ctype, opcode_float(OP_NEG, ctype), pre);
  1061. }
  1062. return VOID;
  1063. }
  1064. static pseudo_t linearize_preop(struct entrypoint *ep, struct expression *expr)
  1065. {
  1066. /*
  1067. * '*' is an lvalue access, and is fundamentally different
  1068. * from an arithmetic operation. Maybe it should have an
  1069. * expression type of its own..
  1070. */
  1071. if (expr->op == '*')
  1072. return linearize_access(ep, expr);
  1073. if (expr->op == SPECIAL_INCREMENT || expr->op == SPECIAL_DECREMENT)
  1074. return linearize_inc_dec(ep, expr, 0);
  1075. return linearize_regular_preop(ep, expr);
  1076. }
  1077. static pseudo_t linearize_postop(struct entrypoint *ep, struct expression *expr)
  1078. {
  1079. return linearize_inc_dec(ep, expr, 1);
  1080. }
  1081. /*
  1082. * Casts to pointers are "less safe" than other casts, since
  1083. * they imply type-unsafe accesses. "void *" is a special
  1084. * case, since you can't access through it anyway without another
  1085. * cast.
  1086. */
  1087. enum mtype {
  1088. MTYPE_UINT,
  1089. MTYPE_SINT,
  1090. MTYPE_PTR,
  1091. MTYPE_VPTR, // TODO: must be removed ?
  1092. MTYPE_FLOAT,
  1093. MTYPE_BAD,
  1094. };
  1095. static enum mtype get_mtype(struct symbol *s)
  1096. {
  1097. int sign = (s->ctype.modifiers & MOD_SIGNED) ? 1 : 0;
  1098. retry: switch (s->type) {
  1099. case SYM_NODE:
  1100. s = s->ctype.base_type;
  1101. goto retry;
  1102. case SYM_PTR:
  1103. if (s->ctype.base_type == &void_ctype)
  1104. return MTYPE_VPTR;
  1105. return MTYPE_PTR;
  1106. case SYM_BITFIELD:
  1107. case SYM_RESTRICT:
  1108. case SYM_FOULED:
  1109. case SYM_ENUM:
  1110. s = s->ctype.base_type;
  1111. /* fall-through */
  1112. case_int:
  1113. return sign ? MTYPE_SINT : MTYPE_UINT;
  1114. case SYM_BASETYPE:
  1115. if (s->ctype.base_type == &fp_type)
  1116. return MTYPE_FLOAT;
  1117. if (s->ctype.base_type == &int_type)
  1118. goto case_int;
  1119. /* fall-through */
  1120. default:
  1121. return MTYPE_BAD;
  1122. }
  1123. }
  1124. static int get_cast_opcode(struct symbol *dst, struct symbol *src)
  1125. {
  1126. enum mtype stype = get_mtype(src);
  1127. enum mtype dtype = get_mtype(dst);
  1128. switch (dtype) {
  1129. case MTYPE_FLOAT:
  1130. switch (stype) {
  1131. case MTYPE_FLOAT:
  1132. if (dst->bit_size == src->bit_size)
  1133. return OP_NOP;
  1134. return OP_FCVTF;
  1135. case MTYPE_UINT:
  1136. return OP_UCVTF;
  1137. case MTYPE_SINT:
  1138. return OP_SCVTF;
  1139. default:
  1140. return OP_BADOP;
  1141. }
  1142. case MTYPE_PTR:
  1143. switch (stype) {
  1144. case MTYPE_UINT:
  1145. case MTYPE_SINT:
  1146. return OP_UTPTR;
  1147. case MTYPE_PTR:
  1148. case MTYPE_VPTR:
  1149. return OP_PTRCAST;
  1150. default:
  1151. return OP_BADOP;
  1152. }
  1153. case MTYPE_VPTR:
  1154. switch (stype) {
  1155. case MTYPE_PTR:
  1156. case MTYPE_VPTR:
  1157. case MTYPE_UINT:
  1158. stype = MTYPE_UINT;
  1159. /* fall through */
  1160. case MTYPE_SINT:
  1161. break;
  1162. default:
  1163. return OP_BADOP;
  1164. }
  1165. /* fall through */
  1166. case MTYPE_UINT:
  1167. case MTYPE_SINT:
  1168. switch (stype) {
  1169. case MTYPE_FLOAT:
  1170. return dtype == MTYPE_UINT ? OP_FCVTU : OP_FCVTS;
  1171. case MTYPE_PTR:
  1172. return OP_PTRTU;
  1173. case MTYPE_VPTR:
  1174. case MTYPE_UINT:
  1175. case MTYPE_SINT:
  1176. if (dst->bit_size ==src->bit_size)
  1177. return OP_NOP;
  1178. if (dst->bit_size < src->bit_size)
  1179. return OP_TRUNC;
  1180. return stype == MTYPE_SINT ? OP_SEXT : OP_ZEXT;
  1181. default:
  1182. return OP_BADOP;
  1183. }
  1184. /* fall through */
  1185. default:
  1186. if (src->type == SYM_NODE)
  1187. src = src->ctype.base_type;
  1188. if (dst->type == SYM_NODE)
  1189. dst = dst->ctype.base_type;
  1190. if (src == dst)
  1191. return OP_NOP;
  1192. return OP_BADOP;
  1193. }
  1194. }
  1195. static pseudo_t cast_pseudo(struct entrypoint *ep, pseudo_t src, struct symbol *from, struct symbol *to)
  1196. {
  1197. const struct position pos = current_pos;
  1198. pseudo_t result;
  1199. struct instruction *insn;
  1200. int opcode;
  1201. if (src == VOID)
  1202. return VOID;
  1203. if (!from || !to)
  1204. return VOID;
  1205. if (from->bit_size < 0 || to->bit_size < 0)
  1206. return VOID;
  1207. opcode = get_cast_opcode(to, from);
  1208. switch (opcode) {
  1209. case OP_NOP:
  1210. return src;
  1211. case OP_UTPTR:
  1212. if (from->bit_size == to->bit_size)
  1213. break;
  1214. if (src == value_pseudo(0))
  1215. break;
  1216. if (Wint_to_pointer_cast)
  1217. warning(pos, "non size-preserving integer to pointer cast");
  1218. src = cast_pseudo(ep, src, from, size_t_ctype);
  1219. from = size_t_ctype;
  1220. break;
  1221. case OP_PTRTU:
  1222. if (from->bit_size == to->bit_size)
  1223. break;
  1224. if (Wpointer_to_int_cast)
  1225. warning(pos, "non size-preserving pointer to integer cast");
  1226. /* XXX fixme this can loop forever */ src = cast_pseudo(ep, src, from, size_t_ctype);
  1227. return cast_pseudo(ep, src, size_t_ctype, to);
  1228. case OP_BADOP:
  1229. return VOID;
  1230. default:
  1231. break;
  1232. }
  1233. insn = alloc_typed_instruction(opcode, to);
  1234. result = alloc_pseudo(insn);
  1235. insn->target = result;
  1236. insn->orig_type = from;
  1237. use_pseudo(insn, src, &insn->src);
  1238. add_one_insn(ep, insn);
  1239. return result;
  1240. }
  1241. static int map_opcode(int opcode, struct symbol *ctype)
  1242. {
  1243. if (ctype && is_float_type(ctype))
  1244. return opcode_table[opcode].to_float;
  1245. if (ctype && (ctype->ctype.modifiers & MOD_SIGNED)) {
  1246. switch(opcode) {
  1247. case OP_DIVU: case OP_MODU: case OP_LSR:
  1248. opcode++;
  1249. }
  1250. }
  1251. return opcode;
  1252. }
  1253. static inline pseudo_t add_convert_to_bool(struct entrypoint *ep, pseudo_t src, struct symbol *type)
  1254. {
  1255. pseudo_t zero;
  1256. int op;
  1257. if (!type || src == VOID)
  1258. return VOID;
  1259. if (is_bool_type(type))
  1260. return src;
  1261. if (src->type == PSEUDO_VAL && (src->value == 0 || src->value == 1))
  1262. return src;
  1263. if (is_float_type(type)) {
  1264. zero = add_setfval(ep, type, 0.0);
  1265. op = map_opcode(OP_SET_NE, type);
  1266. } else {
  1267. zero = value_pseudo(0);
  1268. op = OP_SET_NE;
  1269. }
  1270. return add_binary_op(ep, &bool_ctype, op, src, zero);
  1271. }
  1272. static pseudo_t linearize_expression_to_bool(struct entrypoint *ep, struct expression *expr)
  1273. {
  1274. pseudo_t dst;
  1275. dst = linearize_expression(ep, expr);
  1276. dst = add_convert_to_bool(ep, dst, expr->ctype);
  1277. return dst;
  1278. }
  1279. static pseudo_t linearize_assignment(struct entrypoint *ep, struct expression *expr)
  1280. {
  1281. struct access_data ad = { NULL, };
  1282. struct expression *target = expr->left;
  1283. struct expression *src = expr->right;
  1284. struct symbol *ctype;
  1285. pseudo_t value;
  1286. value = linearize_expression(ep, src);
  1287. if (!target || !linearize_address_gen(ep, target, &ad))
  1288. return value;
  1289. if (expr->op != '=') {
  1290. pseudo_t oldvalue = linearize_load_gen(ep, &ad);
  1291. pseudo_t dst;
  1292. static const int op_trans[] = {
  1293. [SPECIAL_ADD_ASSIGN - SPECIAL_BASE] = OP_ADD,
  1294. [SPECIAL_SUB_ASSIGN - SPECIAL_BASE] = OP_SUB,
  1295. [SPECIAL_MUL_ASSIGN - SPECIAL_BASE] = OP_MUL,
  1296. [SPECIAL_DIV_ASSIGN - SPECIAL_BASE] = OP_DIVU,
  1297. [SPECIAL_MOD_ASSIGN - SPECIAL_BASE] = OP_MODU,
  1298. [SPECIAL_SHL_ASSIGN - SPECIAL_BASE] = OP_SHL,
  1299. [SPECIAL_SHR_ASSIGN - SPECIAL_BASE] = OP_LSR,
  1300. [SPECIAL_AND_ASSIGN - SPECIAL_BASE] = OP_AND,
  1301. [SPECIAL_OR_ASSIGN - SPECIAL_BASE] = OP_OR,
  1302. [SPECIAL_XOR_ASSIGN - SPECIAL_BASE] = OP_XOR
  1303. };
  1304. int opcode;
  1305. if (!src)
  1306. return VOID;
  1307. ctype = src->ctype;
  1308. oldvalue = cast_pseudo(ep, oldvalue, target->ctype, ctype);
  1309. opcode = map_opcode(op_trans[expr->op - SPECIAL_BASE], ctype);
  1310. dst = add_binary_op(ep, ctype, opcode, oldvalue, value);
  1311. taint_undefined_behaviour(dst->def);
  1312. value = cast_pseudo(ep, dst, ctype, expr->ctype);
  1313. }
  1314. value = linearize_store_gen(ep, value, &ad);
  1315. return value;
  1316. }
  1317. static pseudo_t linearize_call_expression(struct entrypoint *ep, struct expression *expr)
  1318. {
  1319. struct expression *arg, *fn;
  1320. struct instruction *insn = alloc_typed_instruction(OP_CALL, expr->ctype);
  1321. pseudo_t retval, call;
  1322. struct ctype *ctype = NULL;
  1323. struct symbol *fntype;
  1324. struct context *context;
  1325. if (!expr->ctype)
  1326. return VOID;
  1327. fn = expr->fn;
  1328. fntype = fn->ctype;
  1329. // handle builtins
  1330. if (fntype->op && fntype->op->linearize) {
  1331. retval = fntype->op->linearize(ep, expr);
  1332. if (retval)
  1333. return retval;
  1334. }
  1335. ctype = &fntype->ctype;
  1336. if (fntype->type == SYM_NODE)
  1337. fntype = fntype->ctype.base_type;
  1338. add_symbol(&insn->fntypes, fntype);
  1339. FOR_EACH_PTR(expr->args, arg) {
  1340. pseudo_t new = linearize_expression(ep, arg);
  1341. use_pseudo(insn, new, add_pseudo(&insn->arguments, new));
  1342. add_symbol(&insn->fntypes, arg->ctype);
  1343. } END_FOR_EACH_PTR(arg);
  1344. if (fn->type == EXPR_PREOP && fn->op == '*' && is_func_type(fn->ctype))
  1345. fn = fn->unop;
  1346. if (fn->type == EXPR_SYMBOL) {
  1347. call = symbol_pseudo(ep, fn->symbol);
  1348. } else {
  1349. call = linearize_expression(ep, fn);
  1350. }
  1351. use_pseudo(insn, call, &insn->func);
  1352. retval = VOID;
  1353. if (expr->ctype != &void_ctype)
  1354. retval = alloc_pseudo(insn);
  1355. insn->target = retval;
  1356. add_one_insn(ep, insn);
  1357. if (ctype) {
  1358. FOR_EACH_PTR(ctype->contexts, context) {
  1359. int in = context->in;
  1360. int out = context->out;
  1361. int check = 0;
  1362. int context_diff;
  1363. if (in < 0) {
  1364. check = 1;
  1365. in = 0;
  1366. }
  1367. if (out < 0) {
  1368. check = 0;
  1369. out = 0;
  1370. }
  1371. context_diff = out - in;
  1372. if (check || context_diff) {
  1373. insn = alloc_instruction(OP_CONTEXT, 0);
  1374. insn->increment = context_diff;
  1375. insn->check = check;
  1376. insn->context_expr = context->context;
  1377. add_one_insn(ep, insn);
  1378. }
  1379. } END_FOR_EACH_PTR(context);
  1380. if (ctype->modifiers & MOD_NORETURN)
  1381. add_unreachable(ep);
  1382. }
  1383. return retval;
  1384. }
  1385. static pseudo_t linearize_binop_bool(struct entrypoint *ep, struct expression *expr)
  1386. {
  1387. pseudo_t src1, src2, dst;
  1388. int op = (expr->op == SPECIAL_LOGICAL_OR) ? OP_OR : OP_AND;
  1389. src1 = linearize_expression_to_bool(ep, expr->left);
  1390. src2 = linearize_expression_to_bool(ep, expr->right);
  1391. dst = add_binary_op(ep, &bool_ctype, op, src1, src2);
  1392. if (expr->ctype != &bool_ctype)
  1393. dst = cast_pseudo(ep, dst, &bool_ctype, expr->ctype);
  1394. return dst;
  1395. }
  1396. static pseudo_t linearize_binop(struct entrypoint *ep, struct expression *expr)
  1397. {
  1398. pseudo_t src1, src2, dst;
  1399. static const int opcode[] = {
  1400. ['+'] = OP_ADD, ['-'] = OP_SUB,
  1401. ['*'] = OP_MUL, ['/'] = OP_DIVU,
  1402. ['%'] = OP_MODU, ['&'] = OP_AND,
  1403. ['|'] = OP_OR, ['^'] = OP_XOR,
  1404. [SPECIAL_LEFTSHIFT] = OP_SHL,
  1405. [SPECIAL_RIGHTSHIFT] = OP_LSR,
  1406. };
  1407. int op;
  1408. src1 = linearize_expression(ep, expr->left);
  1409. src2 = linearize_expression(ep, expr->right);
  1410. op = map_opcode(opcode[expr->op], expr->ctype);
  1411. dst = add_binary_op(ep, expr->ctype, op, src1, src2);
  1412. taint_undefined_behaviour(dst->def);
  1413. return dst;
  1414. }
  1415. static pseudo_t linearize_logical_branch(struct entrypoint *ep, struct expression *expr, struct basic_block *bb_true, struct basic_block *bb_false);
  1416. static pseudo_t linearize_cond_branch(struct entrypoint *ep, struct expression *expr, struct basic_block *bb_true, struct basic_block *bb_false);
  1417. static pseudo_t linearize_select(struct entrypoint *ep, struct expression *expr)
  1418. {
  1419. pseudo_t cond, valt, valf, res;
  1420. struct instruction *insn;
  1421. valt = linearize_expression(ep, expr->cond_true);
  1422. valf = linearize_expression(ep, expr->cond_false);
  1423. cond = linearize_expression(ep, expr->conditional);
  1424. insn = alloc_typed_instruction(OP_SEL, expr->ctype);
  1425. if (!expr->cond_true)
  1426. valt = cond;
  1427. use_pseudo(insn, cond, &insn->src1);
  1428. use_pseudo(insn, valt, &insn->src2);
  1429. use_pseudo(insn, valf, &insn->src3);
  1430. res = alloc_pseudo(insn);
  1431. insn->target = res;
  1432. add_one_insn(ep, insn);
  1433. return res;
  1434. }
  1435. static pseudo_t add_join_conditional(struct entrypoint *ep, struct expression *expr,
  1436. pseudo_t phi1, pseudo_t phi2)
  1437. {
  1438. pseudo_t target;
  1439. struct instruction *phi_node;
  1440. if (phi1 == VOID)
  1441. return phi2;
  1442. if (phi2 == VOID)
  1443. return phi1;
  1444. phi_node = alloc_typed_instruction(OP_PHI, expr->ctype);
  1445. use_pseudo(phi_node, phi1, add_pseudo(&phi_node->phi_list, phi1));
  1446. use_pseudo(phi_node, phi2, add_pseudo(&phi_node->phi_list, phi2));
  1447. phi_node->target = target = alloc_pseudo(phi_node);
  1448. add_one_insn(ep, phi_node);
  1449. return target;
  1450. }
  1451. static pseudo_t linearize_short_conditional(struct entrypoint *ep, struct expression *expr,
  1452. struct expression *cond,
  1453. struct expression *expr_false)
  1454. {
  1455. pseudo_t src1, src2;
  1456. struct basic_block *bb_false;
  1457. struct basic_block *merge;
  1458. pseudo_t phi1, phi2;
  1459. if (!expr_false || !ep->active)
  1460. return VOID;
  1461. bb_false = alloc_basic_block(ep, expr_false->pos);
  1462. merge = alloc_basic_block(ep, expr->pos);
  1463. src1 = linearize_expression(ep, cond);
  1464. phi1 = alloc_phi(ep->active, src1, expr->ctype);
  1465. add_branch(ep, src1, merge, bb_false);
  1466. set_activeblock(ep, bb_false);
  1467. src2 = linearize_expression(ep, expr_false);
  1468. phi2 = alloc_phi(ep->active, src2, expr->ctype);
  1469. set_activeblock(ep, merge);
  1470. return add_join_conditional(ep, expr, phi1, phi2);
  1471. }
  1472. static pseudo_t linearize_conditional(struct entrypoint *ep, struct expression *expr,
  1473. struct expression *cond,
  1474. struct expression *expr_true,
  1475. struct expression *expr_false)
  1476. {
  1477. pseudo_t src1, src2;
  1478. pseudo_t phi1, phi2;
  1479. struct basic_block *bb_true, *bb_false, *merge;
  1480. if (!cond || !expr_true || !expr_false || !ep->active)
  1481. return VOID;
  1482. bb_true = alloc_basic_block(ep, expr_true->pos);
  1483. bb_false = alloc_basic_block(ep, expr_false->pos);
  1484. merge = alloc_basic_block(ep, expr->pos);
  1485. linearize_cond_branch(ep, cond, bb_true, bb_false);
  1486. set_activeblock(ep, bb_true);
  1487. src1 = linearize_expression(ep, expr_true);
  1488. phi1 = alloc_phi(ep->active, src1, expr->ctype);
  1489. add_goto(ep, merge);
  1490. set_activeblock(ep, bb_false);
  1491. src2 = linearize_expression(ep, expr_false);
  1492. phi2 = alloc_phi(ep->active, src2, expr->ctype);
  1493. set_activeblock(ep, merge);
  1494. return add_join_conditional(ep, expr, phi1, phi2);
  1495. }
  1496. static void insert_phis(struct basic_block *bb, pseudo_t src, struct symbol *ctype,
  1497. struct instruction *node)
  1498. {
  1499. struct basic_block *parent;
  1500. FOR_EACH_PTR(bb->parents, parent) {
  1501. struct instruction *br = delete_last_instruction(&parent->insns);
  1502. pseudo_t phi = alloc_phi(parent, src, ctype);
  1503. add_instruction(&parent->insns, br);
  1504. use_pseudo(node, phi, add_pseudo(&node->phi_list, phi));
  1505. } END_FOR_EACH_PTR(parent);
  1506. }
  1507. static pseudo_t linearize_logical(struct entrypoint *ep, struct expression *expr)
  1508. {
  1509. struct symbol *ctype = expr->ctype;
  1510. struct basic_block *other, *merge;
  1511. struct instruction *node;
  1512. pseudo_t src1, src2, phi2;
  1513. if (!ep->active || !expr->left || !expr->right)
  1514. return VOID;
  1515. other = alloc_basic_block(ep, expr->right->pos);
  1516. merge = alloc_basic_block(ep, expr->pos);
  1517. node = alloc_phi_node(merge, ctype, NULL);
  1518. // LHS and its shortcut
  1519. if (expr->op == SPECIAL_LOGICAL_OR) {
  1520. linearize_cond_branch(ep, expr->left, merge, other);
  1521. src1 = value_pseudo(1);
  1522. } else {
  1523. linearize_cond_branch(ep, expr->left, other, merge);
  1524. src1 = value_pseudo(0);
  1525. }
  1526. insert_phis(merge, src1, ctype, node);
  1527. // RHS
  1528. set_activeblock(ep, other);
  1529. src2 = linearize_expression_to_bool(ep, expr->right);
  1530. src2 = cast_pseudo(ep, src2, &bool_ctype, ctype);
  1531. phi2 = alloc_phi(ep->active, src2, ctype);
  1532. use_pseudo(node, phi2, add_pseudo(&node->phi_list, phi2));
  1533. // join
  1534. set_activeblock(ep, merge);
  1535. add_instruction(&merge->insns, node);
  1536. return node->target;
  1537. }
  1538. static pseudo_t linearize_compare(struct entrypoint *ep, struct expression *expr)
  1539. {
  1540. static const int cmpop[] = {
  1541. ['>'] = OP_SET_GT, ['<'] = OP_SET_LT,
  1542. [SPECIAL_EQUAL] = OP_SET_EQ,
  1543. [SPECIAL_NOTEQUAL] = OP_SET_NE,
  1544. [SPECIAL_GTE] = OP_SET_GE,
  1545. [SPECIAL_LTE] = OP_SET_LE,
  1546. [SPECIAL_UNSIGNED_LT] = OP_SET_B,
  1547. [SPECIAL_UNSIGNED_GT] = OP_SET_A,
  1548. [SPECIAL_UNSIGNED_LTE] = OP_SET_BE,
  1549. [SPECIAL_UNSIGNED_GTE] = OP_SET_AE,
  1550. };
  1551. int op = opcode_float(cmpop[expr->op], expr->right->ctype);
  1552. pseudo_t src1 = linearize_expression(ep, expr->left);
  1553. pseudo_t src2 = linearize_expression(ep, expr->right);
  1554. pseudo_t dst = add_binary_op(ep, expr->ctype, op, src1, src2);
  1555. return dst;
  1556. }
  1557. static pseudo_t linearize_cond_branch(struct entrypoint *ep, struct expression *expr, struct basic_block *bb_true, struct basic_block *bb_false)
  1558. {
  1559. pseudo_t cond;
  1560. if (!expr || !valid_type(expr->ctype) || !bb_reachable(ep->active))
  1561. return VOID;
  1562. switch (expr->type) {
  1563. case EXPR_STRING:
  1564. case EXPR_VALUE:
  1565. add_goto(ep, expr->value ? bb_true : bb_false);
  1566. return VOID;
  1567. case EXPR_FVALUE:
  1568. add_goto(ep, expr->fvalue ? bb_true : bb_false);
  1569. return VOID;
  1570. case EXPR_LOGICAL:
  1571. linearize_logical_branch(ep, expr, bb_true, bb_false);
  1572. return VOID;
  1573. case EXPR_COMPARE:
  1574. cond = linearize_compare(ep, expr);
  1575. add_branch(ep, cond, bb_true, bb_false);
  1576. break;
  1577. case EXPR_PREOP:
  1578. if (expr->op == '!')
  1579. return linearize_cond_branch(ep, expr->unop, bb_false, bb_true);
  1580. /* fall through */
  1581. default: {
  1582. cond = linearize_expression_to_bool(ep, expr);
  1583. add_branch(ep, cond, bb_true, bb_false);
  1584. return VOID;
  1585. }
  1586. }
  1587. return VOID;
  1588. }
  1589. static pseudo_t linearize_logical_branch(struct entrypoint *ep, struct expression *expr, struct basic_block *bb_true, struct basic_block *bb_false)
  1590. {
  1591. struct basic_block *next = alloc_basic_block(ep, expr->pos);
  1592. if (expr->op == SPECIAL_LOGICAL_OR)
  1593. linearize_cond_branch(ep, expr->left, bb_true, next);
  1594. else
  1595. linearize_cond_branch(ep, expr->left, next, bb_false);
  1596. set_activeblock(ep, next);
  1597. linearize_cond_branch(ep, expr->right, bb_true, bb_false);
  1598. return VOID;
  1599. }
  1600. static pseudo_t linearize_cast(struct entrypoint *ep, struct expression *expr)
  1601. {
  1602. pseudo_t src;
  1603. struct expression *orig = expr->cast_expression;
  1604. if (!orig)
  1605. return VOID;
  1606. src = linearize_expression(ep, orig);
  1607. return cast_pseudo(ep, src, orig->ctype, expr->ctype);
  1608. }
  1609. static pseudo_t linearize_initializer(struct entrypoint *ep, struct expression *initializer, struct access_data *ad)
  1610. {
  1611. switch (initializer->type) {
  1612. case EXPR_INITIALIZER: {
  1613. struct expression *expr;
  1614. FOR_EACH_PTR(initializer->expr_list, expr) {
  1615. linearize_initializer(ep, expr, ad);
  1616. } END_FOR_EACH_PTR(expr);
  1617. break;
  1618. }
  1619. case EXPR_POS:
  1620. ad->offset = initializer->init_offset;
  1621. linearize_initializer(ep, initializer->init_expr, ad);
  1622. break;
  1623. default: {
  1624. pseudo_t value = linearize_expression(ep, initializer);
  1625. ad->type = initializer->ctype;
  1626. linearize_store_gen(ep, value, ad);
  1627. return value;
  1628. }
  1629. }
  1630. return VOID;
  1631. }
  1632. static void linearize_argument(struct entrypoint *ep, struct symbol *arg, int nr)
  1633. {
  1634. struct access_data ad = { NULL, };
  1635. ad.type = arg;
  1636. ad.address = symbol_pseudo(ep, arg);
  1637. linearize_store_gen(ep, argument_pseudo(ep, nr), &ad);
  1638. }
  1639. static pseudo_t linearize_expression(struct entrypoint *ep, struct expression *expr)
  1640. {
  1641. if (!expr || !valid_type(expr->ctype))
  1642. return VOID;
  1643. current_pos = expr->pos;
  1644. switch (expr->type) {
  1645. case EXPR_SYMBOL:
  1646. linearize_one_symbol(ep, expr->symbol);
  1647. return add_symbol_address(ep, expr->symbol);
  1648. case EXPR_VALUE:
  1649. return value_pseudo(expr->value);
  1650. case EXPR_STRING:
  1651. case EXPR_LABEL:
  1652. return add_setval(ep, expr->ctype, expr);
  1653. case EXPR_FVALUE:
  1654. return add_setfval(ep, expr->ctype, expr->fvalue);
  1655. case EXPR_STATEMENT:
  1656. return linearize_statement(ep, expr->statement);
  1657. case EXPR_CALL:
  1658. return linearize_call_expression(ep, expr);
  1659. case EXPR_BINOP:
  1660. if (expr->op == SPECIAL_LOGICAL_AND || expr->op == SPECIAL_LOGICAL_OR)
  1661. return linearize_binop_bool(ep, expr);
  1662. return linearize_binop(ep, expr);
  1663. case EXPR_LOGICAL:
  1664. return linearize_logical(ep, expr);
  1665. case EXPR_COMPARE:
  1666. return linearize_compare(ep, expr);
  1667. case EXPR_SELECT:
  1668. return linearize_select(ep, expr);
  1669. case EXPR_CONDITIONAL:
  1670. if (!expr->cond_true)
  1671. return linearize_short_conditional(ep, expr, expr->conditional, expr->cond_false);
  1672. return linearize_conditional(ep, expr, expr->conditional,
  1673. expr->cond_true, expr->cond_false);
  1674. case EXPR_COMMA:
  1675. linearize_expression(ep, expr->left);
  1676. return linearize_expression(ep, expr->right);
  1677. case EXPR_ASSIGNMENT:
  1678. return linearize_assignment(ep, expr);
  1679. case EXPR_PREOP:
  1680. return linearize_preop(ep, expr);
  1681. case EXPR_POSTOP:
  1682. return linearize_postop(ep, expr);
  1683. case EXPR_CAST:
  1684. case EXPR_FORCE_CAST:
  1685. case EXPR_IMPLIED_CAST:
  1686. return linearize_cast(ep, expr);
  1687. case EXPR_SLICE:
  1688. return linearize_slice(ep, expr);
  1689. case EXPR_INITIALIZER:
  1690. case EXPR_POS:
  1691. warning(expr->pos, "unexpected initializer expression (%d %d)", expr->type, expr->op);
  1692. return VOID;
  1693. default:
  1694. warning(expr->pos, "unknown expression (%d %d)", expr->type, expr->op);
  1695. return VOID;
  1696. }
  1697. return VOID;
  1698. }
  1699. static pseudo_t linearize_one_symbol(struct entrypoint *ep, struct symbol *sym)
  1700. {
  1701. struct access_data ad = { NULL, };
  1702. pseudo_t value;
  1703. if (!sym || !sym->initializer || sym->initialized)
  1704. return VOID;
  1705. /* We need to output these puppies some day too.. */
  1706. if (sym->ctype.modifiers & (MOD_STATIC | MOD_TOPLEVEL))
  1707. return VOID;
  1708. sym->initialized = 1;
  1709. ad.address = symbol_pseudo(ep, sym);
  1710. if (sym->initializer && !is_scalar_type(sym)) {
  1711. // default zero initialization [6.7.9.21]
  1712. // FIXME: this init the whole aggregate while
  1713. // only the existing fields need to be initialized.
  1714. // FIXME: this init the whole aggregate even if
  1715. // all fields arelater explicitely initialized.
  1716. ad.type = sym;
  1717. ad.address = symbol_pseudo(ep, sym);
  1718. linearize_store_gen(ep, value_pseudo(0), &ad);
  1719. }
  1720. value = linearize_initializer(ep, sym->initializer, &ad);
  1721. return value;
  1722. }
  1723. static pseudo_t linearize_compound_statement(struct entrypoint *ep, struct statement *stmt)
  1724. {
  1725. pseudo_t pseudo;
  1726. struct statement *s;
  1727. pseudo = VOID;
  1728. FOR_EACH_PTR(stmt->stmts, s) {
  1729. pseudo = linearize_statement(ep, s);
  1730. } END_FOR_EACH_PTR(s);
  1731. return pseudo;
  1732. }
  1733. static void add_return(struct entrypoint *ep, struct basic_block *bb, struct symbol *ctype, pseudo_t src)
  1734. {
  1735. struct instruction *phi_node = first_instruction(bb->insns);
  1736. pseudo_t phi;
  1737. if (!phi_node) {
  1738. phi_node = alloc_typed_instruction(OP_PHI, ctype);
  1739. phi_node->target = alloc_pseudo(phi_node);
  1740. phi_node->bb = bb;
  1741. add_instruction(&bb->insns, phi_node);
  1742. }
  1743. phi = alloc_phi(ep->active, src, ctype);
  1744. phi->ident = &return_ident;
  1745. use_pseudo(phi_node, phi, add_pseudo(&phi_node->phi_list, phi));
  1746. }
  1747. static pseudo_t linearize_fn_statement(struct entrypoint *ep, struct statement *stmt)
  1748. {
  1749. struct instruction *phi_node;
  1750. struct basic_block *bb;
  1751. pseudo_t pseudo;
  1752. pseudo = linearize_compound_statement(ep, stmt);
  1753. if (!is_void_type(stmt->ret)) { // non-void function
  1754. struct basic_block *active = ep->active;
  1755. if (active && !bb_terminated(active)) { // missing return
  1756. struct basic_block *bb_ret;
  1757. bb_ret = get_bound_block(ep, stmt->ret);
  1758. add_return(ep, bb_ret, stmt->ret, undef_pseudo());
  1759. }
  1760. }
  1761. bb = add_label(ep, stmt->ret);
  1762. phi_node = first_instruction(bb->insns);
  1763. if (phi_node)
  1764. pseudo = phi_node->target;
  1765. return pseudo;
  1766. }
  1767. static pseudo_t linearize_inlined_call(struct entrypoint *ep, struct statement *stmt)
  1768. {
  1769. struct instruction *insn = alloc_instruction(OP_INLINED_CALL, 0);
  1770. struct statement *args = stmt->args;
  1771. struct basic_block *bb;
  1772. pseudo_t pseudo;
  1773. if (args) {
  1774. struct symbol *sym;
  1775. concat_symbol_list(args->declaration, &ep->syms);
  1776. FOR_EACH_PTR(args->declaration, sym) {
  1777. pseudo_t value = linearize_one_symbol(ep, sym);
  1778. add_pseudo(&insn->arguments, value);
  1779. } END_FOR_EACH_PTR(sym);
  1780. }
  1781. pseudo = linearize_fn_statement(ep, stmt);
  1782. insn->target = pseudo;
  1783. use_pseudo(insn, symbol_pseudo(ep, stmt->inline_fn), &insn->func);
  1784. bb = ep->active;
  1785. if (!bb->insns)
  1786. bb->pos = stmt->pos;
  1787. add_one_insn(ep, insn);
  1788. return pseudo;
  1789. }
  1790. static pseudo_t linearize_context(struct entrypoint *ep, struct statement *stmt)
  1791. {
  1792. struct instruction *insn = alloc_instruction(OP_CONTEXT, 0);
  1793. struct expression *expr = stmt->expression;
  1794. insn->increment = get_expression_value(expr);
  1795. insn->context_expr = stmt->context;
  1796. add_one_insn(ep, insn);
  1797. return VOID;
  1798. }
  1799. static pseudo_t linearize_range(struct entrypoint *ep, struct statement *stmt)
  1800. {
  1801. struct instruction *insn = alloc_instruction(OP_RANGE, 0);
  1802. use_pseudo(insn, linearize_expression(ep, stmt->range_expression), &insn->src1);
  1803. use_pseudo(insn, linearize_expression(ep, stmt->range_low), &insn->src2);
  1804. use_pseudo(insn, linearize_expression(ep, stmt->range_high), &insn->src3);
  1805. add_one_insn(ep, insn);
  1806. return VOID;
  1807. }
  1808. ALLOCATOR(asm_rules, "asm rules");
  1809. ALLOCATOR(asm_constraint, "asm constraints");
  1810. static void add_asm_input(struct entrypoint *ep, struct instruction *insn, struct asm_operand *op)
  1811. {
  1812. pseudo_t pseudo = linearize_expression(ep, op->expr);
  1813. struct asm_constraint *rule = __alloc_asm_constraint(0);
  1814. rule->ident = op->name;
  1815. rule->constraint = op->constraint ? op->constraint->string->data : "";
  1816. use_pseudo(insn, pseudo, &rule->pseudo);
  1817. add_ptr_list(&insn->asm_rules->inputs, rule);
  1818. }
  1819. static void add_asm_output(struct entrypoint *ep, struct instruction *insn, struct asm_operand *op)
  1820. {
  1821. struct access_data ad = { NULL, };
  1822. pseudo_t pseudo;
  1823. struct asm_constraint *rule;
  1824. if (op->is_memory) {
  1825. pseudo = linearize_expression(ep, op->expr);
  1826. } else {
  1827. if (!linearize_address_gen(ep, op->expr, &ad))
  1828. return;
  1829. pseudo = alloc_pseudo(insn);
  1830. linearize_store_gen(ep, pseudo, &ad);
  1831. }
  1832. rule = __alloc_asm_constraint(0);
  1833. rule->is_memory = op->is_memory;
  1834. rule->ident = op->name;
  1835. rule->constraint = op->constraint ? op->constraint->string->data : "";
  1836. use_pseudo(insn, pseudo, &rule->pseudo);
  1837. add_ptr_list(&insn->asm_rules->outputs, rule);
  1838. }
  1839. static pseudo_t linearize_asm_statement(struct entrypoint *ep, struct statement *stmt)
  1840. {
  1841. struct instruction *insn;
  1842. struct expression *expr;
  1843. struct asm_rules *rules;
  1844. struct asm_operand *op;
  1845. insn = alloc_instruction(OP_ASM, 0);
  1846. expr = stmt->asm_string;
  1847. if (!expr || expr->type != EXPR_STRING) {
  1848. warning(stmt->pos, "expected string in inline asm");
  1849. return VOID;
  1850. }
  1851. insn->string = expr->string->data;
  1852. rules = __alloc_asm_rules(0);
  1853. insn->asm_rules = rules;
  1854. /* Gather the inputs.. */
  1855. FOR_EACH_PTR(stmt->asm_inputs, op) {
  1856. add_asm_input(ep, insn, op);
  1857. } END_FOR_EACH_PTR(op);
  1858. add_one_insn(ep, insn);
  1859. /* Assign the outputs */
  1860. FOR_EACH_PTR(stmt->asm_outputs, op) {
  1861. add_asm_output(ep, insn, op);
  1862. } END_FOR_EACH_PTR(op);
  1863. return VOID;
  1864. }
  1865. static int multijmp_cmp(const void *_a, const void *_b)
  1866. {
  1867. const struct multijmp *a = _a;
  1868. const struct multijmp *b = _b;
  1869. // "default" case?
  1870. if (a->begin > a->end) {
  1871. if (b->begin > b->end)
  1872. return 0;
  1873. return 1;
  1874. }
  1875. if (b->begin > b->end)
  1876. return -1;
  1877. if (a->begin == b->begin) {
  1878. if (a->end == b->end)
  1879. return 0;
  1880. return (a->end < b->end) ? -1 : 1;
  1881. }
  1882. return a->begin < b->begin ? -1 : 1;
  1883. }
  1884. static void sort_switch_cases(struct instruction *insn)
  1885. {
  1886. sort_list((struct ptr_list **)&insn->multijmp_list, multijmp_cmp);
  1887. }
  1888. static pseudo_t linearize_declaration(struct entrypoint *ep, struct statement *stmt)
  1889. {
  1890. struct symbol *sym;
  1891. concat_symbol_list(stmt->declaration, &ep->syms);
  1892. FOR_EACH_PTR(stmt->declaration, sym) {
  1893. linearize_one_symbol(ep, sym);
  1894. } END_FOR_EACH_PTR(sym);
  1895. return VOID;
  1896. }
  1897. static pseudo_t linearize_return(struct entrypoint *ep, struct statement *stmt)
  1898. {
  1899. struct expression *expr = stmt->expression;
  1900. struct symbol *ret = stmt->ret_target;
  1901. struct basic_block *bb_return = get_bound_block(ep, ret);
  1902. struct basic_block *active;
  1903. pseudo_t src = linearize_expression(ep, expr);
  1904. active = ep->active;
  1905. if (active && !is_void_type(ret)) {
  1906. add_return(ep, bb_return, ret, src);
  1907. }
  1908. add_goto(ep, bb_return);
  1909. return VOID;
  1910. }
  1911. static pseudo_t linearize_switch(struct entrypoint *ep, struct statement *stmt)
  1912. {
  1913. struct symbol *sym;
  1914. struct instruction *switch_ins;
  1915. struct basic_block *switch_end = alloc_basic_block(ep, stmt->pos);
  1916. struct basic_block *active, *default_case;
  1917. struct expression *expr = stmt->switch_expression;
  1918. struct multijmp *jmp;
  1919. pseudo_t pseudo;
  1920. if (!expr || !expr->ctype)
  1921. return VOID;
  1922. pseudo = linearize_expression(ep, expr);
  1923. active = ep->active;
  1924. if (!active) {
  1925. active = alloc_basic_block(ep, stmt->pos);
  1926. set_activeblock(ep, active);
  1927. }
  1928. switch_ins = alloc_typed_instruction(OP_SWITCH, expr->ctype);
  1929. use_pseudo(switch_ins, pseudo, &switch_ins->cond);
  1930. add_one_insn(ep, switch_ins);
  1931. finish_block(ep);
  1932. default_case = NULL;
  1933. FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
  1934. struct statement *case_stmt = sym->stmt;
  1935. struct basic_block *bb_case = get_bound_block(ep, sym);
  1936. if (!case_stmt->case_expression) {
  1937. default_case = bb_case;
  1938. continue;
  1939. } else if (case_stmt->case_expression->type != EXPR_VALUE) {
  1940. continue;
  1941. } else {
  1942. struct expression *case_to = case_stmt->case_to;
  1943. long long begin, end;
  1944. begin = end = case_stmt->case_expression->value;
  1945. if (case_to && case_to->type == EXPR_VALUE)
  1946. end = case_to->value;
  1947. if (begin > end)
  1948. jmp = alloc_multijmp(bb_case, end, begin);
  1949. else
  1950. jmp = alloc_multijmp(bb_case, begin, end);
  1951. }
  1952. add_multijmp(&switch_ins->multijmp_list, jmp);
  1953. add_bb(&bb_case->parents, active);
  1954. add_bb(&active->children, bb_case);
  1955. } END_FOR_EACH_PTR(sym);
  1956. bind_label(stmt->switch_break, switch_end, stmt->pos);
  1957. /* And linearize the actual statement */
  1958. linearize_statement(ep, stmt->switch_statement);
  1959. set_activeblock(ep, switch_end);
  1960. if (!default_case)
  1961. default_case = switch_end;
  1962. jmp = alloc_multijmp(default_case, 1, 0);
  1963. add_multijmp(&switch_ins->multijmp_list, jmp);
  1964. add_bb(&default_case->parents, active);
  1965. add_bb(&active->children, default_case);
  1966. sort_switch_cases(switch_ins);
  1967. return VOID;
  1968. }
  1969. static pseudo_t linearize_iterator(struct entrypoint *ep, struct statement *stmt)
  1970. {
  1971. struct statement *pre_statement = stmt->iterator_pre_statement;
  1972. struct expression *pre_condition = stmt->iterator_pre_condition;
  1973. struct statement *statement = stmt->iterator_statement;
  1974. struct statement *post_statement = stmt->iterator_post_statement;
  1975. struct expression *post_condition = stmt->iterator_post_condition;
  1976. struct basic_block *loop_top, *loop_body, *loop_continue, *loop_end;
  1977. struct symbol *sym;
  1978. FOR_EACH_PTR(stmt->iterator_syms, sym) {
  1979. linearize_one_symbol(ep, sym);
  1980. } END_FOR_EACH_PTR(sym);
  1981. concat_symbol_list(stmt->iterator_syms, &ep->syms);
  1982. linearize_statement(ep, pre_statement);
  1983. loop_body = loop_top = alloc_basic_block(ep, stmt->pos);
  1984. loop_continue = alloc_basic_block(ep, stmt->pos);
  1985. loop_end = alloc_basic_block(ep, stmt->pos);
  1986. /* An empty post-condition means that it's the same as the pre-condition */
  1987. if (!post_condition) {
  1988. loop_top = alloc_basic_block(ep, stmt->pos);
  1989. set_activeblock(ep, loop_top);
  1990. }
  1991. if (pre_condition)
  1992. linearize_cond_branch(ep, pre_condition, loop_body, loop_end);
  1993. bind_label(stmt->iterator_continue, loop_continue, stmt->pos);
  1994. bind_label(stmt->iterator_break, loop_end, stmt->pos);
  1995. set_activeblock(ep, loop_body);
  1996. linearize_statement(ep, statement);
  1997. add_goto(ep, loop_continue);
  1998. set_activeblock(ep, loop_continue);
  1999. linearize_statement(ep, post_statement);
  2000. if (!post_condition)
  2001. add_goto(ep, loop_top);
  2002. else
  2003. linearize_cond_branch(ep, post_condition, loop_top, loop_end);
  2004. set_activeblock(ep, loop_end);
  2005. return VOID;
  2006. }
  2007. static pseudo_t linearize_statement(struct entrypoint *ep, struct statement *stmt)
  2008. {
  2009. struct basic_block *bb;
  2010. if (!stmt)
  2011. return VOID;
  2012. bb = ep->active;
  2013. if (bb && !bb->insns)
  2014. bb->pos = stmt->pos;
  2015. current_pos = stmt->pos;
  2016. switch (stmt->type) {
  2017. case STMT_NONE:
  2018. break;
  2019. case STMT_DECLARATION:
  2020. return linearize_declaration(ep, stmt);
  2021. case STMT_CONTEXT:
  2022. return linearize_context(ep, stmt);
  2023. case STMT_RANGE:
  2024. return linearize_range(ep, stmt);
  2025. case STMT_EXPRESSION:
  2026. return linearize_expression(ep, stmt->expression);
  2027. case STMT_ASM:
  2028. return linearize_asm_statement(ep, stmt);
  2029. case STMT_RETURN:
  2030. return linearize_return(ep, stmt);
  2031. case STMT_CASE: {
  2032. add_label(ep, stmt->case_label);
  2033. linearize_statement(ep, stmt->case_statement);
  2034. break;
  2035. }
  2036. case STMT_LABEL: {
  2037. struct symbol *label = stmt->label_identifier;
  2038. if (label->used) {
  2039. add_label(ep, label);
  2040. }
  2041. return linearize_statement(ep, stmt->label_statement);
  2042. }
  2043. case STMT_GOTO: {
  2044. struct symbol *sym;
  2045. struct expression *expr;
  2046. struct instruction *goto_ins;
  2047. struct basic_block *active;
  2048. pseudo_t pseudo;
  2049. active = ep->active;
  2050. if (!bb_reachable(active))
  2051. break;
  2052. if (stmt->goto_label) {
  2053. add_goto(ep, get_bound_block(ep, stmt->goto_label));
  2054. break;
  2055. }
  2056. expr = stmt->goto_expression;
  2057. if (!expr)
  2058. break;
  2059. /* This can happen as part of simplification */
  2060. if (expr->type == EXPR_LABEL) {
  2061. add_goto(ep, get_bound_block(ep, expr->label_symbol));
  2062. break;
  2063. }
  2064. pseudo = linearize_expression(ep, expr);
  2065. goto_ins = alloc_instruction(OP_COMPUTEDGOTO, 0);
  2066. use_pseudo(goto_ins, pseudo, &goto_ins->src);
  2067. add_one_insn(ep, goto_ins);
  2068. FOR_EACH_PTR(stmt->target_list, sym) {
  2069. struct basic_block *bb_computed = get_bound_block(ep, sym);
  2070. struct multijmp *jmp = alloc_multijmp(bb_computed, 1, 0);
  2071. add_multijmp(&goto_ins->multijmp_list, jmp);
  2072. add_bb(&bb_computed->parents, ep->active);
  2073. add_bb(&active->children, bb_computed);
  2074. } END_FOR_EACH_PTR(sym);
  2075. finish_block(ep);
  2076. break;
  2077. }
  2078. case STMT_COMPOUND:
  2079. if (stmt->inline_fn)
  2080. return linearize_inlined_call(ep, stmt);
  2081. return linearize_compound_statement(ep, stmt);
  2082. /*
  2083. * This could take 'likely/unlikely' into account, and
  2084. * switch the arms around appropriately..
  2085. */
  2086. case STMT_IF: {
  2087. struct basic_block *bb_true, *bb_false, *endif;
  2088. struct expression *cond = stmt->if_conditional;
  2089. bb_true = alloc_basic_block(ep, stmt->pos);
  2090. bb_false = endif = alloc_basic_block(ep, stmt->pos);
  2091. // If the condition is invalid, the following
  2092. // statement(s) are not evaluated.
  2093. if (!cond || !valid_type(cond->ctype))
  2094. return VOID;
  2095. linearize_cond_branch(ep, cond, bb_true, bb_false);
  2096. set_activeblock(ep, bb_true);
  2097. linearize_statement(ep, stmt->if_true);
  2098. if (stmt->if_false) {
  2099. endif = alloc_basic_block(ep, stmt->pos);
  2100. add_goto(ep, endif);
  2101. set_activeblock(ep, bb_false);
  2102. linearize_statement(ep, stmt->if_false);
  2103. }
  2104. set_activeblock(ep, endif);
  2105. break;
  2106. }
  2107. case STMT_SWITCH:
  2108. return linearize_switch(ep, stmt);
  2109. case STMT_ITERATOR:
  2110. return linearize_iterator(ep, stmt);
  2111. default:
  2112. break;
  2113. }
  2114. return VOID;
  2115. }
  2116. static void check_tainted_insn(struct instruction *insn)
  2117. {
  2118. unsigned long long uval;
  2119. long long sval;
  2120. pseudo_t src2;
  2121. switch (insn->opcode) {
  2122. case OP_DIVU: case OP_DIVS:
  2123. case OP_MODU: case OP_MODS:
  2124. if (insn->src2 == value_pseudo(0))
  2125. warning(insn->pos, "divide by zero");
  2126. break;
  2127. case OP_SHL: case OP_LSR: case OP_ASR:
  2128. src2 = insn->src2;
  2129. if (src2->type != PSEUDO_VAL)
  2130. break;
  2131. uval = src2->value;
  2132. if (uval < insn->size)
  2133. break;
  2134. sval = sign_extend(uval, insn->size);
  2135. if (Wshift_count_negative && sval < 0)
  2136. warning(insn->pos, "shift count is negative (%lld)", sval);
  2137. else if (Wshift_count_overflow)
  2138. warning(insn->pos, "shift too big (%llu) for type %s", uval, show_typename(insn->type));
  2139. }
  2140. }
  2141. ///
  2142. // issue warnings after all possible DCE
  2143. static void late_warnings(struct entrypoint *ep)
  2144. {
  2145. struct basic_block *bb;
  2146. FOR_EACH_PTR(ep->bbs, bb) {
  2147. struct instruction *insn;
  2148. FOR_EACH_PTR(bb->insns, insn) {
  2149. if (!insn->bb)
  2150. continue;
  2151. if (insn->tainted)
  2152. check_tainted_insn(insn);
  2153. } END_FOR_EACH_PTR(insn);
  2154. } END_FOR_EACH_PTR(bb);
  2155. }
  2156. static struct entrypoint *linearize_fn(struct symbol *sym, struct symbol *base_type)
  2157. {
  2158. struct statement *stmt = base_type->stmt;
  2159. struct entrypoint *ep;
  2160. struct basic_block *bb;
  2161. struct symbol *ret_type;
  2162. struct symbol *arg;
  2163. struct instruction *entry;
  2164. struct instruction *ret;
  2165. pseudo_t result;
  2166. int i;
  2167. if (!stmt || sym->bogus_linear)
  2168. return NULL;
  2169. ep = alloc_entrypoint();
  2170. ep->name = sym;
  2171. sym->ep = ep;
  2172. bb = alloc_basic_block(ep, sym->pos);
  2173. set_activeblock(ep, bb);
  2174. if (stmt->type == STMT_ASM) { // top-level asm
  2175. linearize_asm_statement(ep, stmt);
  2176. return ep;
  2177. }
  2178. entry = alloc_instruction(OP_ENTRY, 0);
  2179. add_one_insn(ep, entry);
  2180. ep->entry = entry;
  2181. concat_symbol_list(base_type->arguments, &ep->syms);
  2182. /* FIXME!! We should do something else about varargs.. */
  2183. i = 0;
  2184. FOR_EACH_PTR(base_type->arguments, arg) {
  2185. linearize_argument(ep, arg, ++i);
  2186. } END_FOR_EACH_PTR(arg);
  2187. result = linearize_fn_statement(ep, stmt);
  2188. ret_type = base_type->ctype.base_type;
  2189. ret = alloc_typed_instruction(OP_RET, ret_type);
  2190. if (type_size(ret_type) > 0)
  2191. use_pseudo(ret, result, &ret->src);
  2192. add_one_insn(ep, ret);
  2193. optimize(ep);
  2194. late_warnings(ep);
  2195. return ep;
  2196. }
  2197. struct entrypoint *linearize_symbol(struct symbol *sym)
  2198. {
  2199. struct symbol *base_type;
  2200. if (!sym)
  2201. return NULL;
  2202. current_pos = sym->pos;
  2203. base_type = sym->ctype.base_type;
  2204. if (!base_type)
  2205. return NULL;
  2206. if (base_type->type == SYM_FN)
  2207. return linearize_fn(sym, base_type);
  2208. return NULL;
  2209. }
  2210. /*
  2211. * Builtin functions
  2212. */
  2213. static pseudo_t linearize_fma(struct entrypoint *ep, struct expression *expr)
  2214. {
  2215. struct instruction *insn = alloc_typed_instruction(OP_FMADD, expr->ctype);
  2216. struct expression *arg;
  2217. PREPARE_PTR_LIST(expr->args, arg);
  2218. insn->src1 = linearize_expression(ep, arg);
  2219. NEXT_PTR_LIST(arg)
  2220. insn->src2 = linearize_expression(ep, arg);
  2221. NEXT_PTR_LIST(arg)
  2222. insn->src3 = linearize_expression(ep, arg);
  2223. FINISH_PTR_LIST(arg);
  2224. add_one_insn(ep, insn);
  2225. return insn->target = alloc_pseudo(insn);
  2226. }
  2227. static pseudo_t linearize_unreachable(struct entrypoint *ep, struct expression *exp)
  2228. {
  2229. add_unreachable(ep);
  2230. return VOID;
  2231. }
  2232. static struct sym_init {
  2233. const char *name;
  2234. pseudo_t (*linearize)(struct entrypoint *, struct expression*);
  2235. struct symbol_op op;
  2236. } builtins_table[] = {
  2237. // must be declared in builtin.c:declare_builtins[]
  2238. { "__builtin_fma", linearize_fma },
  2239. { "__builtin_fmaf", linearize_fma },
  2240. { "__builtin_fmal", linearize_fma },
  2241. { "__builtin_unreachable", linearize_unreachable },
  2242. { }
  2243. };
  2244. void init_linearized_builtins(int stream)
  2245. {
  2246. struct sym_init *ptr;
  2247. for (ptr = builtins_table; ptr->name; ptr++) {
  2248. struct symbol *sym;
  2249. sym = create_symbol(stream, ptr->name, SYM_NODE, NS_SYMBOL);
  2250. if (!sym->op)
  2251. sym->op = &ptr->op;
  2252. sym->op->type |= KW_BUILTIN;
  2253. ptr->op.linearize = ptr->linearize;
  2254. }
  2255. }