tilde_expr.cpp 112 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504
  1. gb_internal cgValue cg_flatten_value(cgProcedure *p, cgValue value) {
  2. GB_ASSERT(value.kind != cgValue_Multi);
  3. if (value.kind == cgValue_Symbol) {
  4. GB_ASSERT(is_type_internally_pointer_like(value.type));
  5. return cg_value(tb_inst_get_symbol_address(p->func, value.symbol), value.type);
  6. } else if (value.kind == cgValue_Addr) {
  7. // TODO(bill): Is this a good idea?
  8. // this converts an lvalue to an rvalue if trivially possible
  9. TB_DataType dt = cg_data_type(value.type);
  10. if (!TB_IS_VOID_TYPE(dt)) {
  11. TB_CharUnits align = cast(TB_CharUnits)type_align_of(value.type);
  12. return cg_value(tb_inst_load(p->func, dt, value.node, align, false), value.type);
  13. }
  14. }
  15. return value;
  16. }
  17. gb_internal cgValue cg_emit_select(cgProcedure *p, cgValue const &cond, cgValue const &x, cgValue const &y) {
  18. GB_ASSERT(x.kind == y.kind);
  19. GB_ASSERT(cond.kind == cgValue_Value);
  20. cgValue res = x;
  21. res.node = tb_inst_select(p->func, cond.node, x.node, y.node);
  22. return res;
  23. }
  24. gb_internal bool cg_is_expr_untyped_const(Ast *expr) {
  25. auto const &tv = type_and_value_of_expr(expr);
  26. if (is_type_untyped(tv.type)) {
  27. return tv.value.kind != ExactValue_Invalid;
  28. }
  29. return false;
  30. }
  31. gb_internal cgValue cg_expr_untyped_const_to_typed(cgProcedure *p, Ast *expr, Type *t) {
  32. GB_ASSERT(is_type_typed(t));
  33. auto const &tv = type_and_value_of_expr(expr);
  34. return cg_const_value(p, t, tv.value);
  35. }
  36. gb_internal cgContextData *cg_push_context_onto_stack(cgProcedure *p, cgAddr ctx) {
  37. ctx.kind = cgAddr_Context;
  38. cgContextData *cd = array_add_and_get(&p->context_stack);
  39. cd->ctx = ctx;
  40. cd->scope_index = p->scope_index;
  41. return cd;
  42. }
  43. gb_internal cgAddr cg_find_or_generate_context_ptr(cgProcedure *p) {
  44. if (p->context_stack.count > 0) {
  45. return p->context_stack[p->context_stack.count-1].ctx;
  46. }
  47. Type *pt = base_type(p->type);
  48. GB_ASSERT(pt->kind == Type_Proc);
  49. GB_ASSERT(pt->Proc.calling_convention != ProcCC_Odin);
  50. cgAddr c = cg_add_local(p, t_context, nullptr, true);
  51. tb_node_append_attrib(c.addr.node, tb_function_attrib_variable(p->func, -1, "context", cg_debug_type(p->module, t_context)));
  52. c.kind = cgAddr_Context;
  53. // lb_emit_init_context(p, c);
  54. cg_push_context_onto_stack(p, c);
  55. // lb_add_debug_context_variable(p, c);
  56. return c;
  57. }
  58. gb_internal cgValue cg_find_value_from_entity(cgModule *m, Entity *e) {
  59. e = strip_entity_wrapping(e);
  60. GB_ASSERT(e != nullptr);
  61. GB_ASSERT(e->token.string != "_");
  62. if (e->kind == Entity_Procedure) {
  63. return cg_find_procedure_value_from_entity(m, e);
  64. }
  65. cgValue *found = nullptr;
  66. rw_mutex_shared_lock(&m->values_mutex);
  67. found = map_get(&m->values, e);
  68. rw_mutex_shared_unlock(&m->values_mutex);
  69. if (found) {
  70. return *found;
  71. }
  72. GB_PANIC("\n\tError in: %s, missing value '%.*s'\n", token_pos_to_string(e->token.pos), LIT(e->token.string));
  73. return {};
  74. }
  75. gb_internal cgAddr cg_build_addr_from_entity(cgProcedure *p, Entity *e, Ast *expr) {
  76. GB_ASSERT(e != nullptr);
  77. if (e->kind == Entity_Constant) {
  78. Type *t = default_type(type_of_expr(expr));
  79. cgValue v = cg_const_value(p, t, e->Constant.value);
  80. GB_PANIC("TODO(bill): cg_add_global_generated");
  81. // return cg_add_global_generated(p->module, t, v);
  82. return {};
  83. }
  84. cgAddr *local_found = map_get(&p->variable_map, e);
  85. if (local_found) {
  86. return *local_found;
  87. }
  88. cgValue v = {};
  89. cgModule *m = p->module;
  90. rw_mutex_lock(&m->values_mutex);
  91. cgValue *found = map_get(&m->values, e);
  92. rw_mutex_unlock(&m->values_mutex);
  93. if (found) {
  94. v = *found;
  95. } else if (e->kind == Entity_Variable && e->flags & EntityFlag_Using) {
  96. GB_PANIC("TODO(bill): cg_get_using_variable");
  97. // NOTE(bill): Calculate the using variable every time
  98. // v = cg_get_using_variable(p, e);
  99. } else if (e->flags & EntityFlag_SoaPtrField) {
  100. GB_PANIC("TODO(bill): cg_get_soa_variable_addr");
  101. // return cg_get_soa_variable_addr(p, e);
  102. }
  103. if (v.node == nullptr) {
  104. cgValue v = cg_find_value_from_entity(m, e);
  105. v = cg_flatten_value(p, v);
  106. return cg_addr(v);
  107. }
  108. return cg_addr(v);
  109. }
  110. gb_internal cgValue cg_emit_union_tag_ptr(cgProcedure *p, cgValue const &parent_ptr) {
  111. Type *t = parent_ptr.type;
  112. Type *ut = base_type(type_deref(t));
  113. GB_ASSERT_MSG(is_type_pointer(t), "%s", type_to_string(t));
  114. GB_ASSERT_MSG(ut->kind == Type_Union, "%s", type_to_string(t));
  115. GB_ASSERT(!is_type_union_maybe_pointer_original_alignment(ut));
  116. GB_ASSERT(!is_type_union_maybe_pointer(ut));
  117. GB_ASSERT(type_size_of(ut) > 0);
  118. Type *tag_type = union_tag_type(ut);
  119. i64 tag_offset = ut->Union.variant_block_size;
  120. GB_ASSERT(parent_ptr.kind == cgValue_Value);
  121. TB_Node *ptr = parent_ptr.node;
  122. TB_Node *tag_ptr = tb_inst_member_access(p->func, ptr, tag_offset);
  123. return cg_value(tag_ptr, alloc_type_pointer(tag_type));
  124. }
  125. gb_internal cgValue cg_correct_endianness(cgProcedure *p, cgValue value) {
  126. Type *src = core_type(value.type);
  127. GB_ASSERT(is_type_integer(src) || is_type_float(src));
  128. if (is_type_different_to_arch_endianness(src)) {
  129. GB_PANIC("TODO(bill): cg_correct_endianness");
  130. // Type *platform_src_type = integer_endian_type_to_platform_type(src);
  131. // value = cg_emit_byte_swap(p, value, platform_src_type);
  132. }
  133. return value;
  134. }
  135. gb_internal cgValue cg_emit_transmute(cgProcedure *p, cgValue value, Type *type) {
  136. GB_ASSERT(type_size_of(value.type) == type_size_of(type));
  137. value = cg_flatten_value(p, value);
  138. if (are_types_identical(value.type, type)) {
  139. return value;
  140. }
  141. if (are_types_identical(core_type(value.type), core_type(type))) {
  142. value.type = type;
  143. return value;
  144. }
  145. i64 src_align = type_align_of(value.type);
  146. i64 dst_align = type_align_of(type);
  147. if (dst_align > src_align) {
  148. cgAddr local = cg_add_local(p, type, nullptr, false);
  149. cgValue dst = local.addr;
  150. dst.type = alloc_type_pointer(value.type);
  151. cg_emit_store(p, dst, value);
  152. return cg_addr_load(p, local);
  153. }
  154. TB_DataType dt = cg_data_type(type);
  155. switch (value.kind) {
  156. case cgValue_Value:
  157. GB_ASSERT_MSG(!TB_IS_VOID_TYPE(dt), "%d %s -> %s", dt.type, type_to_string(value.type), type_to_string(type));
  158. value.type = type;
  159. if (value.node->dt.raw != dt.raw) {
  160. value.node = tb_inst_bitcast(p->func, value.node, dt);
  161. }
  162. return value;
  163. case cgValue_Addr:
  164. value.type = type;
  165. return value;
  166. case cgValue_Symbol:
  167. GB_PANIC("should be handled above");
  168. break;
  169. case cgValue_Multi:
  170. GB_PANIC("cannot transmute multiple values at once");
  171. break;
  172. }
  173. return value;
  174. }
  175. gb_internal cgValue cg_emit_byte_swap(cgProcedure *p, cgValue value, Type *end_type) {
  176. GB_ASSERT(type_size_of(value.type) == type_size_of(end_type));
  177. if (type_size_of(value.type) < 2) {
  178. return value;
  179. }
  180. if (is_type_float(value.type)) {
  181. i64 sz = type_size_of(value.type);
  182. Type *integer_type = nullptr;
  183. switch (sz) {
  184. case 2: integer_type = t_u16; break;
  185. case 4: integer_type = t_u32; break;
  186. case 8: integer_type = t_u64; break;
  187. }
  188. GB_ASSERT(integer_type != nullptr);
  189. value = cg_emit_transmute(p, value, integer_type);
  190. }
  191. GB_ASSERT(value.kind == cgValue_Value);
  192. value.node = tb_inst_bswap(p->func, value.node);
  193. return cg_emit_transmute(p, value, end_type);
  194. }
  195. gb_internal cgValue cg_emit_comp(cgProcedure *p, TokenKind op_kind, cgValue left, cgValue right) {
  196. GB_ASSERT(gb_is_between(op_kind, Token__ComparisonBegin+1, Token__ComparisonEnd-1));
  197. Type *a = core_type(left.type);
  198. Type *b = core_type(right.type);
  199. cgValue nil_check = {};
  200. if (is_type_array_like(left.type) || is_type_array_like(right.type)) {
  201. // don't do `nil` check if it is array-like
  202. } else if (is_type_untyped_nil(left.type)) {
  203. nil_check = cg_emit_comp_against_nil(p, op_kind, right);
  204. } else if (is_type_untyped_nil(right.type)) {
  205. nil_check = cg_emit_comp_against_nil(p, op_kind, left);
  206. }
  207. if (nil_check.node != nullptr) {
  208. return nil_check;
  209. }
  210. if (are_types_identical(a, b)) {
  211. // NOTE(bill): No need for a conversion
  212. } /*else if (cg_is_const(left) || cg_is_const_nil(left)) {
  213. left = cg_emit_conv(p, left, right.type);
  214. } else if (cg_is_const(right) || cg_is_const_nil(right)) {
  215. right = cg_emit_conv(p, right, left.type);
  216. }*/ else {
  217. Type *lt = left.type;
  218. Type *rt = right.type;
  219. lt = left.type;
  220. rt = right.type;
  221. i64 ls = type_size_of(lt);
  222. i64 rs = type_size_of(rt);
  223. // NOTE(bill): Quick heuristic, larger types are usually the target type
  224. if (ls < rs) {
  225. left = cg_emit_conv(p, left, rt);
  226. } else if (ls > rs) {
  227. right = cg_emit_conv(p, right, lt);
  228. } else {
  229. if (is_type_union(rt)) {
  230. left = cg_emit_conv(p, left, rt);
  231. } else {
  232. right = cg_emit_conv(p, right, lt);
  233. }
  234. }
  235. }
  236. a = core_type(left.type);
  237. b = core_type(right.type);
  238. left = cg_flatten_value(p, left);
  239. right = cg_flatten_value(p, right);
  240. if (is_type_matrix(a) && (op_kind == Token_CmpEq || op_kind == Token_NotEq)) {
  241. GB_PANIC("TODO(bill): cg_emit_comp matrix");
  242. // Type *tl = base_type(a);
  243. // lbValue lhs = lb_address_from_load_or_generate_local(p, left);
  244. // lbValue rhs = lb_address_from_load_or_generate_local(p, right);
  245. // // TODO(bill): Test to see if this is actually faster!!!!
  246. // auto args = array_make<lbValue>(permanent_allocator(), 3);
  247. // args[0] = lb_emit_conv(p, lhs, t_rawptr);
  248. // args[1] = lb_emit_conv(p, rhs, t_rawptr);
  249. // args[2] = lb_const_int(p->module, t_int, type_size_of(tl));
  250. // lbValue val = lb_emit_runtime_call(p, "memory_compare", args);
  251. // lbValue res = lb_emit_comp(p, op_kind, val, lb_const_nil(p->module, val.type));
  252. // return lb_emit_conv(p, res, t_bool);
  253. }
  254. if (is_type_array_like(a)) {
  255. GB_PANIC("TODO(bill): cg_emit_comp is_type_array_like");
  256. // Type *tl = base_type(a);
  257. // lbValue lhs = lb_address_from_load_or_generate_local(p, left);
  258. // lbValue rhs = lb_address_from_load_or_generate_local(p, right);
  259. // TokenKind cmp_op = Token_And;
  260. // lbValue res = lb_const_bool(p->module, t_bool, true);
  261. // if (op_kind == Token_NotEq) {
  262. // res = lb_const_bool(p->module, t_bool, false);
  263. // cmp_op = Token_Or;
  264. // } else if (op_kind == Token_CmpEq) {
  265. // res = lb_const_bool(p->module, t_bool, true);
  266. // cmp_op = Token_And;
  267. // }
  268. // bool inline_array_arith = lb_can_try_to_inline_array_arith(tl);
  269. // i32 count = 0;
  270. // switch (tl->kind) {
  271. // case Type_Array: count = cast(i32)tl->Array.count; break;
  272. // case Type_EnumeratedArray: count = cast(i32)tl->EnumeratedArray.count; break;
  273. // }
  274. // if (inline_array_arith) {
  275. // // inline
  276. // lbAddr val = lb_add_local_generated(p, t_bool, false);
  277. // lb_addr_store(p, val, res);
  278. // for (i32 i = 0; i < count; i++) {
  279. // lbValue x = lb_emit_load(p, lb_emit_array_epi(p, lhs, i));
  280. // lbValue y = lb_emit_load(p, lb_emit_array_epi(p, rhs, i));
  281. // lbValue cmp = lb_emit_comp(p, op_kind, x, y);
  282. // lbValue new_res = lb_emit_arith(p, cmp_op, lb_addr_load(p, val), cmp, t_bool);
  283. // lb_addr_store(p, val, lb_emit_conv(p, new_res, t_bool));
  284. // }
  285. // return lb_addr_load(p, val);
  286. // } else {
  287. // if (is_type_simple_compare(tl) && (op_kind == Token_CmpEq || op_kind == Token_NotEq)) {
  288. // // TODO(bill): Test to see if this is actually faster!!!!
  289. // auto args = array_make<lbValue>(permanent_allocator(), 3);
  290. // args[0] = lb_emit_conv(p, lhs, t_rawptr);
  291. // args[1] = lb_emit_conv(p, rhs, t_rawptr);
  292. // args[2] = lb_const_int(p->module, t_int, type_size_of(tl));
  293. // lbValue val = lb_emit_runtime_call(p, "memory_compare", args);
  294. // lbValue res = lb_emit_comp(p, op_kind, val, lb_const_nil(p->module, val.type));
  295. // return lb_emit_conv(p, res, t_bool);
  296. // } else {
  297. // lbAddr val = lb_add_local_generated(p, t_bool, false);
  298. // lb_addr_store(p, val, res);
  299. // auto loop_data = lb_loop_start(p, count, t_i32);
  300. // {
  301. // lbValue i = loop_data.idx;
  302. // lbValue x = lb_emit_load(p, lb_emit_array_ep(p, lhs, i));
  303. // lbValue y = lb_emit_load(p, lb_emit_array_ep(p, rhs, i));
  304. // lbValue cmp = lb_emit_comp(p, op_kind, x, y);
  305. // lbValue new_res = lb_emit_arith(p, cmp_op, lb_addr_load(p, val), cmp, t_bool);
  306. // lb_addr_store(p, val, lb_emit_conv(p, new_res, t_bool));
  307. // }
  308. // lb_loop_end(p, loop_data);
  309. // return lb_addr_load(p, val);
  310. // }
  311. // }
  312. }
  313. if ((is_type_struct(a) || is_type_union(a)) && is_type_comparable(a)) {
  314. GB_PANIC("TODO(bill): cg_compare_records");
  315. // return cg_compare_records(p, op_kind, left, right, a);
  316. }
  317. if ((is_type_struct(b) || is_type_union(b)) && is_type_comparable(b)) {
  318. GB_PANIC("TODO(bill): cg_compare_records");
  319. // return cg_compare_records(p, op_kind, left, right, b);
  320. }
  321. if (is_type_string(a)) {
  322. if (is_type_cstring(a)) {
  323. left = cg_emit_conv(p, left, t_string);
  324. right = cg_emit_conv(p, right, t_string);
  325. }
  326. char const *runtime_procedure = nullptr;
  327. switch (op_kind) {
  328. case Token_CmpEq: runtime_procedure = "string_eq"; break;
  329. case Token_NotEq: runtime_procedure = "string_ne"; break;
  330. case Token_Lt: runtime_procedure = "string_lt"; break;
  331. case Token_Gt: runtime_procedure = "string_gt"; break;
  332. case Token_LtEq: runtime_procedure = "string_le"; break;
  333. case Token_GtEq: runtime_procedure = "string_gt"; break;
  334. }
  335. GB_ASSERT(runtime_procedure != nullptr);
  336. GB_PANIC("TODO(bill): cg_emit_runtime_call");
  337. // auto args = array_make<lbValue>(permanent_allocator(), 2);
  338. // args[0] = left;
  339. // args[1] = right;
  340. // return cg_emit_runtime_call(p, runtime_procedure, args);
  341. }
  342. if (is_type_complex(a)) {
  343. char const *runtime_procedure = "";
  344. i64 sz = 8*type_size_of(a);
  345. switch (sz) {
  346. case 32:
  347. switch (op_kind) {
  348. case Token_CmpEq: runtime_procedure = "complex32_eq"; break;
  349. case Token_NotEq: runtime_procedure = "complex32_ne"; break;
  350. }
  351. break;
  352. case 64:
  353. switch (op_kind) {
  354. case Token_CmpEq: runtime_procedure = "complex64_eq"; break;
  355. case Token_NotEq: runtime_procedure = "complex64_ne"; break;
  356. }
  357. break;
  358. case 128:
  359. switch (op_kind) {
  360. case Token_CmpEq: runtime_procedure = "complex128_eq"; break;
  361. case Token_NotEq: runtime_procedure = "complex128_ne"; break;
  362. }
  363. break;
  364. }
  365. GB_ASSERT(runtime_procedure != nullptr);
  366. GB_PANIC("TODO(bill): cg_emit_runtime_call");
  367. // auto args = array_make<lbValue>(permanent_allocator(), 2);
  368. // args[0] = left;
  369. // args[1] = right;
  370. // return lb_emit_runtime_call(p, runtime_procedure, args);
  371. }
  372. if (is_type_quaternion(a)) {
  373. char const *runtime_procedure = "";
  374. i64 sz = 8*type_size_of(a);
  375. switch (sz) {
  376. case 64:
  377. switch (op_kind) {
  378. case Token_CmpEq: runtime_procedure = "quaternion64_eq"; break;
  379. case Token_NotEq: runtime_procedure = "quaternion64_ne"; break;
  380. }
  381. break;
  382. case 128:
  383. switch (op_kind) {
  384. case Token_CmpEq: runtime_procedure = "quaternion128_eq"; break;
  385. case Token_NotEq: runtime_procedure = "quaternion128_ne"; break;
  386. }
  387. break;
  388. case 256:
  389. switch (op_kind) {
  390. case Token_CmpEq: runtime_procedure = "quaternion256_eq"; break;
  391. case Token_NotEq: runtime_procedure = "quaternion256_ne"; break;
  392. }
  393. break;
  394. }
  395. GB_ASSERT(runtime_procedure != nullptr);
  396. GB_PANIC("TODO(bill): cg_emit_runtime_call");
  397. // auto args = array_make<lbValue>(permanent_allocator(), 2);
  398. // args[0] = left;
  399. // args[1] = right;
  400. // return lb_emit_runtime_call(p, runtime_procedure, args);
  401. }
  402. if (is_type_bit_set(a)) {
  403. switch (op_kind) {
  404. case Token_Lt:
  405. case Token_LtEq:
  406. case Token_Gt:
  407. case Token_GtEq:
  408. {
  409. Type *it = bit_set_to_int(a);
  410. cgValue lhs = cg_emit_transmute(p, left, it);
  411. cgValue rhs = cg_emit_transmute(p, right, it);
  412. cgValue res = cg_emit_arith(p, Token_And, lhs, rhs, it);
  413. GB_ASSERT(lhs.kind == cgValue_Value);
  414. GB_ASSERT(rhs.kind == cgValue_Value);
  415. GB_ASSERT(res.kind == cgValue_Value);
  416. if (op_kind == Token_Lt || op_kind == Token_LtEq) {
  417. // (lhs & rhs) == lhs
  418. res = cg_value(tb_inst_cmp_eq(p->func, res.node, lhs.node), t_bool);
  419. } else if (op_kind == Token_Gt || op_kind == Token_GtEq) {
  420. // (lhs & rhs) == rhs
  421. res = cg_value(tb_inst_cmp_eq(p->func, res.node, rhs.node), t_bool);
  422. }
  423. // NOTE(bill): Strict subsets
  424. if (op_kind == Token_Lt || op_kind == Token_Gt) {
  425. // res &~ (lhs == rhs)
  426. cgValue eq = cg_value(tb_inst_cmp_eq(p->func, lhs.node, rhs.node), t_bool);
  427. res = cg_emit_arith(p, Token_AndNot, res, eq, t_bool);
  428. }
  429. return res;
  430. }
  431. case Token_CmpEq:
  432. GB_ASSERT(left.kind == cgValue_Value);
  433. GB_ASSERT(right.kind == cgValue_Value);
  434. return cg_value(tb_inst_cmp_eq(p->func, left.node, right.node), t_bool);
  435. case Token_NotEq:
  436. GB_ASSERT(left.kind == cgValue_Value);
  437. GB_ASSERT(right.kind == cgValue_Value);
  438. return cg_value(tb_inst_cmp_ne(p->func, left.node, right.node), t_bool);
  439. }
  440. }
  441. if (op_kind != Token_CmpEq && op_kind != Token_NotEq) {
  442. Type *t = left.type;
  443. if (is_type_integer(t) && is_type_different_to_arch_endianness(t)) {
  444. Type *platform_type = integer_endian_type_to_platform_type(t);
  445. cgValue x = cg_emit_byte_swap(p, left, platform_type);
  446. cgValue y = cg_emit_byte_swap(p, right, platform_type);
  447. left = x;
  448. right = y;
  449. } else if (is_type_float(t) && is_type_different_to_arch_endianness(t)) {
  450. Type *platform_type = integer_endian_type_to_platform_type(t);
  451. cgValue x = cg_emit_conv(p, left, platform_type);
  452. cgValue y = cg_emit_conv(p, right, platform_type);
  453. left = x;
  454. right = y;
  455. }
  456. }
  457. a = core_type(left.type);
  458. b = core_type(right.type);
  459. if (is_type_integer(a) ||
  460. is_type_boolean(a) ||
  461. is_type_pointer(a) ||
  462. is_type_multi_pointer(a) ||
  463. is_type_proc(a) ||
  464. is_type_enum(a) ||
  465. is_type_typeid(a)) {
  466. TB_Node *lhs = left.node;
  467. TB_Node *rhs = right.node;
  468. TB_Node *res = nullptr;
  469. bool is_signed = is_type_integer(left.type) && !is_type_unsigned(left.type);
  470. switch (op_kind) {
  471. case Token_CmpEq: res = tb_inst_cmp_eq(p->func, lhs, rhs); break;
  472. case Token_NotEq: res = tb_inst_cmp_ne(p->func, lhs, rhs); break;
  473. case Token_Gt: res = tb_inst_cmp_igt(p->func, lhs, rhs, is_signed); break;
  474. case Token_GtEq: res = tb_inst_cmp_ige(p->func, lhs, rhs, is_signed); break;
  475. case Token_Lt: res = tb_inst_cmp_ilt(p->func, lhs, rhs, is_signed); break;
  476. case Token_LtEq: res = tb_inst_cmp_ile(p->func, lhs, rhs, is_signed); break;
  477. }
  478. GB_ASSERT(res != nullptr);
  479. return cg_value(res, t_bool);
  480. } else if (is_type_float(a)) {
  481. TB_Node *lhs = left.node;
  482. TB_Node *rhs = right.node;
  483. TB_Node *res = nullptr;
  484. switch (op_kind) {
  485. case Token_CmpEq: res = tb_inst_cmp_eq(p->func, lhs, rhs); break;
  486. case Token_NotEq: res = tb_inst_cmp_ne(p->func, lhs, rhs); break;
  487. case Token_Gt: res = tb_inst_cmp_fgt(p->func, lhs, rhs); break;
  488. case Token_GtEq: res = tb_inst_cmp_fge(p->func, lhs, rhs); break;
  489. case Token_Lt: res = tb_inst_cmp_flt(p->func, lhs, rhs); break;
  490. case Token_LtEq: res = tb_inst_cmp_fle(p->func, lhs, rhs); break;
  491. }
  492. GB_ASSERT(res != nullptr);
  493. return cg_value(res, t_bool);
  494. } else if (is_type_simd_vector(a)) {
  495. GB_PANIC("TODO(bill): #simd vector");
  496. // LLVMValueRef mask = nullptr;
  497. // Type *elem = base_array_type(a);
  498. // if (is_type_float(elem)) {
  499. // LLVMRealPredicate pred = {};
  500. // switch (op_kind) {
  501. // case Token_CmpEq: pred = LLVMRealOEQ; break;
  502. // case Token_NotEq: pred = LLVMRealONE; break;
  503. // }
  504. // mask = LLVMBuildFCmp(p->builder, pred, left.value, right.value, "");
  505. // } else {
  506. // LLVMIntPredicate pred = {};
  507. // switch (op_kind) {
  508. // case Token_CmpEq: pred = LLVMIntEQ; break;
  509. // case Token_NotEq: pred = LLVMIntNE; break;
  510. // }
  511. // mask = LLVMBuildICmp(p->builder, pred, left.value, right.value, "");
  512. // }
  513. // GB_ASSERT_MSG(mask != nullptr, "Unhandled comparison kind %s (%s) %.*s %s (%s)", type_to_string(left.type), type_to_string(base_type(left.type)), LIT(token_strings[op_kind]), type_to_string(right.type), type_to_string(base_type(right.type)));
  514. // /* NOTE(bill, 2022-05-28):
  515. // Thanks to Per Vognsen, sign extending <N x i1> to
  516. // a vector of the same width as the input vector, bit casting to an integer,
  517. // and then comparing against zero is the better option
  518. // See: https://lists.llvm.org/pipermail/llvm-dev/2012-September/053046.html
  519. // // Example assuming 128-bit vector
  520. // %1 = <4 x float> ...
  521. // %2 = <4 x float> ...
  522. // %3 = fcmp oeq <4 x float> %1, %2
  523. // %4 = sext <4 x i1> %3 to <4 x i32>
  524. // %5 = bitcast <4 x i32> %4 to i128
  525. // %6 = icmp ne i128 %5, 0
  526. // br i1 %6, label %true1, label %false2
  527. // This will result in 1 cmpps + 1 ptest + 1 br
  528. // (even without SSE4.1, contrary to what the mail list states, because of pmovmskb)
  529. // */
  530. // unsigned count = cast(unsigned)get_array_type_count(a);
  531. // unsigned elem_sz = cast(unsigned)(type_size_of(elem)*8);
  532. // LLVMTypeRef mask_type = LLVMVectorType(LLVMIntTypeInContext(p->module->ctx, elem_sz), count);
  533. // mask = LLVMBuildSExtOrBitCast(p->builder, mask, mask_type, "");
  534. // LLVMTypeRef mask_int_type = LLVMIntTypeInContext(p->module->ctx, cast(unsigned)(8*type_size_of(a)));
  535. // LLVMValueRef mask_int = LLVMBuildBitCast(p->builder, mask, mask_int_type, "");
  536. // res.value = LLVMBuildICmp(p->builder, LLVMIntNE, mask_int, LLVMConstNull(LLVMTypeOf(mask_int)), "");
  537. // return res;
  538. }
  539. GB_PANIC("Unhandled comparison kind %s (%s) %.*s %s (%s)", type_to_string(left.type), type_to_string(base_type(left.type)), LIT(token_strings[op_kind]), type_to_string(right.type), type_to_string(base_type(right.type)));
  540. return {};
  541. }
  542. gb_internal cgValue cg_emit_comp_against_nil(cgProcedure *p, TokenKind op_kind, cgValue x) {
  543. GB_ASSERT(op_kind == Token_CmpEq || op_kind == Token_NotEq);
  544. x = cg_flatten_value(p, x);
  545. cgValue res = {};
  546. Type *t = x.type;
  547. TB_DataType dt = cg_data_type(t);
  548. Type *bt = base_type(t);
  549. TypeKind type_kind = bt->kind;
  550. switch (type_kind) {
  551. case Type_Basic:
  552. switch (bt->Basic.kind) {
  553. case Basic_rawptr:
  554. case Basic_cstring:
  555. GB_ASSERT(x.kind == cgValue_Value);
  556. if (op_kind == Token_CmpEq) {
  557. return cg_value(tb_inst_cmp_eq(p->func, x.node, tb_inst_uint(p->func, dt, 0)), t_bool);
  558. } else if (op_kind == Token_NotEq) {
  559. return cg_value(tb_inst_cmp_ne(p->func, x.node, tb_inst_uint(p->func, dt, 0)), t_bool);
  560. }
  561. break;
  562. case Basic_any:
  563. {
  564. GB_ASSERT(x.kind == cgValue_Addr);
  565. // // TODO(bill): is this correct behaviour for nil comparison for any?
  566. cgValue data = cg_emit_struct_ev(p, x, 0);
  567. cgValue id = cg_emit_struct_ev(p, x, 1);
  568. if (op_kind == Token_CmpEq) {
  569. TB_Node *a = tb_inst_cmp_eq(p->func, data.node, tb_inst_uint(p->func, data.node->dt, 0));
  570. TB_Node *b = tb_inst_cmp_eq(p->func, id.node, tb_inst_uint(p->func, id.node->dt, 0));
  571. TB_Node *c = tb_inst_or(p->func, a, b);
  572. return cg_value(c, t_bool);
  573. } else if (op_kind == Token_NotEq) {
  574. TB_Node *a = tb_inst_cmp_ne(p->func, data.node, tb_inst_uint(p->func, data.node->dt, 0));
  575. TB_Node *b = tb_inst_cmp_ne(p->func, id.node, tb_inst_uint(p->func, id.node->dt, 0));
  576. TB_Node *c = tb_inst_and(p->func, a, b);
  577. return cg_value(c, t_bool);
  578. }
  579. }
  580. break;
  581. case Basic_typeid:
  582. cgValue invalid_typeid = cg_const_value(p, t_typeid, exact_value_i64(0));
  583. return cg_emit_comp(p, op_kind, x, invalid_typeid);
  584. }
  585. break;
  586. case Type_Enum:
  587. case Type_Pointer:
  588. case Type_MultiPointer:
  589. case Type_Proc:
  590. case Type_BitSet:
  591. GB_ASSERT(x.kind == cgValue_Value);
  592. if (op_kind == Token_CmpEq) {
  593. return cg_value(tb_inst_cmp_eq(p->func, x.node, tb_inst_uint(p->func, dt, 0)), t_bool);
  594. } else if (op_kind == Token_NotEq) {
  595. return cg_value(tb_inst_cmp_ne(p->func, x.node, tb_inst_uint(p->func, dt, 0)), t_bool);
  596. }
  597. break;
  598. case Type_Slice:
  599. case Type_DynamicArray:
  600. case Type_Map:
  601. {
  602. // NOTE(bill): all of their data "pointer-like" fields are at the 0-index
  603. cgValue data = cg_emit_struct_ev(p, x, 0);
  604. if (op_kind == Token_CmpEq) {
  605. TB_Node *a = tb_inst_cmp_eq(p->func, data.node, tb_inst_uint(p->func, data.node->dt, 0));
  606. return cg_value(a, t_bool);
  607. } else if (op_kind == Token_NotEq) {
  608. TB_Node *a = tb_inst_cmp_ne(p->func, data.node, tb_inst_uint(p->func, data.node->dt, 0));
  609. return cg_value(a, t_bool);
  610. }
  611. }
  612. break;
  613. case Type_Union:
  614. {
  615. if (type_size_of(t) == 0) {
  616. return cg_const_bool(p, t_bool, op_kind == Token_CmpEq);
  617. } else if (is_type_union_maybe_pointer(t)) {
  618. cgValue tag = cg_emit_transmute(p, x, t_rawptr);
  619. return cg_emit_comp_against_nil(p, op_kind, tag);
  620. } else {
  621. GB_ASSERT("TODO(bill): cg_emit_union_tag_value");
  622. // cgValue tag = cg_emit_union_tag_value(p, x);
  623. // return cg_emit_comp(p, op_kind, tag, cg_zero(p->module, tag.type));
  624. }
  625. }
  626. break;
  627. case Type_Struct:
  628. GB_PANIC("TODO(bill): cg_emit_struct_ev");
  629. // if (is_type_soa_struct(t)) {
  630. // Type *bt = base_type(t);
  631. // if (bt->Struct.soa_kind == StructSoa_Slice) {
  632. // LLVMValueRef the_value = {};
  633. // if (bt->Struct.fields.count == 0) {
  634. // cgValue len = cg_soa_struct_len(p, x);
  635. // the_value = len.value;
  636. // } else {
  637. // cgValue first_field = cg_emit_struct_ev(p, x, 0);
  638. // the_value = first_field.value;
  639. // }
  640. // if (op_kind == Token_CmpEq) {
  641. // res.value = LLVMBuildIsNull(p->builder, the_value, "");
  642. // return res;
  643. // } else if (op_kind == Token_NotEq) {
  644. // res.value = LLVMBuildIsNotNull(p->builder, the_value, "");
  645. // return res;
  646. // }
  647. // } else if (bt->Struct.soa_kind == StructSoa_Dynamic) {
  648. // LLVMValueRef the_value = {};
  649. // if (bt->Struct.fields.count == 0) {
  650. // cgValue cap = cg_soa_struct_cap(p, x);
  651. // the_value = cap.value;
  652. // } else {
  653. // cgValue first_field = cg_emit_struct_ev(p, x, 0);
  654. // the_value = first_field.value;
  655. // }
  656. // if (op_kind == Token_CmpEq) {
  657. // res.value = LLVMBuildIsNull(p->builder, the_value, "");
  658. // return res;
  659. // } else if (op_kind == Token_NotEq) {
  660. // res.value = LLVMBuildIsNotNull(p->builder, the_value, "");
  661. // return res;
  662. // }
  663. // }
  664. // } else if (is_type_struct(t) && type_has_nil(t)) {
  665. // auto args = array_make<cgValue>(permanent_allocator(), 2);
  666. // cgValue lhs = cg_address_from_load_or_generate_local(p, x);
  667. // args[0] = cg_emit_conv(p, lhs, t_rawptr);
  668. // args[1] = cg_const_int(p->module, t_int, type_size_of(t));
  669. // cgValue val = cg_emit_runtime_call(p, "memory_compare_zero", args);
  670. // cgValue res = cg_emit_comp(p, op_kind, val, cg_const_int(p->module, t_int, 0));
  671. // return res;
  672. // }
  673. break;
  674. }
  675. GB_PANIC("Unknown handled type: %s -> %s", type_to_string(t), type_to_string(bt));
  676. return {};
  677. }
  678. gb_internal cgValue cg_emit_conv(cgProcedure *p, cgValue value, Type *t) {
  679. t = reduce_tuple_to_single_type(t);
  680. value = cg_flatten_value(p, value);
  681. Type *src_type = value.type;
  682. if (are_types_identical(t, src_type)) {
  683. return value;
  684. }
  685. if (is_type_untyped_uninit(src_type)) {
  686. // return cg_const_undef(m, t);
  687. return cg_const_nil(p, t);
  688. }
  689. if (is_type_untyped_nil(src_type)) {
  690. return cg_const_nil(p, t);
  691. }
  692. Type *src = core_type(src_type);
  693. Type *dst = core_type(t);
  694. GB_ASSERT(src != nullptr);
  695. GB_ASSERT(dst != nullptr);
  696. if (are_types_identical(src, dst)) {
  697. return cg_emit_transmute(p, value, t);
  698. }
  699. TB_DataType st = cg_data_type(src);
  700. if (value.kind == cgValue_Value && !TB_IS_VOID_TYPE(value.node->dt)) {
  701. st = value.node->dt;
  702. }
  703. TB_DataType dt = cg_data_type(t);
  704. if (is_type_integer(src) && is_type_integer(dst)) {
  705. GB_ASSERT(src->kind == Type_Basic &&
  706. dst->kind == Type_Basic);
  707. GB_ASSERT(value.kind == cgValue_Value);
  708. i64 sz = type_size_of(default_type(src));
  709. i64 dz = type_size_of(default_type(dst));
  710. if (sz == dz) {
  711. if (dz > 1 && !types_have_same_internal_endian(src, dst)) {
  712. return cg_emit_byte_swap(p, value, t);
  713. }
  714. value.type = t;
  715. return value;
  716. }
  717. if (sz > 1 && is_type_different_to_arch_endianness(src)) {
  718. Type *platform_src_type = integer_endian_type_to_platform_type(src);
  719. value = cg_emit_byte_swap(p, value, platform_src_type);
  720. }
  721. TB_Node* (*op)(TB_Function* f, TB_Node* src, TB_DataType dt) = tb_inst_trunc;
  722. if (dz < sz) {
  723. op = tb_inst_trunc;
  724. } else if (dz == sz) {
  725. op = tb_inst_bitcast;
  726. } else if (dz > sz) {
  727. op = is_type_unsigned(src) ? tb_inst_zxt : tb_inst_sxt; // zero extent
  728. }
  729. if (dz > 1 && is_type_different_to_arch_endianness(dst)) {
  730. Type *platform_dst_type = integer_endian_type_to_platform_type(dst);
  731. cgValue res = cg_value(op(p->func, value.node, cg_data_type(platform_dst_type)), platform_dst_type);
  732. return cg_emit_byte_swap(p, res, t);
  733. } else {
  734. return cg_value(op(p->func, value.node, dt), t);
  735. }
  736. }
  737. // boolean -> boolean/integer
  738. if (is_type_boolean(src) && (is_type_boolean(dst) || is_type_integer(dst))) {
  739. TB_Node *v = tb_inst_cmp_ne(p->func, value.node, tb_inst_uint(p->func, st, 0));
  740. return cg_value(tb_inst_zxt(p->func, v, dt), t);
  741. }
  742. // integer -> boolean
  743. if (is_type_integer(src) && is_type_boolean(dst)) {
  744. TB_Node *v = tb_inst_cmp_ne(p->func, value.node, tb_inst_uint(p->func, st, 0));
  745. return cg_value(tb_inst_zxt(p->func, v, dt), t);
  746. }
  747. if (is_type_cstring(src) && is_type_u8_ptr(dst)) {
  748. return cg_emit_transmute(p, value, dst);
  749. }
  750. if (is_type_u8_ptr(src) && is_type_cstring(dst)) {
  751. return cg_emit_transmute(p, value, dst);
  752. }
  753. if (is_type_cstring(src) && is_type_u8_multi_ptr(dst)) {
  754. return cg_emit_transmute(p, value, dst);
  755. }
  756. if (is_type_u8_multi_ptr(src) && is_type_cstring(dst)) {
  757. return cg_emit_transmute(p, value, dst);
  758. }
  759. if (is_type_cstring(src) && is_type_rawptr(dst)) {
  760. return cg_emit_transmute(p, value, dst);
  761. }
  762. if (is_type_rawptr(src) && is_type_cstring(dst)) {
  763. return cg_emit_transmute(p, value, dst);
  764. }
  765. if (are_types_identical(src, t_cstring) && are_types_identical(dst, t_string)) {
  766. GB_PANIC("TODO(bill): cstring_to_string call");
  767. // TEMPORARY_ALLOCATOR_GUARD();
  768. // lbValue c = lb_emit_conv(p, value, t_cstring);
  769. // auto args = array_make<lbValue>(temporary_allocator(), 1);
  770. // args[0] = c;
  771. // lbValue s = lb_emit_runtime_call(p, "cstring_to_string", args);
  772. // return lb_emit_conv(p, s, dst);
  773. }
  774. // float -> float
  775. if (is_type_float(src) && is_type_float(dst)) {
  776. i64 sz = type_size_of(src);
  777. i64 dz = type_size_of(dst);
  778. if (sz == 2 || dz == 2) {
  779. GB_PANIC("TODO(bill): f16 conversions");
  780. }
  781. if (dz == sz) {
  782. if (types_have_same_internal_endian(src, dst)) {
  783. return cg_value(value.node, t);
  784. } else {
  785. return cg_emit_byte_swap(p, value, t);
  786. }
  787. }
  788. if (is_type_different_to_arch_endianness(src) || is_type_different_to_arch_endianness(dst)) {
  789. Type *platform_src_type = integer_endian_type_to_platform_type(src);
  790. Type *platform_dst_type = integer_endian_type_to_platform_type(dst);
  791. cgValue res = {};
  792. res = cg_emit_conv(p, value, platform_src_type);
  793. res = cg_emit_conv(p, res, platform_dst_type);
  794. if (is_type_different_to_arch_endianness(dst)) {
  795. res = cg_emit_byte_swap(p, res, t);
  796. }
  797. return cg_emit_conv(p, res, t);
  798. }
  799. if (dz >= sz) {
  800. return cg_value(tb_inst_fpxt(p->func, value.node, dt), t);
  801. }
  802. return cg_value(tb_inst_trunc(p->func, value.node, dt), t);
  803. }
  804. if (is_type_complex(src) && is_type_complex(dst)) {
  805. GB_PANIC("TODO(bill): complex -> complex");
  806. }
  807. if (is_type_quaternion(src) && is_type_quaternion(dst)) {
  808. // @QuaternionLayout
  809. GB_PANIC("TODO(bill): quaternion -> quaternion");
  810. }
  811. if (is_type_integer(src) && is_type_complex(dst)) {
  812. GB_PANIC("TODO(bill): int -> complex");
  813. }
  814. if (is_type_float(src) && is_type_complex(dst)) {
  815. GB_PANIC("TODO(bill): float -> complex");
  816. }
  817. if (is_type_integer(src) && is_type_quaternion(dst)) {
  818. GB_PANIC("TODO(bill): int -> quaternion");
  819. }
  820. if (is_type_float(src) && is_type_quaternion(dst)) {
  821. GB_PANIC("TODO(bill): float -> quaternion");
  822. }
  823. if (is_type_complex(src) && is_type_quaternion(dst)) {
  824. GB_PANIC("TODO(bill): complex -> quaternion");
  825. }
  826. // float <-> integer
  827. if (is_type_float(src) && is_type_integer(dst)) {
  828. if (is_type_different_to_arch_endianness(src) || is_type_different_to_arch_endianness(dst)) {
  829. Type *platform_src_type = integer_endian_type_to_platform_type(src);
  830. Type *platform_dst_type = integer_endian_type_to_platform_type(dst);
  831. cgValue res = {};
  832. res = cg_emit_conv(p, value, platform_src_type);
  833. res = cg_emit_conv(p, res, platform_dst_type);
  834. return cg_emit_conv(p, res, t);
  835. }
  836. // if (is_type_integer_128bit(dst)) {
  837. // TEMPORARY_ALLOCATOR_GUARD();
  838. // auto args = array_make<lbValue>(temporary_allocator(), 1);
  839. // args[0] = value;
  840. // char const *call = "fixunsdfdi";
  841. // if (is_type_unsigned(dst)) {
  842. // call = "fixunsdfti";
  843. // }
  844. // lbValue res_i128 = lb_emit_runtime_call(p, call, args);
  845. // return lb_emit_conv(p, res_i128, t);
  846. // }
  847. bool is_signed = !is_type_unsigned(dst);
  848. return cg_value(tb_inst_float2int(p->func, value.node, dt, is_signed), t);
  849. }
  850. if (is_type_integer(src) && is_type_float(dst)) {
  851. if (is_type_different_to_arch_endianness(src) || is_type_different_to_arch_endianness(dst)) {
  852. Type *platform_src_type = integer_endian_type_to_platform_type(src);
  853. Type *platform_dst_type = integer_endian_type_to_platform_type(dst);
  854. cgValue res = {};
  855. res = cg_emit_conv(p, value, platform_src_type);
  856. res = cg_emit_conv(p, res, platform_dst_type);
  857. if (is_type_different_to_arch_endianness(dst)) {
  858. res = cg_emit_byte_swap(p, res, t);
  859. }
  860. return cg_emit_conv(p, res, t);
  861. }
  862. // if (is_type_integer_128bit(src)) {
  863. // TEMPORARY_ALLOCATOR_GUARD();
  864. // auto args = array_make<lbValue>(temporary_allocator(), 1);
  865. // args[0] = value;
  866. // char const *call = "floattidf";
  867. // if (is_type_unsigned(src)) {
  868. // call = "floattidf_unsigned";
  869. // }
  870. // lbValue res_f64 = lb_emit_runtime_call(p, call, args);
  871. // return lb_emit_conv(p, res_f64, t);
  872. // }
  873. bool is_signed = !is_type_unsigned(dst);
  874. return cg_value(tb_inst_int2float(p->func, value.node, dt, is_signed), t);
  875. }
  876. if (is_type_simd_vector(dst)) {
  877. GB_PANIC("TODO(bill): ? -> #simd vector");
  878. }
  879. // Pointer <-> uintptr
  880. if (is_type_pointer(src) && is_type_uintptr(dst)) {
  881. return cg_value(tb_inst_ptr2int(p->func, value.node, dt), t);
  882. }
  883. if (is_type_uintptr(src) && is_type_pointer(dst)) {
  884. return cg_value(tb_inst_int2ptr(p->func, value.node), t);
  885. }
  886. if (is_type_multi_pointer(src) && is_type_uintptr(dst)) {
  887. return cg_value(tb_inst_ptr2int(p->func, value.node, dt), t);
  888. }
  889. if (is_type_uintptr(src) && is_type_multi_pointer(dst)) {
  890. return cg_value(tb_inst_int2ptr(p->func, value.node), t);
  891. }
  892. if (is_type_union(dst)) {
  893. GB_PANIC("TODO(bill): ? -> union");
  894. }
  895. // NOTE(bill): This has to be done before 'Pointer <-> Pointer' as it's
  896. // subtype polymorphism casting
  897. if (check_is_assignable_to_using_subtype(src_type, t)) {
  898. GB_PANIC("TODO(bill): ? -> subtyping");
  899. }
  900. // Pointer <-> Pointer
  901. if (is_type_pointer(src) && is_type_pointer(dst)) {
  902. return cg_value(value.node, t);
  903. }
  904. if (is_type_multi_pointer(src) && is_type_pointer(dst)) {
  905. return cg_value(value.node, t);
  906. }
  907. if (is_type_pointer(src) && is_type_multi_pointer(dst)) {
  908. return cg_value(value.node, t);
  909. }
  910. if (is_type_multi_pointer(src) && is_type_multi_pointer(dst)) {
  911. return cg_value(value.node, t);
  912. }
  913. // proc <-> proc
  914. if (is_type_proc(src) && is_type_proc(dst)) {
  915. return cg_value(value.node, t);
  916. }
  917. // pointer -> proc
  918. if (is_type_pointer(src) && is_type_proc(dst)) {
  919. return cg_value(value.node, t);
  920. }
  921. // proc -> pointer
  922. if (is_type_proc(src) && is_type_pointer(dst)) {
  923. return cg_value(value.node, t);
  924. }
  925. // []byte/[]u8 <-> string
  926. if (is_type_u8_slice(src) && is_type_string(dst)) {
  927. return cg_emit_transmute(p, value, t);
  928. }
  929. if (is_type_string(src) && is_type_u8_slice(dst)) {
  930. return cg_emit_transmute(p, value, t);
  931. }
  932. if (is_type_matrix(dst) && !is_type_matrix(src)) {
  933. GB_PANIC("TODO(bill): !matrix -> matrix");
  934. }
  935. if (is_type_matrix(dst) && is_type_matrix(src)) {
  936. GB_PANIC("TODO(bill): matrix -> matrix");
  937. }
  938. if (is_type_any(dst)) {
  939. GB_PANIC("TODO(bill): ? -> any");
  940. }
  941. i64 src_sz = type_size_of(src);
  942. i64 dst_sz = type_size_of(dst);
  943. if (src_sz == dst_sz) {
  944. // bit_set <-> integer
  945. if (is_type_integer(src) && is_type_bit_set(dst)) {
  946. cgValue v = cg_emit_conv(p, value, bit_set_to_int(dst));
  947. return cg_emit_transmute(p, v, t);
  948. }
  949. if (is_type_bit_set(src) && is_type_integer(dst)) {
  950. cgValue bs = cg_emit_transmute(p, value, bit_set_to_int(src));
  951. return cg_emit_conv(p, bs, dst);
  952. }
  953. // typeid <-> integer
  954. if (is_type_integer(src) && is_type_typeid(dst)) {
  955. return cg_emit_transmute(p, value, dst);
  956. }
  957. if (is_type_typeid(src) && is_type_integer(dst)) {
  958. return cg_emit_transmute(p, value, dst);
  959. }
  960. }
  961. if (is_type_untyped(src)) {
  962. if (is_type_string(src) && is_type_string(dst)) {
  963. cgAddr result = cg_add_local(p, t, nullptr, false);
  964. cg_addr_store(p, result, value);
  965. return cg_addr_load(p, result);
  966. }
  967. }
  968. gb_printf_err("%.*s\n", LIT(p->name));
  969. gb_printf_err("cg_emit_conv: src -> dst\n");
  970. gb_printf_err("Not Identical %s != %s\n", type_to_string(src_type), type_to_string(t));
  971. gb_printf_err("Not Identical %s != %s\n", type_to_string(src), type_to_string(dst));
  972. gb_printf_err("Not Identical %p != %p\n", src_type, t);
  973. gb_printf_err("Not Identical %p != %p\n", src, dst);
  974. GB_PANIC("Invalid type conversion: '%s' to '%s' for procedure '%.*s'",
  975. type_to_string(src_type), type_to_string(t),
  976. LIT(p->name));
  977. return {};
  978. }
  979. gb_internal cgValue cg_emit_arith(cgProcedure *p, TokenKind op, cgValue lhs, cgValue rhs, Type *type) {
  980. if (is_type_array_like(lhs.type) || is_type_array_like(rhs.type)) {
  981. GB_PANIC("TODO(bill): cg_emit_arith_array");
  982. } else if (is_type_matrix(lhs.type) || is_type_matrix(rhs.type)) {
  983. GB_PANIC("TODO(bill): cg_emit_arith_matrix");
  984. } else if (is_type_complex(type)) {
  985. GB_PANIC("TODO(bill): cg_emit_arith complex");
  986. } else if (is_type_quaternion(type)) {
  987. GB_PANIC("TODO(bill): cg_emit_arith quaternion");
  988. }
  989. lhs = cg_flatten_value(p, cg_emit_conv(p, lhs, type));
  990. rhs = cg_flatten_value(p, cg_emit_conv(p, rhs, type));
  991. GB_ASSERT(lhs.kind == cgValue_Value);
  992. GB_ASSERT(rhs.kind == cgValue_Value);
  993. if (is_type_integer(type) && is_type_different_to_arch_endianness(type)) {
  994. switch (op) {
  995. case Token_AndNot:
  996. case Token_And:
  997. case Token_Or:
  998. case Token_Xor:
  999. goto handle_op;
  1000. }
  1001. Type *platform_type = integer_endian_type_to_platform_type(type);
  1002. cgValue x = cg_emit_byte_swap(p, lhs, integer_endian_type_to_platform_type(lhs.type));
  1003. cgValue y = cg_emit_byte_swap(p, rhs, integer_endian_type_to_platform_type(rhs.type));
  1004. cgValue res = cg_emit_arith(p, op, x, y, platform_type);
  1005. return cg_emit_byte_swap(p, res, type);
  1006. }
  1007. if (is_type_float(type) && is_type_different_to_arch_endianness(type)) {
  1008. Type *platform_type = integer_endian_type_to_platform_type(type);
  1009. cgValue x = cg_emit_conv(p, lhs, integer_endian_type_to_platform_type(lhs.type));
  1010. cgValue y = cg_emit_conv(p, rhs, integer_endian_type_to_platform_type(rhs.type));
  1011. cgValue res = cg_emit_arith(p, op, x, y, platform_type);
  1012. return cg_emit_byte_swap(p, res, type);
  1013. }
  1014. handle_op:;
  1015. // NOTE(bill): Bit Set Aliases for + and -
  1016. if (is_type_bit_set(type)) {
  1017. switch (op) {
  1018. case Token_Add: op = Token_Or; break;
  1019. case Token_Sub: op = Token_AndNot; break;
  1020. }
  1021. }
  1022. TB_ArithmeticBehavior arith_behavior = cast(TB_ArithmeticBehavior)0;
  1023. Type *integral_type = type;
  1024. if (is_type_simd_vector(integral_type)) {
  1025. GB_PANIC("TODO(bill): cg_emit_arith #simd vector");
  1026. // integral_type = core_array_type(integral_type);
  1027. }
  1028. switch (op) {
  1029. case Token_Add:
  1030. if (is_type_float(integral_type)) {
  1031. return cg_value(tb_inst_fadd(p->func, lhs.node, rhs.node), type);
  1032. }
  1033. return cg_value(tb_inst_add(p->func, lhs.node, rhs.node, arith_behavior), type);
  1034. case Token_Sub:
  1035. if (is_type_float(integral_type)) {
  1036. return cg_value(tb_inst_fsub(p->func, lhs.node, rhs.node), type);
  1037. }
  1038. return cg_value(tb_inst_sub(p->func, lhs.node, rhs.node, arith_behavior), type);
  1039. case Token_Mul:
  1040. if (is_type_float(integral_type)) {
  1041. return cg_value(tb_inst_fmul(p->func, lhs.node, rhs.node), type);
  1042. }
  1043. return cg_value(tb_inst_mul(p->func, lhs.node, rhs.node, arith_behavior), type);
  1044. case Token_Quo:
  1045. if (is_type_float(integral_type)) {
  1046. return cg_value(tb_inst_fdiv(p->func, lhs.node, rhs.node), type);
  1047. }
  1048. return cg_value(tb_inst_div(p->func, lhs.node, rhs.node, !is_type_unsigned(integral_type)), type);
  1049. case Token_Mod:
  1050. if (is_type_float(integral_type)) {
  1051. GB_PANIC("TODO(bill): float %% float");
  1052. }
  1053. return cg_value(tb_inst_mod(p->func, lhs.node, rhs.node, !is_type_unsigned(integral_type)), type);
  1054. case Token_ModMod:
  1055. if (is_type_unsigned(integral_type)) {
  1056. return cg_value(tb_inst_mod(p->func, lhs.node, rhs.node, false), type);
  1057. } else {
  1058. TB_Node *a = tb_inst_mod(p->func, lhs.node, rhs.node, true);
  1059. TB_Node *b = tb_inst_add(p->func, a, rhs.node, arith_behavior);
  1060. TB_Node *c = tb_inst_mod(p->func, b, rhs.node, true);
  1061. return cg_value(c, type);
  1062. }
  1063. case Token_And:
  1064. return cg_value(tb_inst_and(p->func, lhs.node, rhs.node), type);
  1065. case Token_Or:
  1066. return cg_value(tb_inst_or(p->func, lhs.node, rhs.node), type);
  1067. case Token_Xor:
  1068. return cg_value(tb_inst_xor(p->func, lhs.node, rhs.node), type);
  1069. case Token_Shl:
  1070. {
  1071. rhs = cg_emit_conv(p, rhs, lhs.type);
  1072. TB_DataType dt = cg_data_type(lhs.type);
  1073. TB_Node *lhsval = lhs.node;
  1074. TB_Node *bits = rhs.node;
  1075. TB_Node *bit_size = tb_inst_uint(p->func, dt, 8*type_size_of(lhs.type));
  1076. TB_Node *zero = tb_inst_uint(p->func, dt, 0);
  1077. TB_Node *width_test = tb_inst_cmp_ilt(p->func, bits, bit_size, false);
  1078. TB_Node *res = tb_inst_shl(p->func, lhsval, bits, arith_behavior);
  1079. res = tb_inst_select(p->func, width_test, res, zero);
  1080. return cg_value(res, type);
  1081. }
  1082. case Token_Shr:
  1083. {
  1084. rhs = cg_emit_conv(p, rhs, lhs.type);
  1085. TB_DataType dt = cg_data_type(lhs.type);
  1086. TB_Node *lhsval = lhs.node;
  1087. TB_Node *bits = rhs.node;
  1088. TB_Node *bit_size = tb_inst_uint(p->func, dt, 8*type_size_of(lhs.type));
  1089. TB_Node *zero = tb_inst_uint(p->func, dt, 0);
  1090. TB_Node *width_test = tb_inst_cmp_ilt(p->func, bits, bit_size, false);
  1091. TB_Node *res = nullptr;
  1092. if (is_type_unsigned(integral_type)) {
  1093. res = tb_inst_shr(p->func, lhsval, bits);
  1094. } else {
  1095. res = tb_inst_sar(p->func, lhsval, bits);
  1096. }
  1097. res = tb_inst_select(p->func, width_test, res, zero);
  1098. return cg_value(res, type);
  1099. }
  1100. case Token_AndNot:
  1101. return cg_value(tb_inst_and(p->func, lhs.node, tb_inst_not(p->func, rhs.node)), type);
  1102. }
  1103. GB_PANIC("unhandled operator of cg_emit_arith");
  1104. return {};
  1105. }
  1106. gb_internal void cg_fill_slice(cgProcedure *p, cgAddr const &slice, cgValue data, cgValue len) {
  1107. cgValue slice_ptr = cg_addr_get_ptr(p, slice);
  1108. cgValue data_ptr = cg_emit_struct_ep(p, slice_ptr, 0);
  1109. cgValue len_ptr = cg_emit_struct_ep(p, slice_ptr, 1);
  1110. data = cg_emit_conv(p, data, type_deref(data_ptr.type));
  1111. len = cg_emit_conv(p, len, t_int);
  1112. cg_emit_store(p, data_ptr, data);
  1113. cg_emit_store(p, len_ptr, len);
  1114. }
  1115. gb_internal cgAddr cg_build_addr_slice_expr(cgProcedure *p, Ast *expr) {
  1116. ast_node(se, SliceExpr, expr);
  1117. cgValue low = cg_const_int(p, t_int, 0);
  1118. cgValue high = {};
  1119. if (se->low != nullptr) {
  1120. low = cg_correct_endianness(p, cg_build_expr(p, se->low));
  1121. }
  1122. if (se->high != nullptr) {
  1123. high = cg_correct_endianness(p, cg_build_expr(p, se->high));
  1124. }
  1125. bool no_indices = se->low == nullptr && se->high == nullptr;
  1126. gb_unused(no_indices);
  1127. cgAddr addr = cg_build_addr(p, se->expr);
  1128. cgValue base = cg_addr_load(p, addr);
  1129. Type *type = base_type(base.type);
  1130. if (is_type_pointer(type)) {
  1131. type = base_type(type_deref(type));
  1132. addr = cg_addr(base);
  1133. base = cg_addr_load(p, addr);
  1134. }
  1135. switch (type->kind) {
  1136. case Type_Basic:
  1137. case Type_Slice: {
  1138. if (type->kind == Type_Basic) {
  1139. GB_ASSERT(type->Basic.kind == Basic_string);
  1140. }
  1141. Type *slice_type = type;
  1142. if (high.node == nullptr) {
  1143. cgValue len = cg_builtin_len(p, base);
  1144. high = len;
  1145. }
  1146. if (!no_indices) {
  1147. // cg_emit_slice_bounds_check(p, se->open, low, high, len, se->low != nullptr);
  1148. }
  1149. cgValue elem = cg_emit_ptr_offset(p, cg_builtin_raw_data(p, base), low);
  1150. cgValue new_len = cg_emit_arith(p, Token_Sub, high, low, t_int);
  1151. cgAddr slice = cg_add_local(p, slice_type, nullptr, true);
  1152. cg_fill_slice(p, slice, elem, new_len);
  1153. return slice;
  1154. }
  1155. case Type_RelativeSlice:
  1156. GB_PANIC("TODO(bill): Type_RelativeSlice should be handled above already on the cg_addr_load");
  1157. break;
  1158. case Type_DynamicArray: {
  1159. // Type *elem_type = type->DynamicArray.elem;
  1160. // Type *slice_type = alloc_type_slice(elem_type);
  1161. // lbValue len = lb_dynamic_array_len(p, base);
  1162. // if (high.value == nullptr) high = len;
  1163. // if (!no_indices) {
  1164. // lb_emit_slice_bounds_check(p, se->open, low, high, len, se->low != nullptr);
  1165. // }
  1166. // lbValue elem = lb_emit_ptr_offset(p, lb_dynamic_array_elem(p, base), low);
  1167. // lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int);
  1168. // lbAddr slice = lb_add_local_generated(p, slice_type, false);
  1169. // lb_fill_slice(p, slice, elem, new_len);
  1170. // return slice;
  1171. GB_PANIC("cg_build_addr_slice_expr Type_DynamicArray");
  1172. break;
  1173. }
  1174. case Type_MultiPointer: {
  1175. Type *res_type = type_of_expr(expr);
  1176. if (se->high == nullptr) {
  1177. cgAddr res = cg_add_local(p, res_type, nullptr, false);
  1178. GB_ASSERT(base.kind == cgValue_Value);
  1179. GB_ASSERT(low.kind == cgValue_Value);
  1180. i64 stride = type_size_of(type->MultiPointer.elem);
  1181. cgValue offset = cg_value(tb_inst_array_access(p->func, base.node, low.node, stride), base.type);
  1182. cg_addr_store(p, res, offset);
  1183. return res;
  1184. } else {
  1185. cgAddr res = cg_add_local(p, res_type, nullptr, true);
  1186. low = cg_emit_conv(p, low, t_int);
  1187. high = cg_emit_conv(p, high, t_int);
  1188. // cg_emit_multi_pointer_slice_bounds_check(p, se->open, low, high);
  1189. i64 stride = type_size_of(type->MultiPointer.elem);
  1190. TB_Node *offset = tb_inst_array_access(p->func, base.node, low.node, stride);
  1191. TB_Node *len = tb_inst_sub(p->func, high.node, low.node, cast(TB_ArithmeticBehavior)0);
  1192. TB_Node *data_ptr = tb_inst_member_access(p->func, res.addr.node, type_offset_of(res_type, 0));
  1193. TB_Node *len_ptr = tb_inst_member_access(p->func, res.addr.node, type_offset_of(res_type, 1));
  1194. tb_inst_store(p->func, TB_TYPE_PTR, data_ptr, offset, cast(TB_CharUnits)build_context.ptr_size, false);
  1195. tb_inst_store(p->func, TB_TYPE_INT, len_ptr, len, cast(TB_CharUnits)build_context.int_size, false);
  1196. return res;
  1197. }
  1198. }
  1199. case Type_Array: {
  1200. Type *slice_type = type_of_expr(expr);
  1201. GB_ASSERT(is_type_slice(slice_type));
  1202. cgValue len = cg_const_int(p, t_int, type->Array.count);
  1203. if (high.node == nullptr) high = len;
  1204. // bool low_const = type_and_value_of_expr(se->low).mode == Addressing_Constant;
  1205. // bool high_const = type_and_value_of_expr(se->high).mode == Addressing_Constant;
  1206. // if (!low_const || !high_const) {
  1207. // if (!no_indices) {
  1208. // lb_emit_slice_bounds_check(p, se->open, low, high, len, se->low != nullptr);
  1209. // }
  1210. // }
  1211. cgValue elem = cg_emit_ptr_offset(p, cg_builtin_raw_data(p, cg_addr_get_ptr(p, addr)), low);
  1212. cgValue new_len = cg_emit_arith(p, Token_Sub, high, low, t_int);
  1213. cgAddr slice = cg_add_local(p, slice_type, nullptr, true);
  1214. cg_fill_slice(p, slice, elem, new_len);
  1215. return slice;
  1216. }
  1217. case Type_Struct:
  1218. // if (is_type_soa_struct(type)) {
  1219. // lbValue len = lb_soa_struct_len(p, lb_addr_get_ptr(p, addr));
  1220. // if (high.value == nullptr) high = len;
  1221. // if (!no_indices) {
  1222. // lb_emit_slice_bounds_check(p, se->open, low, high, len, se->low != nullptr);
  1223. // }
  1224. // #if 1
  1225. // lbAddr dst = lb_add_local_generated(p, type_of_expr(expr), true);
  1226. // if (type->Struct.soa_kind == StructSoa_Fixed) {
  1227. // i32 field_count = cast(i32)type->Struct.fields.count;
  1228. // for (i32 i = 0; i < field_count; i++) {
  1229. // lbValue field_dst = lb_emit_struct_ep(p, dst.addr, i);
  1230. // lbValue field_src = lb_emit_struct_ep(p, lb_addr_get_ptr(p, addr), i);
  1231. // field_src = lb_emit_array_ep(p, field_src, low);
  1232. // lb_emit_store(p, field_dst, field_src);
  1233. // }
  1234. // lbValue len_dst = lb_emit_struct_ep(p, dst.addr, field_count);
  1235. // lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int);
  1236. // lb_emit_store(p, len_dst, new_len);
  1237. // } else if (type->Struct.soa_kind == StructSoa_Slice) {
  1238. // if (no_indices) {
  1239. // lb_addr_store(p, dst, base);
  1240. // } else {
  1241. // i32 field_count = cast(i32)type->Struct.fields.count - 1;
  1242. // for (i32 i = 0; i < field_count; i++) {
  1243. // lbValue field_dst = lb_emit_struct_ep(p, dst.addr, i);
  1244. // lbValue field_src = lb_emit_struct_ev(p, base, i);
  1245. // field_src = lb_emit_ptr_offset(p, field_src, low);
  1246. // lb_emit_store(p, field_dst, field_src);
  1247. // }
  1248. // lbValue len_dst = lb_emit_struct_ep(p, dst.addr, field_count);
  1249. // lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int);
  1250. // lb_emit_store(p, len_dst, new_len);
  1251. // }
  1252. // } else if (type->Struct.soa_kind == StructSoa_Dynamic) {
  1253. // i32 field_count = cast(i32)type->Struct.fields.count - 3;
  1254. // for (i32 i = 0; i < field_count; i++) {
  1255. // lbValue field_dst = lb_emit_struct_ep(p, dst.addr, i);
  1256. // lbValue field_src = lb_emit_struct_ev(p, base, i);
  1257. // field_src = lb_emit_ptr_offset(p, field_src, low);
  1258. // lb_emit_store(p, field_dst, field_src);
  1259. // }
  1260. // lbValue len_dst = lb_emit_struct_ep(p, dst.addr, field_count);
  1261. // lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int);
  1262. // lb_emit_store(p, len_dst, new_len);
  1263. // }
  1264. // return dst;
  1265. // #endif
  1266. // }
  1267. GB_PANIC("cg_build_addr_slice_expr Type_Struct");
  1268. break;
  1269. }
  1270. GB_PANIC("Unknown slicable type");
  1271. return {};
  1272. }
  1273. gb_internal cgValue cg_emit_unary_arith(cgProcedure *p, TokenKind op, cgValue x, Type *type) {
  1274. switch (op) {
  1275. case Token_Add:
  1276. return x;
  1277. case Token_Not: // Boolean not
  1278. case Token_Xor: // Bitwise not
  1279. case Token_Sub: // Number negation
  1280. break;
  1281. case Token_Pointer:
  1282. GB_PANIC("This should be handled elsewhere");
  1283. break;
  1284. }
  1285. x = cg_flatten_value(p, x);
  1286. if (is_type_array_like(x.type)) {
  1287. GB_PANIC("TODO(bill): cg_emit_unary_arith is_type_array_like");
  1288. // // IMPORTANT TODO(bill): This is very wasteful with regards to stack memory
  1289. // Type *tl = base_type(x.type);
  1290. // cgValue val = cg_address_from_load_or_generate_local(p, x);
  1291. // GB_ASSERT(is_type_array_like(type));
  1292. // Type *elem_type = base_array_type(type);
  1293. // // NOTE(bill): Doesn't need to be zero because it will be initialized in the loops
  1294. // cgAddr res_addr = cg_add_local(p, type, nullptr, false);
  1295. // cgValue res = cg_addr_get_ptr(p, res_addr);
  1296. // bool inline_array_arith = cg_can_try_to_inline_array_arith(type);
  1297. // i32 count = cast(i32)get_array_type_count(tl);
  1298. // LLVMTypeRef vector_type = nullptr;
  1299. // if (op != Token_Not && cg_try_vector_cast(p->module, val, &vector_type)) {
  1300. // LLVMValueRef vp = LLVMBuildPointerCast(p->builder, val.value, LLVMPointerType(vector_type, 0), "");
  1301. // LLVMValueRef v = LLVMBuildLoad2(p->builder, vector_type, vp, "");
  1302. // LLVMValueRef opv = nullptr;
  1303. // switch (op) {
  1304. // case Token_Xor:
  1305. // opv = LLVMBuildNot(p->builder, v, "");
  1306. // break;
  1307. // case Token_Sub:
  1308. // if (is_type_float(elem_type)) {
  1309. // opv = LLVMBuildFNeg(p->builder, v, "");
  1310. // } else {
  1311. // opv = LLVMBuildNeg(p->builder, v, "");
  1312. // }
  1313. // break;
  1314. // }
  1315. // if (opv != nullptr) {
  1316. // LLVMSetAlignment(res.value, cast(unsigned)cg_alignof(vector_type));
  1317. // LLVMValueRef res_ptr = LLVMBuildPointerCast(p->builder, res.value, LLVMPointerType(vector_type, 0), "");
  1318. // LLVMBuildStore(p->builder, opv, res_ptr);
  1319. // return cg_emit_conv(p, cg_emit_load(p, res), type);
  1320. // }
  1321. // }
  1322. // if (inline_array_arith) {
  1323. // // inline
  1324. // for (i32 i = 0; i < count; i++) {
  1325. // cgValue e = cg_emit_load(p, cg_emit_array_epi(p, val, i));
  1326. // cgValue z = cg_emit_unary_arith(p, op, e, elem_type);
  1327. // cg_emit_store(p, cg_emit_array_epi(p, res, i), z);
  1328. // }
  1329. // } else {
  1330. // auto loop_data = cg_loop_start(p, count, t_i32);
  1331. // cgValue e = cg_emit_load(p, cg_emit_array_ep(p, val, loop_data.idx));
  1332. // cgValue z = cg_emit_unary_arith(p, op, e, elem_type);
  1333. // cg_emit_store(p, cg_emit_array_ep(p, res, loop_data.idx), z);
  1334. // cg_loop_end(p, loop_data);
  1335. // }
  1336. // return cg_emit_load(p, res);
  1337. }
  1338. if (op == Token_Xor) {
  1339. GB_ASSERT(x.kind == cgValue_Value);
  1340. cgValue cmp = cg_value(tb_inst_not(p->func, x.node), x.type);
  1341. return cg_emit_conv(p, cmp, type);
  1342. }
  1343. if (op == Token_Not) {
  1344. TB_Node *zero = cg_const_nil(p, x.type).node;
  1345. cgValue cmp = cg_value(tb_inst_cmp_ne(p->func, x.node, zero), x.type);
  1346. return cg_emit_conv(p, cmp, type);
  1347. }
  1348. if (op == Token_Sub && is_type_integer(type) && is_type_different_to_arch_endianness(type)) {
  1349. Type *platform_type = integer_endian_type_to_platform_type(type);
  1350. cgValue v = cg_emit_byte_swap(p, x, platform_type);
  1351. cgValue res = cg_value(tb_inst_neg(p->func, v.node), platform_type);
  1352. return cg_emit_byte_swap(p, res, type);
  1353. }
  1354. if (op == Token_Sub && is_type_float(type) && is_type_different_to_arch_endianness(type)) {
  1355. Type *platform_type = integer_endian_type_to_platform_type(type);
  1356. cgValue v = cg_emit_byte_swap(p, x, platform_type);
  1357. cgValue res = cg_value(tb_inst_neg(p->func, v.node), platform_type);
  1358. return cg_emit_byte_swap(p, res, type);
  1359. }
  1360. cgValue res = {};
  1361. if (op == Token_Sub) { // Number negation
  1362. if (is_type_integer(x.type)) {
  1363. res = cg_value(tb_inst_neg(p->func, x.node), x.type);
  1364. } else if (is_type_float(x.type)) {
  1365. res = cg_value(tb_inst_neg(p->func, x.node), x.type);
  1366. } else if (is_type_complex(x.type)) {
  1367. GB_PANIC("TODO(bill): neg complex");
  1368. // LLVMValueRef v0 = LLVMBuildFNeg(p->builder, LLVMBuildExtractValue(p->builder, x.value, 0, ""), "");
  1369. // LLVMValueRef v1 = LLVMBuildFNeg(p->builder, LLVMBuildExtractValue(p->builder, x.value, 1, ""), "");
  1370. // cgAddr addr = cg_add_local_generated(p, x.type, false);
  1371. // LLVMTypeRef type = llvm_addr_type(p->module, addr.addr);
  1372. // LLVMBuildStore(p->builder, v0, LLVMBuildStructGEP2(p->builder, type, addr.addr.value, 0, ""));
  1373. // LLVMBuildStore(p->builder, v1, LLVMBuildStructGEP2(p->builder, type, addr.addr.value, 1, ""));
  1374. // return cg_addr_load(p, addr);
  1375. } else if (is_type_quaternion(x.type)) {
  1376. GB_PANIC("TODO(bill): neg quaternion");
  1377. // LLVMValueRef v0 = LLVMBuildFNeg(p->builder, LLVMBuildExtractValue(p->builder, x.value, 0, ""), "");
  1378. // LLVMValueRef v1 = LLVMBuildFNeg(p->builder, LLVMBuildExtractValue(p->builder, x.value, 1, ""), "");
  1379. // LLVMValueRef v2 = LLVMBuildFNeg(p->builder, LLVMBuildExtractValue(p->builder, x.value, 2, ""), "");
  1380. // LLVMValueRef v3 = LLVMBuildFNeg(p->builder, LLVMBuildExtractValue(p->builder, x.value, 3, ""), "");
  1381. // cgAddr addr = cg_add_local_generated(p, x.type, false);
  1382. // LLVMTypeRef type = llvm_addr_type(p->module, addr.addr);
  1383. // LLVMBuildStore(p->builder, v0, LLVMBuildStructGEP2(p->builder, type, addr.addr.value, 0, ""));
  1384. // LLVMBuildStore(p->builder, v1, LLVMBuildStructGEP2(p->builder, type, addr.addr.value, 1, ""));
  1385. // LLVMBuildStore(p->builder, v2, LLVMBuildStructGEP2(p->builder, type, addr.addr.value, 2, ""));
  1386. // LLVMBuildStore(p->builder, v3, LLVMBuildStructGEP2(p->builder, type, addr.addr.value, 3, ""));
  1387. // return cg_addr_load(p, addr);
  1388. } else if (is_type_simd_vector(x.type)) {
  1389. GB_PANIC("TODO(bill): neg simd");
  1390. // Type *elem = base_array_type(x.type);
  1391. // if (is_type_float(elem)) {
  1392. // res.value = LLVMBuildFNeg(p->builder, x.value, "");
  1393. // } else {
  1394. // res.value = LLVMBuildNeg(p->builder, x.value, "");
  1395. // }
  1396. } else if (is_type_matrix(x.type)) {
  1397. GB_PANIC("TODO(bill): neg matrix");
  1398. // cgValue zero = {};
  1399. // zero.value = LLVMConstNull(cg_type(p->module, type));
  1400. // zero.type = type;
  1401. // return cg_emit_arith_matrix(p, Token_Sub, zero, x, type, true);
  1402. } else {
  1403. GB_PANIC("Unhandled type %s", type_to_string(x.type));
  1404. }
  1405. res.type = x.type;
  1406. return res;
  1407. }
  1408. return res;
  1409. }
  1410. gb_internal void cg_emit_if(cgProcedure *p, cgValue const &cond, TB_Node *true_region, TB_Node *false_region) {
  1411. GB_ASSERT(cond.kind == cgValue_Value);
  1412. tb_inst_if(p->func, cond.node, true_region, false_region);
  1413. }
  1414. gb_internal void cg_build_try_lhs_rhs(cgProcedure *p, Ast *arg, Type *final_type, cgValue *lhs_, cgValue *rhs_) {
  1415. cgValue lhs = {};
  1416. cgValue rhs = {};
  1417. cgValue value = cg_build_expr(p, arg);
  1418. if (value.kind == cgValue_Multi) {
  1419. auto const &values = value.multi->values;
  1420. if (values.count == 2) {
  1421. lhs = values[0];
  1422. rhs = values[1];
  1423. } else {
  1424. rhs = values[values.count-1];
  1425. if (values.count > 1) {
  1426. lhs = cg_value_multi(slice(values, 0, values.count-1), final_type);
  1427. }
  1428. }
  1429. } else {
  1430. rhs = value;
  1431. }
  1432. GB_ASSERT(rhs.node != nullptr);
  1433. if (lhs_) *lhs_ = lhs;
  1434. if (rhs_) *rhs_ = rhs;
  1435. }
  1436. gb_internal cgValue cg_emit_try_has_value(cgProcedure *p, cgValue rhs) {
  1437. cgValue has_value = {};
  1438. if (is_type_boolean(rhs.type)) {
  1439. has_value = rhs;
  1440. } else {
  1441. GB_ASSERT_MSG(type_has_nil(rhs.type), "%s", type_to_string(rhs.type));
  1442. has_value = cg_emit_comp_against_nil(p, Token_CmpEq, rhs);
  1443. }
  1444. GB_ASSERT(has_value.node != nullptr);
  1445. return has_value;
  1446. }
  1447. gb_internal cgValue cg_build_or_return(cgProcedure *p, Ast *arg, Type *final_type) {
  1448. cgValue lhs = {};
  1449. cgValue rhs = {};
  1450. cg_build_try_lhs_rhs(p, arg, final_type, &lhs, &rhs);
  1451. TB_Node *return_region = cg_control_region(p, "or_return_return");
  1452. TB_Node *continue_region = cg_control_region(p, "or_return_continue");
  1453. cgValue cond = cg_emit_try_has_value(p, rhs);
  1454. cg_emit_if(p, cond, continue_region, return_region);
  1455. tb_inst_set_control(p->func, return_region);
  1456. {
  1457. Type *proc_type = base_type(p->type);
  1458. Type *results = proc_type->Proc.results;
  1459. GB_ASSERT(results != nullptr && results->kind == Type_Tuple);
  1460. TypeTuple *tuple = &results->Tuple;
  1461. GB_ASSERT(tuple->variables.count != 0);
  1462. Entity *end_entity = tuple->variables[tuple->variables.count-1];
  1463. rhs = cg_emit_conv(p, rhs, end_entity->type);
  1464. if (p->type->Proc.has_named_results) {
  1465. GB_ASSERT(end_entity->token.string.len != 0);
  1466. // NOTE(bill): store the named values before returning
  1467. cgAddr found = map_must_get(&p->variable_map, end_entity);
  1468. cg_addr_store(p, found, rhs);
  1469. cg_build_return_stmt(p, {});
  1470. } else {
  1471. GB_ASSERT(tuple->variables.count == 1);
  1472. Slice<cgValue> results = {};
  1473. results.data = &rhs;
  1474. results.count = 1;;
  1475. cg_build_return_stmt_internal(p, results);
  1476. }
  1477. }
  1478. tb_inst_set_control(p->func, continue_region);
  1479. if (final_type != nullptr && !is_type_tuple(final_type)) {
  1480. return cg_emit_conv(p, lhs, final_type);
  1481. }
  1482. return {};
  1483. }
  1484. gb_internal cgValue cg_build_or_else(cgProcedure *p, Ast *arg, Ast *else_expr, Type *final_type) {
  1485. if (arg->state_flags & StateFlag_DirectiveWasFalse) {
  1486. return cg_build_expr(p, else_expr);
  1487. }
  1488. cgValue lhs = {};
  1489. cgValue rhs = {};
  1490. cg_build_try_lhs_rhs(p, arg, final_type, &lhs, &rhs);
  1491. GB_ASSERT(else_expr != nullptr);
  1492. if (is_diverging_expr(else_expr)) {
  1493. TB_Node *then = cg_control_region(p, "or_else_then");
  1494. TB_Node *else_ = cg_control_region(p, "or_else_else");
  1495. cg_emit_if(p, cg_emit_try_has_value(p, rhs), then, else_);
  1496. // NOTE(bill): else block needs to be straight afterwards to make sure that the actual value is used
  1497. // from the then block
  1498. tb_inst_set_control(p->func, else_);
  1499. cg_build_expr(p, else_expr);
  1500. tb_inst_set_control(p->func, then);
  1501. return cg_emit_conv(p, lhs, final_type);
  1502. } else {
  1503. TB_Node *incoming_values[2] = {};
  1504. TB_Node *incoming_regions[2] = {};
  1505. TB_Node *then = cg_control_region(p, "or_else_then");
  1506. TB_Node *done = cg_control_region(p, "or_else_done"); // NOTE(bill): Append later
  1507. TB_Node *else_ = cg_control_region(p, "or_else_else");
  1508. cg_emit_if(p, cg_emit_try_has_value(p, rhs), then, else_);
  1509. tb_inst_set_control(p->func, then);
  1510. cgValue x = cg_emit_conv(p, lhs, final_type);
  1511. incoming_values[0] = x.node;
  1512. incoming_regions[0] = tb_inst_get_control(p->func);
  1513. tb_inst_goto(p->func, done);
  1514. tb_inst_set_control(p->func, else_);
  1515. cgValue y = cg_emit_conv(p, cg_build_expr(p, else_expr), final_type);
  1516. incoming_values[1] = y.node;
  1517. incoming_regions[1] = tb_inst_get_control(p->func);
  1518. tb_inst_goto(p->func, done);
  1519. tb_inst_set_control(p->func, done);
  1520. GB_ASSERT(x.kind == y.kind);
  1521. GB_ASSERT(incoming_values[0]->dt.raw == incoming_values[1]->dt.raw);
  1522. cgValue res = {};
  1523. res.kind = x.kind;
  1524. res.type = final_type;
  1525. res.node = tb_inst_incomplete_phi(p->func, incoming_values[0]->dt, done, 2);
  1526. tb_inst_add_phi_operand(p->func, res.node, incoming_regions[0], incoming_values[0]);
  1527. tb_inst_add_phi_operand(p->func, res.node, incoming_regions[1], incoming_values[1]);
  1528. return res;
  1529. }
  1530. }
  1531. gb_internal isize cg_control_region_pred_count(TB_Node *region) {
  1532. GB_ASSERT(region->type == TB_REGION);
  1533. GB_ASSERT(region->input_count > 0);
  1534. return region->input_count;
  1535. }
  1536. gb_internal cgValue cg_build_logical_binary_expr(cgProcedure *p, TokenKind op, Ast *left, Ast *right, Type *final_type) {
  1537. TB_Node *rhs = cg_control_region(p, "logical_cmp_rhs");
  1538. TB_Node *done = cg_control_region(p, "logical_cmp_done");
  1539. cgValue short_circuit = {};
  1540. if (op == Token_CmpAnd) {
  1541. cg_build_cond(p, left, rhs, done);
  1542. short_circuit = cg_const_bool(p, t_bool, false);
  1543. } else if (op == Token_CmpOr) {
  1544. cg_build_cond(p, left, done, rhs);
  1545. short_circuit = cg_const_bool(p, t_bool, true);
  1546. }
  1547. if (rhs->input_count == 0) {
  1548. tb_inst_set_control(p->func, done);
  1549. return cg_emit_conv(p, short_circuit, final_type);
  1550. }
  1551. if (done->input_count == 0) {
  1552. tb_inst_set_control(p->func, rhs);
  1553. return cg_build_expr(p, right);
  1554. }
  1555. tb_inst_set_control(p->func, rhs);
  1556. cgValue edge = cg_build_expr(p, right);
  1557. TB_Node *edge_region = tb_inst_get_control(p->func);
  1558. tb_inst_goto(p->func, done);
  1559. tb_inst_set_control(p->func, done);
  1560. TB_DataType dt = edge.node->dt;
  1561. TB_Node *phi = tb_inst_incomplete_phi(p->func, dt, done, done->input_count);
  1562. for (size_t i = 0; i < done->input_count; i++) {
  1563. TB_Node *val = short_circuit.node;
  1564. TB_Node *region = done->inputs[i];
  1565. if (region == edge_region) {
  1566. val = edge.node;
  1567. }
  1568. tb_inst_add_phi_operand(p->func, phi, region, val);
  1569. }
  1570. return cg_emit_conv(p, cg_value(phi, t_bool), final_type);
  1571. }
  1572. gb_internal cgValue cg_build_binary_expr(cgProcedure *p, Ast *expr) {
  1573. ast_node(be, BinaryExpr, expr);
  1574. TypeAndValue tv = type_and_value_of_expr(expr);
  1575. if (is_type_matrix(be->left->tav.type) || is_type_matrix(be->right->tav.type)) {
  1576. cgValue left = cg_build_expr(p, be->left);
  1577. cgValue right = cg_build_expr(p, be->right);
  1578. GB_PANIC("TODO(bill): cg_emit_arith_matrix");
  1579. // return cg_emit_arith_matrix(p, be->op.kind, left, right, default_type(tv.type), false);
  1580. }
  1581. switch (be->op.kind) {
  1582. case Token_Add:
  1583. case Token_Sub:
  1584. case Token_Mul:
  1585. case Token_Quo:
  1586. case Token_Mod:
  1587. case Token_ModMod:
  1588. case Token_And:
  1589. case Token_Or:
  1590. case Token_Xor:
  1591. case Token_AndNot: {
  1592. Type *type = default_type(tv.type);
  1593. cgValue left = cg_build_expr(p, be->left);
  1594. cgValue right = cg_build_expr(p, be->right);
  1595. return cg_emit_arith(p, be->op.kind, left, right, type);
  1596. }
  1597. case Token_Shl:
  1598. case Token_Shr: {
  1599. cgValue left, right;
  1600. Type *type = default_type(tv.type);
  1601. left = cg_build_expr(p, be->left);
  1602. if (cg_is_expr_untyped_const(be->right)) {
  1603. // NOTE(bill): RHS shift operands can still be untyped
  1604. // Just bypass the standard cg_build_expr
  1605. right = cg_expr_untyped_const_to_typed(p, be->right, type);
  1606. } else {
  1607. right = cg_build_expr(p, be->right);
  1608. }
  1609. return cg_emit_arith(p, be->op.kind, left, right, type);
  1610. }
  1611. case Token_CmpEq:
  1612. case Token_NotEq:
  1613. if (is_type_untyped_nil(be->right->tav.type)) {
  1614. // `x == nil` or `x != nil`
  1615. cgValue left = cg_build_expr(p, be->left);
  1616. cgValue cmp = cg_emit_comp_against_nil(p, be->op.kind, left);
  1617. Type *type = default_type(tv.type);
  1618. return cg_emit_conv(p, cmp, type);
  1619. } else if (is_type_untyped_nil(be->left->tav.type)) {
  1620. // `nil == x` or `nil != x`
  1621. cgValue right = cg_build_expr(p, be->right);
  1622. cgValue cmp = cg_emit_comp_against_nil(p, be->op.kind, right);
  1623. Type *type = default_type(tv.type);
  1624. return cg_emit_conv(p, cmp, type);
  1625. }/* else if (cg_is_empty_string_constant(be->right)) {
  1626. // `x == ""` or `x != ""`
  1627. cgValue s = cg_build_expr(p, be->left);
  1628. s = cg_emit_conv(p, s, t_string);
  1629. cgValue len = cg_string_len(p, s);
  1630. cgValue cmp = cg_emit_comp(p, be->op.kind, len, cg_const_int(p->module, t_int, 0));
  1631. Type *type = default_type(tv.type);
  1632. return cg_emit_conv(p, cmp, type);
  1633. } else if (cg_is_empty_string_constant(be->left)) {
  1634. // `"" == x` or `"" != x`
  1635. cgValue s = cg_build_expr(p, be->right);
  1636. s = cg_emit_conv(p, s, t_string);
  1637. cgValue len = cg_string_len(p, s);
  1638. cgValue cmp = cg_emit_comp(p, be->op.kind, len, cg_const_int(p->module, t_int, 0));
  1639. Type *type = default_type(tv.type);
  1640. return cg_emit_conv(p, cmp, type);
  1641. }*/
  1642. /*fallthrough*/
  1643. case Token_Lt:
  1644. case Token_LtEq:
  1645. case Token_Gt:
  1646. case Token_GtEq:
  1647. {
  1648. cgValue left = {};
  1649. cgValue right = {};
  1650. if (be->left->tav.mode == Addressing_Type) {
  1651. left = cg_typeid(p, be->left->tav.type);
  1652. }
  1653. if (be->right->tav.mode == Addressing_Type) {
  1654. right = cg_typeid(p, be->right->tav.type);
  1655. }
  1656. if (left.node == nullptr) left = cg_build_expr(p, be->left);
  1657. if (right.node == nullptr) right = cg_build_expr(p, be->right);
  1658. cgValue cmp = cg_emit_comp(p, be->op.kind, left, right);
  1659. Type *type = default_type(tv.type);
  1660. return cg_emit_conv(p, cmp, type);
  1661. }
  1662. case Token_CmpAnd:
  1663. case Token_CmpOr:
  1664. return cg_build_logical_binary_expr(p, be->op.kind, be->left, be->right, tv.type);
  1665. case Token_in:
  1666. case Token_not_in:
  1667. {
  1668. cgValue left = cg_build_expr(p, be->left);
  1669. cgValue right = cg_build_expr(p, be->right);
  1670. Type *rt = base_type(right.type);
  1671. if (is_type_pointer(rt)) {
  1672. right = cg_emit_load(p, right);
  1673. rt = base_type(type_deref(rt));
  1674. }
  1675. switch (rt->kind) {
  1676. case Type_Map:
  1677. {
  1678. GB_PANIC("TODO(bill): in/not_in for maps");
  1679. // cgValue map_ptr = cg_address_from_load_or_generate_local(p, right);
  1680. // cgValue key = left;
  1681. // cgValue ptr = cg_internal_dynamic_map_get_ptr(p, map_ptr, key);
  1682. // if (be->op.kind == Token_in) {
  1683. // return cg_emit_conv(p, cg_emit_comp_against_nil(p, Token_NotEq, ptr), t_bool);
  1684. // } else {
  1685. // return cg_emit_conv(p, cg_emit_comp_against_nil(p, Token_CmpEq, ptr), t_bool);
  1686. // }
  1687. }
  1688. break;
  1689. case Type_BitSet:
  1690. {
  1691. Type *key_type = rt->BitSet.elem;
  1692. GB_ASSERT(are_types_identical(left.type, key_type));
  1693. Type *it = bit_set_to_int(rt);
  1694. left = cg_emit_conv(p, left, it);
  1695. if (is_type_different_to_arch_endianness(it)) {
  1696. left = cg_emit_byte_swap(p, left, integer_endian_type_to_platform_type(it));
  1697. }
  1698. cgValue lower = cg_const_value(p, left.type, exact_value_i64(rt->BitSet.lower));
  1699. cgValue key = cg_emit_arith(p, Token_Sub, left, lower, left.type);
  1700. cgValue bit = cg_emit_arith(p, Token_Shl, cg_const_int(p, left.type, 1), key, left.type);
  1701. bit = cg_emit_conv(p, bit, it);
  1702. cgValue old_value = cg_emit_transmute(p, right, it);
  1703. cgValue new_value = cg_emit_arith(p, Token_And, old_value, bit, it);
  1704. GB_PANIC("TODO(bill): cg_emit_comp");
  1705. // TokenKind op = (be->op.kind == Token_in) ? Token_NotEq : Token_CmpEq;
  1706. // return cg_emit_conv(p, cg_emit_comp(p, op, new_value, cg_const_int(p, new_value.type, 0)), t_bool);
  1707. }
  1708. break;
  1709. default:
  1710. GB_PANIC("Invalid 'in' type");
  1711. }
  1712. break;
  1713. }
  1714. break;
  1715. default:
  1716. GB_PANIC("Invalid binary expression");
  1717. break;
  1718. }
  1719. return {};
  1720. }
  1721. gb_internal cgValue cg_build_cond(cgProcedure *p, Ast *cond, TB_Node *true_block, TB_Node *false_block) {
  1722. cond = unparen_expr(cond);
  1723. GB_ASSERT(cond != nullptr);
  1724. GB_ASSERT(true_block != nullptr);
  1725. GB_ASSERT(false_block != nullptr);
  1726. // Use to signal not to do compile time short circuit for consts
  1727. cgValue no_comptime_short_circuit = {};
  1728. switch (cond->kind) {
  1729. case_ast_node(ue, UnaryExpr, cond);
  1730. if (ue->op.kind == Token_Not) {
  1731. cgValue cond_val = cg_build_cond(p, ue->expr, false_block, true_block);
  1732. return cond_val;
  1733. // if (cond_val.value && LLVMIsConstant(cond_val.value)) {
  1734. // return cg_const_bool(p->module, cond_val.type, LLVMConstIntGetZExtValue(cond_val.value) == 0);
  1735. // }
  1736. // return no_comptime_short_circuit;
  1737. }
  1738. case_end;
  1739. case_ast_node(be, BinaryExpr, cond);
  1740. if (be->op.kind == Token_CmpAnd) {
  1741. TB_Node *block = cg_control_region(p, "cmp_and");
  1742. cg_build_cond(p, be->left, block, false_block);
  1743. tb_inst_set_control(p->func, block);
  1744. cg_build_cond(p, be->right, true_block, false_block);
  1745. return no_comptime_short_circuit;
  1746. } else if (be->op.kind == Token_CmpOr) {
  1747. TB_Node *block = cg_control_region(p, "cmp_or");
  1748. cg_build_cond(p, be->left, true_block, block);
  1749. tb_inst_set_control(p->func, block);
  1750. cg_build_cond(p, be->right, true_block, false_block);
  1751. return no_comptime_short_circuit;
  1752. }
  1753. case_end;
  1754. }
  1755. cgValue v = {};
  1756. if (cg_is_expr_untyped_const(cond)) {
  1757. v = cg_expr_untyped_const_to_typed(p, cond, t_bool);
  1758. } else {
  1759. v = cg_build_expr(p, cond);
  1760. }
  1761. cg_emit_if(p, v, true_block, false_block);
  1762. return v;
  1763. }
  1764. gb_internal cgValue cg_build_expr_internal(cgProcedure *p, Ast *expr);
  1765. gb_internal cgValue cg_build_expr(cgProcedure *p, Ast *expr) {
  1766. cg_set_debug_pos_from_node(p, expr);
  1767. u16 prev_state_flags = p->state_flags;
  1768. defer (p->state_flags = prev_state_flags);
  1769. if (expr->state_flags != 0) {
  1770. u16 in = expr->state_flags;
  1771. u16 out = p->state_flags;
  1772. if (in & StateFlag_bounds_check) {
  1773. out |= StateFlag_bounds_check;
  1774. out &= ~StateFlag_no_bounds_check;
  1775. } else if (in & StateFlag_no_bounds_check) {
  1776. out |= StateFlag_no_bounds_check;
  1777. out &= ~StateFlag_bounds_check;
  1778. }
  1779. if (in & StateFlag_type_assert) {
  1780. out |= StateFlag_type_assert;
  1781. out &= ~StateFlag_no_type_assert;
  1782. } else if (in & StateFlag_no_type_assert) {
  1783. out |= StateFlag_no_type_assert;
  1784. out &= ~StateFlag_type_assert;
  1785. }
  1786. p->state_flags = out;
  1787. }
  1788. // IMPORTANT NOTE(bill):
  1789. // Selector Call Expressions (foo->bar(...))
  1790. // must only evaluate `foo` once as it gets transformed into
  1791. // `foo.bar(foo, ...)`
  1792. // And if `foo` is a procedure call or something more complex, storing the value
  1793. // once is a very good idea
  1794. // If a stored value is found, it must be removed from the cache
  1795. if (expr->state_flags & StateFlag_SelectorCallExpr) {
  1796. // cgValue *pp = map_get(&p->selector_values, expr);
  1797. // if (pp != nullptr) {
  1798. // cgValue res = *pp;
  1799. // map_remove(&p->selector_values, expr);
  1800. // return res;
  1801. // }
  1802. // cgAddr *pa = map_get(&p->selector_addr, expr);
  1803. // if (pa != nullptr) {
  1804. // cgAddr res = *pa;
  1805. // map_remove(&p->selector_addr, expr);
  1806. // return cg_addr_load(p, res);
  1807. // }
  1808. }
  1809. cgValue res = cg_build_expr_internal(p, expr);
  1810. if (res.kind == cgValue_Symbol) {
  1811. GB_ASSERT(is_type_internally_pointer_like(res.type));
  1812. res = cg_value(tb_inst_get_symbol_address(p->func, res.symbol), res.type);
  1813. }
  1814. if (expr->state_flags & StateFlag_SelectorCallExpr) {
  1815. // map_set(&p->selector_values, expr, res);
  1816. }
  1817. return res;
  1818. }
  1819. gb_internal cgValue cg_find_ident(cgProcedure *p, Entity *e, Ast *expr) {
  1820. cgAddr *found_addr = map_get(&p->variable_map, e);
  1821. if (found_addr) {
  1822. return cg_addr_load(p, *found_addr);
  1823. }
  1824. cgValue *found = nullptr;
  1825. rw_mutex_shared_lock(&p->module->values_mutex);
  1826. found = map_get(&p->module->values, e);
  1827. rw_mutex_shared_unlock(&p->module->values_mutex);
  1828. if (found) {
  1829. auto v = *found;
  1830. // NOTE(bill): This is because pointers are already pointers in LLVM
  1831. if (is_type_proc(v.type)) {
  1832. return v;
  1833. }
  1834. return cg_emit_load(p, v);
  1835. } else if (e != nullptr && e->kind == Entity_Variable) {
  1836. return cg_addr_load(p, cg_build_addr(p, expr));
  1837. }
  1838. if (e->kind == Entity_Procedure) {
  1839. return cg_find_procedure_value_from_entity(p->module, e);
  1840. }
  1841. String pkg = {};
  1842. if (e->pkg) {
  1843. pkg = e->pkg->name;
  1844. }
  1845. gb_printf_err("Error in: %s\n", token_pos_to_string(ast_token(expr).pos));
  1846. GB_PANIC("nullptr value for expression from identifier: %.*s.%.*s (%p) : %s @ %p", LIT(pkg), LIT(e->token.string), e, type_to_string(e->type), expr);
  1847. return {};
  1848. }
  1849. cgAddr cg_build_addr_compound_lit(cgProcedure *p, Ast *expr) {
  1850. struct cgCompoundLitElemTempData {
  1851. Ast * expr;
  1852. cgValue value;
  1853. i64 elem_index;
  1854. i64 elem_length;
  1855. cgValue gep;
  1856. };
  1857. auto const &populate = [](cgProcedure *p, Slice<Ast *> const &elems, Array<cgCompoundLitElemTempData> *temp_data, Type *compound_type) {
  1858. Type *bt = base_type(compound_type);
  1859. Type *et = nullptr;
  1860. switch (bt->kind) {
  1861. case Type_Array: et = bt->Array.elem; break;
  1862. case Type_EnumeratedArray: et = bt->EnumeratedArray.elem; break;
  1863. case Type_Slice: et = bt->Slice.elem; break;
  1864. case Type_BitSet: et = bt->BitSet.elem; break;
  1865. case Type_DynamicArray: et = bt->DynamicArray.elem; break;
  1866. case Type_SimdVector: et = bt->SimdVector.elem; break;
  1867. case Type_Matrix: et = bt->Matrix.elem; break;
  1868. }
  1869. GB_ASSERT(et != nullptr);
  1870. // NOTE(bill): Separate value, gep, store into their own chunks
  1871. for_array(i, elems) {
  1872. Ast *elem = elems[i];
  1873. if (elem->kind == Ast_FieldValue) {
  1874. ast_node(fv, FieldValue, elem);
  1875. if (is_ast_range(fv->field)) {
  1876. ast_node(ie, BinaryExpr, fv->field);
  1877. TypeAndValue lo_tav = ie->left->tav;
  1878. TypeAndValue hi_tav = ie->right->tav;
  1879. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  1880. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  1881. TokenKind op = ie->op.kind;
  1882. i64 lo = exact_value_to_i64(lo_tav.value);
  1883. i64 hi = exact_value_to_i64(hi_tav.value);
  1884. if (op != Token_RangeHalf) {
  1885. hi += 1;
  1886. }
  1887. cgValue value = cg_emit_conv(p, cg_build_expr(p, fv->value), et);
  1888. GB_ASSERT((hi-lo) > 0);
  1889. if (bt->kind == Type_Matrix) {
  1890. GB_PANIC("TODO(bill): Type_Matrix");
  1891. // for (i64 k = lo; k < hi; k++) {
  1892. // cgCompoundLitElemTempData data = {};
  1893. // data.value = value;
  1894. // data.elem_index = matrix_row_major_index_to_offset(bt, k);
  1895. // array_add(temp_data, data);
  1896. // }
  1897. } else {
  1898. enum {MAX_ELEMENT_AMOUNT = 32};
  1899. if ((hi-lo) <= MAX_ELEMENT_AMOUNT) {
  1900. for (i64 k = lo; k < hi; k++) {
  1901. cgCompoundLitElemTempData data = {};
  1902. data.value = value;
  1903. data.elem_index = k;
  1904. array_add(temp_data, data);
  1905. }
  1906. } else {
  1907. cgCompoundLitElemTempData data = {};
  1908. data.value = value;
  1909. data.elem_index = lo;
  1910. data.elem_length = hi-lo;
  1911. array_add(temp_data, data);
  1912. }
  1913. }
  1914. } else {
  1915. auto tav = fv->field->tav;
  1916. GB_ASSERT(tav.mode == Addressing_Constant);
  1917. i64 index = exact_value_to_i64(tav.value);
  1918. cgValue value = cg_emit_conv(p, cg_build_expr(p, fv->value), et);
  1919. GB_ASSERT(!is_type_tuple(value.type));
  1920. cgCompoundLitElemTempData data = {};
  1921. data.value = value;
  1922. data.expr = fv->value;
  1923. if (bt->kind == Type_Matrix) {
  1924. GB_PANIC("TODO(bill): Type_Matrix");
  1925. // data.elem_index = matrix_row_major_index_to_offset(bt, index);
  1926. } else {
  1927. data.elem_index = index;
  1928. }
  1929. array_add(temp_data, data);
  1930. }
  1931. } else {
  1932. // if (bt->kind != Type_DynamicArray && lb_is_elem_const(elem, et)) {
  1933. // continue;
  1934. // }
  1935. cgValue field_expr = cg_build_expr(p, elem);
  1936. GB_ASSERT(!is_type_tuple(field_expr.type));
  1937. cgValue ev = cg_emit_conv(p, field_expr, et);
  1938. cgCompoundLitElemTempData data = {};
  1939. data.value = ev;
  1940. if (bt->kind == Type_Matrix) {
  1941. GB_PANIC("TODO(bill): Type_Matrix");
  1942. // data.elem_index = matrix_row_major_index_to_offset(bt, i);
  1943. } else {
  1944. data.elem_index = i;
  1945. }
  1946. array_add(temp_data, data);
  1947. }
  1948. }
  1949. };
  1950. auto const &assign_array = [](cgProcedure *p, Array<cgCompoundLitElemTempData> const &temp_data) {
  1951. for (auto const &td : temp_data) if (td.value.node != nullptr) {
  1952. if (td.elem_length > 0) {
  1953. GB_PANIC("TODO(bill): range");
  1954. // auto loop_data = cg_loop_start(p, cast(isize)td.elem_length, t_i32);
  1955. // {
  1956. // cgValue dst = td.gep;
  1957. // dst = cg_emit_ptr_offset(p, dst, loop_data.idx);
  1958. // cg_emit_store(p, dst, td.value);
  1959. // }
  1960. // cg_loop_end(p, loop_data);
  1961. } else {
  1962. cg_emit_store(p, td.gep, td.value);
  1963. }
  1964. }
  1965. };
  1966. ast_node(cl, CompoundLit, expr);
  1967. Type *type = type_of_expr(expr);
  1968. Type *bt = base_type(type);
  1969. cgAddr v = cg_add_local(p, type, nullptr, true);
  1970. if (cl->elems.count == 0) {
  1971. // No need to create it
  1972. return v;
  1973. }
  1974. TEMPORARY_ALLOCATOR_GUARD();
  1975. Type *et = nullptr;
  1976. switch (bt->kind) {
  1977. case Type_Array: et = bt->Array.elem; break;
  1978. case Type_EnumeratedArray: et = bt->EnumeratedArray.elem; break;
  1979. case Type_Slice: et = bt->Slice.elem; break;
  1980. case Type_BitSet: et = bt->BitSet.elem; break;
  1981. case Type_SimdVector: et = bt->SimdVector.elem; break;
  1982. case Type_Matrix: et = bt->Matrix.elem; break;
  1983. }
  1984. String proc_name = {};
  1985. if (p->entity) {
  1986. proc_name = p->entity->token.string;
  1987. }
  1988. TokenPos pos = ast_token(expr).pos;
  1989. if (cl->elems.count == 0) {
  1990. }
  1991. switch (bt->kind) {
  1992. default: GB_PANIC("Unknown CompoundLit type: %s", type_to_string(type)); break;
  1993. case Type_Struct: {
  1994. TypeStruct *st = &bt->Struct;
  1995. cgValue comp_lit_ptr = cg_addr_get_ptr(p, v);
  1996. for_array(field_index, cl->elems) {
  1997. Ast *elem = cl->elems[field_index];
  1998. cgValue field_expr = {};
  1999. Entity *field = nullptr;
  2000. isize index = field_index;
  2001. if (elem->kind == Ast_FieldValue) {
  2002. ast_node(fv, FieldValue, elem);
  2003. String name = fv->field->Ident.token.string;
  2004. Selection sel = lookup_field(bt, name, false);
  2005. GB_ASSERT(!sel.indirect);
  2006. elem = fv->value;
  2007. if (sel.index.count > 1) {
  2008. cgValue dst = cg_emit_deep_field_gep(p, comp_lit_ptr, sel);
  2009. field_expr = cg_build_expr(p, elem);
  2010. field_expr = cg_emit_conv(p, field_expr, sel.entity->type);
  2011. cg_emit_store(p, dst, field_expr);
  2012. continue;
  2013. }
  2014. index = sel.index[0];
  2015. } else {
  2016. Selection sel = lookup_field_from_index(bt, st->fields[field_index]->Variable.field_index);
  2017. GB_ASSERT(sel.index.count == 1);
  2018. GB_ASSERT(!sel.indirect);
  2019. index = sel.index[0];
  2020. }
  2021. field = st->fields[index];
  2022. Type *ft = field->type;
  2023. field_expr = cg_build_expr(p, elem);
  2024. cgValue gep = {};
  2025. if (st->is_raw_union) {
  2026. gep = cg_emit_conv(p, comp_lit_ptr, alloc_type_pointer(ft));
  2027. } else {
  2028. gep = cg_emit_struct_ep(p, comp_lit_ptr, cast(i32)index);
  2029. }
  2030. Type *fet = field_expr.type;
  2031. GB_ASSERT(fet->kind != Type_Tuple);
  2032. // HACK TODO(bill): THIS IS A MASSIVE HACK!!!!
  2033. if (is_type_union(ft) && !are_types_identical(fet, ft) && !is_type_untyped(fet)) {
  2034. GB_ASSERT_MSG(union_variant_index(ft, fet) >= 0, "%s", type_to_string(fet));
  2035. GB_PANIC("TODO(bill): cg_emit_store_union_variant");
  2036. // cg_emit_store_union_variant(p, gep, field_expr, fet);
  2037. } else {
  2038. cgValue fv = cg_emit_conv(p, field_expr, ft);
  2039. cg_emit_store(p, gep, fv);
  2040. }
  2041. }
  2042. return v;
  2043. }
  2044. // case Type_Map: {
  2045. // GB_ASSERT(!build_context.no_dynamic_literals);
  2046. // cgValue err = cg_dynamic_map_reserve(p, v.addr, 2*cl->elems.count, pos);
  2047. // gb_unused(err);
  2048. // for (Ast *elem : cl->elems) {
  2049. // ast_node(fv, FieldValue, elem);
  2050. // cgValue key = cg_build_expr(p, fv->field);
  2051. // cgValue value = cg_build_expr(p, fv->value);
  2052. // cg_internal_dynamic_map_set(p, v.addr, type, key, value, elem);
  2053. // }
  2054. // break;
  2055. // }
  2056. // case Type_Array: {
  2057. // cg_addr_store(p, v, cg_const_value(p->module, type, exact_value_compound(expr)));
  2058. // auto temp_data = array_make<cgCompoundLitElemTempData>(temporary_allocator(), 0, cl->elems.count);
  2059. // populate(p, cl->elems, &temp_data, type);
  2060. // cgValue dst_ptr = cg_addr_get_ptr(p, v);
  2061. // for_array(i, temp_data) {
  2062. // i32 index = cast(i32)(temp_data[i].elem_index);
  2063. // temp_data[i].gep = cg_emit_array_epi(p, dst_ptr, index);
  2064. // }
  2065. // assign_array(p, temp_data);
  2066. // break;
  2067. // }
  2068. // case Type_EnumeratedArray: {
  2069. // cg_addr_store(p, v, cg_const_value(p->module, type, exact_value_compound(expr)));
  2070. // auto temp_data = array_make<cgCompoundLitElemTempData>(temporary_allocator(), 0, cl->elems.count);
  2071. // populate(p, cl->elems, &temp_data, type);
  2072. // cgValue dst_ptr = cg_addr_get_ptr(p, v);
  2073. // i64 index_offset = exact_value_to_i64(*bt->EnumeratedArray.min_value);
  2074. // for_array(i, temp_data) {
  2075. // i32 index = cast(i32)(temp_data[i].elem_index - index_offset);
  2076. // temp_data[i].gep = cg_emit_array_epi(p, dst_ptr, index);
  2077. // }
  2078. // assign_array(p, temp_data);
  2079. // break;
  2080. // }
  2081. case Type_Slice: {
  2082. isize count = gb_max(cl->elems.count, cl->max_count);
  2083. TB_CharUnits backing_size = cast(TB_CharUnits)(type_size_of(bt->Slice.elem) * count);
  2084. TB_CharUnits align = cast(TB_CharUnits)type_align_of(bt->Slice.elem);
  2085. TB_Node *backing = tb_inst_local(p->func, backing_size, align);
  2086. cgValue data = cg_value(backing, alloc_type_multi_pointer(bt->Slice.elem));
  2087. auto temp_data = array_make<cgCompoundLitElemTempData>(temporary_allocator(), 0, cl->elems.count);
  2088. populate(p, cl->elems, &temp_data, type);
  2089. for_array(i, temp_data) {
  2090. temp_data[i].gep = cg_emit_ptr_offset(p, data, cg_const_int(p, t_int, temp_data[i].elem_index));
  2091. }
  2092. assign_array(p, temp_data);
  2093. cg_fill_slice(p, v, data, cg_const_int(p, t_int, cl->max_count));
  2094. return v;
  2095. }
  2096. // case Type_DynamicArray: {
  2097. // GB_ASSERT(!build_context.no_dynamic_literals);
  2098. // Type *et = bt->DynamicArray.elem;
  2099. // cgValue size = cg_const_int(p->module, t_int, type_size_of(et));
  2100. // cgValue align = cg_const_int(p->module, t_int, type_align_of(et));
  2101. // i64 item_count = gb_max(cl->max_count, cl->elems.count);
  2102. // {
  2103. // auto args = array_make<cgValue>(temporary_allocator(), 5);
  2104. // args[0] = cg_emit_conv(p, cg_addr_get_ptr(p, v), t_rawptr);
  2105. // args[1] = size;
  2106. // args[2] = align;
  2107. // args[3] = cg_const_int(p->module, t_int, item_count);
  2108. // args[4] = cg_emit_source_code_location_as_global(p, proc_name, pos);
  2109. // cg_emit_runtime_call(p, "__dynamic_array_reserve", args);
  2110. // }
  2111. // cgValue items = cg_generate_local_array(p, et, item_count);
  2112. // auto temp_data = array_make<cgCompoundLitElemTempData>(temporary_allocator(), 0, cl->elems.count);
  2113. // populate(p, cl->elems, &temp_data, type);
  2114. // for_array(i, temp_data) {
  2115. // temp_data[i].gep = cg_emit_array_epi(p, items, temp_data[i].elem_index);
  2116. // }
  2117. // assign_array(p, temp_data);
  2118. // {
  2119. // auto args = array_make<cgValue>(temporary_allocator(), 6);
  2120. // args[0] = cg_emit_conv(p, v.addr, t_rawptr);
  2121. // args[1] = size;
  2122. // args[2] = align;
  2123. // args[3] = cg_emit_conv(p, items, t_rawptr);
  2124. // args[4] = cg_const_int(p->module, t_int, item_count);
  2125. // args[5] = cg_emit_source_code_location_as_global(p, proc_name, pos);
  2126. // cg_emit_runtime_call(p, "__dynamic_array_append", args);
  2127. // }
  2128. // break;
  2129. // }
  2130. // case Type_Basic: {
  2131. // GB_ASSERT(is_type_any(bt));
  2132. // cg_addr_store(p, v, cg_const_value(p->module, type, exact_value_compound(expr)));
  2133. // String field_names[2] = {
  2134. // str_lit("data"),
  2135. // str_lit("id"),
  2136. // };
  2137. // Type *field_types[2] = {
  2138. // t_rawptr,
  2139. // t_typeid,
  2140. // };
  2141. // for_array(field_index, cl->elems) {
  2142. // Ast *elem = cl->elems[field_index];
  2143. // cgValue field_expr = {};
  2144. // isize index = field_index;
  2145. // if (elem->kind == Ast_FieldValue) {
  2146. // ast_node(fv, FieldValue, elem);
  2147. // Selection sel = lookup_field(bt, fv->field->Ident.token.string, false);
  2148. // index = sel.index[0];
  2149. // elem = fv->value;
  2150. // } else {
  2151. // TypeAndValue tav = type_and_value_of_expr(elem);
  2152. // Selection sel = lookup_field(bt, field_names[field_index], false);
  2153. // index = sel.index[0];
  2154. // }
  2155. // field_expr = cg_build_expr(p, elem);
  2156. // GB_ASSERT(field_expr.type->kind != Type_Tuple);
  2157. // Type *ft = field_types[index];
  2158. // cgValue fv = cg_emit_conv(p, field_expr, ft);
  2159. // cgValue gep = cg_emit_struct_ep(p, cg_addr_get_ptr(p, v), cast(i32)index);
  2160. // cg_emit_store(p, gep, fv);
  2161. // }
  2162. // break;
  2163. // }
  2164. case Type_BitSet: {
  2165. i64 sz = type_size_of(type);
  2166. if (sz == 0) {
  2167. return v;
  2168. }
  2169. cgValue lower = cg_const_value(p, t_int, exact_value_i64(bt->BitSet.lower));
  2170. Type *it = bit_set_to_int(bt);
  2171. cgValue one = cg_const_value(p, it, exact_value_i64(1));
  2172. for (Ast *elem : cl->elems) {
  2173. GB_ASSERT(elem->kind != Ast_FieldValue);
  2174. cgValue expr = cg_build_expr(p, elem);
  2175. GB_ASSERT(expr.type->kind != Type_Tuple);
  2176. cgValue e = cg_emit_conv(p, expr, it);
  2177. e = cg_emit_arith(p, Token_Sub, e, lower, it);
  2178. e = cg_emit_arith(p, Token_Shl, one, e, it);
  2179. cgValue old_value = cg_emit_transmute(p, cg_addr_load(p, v), it);
  2180. cgValue new_value = cg_emit_arith(p, Token_Or, old_value, e, it);
  2181. new_value = cg_emit_transmute(p, new_value, type);
  2182. cg_addr_store(p, v, new_value);
  2183. }
  2184. return v;
  2185. }
  2186. // case Type_Matrix: {
  2187. // cg_addr_store(p, v, cg_const_value(p->module, type, exact_value_compound(expr)));
  2188. // auto temp_data = array_make<cgCompoundLitElemTempData>(temporary_allocator(), 0, cl->elems.count);
  2189. // populate(p, cl->elems, &temp_data, type);
  2190. // cgValue dst_ptr = cg_addr_get_ptr(p, v);
  2191. // for_array(i, temp_data) {
  2192. // temp_data[i].gep = cg_emit_array_epi(p, dst_ptr, temp_data[i].elem_index);
  2193. // }
  2194. // assign_array(p, temp_data);
  2195. // break;
  2196. // }
  2197. // case Type_SimdVector: {
  2198. // cgValue vector_value = cg_const_value(p->module, type, exact_value_compound(expr));
  2199. // defer (cg_addr_store(p, v, vector_value));
  2200. // auto temp_data = array_make<cgCompoundLitElemTempData>(temporary_allocator(), 0, cl->elems.count);
  2201. // populate(p, cl->elems, &temp_data, type);
  2202. // // TODO(bill): reduce the need for individual `insertelement` if a `shufflevector`
  2203. // // might be a better option
  2204. // for (auto const &td : temp_data) {
  2205. // if (td.value.value != nullptr) {
  2206. // if (td.elem_length > 0) {
  2207. // for (i64 k = 0; k < td.elem_length; k++) {
  2208. // LLVMValueRef index = cg_const_int(p->module, t_u32, td.elem_index + k).value;
  2209. // vector_value.value = LLVMBuildInsertElement(p->builder, vector_value.value, td.value.value, index, "");
  2210. // }
  2211. // } else {
  2212. // LLVMValueRef index = cg_const_int(p->module, t_u32, td.elem_index).value;
  2213. // vector_value.value = LLVMBuildInsertElement(p->builder, vector_value.value, td.value.value, index, "");
  2214. // }
  2215. // }
  2216. // }
  2217. // break;
  2218. // }
  2219. }
  2220. return v;
  2221. }
  2222. gb_internal cgValue cg_build_unary_and(cgProcedure *p, Ast *expr) {
  2223. ast_node(ue, UnaryExpr, expr);
  2224. auto tv = type_and_value_of_expr(expr);
  2225. Ast *ue_expr = unparen_expr(ue->expr);
  2226. if (ue_expr->kind == Ast_IndexExpr && tv.mode == Addressing_OptionalOkPtr && is_type_tuple(tv.type)) {
  2227. GB_PANIC("TODO(bill): &m[k]");
  2228. // Type *tuple = tv.type;
  2229. // Type *map_type = type_of_expr(ue_expr->IndexExpr.expr);
  2230. // Type *ot = base_type(map_type);
  2231. // Type *t = base_type(type_deref(ot));
  2232. // bool deref = t != ot;
  2233. // GB_ASSERT(t->kind == Type_Map);
  2234. // ast_node(ie, IndexExpr, ue_expr);
  2235. // cgValue map_val = cg_build_addr_ptr(p, ie->expr);
  2236. // if (deref) {
  2237. // map_val = cg_emit_load(p, map_val);
  2238. // }
  2239. // cgValue key = lb_build_expr(p, ie->index);
  2240. // key = lb_emit_conv(p, key, t->Map.key);
  2241. // lbAddr addr = lb_addr_map(map_val, key, t, alloc_type_pointer(t->Map.value));
  2242. // lbValue ptr = lb_addr_get_ptr(p, addr);
  2243. // lbValue ok = lb_emit_comp_against_nil(p, Token_NotEq, ptr);
  2244. // ok = lb_emit_conv(p, ok, tuple->Tuple.variables[1]->type);
  2245. // lbAddr res = lb_add_local_generated(p, tuple, false);
  2246. // lbValue gep0 = lb_emit_struct_ep(p, res.addr, 0);
  2247. // lbValue gep1 = lb_emit_struct_ep(p, res.addr, 1);
  2248. // lb_emit_store(p, gep0, ptr);
  2249. // lb_emit_store(p, gep1, ok);
  2250. // return lb_addr_load(p, res);
  2251. } else if (is_type_soa_pointer(tv.type)) {
  2252. GB_PANIC("TODO(bill): &soa[i]");
  2253. // ast_node(ie, IndexExpr, ue_expr);
  2254. // lbValue addr = lb_build_addr_ptr(p, ie->expr);
  2255. // lbValue index = lb_build_expr(p, ie->index);
  2256. // if (!build_context.no_bounds_check) {
  2257. // // TODO(bill): soa bounds checking
  2258. // }
  2259. // return lb_make_soa_pointer(p, tv.type, addr, index);
  2260. } else if (ue_expr->kind == Ast_CompoundLit) {
  2261. cgAddr addr = cg_build_addr_compound_lit(p, expr);
  2262. return addr.addr;
  2263. } else if (ue_expr->kind == Ast_TypeAssertion) {
  2264. GB_PANIC("TODO(bill): &v.(T)");
  2265. // if (is_type_tuple(tv.type)) {
  2266. // Type *tuple = tv.type;
  2267. // Type *ptr_type = tuple->Tuple.variables[0]->type;
  2268. // Type *ok_type = tuple->Tuple.variables[1]->type;
  2269. // ast_node(ta, TypeAssertion, ue_expr);
  2270. // TokenPos pos = ast_token(expr).pos;
  2271. // Type *type = type_of_expr(ue_expr);
  2272. // GB_ASSERT(!is_type_tuple(type));
  2273. // lbValue e = lb_build_expr(p, ta->expr);
  2274. // Type *t = type_deref(e.type);
  2275. // if (is_type_union(t)) {
  2276. // lbValue v = e;
  2277. // if (!is_type_pointer(v.type)) {
  2278. // v = lb_address_from_load_or_generate_local(p, v);
  2279. // }
  2280. // Type *src_type = type_deref(v.type);
  2281. // Type *dst_type = type;
  2282. // lbValue src_tag = {};
  2283. // lbValue dst_tag = {};
  2284. // if (is_type_union_maybe_pointer(src_type)) {
  2285. // src_tag = lb_emit_comp_against_nil(p, Token_NotEq, v);
  2286. // dst_tag = lb_const_bool(p->module, t_bool, true);
  2287. // } else {
  2288. // src_tag = lb_emit_load(p, lb_emit_union_tag_ptr(p, v));
  2289. // dst_tag = lb_const_union_tag(p->module, src_type, dst_type);
  2290. // }
  2291. // lbValue ok = lb_emit_comp(p, Token_CmpEq, src_tag, dst_tag);
  2292. // lbValue data_ptr = lb_emit_conv(p, v, ptr_type);
  2293. // lbAddr res = lb_add_local_generated(p, tuple, true);
  2294. // lbValue gep0 = lb_emit_struct_ep(p, res.addr, 0);
  2295. // lbValue gep1 = lb_emit_struct_ep(p, res.addr, 1);
  2296. // lb_emit_store(p, gep0, lb_emit_select(p, ok, data_ptr, lb_const_nil(p->module, ptr_type)));
  2297. // lb_emit_store(p, gep1, lb_emit_conv(p, ok, ok_type));
  2298. // return lb_addr_load(p, res);
  2299. // } else if (is_type_any(t)) {
  2300. // lbValue v = e;
  2301. // if (is_type_pointer(v.type)) {
  2302. // v = lb_emit_load(p, v);
  2303. // }
  2304. // lbValue data_ptr = lb_emit_conv(p, lb_emit_struct_ev(p, v, 0), ptr_type);
  2305. // lbValue any_id = lb_emit_struct_ev(p, v, 1);
  2306. // lbValue id = lb_typeid(p->module, type);
  2307. // lbValue ok = lb_emit_comp(p, Token_CmpEq, any_id, id);
  2308. // lbAddr res = lb_add_local_generated(p, tuple, false);
  2309. // lbValue gep0 = lb_emit_struct_ep(p, res.addr, 0);
  2310. // lbValue gep1 = lb_emit_struct_ep(p, res.addr, 1);
  2311. // lb_emit_store(p, gep0, lb_emit_select(p, ok, data_ptr, lb_const_nil(p->module, ptr_type)));
  2312. // lb_emit_store(p, gep1, lb_emit_conv(p, ok, ok_type));
  2313. // return lb_addr_load(p, res);
  2314. // } else {
  2315. // GB_PANIC("TODO(bill): type assertion %s", type_to_string(type));
  2316. // }
  2317. // } else {
  2318. // GB_ASSERT(is_type_pointer(tv.type));
  2319. // ast_node(ta, TypeAssertion, ue_expr);
  2320. // TokenPos pos = ast_token(expr).pos;
  2321. // Type *type = type_of_expr(ue_expr);
  2322. // GB_ASSERT(!is_type_tuple(type));
  2323. // lbValue e = lb_build_expr(p, ta->expr);
  2324. // Type *t = type_deref(e.type);
  2325. // if (is_type_union(t)) {
  2326. // lbValue v = e;
  2327. // if (!is_type_pointer(v.type)) {
  2328. // v = lb_address_from_load_or_generate_local(p, v);
  2329. // }
  2330. // Type *src_type = type_deref(v.type);
  2331. // Type *dst_type = type;
  2332. // if ((p->state_flags & StateFlag_no_type_assert) == 0) {
  2333. // lbValue src_tag = {};
  2334. // lbValue dst_tag = {};
  2335. // if (is_type_union_maybe_pointer(src_type)) {
  2336. // src_tag = lb_emit_comp_against_nil(p, Token_NotEq, v);
  2337. // dst_tag = lb_const_bool(p->module, t_bool, true);
  2338. // } else {
  2339. // src_tag = lb_emit_load(p, lb_emit_union_tag_ptr(p, v));
  2340. // dst_tag = lb_const_union_tag(p->module, src_type, dst_type);
  2341. // }
  2342. // isize arg_count = 6;
  2343. // if (build_context.no_rtti) {
  2344. // arg_count = 4;
  2345. // }
  2346. // lbValue ok = lb_emit_comp(p, Token_CmpEq, src_tag, dst_tag);
  2347. // auto args = array_make<lbValue>(permanent_allocator(), arg_count);
  2348. // args[0] = ok;
  2349. // args[1] = lb_find_or_add_entity_string(p->module, get_file_path_string(pos.file_id));
  2350. // args[2] = lb_const_int(p->module, t_i32, pos.line);
  2351. // args[3] = lb_const_int(p->module, t_i32, pos.column);
  2352. // if (!build_context.no_rtti) {
  2353. // args[4] = lb_typeid(p->module, src_type);
  2354. // args[5] = lb_typeid(p->module, dst_type);
  2355. // }
  2356. // lb_emit_runtime_call(p, "type_assertion_check", args);
  2357. // }
  2358. // lbValue data_ptr = v;
  2359. // return lb_emit_conv(p, data_ptr, tv.type);
  2360. // } else if (is_type_any(t)) {
  2361. // lbValue v = e;
  2362. // if (is_type_pointer(v.type)) {
  2363. // v = lb_emit_load(p, v);
  2364. // }
  2365. // lbValue data_ptr = lb_emit_struct_ev(p, v, 0);
  2366. // if ((p->state_flags & StateFlag_no_type_assert) == 0) {
  2367. // GB_ASSERT(!build_context.no_rtti);
  2368. // lbValue any_id = lb_emit_struct_ev(p, v, 1);
  2369. // lbValue id = lb_typeid(p->module, type);
  2370. // lbValue ok = lb_emit_comp(p, Token_CmpEq, any_id, id);
  2371. // auto args = array_make<lbValue>(permanent_allocator(), 6);
  2372. // args[0] = ok;
  2373. // args[1] = lb_find_or_add_entity_string(p->module, get_file_path_string(pos.file_id));
  2374. // args[2] = lb_const_int(p->module, t_i32, pos.line);
  2375. // args[3] = lb_const_int(p->module, t_i32, pos.column);
  2376. // args[4] = any_id;
  2377. // args[5] = id;
  2378. // lb_emit_runtime_call(p, "type_assertion_check", args);
  2379. // }
  2380. // return lb_emit_conv(p, data_ptr, tv.type);
  2381. // } else {
  2382. // GB_PANIC("TODO(bill): type assertion %s", type_to_string(type));
  2383. // }
  2384. // }
  2385. }
  2386. return cg_build_addr_ptr(p, ue->expr);
  2387. }
  2388. gb_internal cgValue cg_build_expr_internal(cgProcedure *p, Ast *expr) {
  2389. expr = unparen_expr(expr);
  2390. TokenPos expr_pos = ast_token(expr).pos;
  2391. TypeAndValue tv = type_and_value_of_expr(expr);
  2392. Type *type = type_of_expr(expr);
  2393. GB_ASSERT_MSG(tv.mode != Addressing_Invalid, "invalid expression '%s' (tv.mode = %d, tv.type = %s) @ %s\n Current Proc: %.*s : %s", expr_to_string(expr), tv.mode, type_to_string(tv.type), token_pos_to_string(expr_pos), LIT(p->name), type_to_string(p->type));
  2394. if (tv.value.kind != ExactValue_Invalid &&
  2395. expr->kind != Ast_CompoundLit) {
  2396. // NOTE(bill): The commented out code below is just for debug purposes only
  2397. // if (is_type_untyped(type)) {
  2398. // gb_printf_err("%s %s : %s @ %p\n", token_pos_to_string(expr_pos), expr_to_string(expr), type_to_string(expr->tav.type), expr);
  2399. // GB_PANIC("%s\n", type_to_string(tv.type));
  2400. // }
  2401. // NOTE(bill): Short on constant values
  2402. return cg_const_value(p, type, tv.value);
  2403. } else if (tv.mode == Addressing_Type) {
  2404. // NOTE(bill, 2023-01-16): is this correct? I hope so at least
  2405. return cg_typeid(p, tv.type);
  2406. }
  2407. switch (expr->kind) {
  2408. case_ast_node(bl, BasicLit, expr);
  2409. TokenPos pos = bl->token.pos;
  2410. GB_PANIC("Non-constant basic literal %s - %.*s", token_pos_to_string(pos), LIT(token_strings[bl->token.kind]));
  2411. case_end;
  2412. case_ast_node(bd, BasicDirective, expr);
  2413. TokenPos pos = bd->token.pos;
  2414. GB_PANIC("Non-constant basic literal %s - %.*s", token_pos_to_string(pos), LIT(bd->name.string));
  2415. case_end;
  2416. case_ast_node(i, Ident, expr);
  2417. Entity *e = entity_from_expr(expr);
  2418. e = strip_entity_wrapping(e);
  2419. GB_ASSERT_MSG(e != nullptr, "%s in %.*s %p", expr_to_string(expr), LIT(p->name), expr);
  2420. if (e->kind == Entity_Builtin) {
  2421. Token token = ast_token(expr);
  2422. GB_PANIC("TODO(bill): lb_build_expr Entity_Builtin '%.*s'\n"
  2423. "\t at %s", LIT(builtin_procs[e->Builtin.id].name),
  2424. token_pos_to_string(token.pos));
  2425. return {};
  2426. } else if (e->kind == Entity_Nil) {
  2427. // TODO(bill): is this correct?
  2428. return cg_value(cast(TB_Node *)nullptr, e->type);
  2429. }
  2430. GB_ASSERT(e->kind != Entity_ProcGroup);
  2431. cgAddr *addr = map_get(&p->variable_map, e);
  2432. if (addr) {
  2433. return cg_addr_load(p, *addr);
  2434. }
  2435. return cg_find_ident(p, e, expr);
  2436. case_end;
  2437. case_ast_node(i, Implicit, expr);
  2438. return cg_addr_load(p, cg_build_addr(p, expr));
  2439. case_end;
  2440. case_ast_node(u, Uninit, expr);
  2441. if (is_type_untyped(type)) {
  2442. return cg_value(cast(TB_Node *)nullptr, t_untyped_uninit);
  2443. }
  2444. return cg_value(tb_inst_poison(p->func), type);
  2445. case_end;
  2446. case_ast_node(de, DerefExpr, expr);
  2447. return cg_addr_load(p, cg_build_addr(p, expr));
  2448. case_end;
  2449. case_ast_node(se, SelectorExpr, expr);
  2450. TypeAndValue tav = type_and_value_of_expr(expr);
  2451. GB_ASSERT(tav.mode != Addressing_Invalid);
  2452. return cg_addr_load(p, cg_build_addr(p, expr));
  2453. case_end;
  2454. case_ast_node(ise, ImplicitSelectorExpr, expr);
  2455. TypeAndValue tav = type_and_value_of_expr(expr);
  2456. GB_ASSERT(tav.mode == Addressing_Constant);
  2457. return cg_const_value(p, type, tv.value);
  2458. case_end;
  2459. case_ast_node(se, SelectorCallExpr, expr);
  2460. GB_ASSERT(se->modified_call);
  2461. return cg_build_call_expr(p, se->call);
  2462. case_end;
  2463. case_ast_node(i, CallExpr, expr);
  2464. return cg_build_call_expr(p, expr);
  2465. case_end;
  2466. case_ast_node(cl, CompoundLit, expr);
  2467. cgAddr addr = cg_build_addr_compound_lit(p, expr);
  2468. return cg_addr_load(p, addr);
  2469. case_end;
  2470. case_ast_node(te, TernaryIfExpr, expr);
  2471. cgValue incoming_values[2] = {};
  2472. TB_Node *incoming_regions[2] = {};
  2473. TB_Node *then = cg_control_region(p, "if_then");
  2474. TB_Node *done = cg_control_region(p, "if_done");
  2475. TB_Node *else_ = cg_control_region(p, "if_else");
  2476. cg_build_cond(p, te->cond, then, else_);
  2477. tb_inst_set_control(p->func, then);
  2478. Type *type = default_type(type_of_expr(expr));
  2479. incoming_values [0] = cg_emit_conv(p, cg_build_expr(p, te->x), type);
  2480. incoming_regions[0] = tb_inst_get_control(p->func);
  2481. cg_emit_goto(p, done);
  2482. tb_inst_set_control(p->func, else_);
  2483. incoming_values [1] = cg_emit_conv(p, cg_build_expr(p, te->y), type);
  2484. incoming_regions[1] = tb_inst_get_control(p->func);
  2485. cg_emit_goto(p, done);
  2486. tb_inst_set_control(p->func, done);
  2487. GB_ASSERT(incoming_values[0].kind == cgValue_Value ||
  2488. incoming_values[0].kind == cgValue_Addr);
  2489. GB_ASSERT(incoming_values[0].kind == incoming_values[1].kind);
  2490. cgValue res = {};
  2491. res.kind = incoming_values[0].kind;
  2492. res.type = type;
  2493. TB_DataType dt = cg_data_type(type);
  2494. if (res.kind == cgValue_Addr) {
  2495. dt = TB_TYPE_PTR;
  2496. }
  2497. res.node = tb_inst_incomplete_phi(p->func, dt, done, 2);
  2498. tb_inst_add_phi_operand(p->func, res.node, incoming_regions[0], incoming_values[0].node);
  2499. tb_inst_add_phi_operand(p->func, res.node, incoming_regions[1], incoming_values[1].node);
  2500. return res;
  2501. case_end;
  2502. case_ast_node(te, TernaryWhenExpr, expr);
  2503. TypeAndValue tav = type_and_value_of_expr(te->cond);
  2504. GB_ASSERT(tav.mode == Addressing_Constant);
  2505. GB_ASSERT(tav.value.kind == ExactValue_Bool);
  2506. if (tav.value.value_bool) {
  2507. return cg_build_expr(p, te->x);
  2508. } else {
  2509. return cg_build_expr(p, te->y);
  2510. }
  2511. case_end;
  2512. case_ast_node(tc, TypeCast, expr);
  2513. cgValue e = cg_build_expr(p, tc->expr);
  2514. switch (tc->token.kind) {
  2515. case Token_cast:
  2516. return cg_emit_conv(p, e, type);
  2517. case Token_transmute:
  2518. return cg_emit_transmute(p, e, type);
  2519. }
  2520. GB_PANIC("Invalid AST TypeCast");
  2521. case_end;
  2522. case_ast_node(ac, AutoCast, expr);
  2523. cgValue value = cg_build_expr(p, ac->expr);
  2524. return cg_emit_conv(p, value, type);
  2525. case_end;
  2526. case_ast_node(se, SliceExpr, expr);
  2527. if (is_type_slice(type_of_expr(se->expr))) {
  2528. // NOTE(bill): Quick optimization
  2529. if (se->high == nullptr &&
  2530. (se->low == nullptr || cg_is_expr_constant_zero(se->low))) {
  2531. return cg_build_expr(p, se->expr);
  2532. }
  2533. }
  2534. return cg_addr_load(p, cg_build_addr(p, expr));
  2535. case_end;
  2536. case_ast_node(ie, IndexExpr, expr);
  2537. return cg_addr_load(p, cg_build_addr(p, expr));
  2538. case_end;
  2539. case_ast_node(ie, MatrixIndexExpr, expr);
  2540. return cg_addr_load(p, cg_build_addr(p, expr));
  2541. case_end;
  2542. case_ast_node(ue, UnaryExpr, expr);
  2543. if (ue->op.kind == Token_And) {
  2544. return cg_build_unary_and(p, expr);
  2545. }
  2546. cgValue v = cg_build_expr(p, ue->expr);
  2547. return cg_emit_unary_arith(p, ue->op.kind, v, type);
  2548. case_end;
  2549. case_ast_node(be, BinaryExpr, expr);
  2550. return cg_build_binary_expr(p, expr);
  2551. case_end;
  2552. case_ast_node(oe, OrReturnExpr, expr);
  2553. return cg_build_or_return(p, oe->expr, tv.type);
  2554. case_end;
  2555. case_ast_node(oe, OrElseExpr, expr);
  2556. return cg_build_or_else(p, oe->x, oe->y, tv.type);
  2557. case_end;
  2558. }
  2559. GB_PANIC("TODO(bill): cg_build_expr_internal %.*s", LIT(ast_strings[expr->kind]));
  2560. return {};
  2561. }
  2562. gb_internal cgValue cg_build_addr_ptr(cgProcedure *p, Ast *expr) {
  2563. cgAddr addr = cg_build_addr(p, expr);
  2564. return cg_addr_get_ptr(p, addr);
  2565. }
  2566. gb_internal cgAddr cg_build_addr_internal(cgProcedure *p, Ast *expr);
  2567. gb_internal cgAddr cg_build_addr(cgProcedure *p, Ast *expr) {
  2568. expr = unparen_expr(expr);
  2569. // IMPORTANT NOTE(bill):
  2570. // Selector Call Expressions (foo->bar(...))
  2571. // must only evaluate `foo` once as it gets transformed into
  2572. // `foo.bar(foo, ...)`
  2573. // And if `foo` is a procedure call or something more complex, storing the value
  2574. // once is a very good idea
  2575. // If a stored value is found, it must be removed from the cache
  2576. if (expr->state_flags & StateFlag_SelectorCallExpr) {
  2577. // lbAddr *pp = map_get(&p->selector_addr, expr);
  2578. // if (pp != nullptr) {
  2579. // lbAddr res = *pp;
  2580. // map_remove(&p->selector_addr, expr);
  2581. // return res;
  2582. // }
  2583. }
  2584. cgAddr addr = cg_build_addr_internal(p, expr);
  2585. if (expr->state_flags & StateFlag_SelectorCallExpr) {
  2586. // map_set(&p->selector_addr, expr, addr);
  2587. }
  2588. return addr;
  2589. }
  2590. gb_internal cgAddr cg_build_addr_index_expr(cgProcedure *p, Ast *expr) {
  2591. ast_node(ie, IndexExpr, expr);
  2592. Type *t = base_type(type_of_expr(ie->expr));
  2593. bool deref = is_type_pointer(t);
  2594. t = base_type(type_deref(t));
  2595. if (is_type_soa_struct(t)) {
  2596. GB_PANIC("TODO(bill): #soa");
  2597. // // SOA STRUCTURES!!!!
  2598. // lbValue val = cg_build_addr_ptr(p, ie->expr);
  2599. // if (deref) {
  2600. // val = cg_emit_load(p, val);
  2601. // }
  2602. // cgValue index = cg_build_expr(p, ie->index);
  2603. // return cg_addr_soa_variable(val, index, ie->index);
  2604. }
  2605. if (ie->expr->tav.mode == Addressing_SoaVariable) {
  2606. GB_PANIC("TODO(bill): #soa");
  2607. // // SOA Structures for slices/dynamic arrays
  2608. // GB_ASSERT(is_type_pointer(type_of_expr(ie->expr)));
  2609. // lbValue field = lb_build_expr(p, ie->expr);
  2610. // lbValue index = lb_build_expr(p, ie->index);
  2611. // if (!build_context.no_bounds_check) {
  2612. // // TODO HACK(bill): Clean up this hack to get the length for bounds checking
  2613. // // GB_ASSERT(LLVMIsALoadInst(field.value));
  2614. // // lbValue a = {};
  2615. // // a.value = LLVMGetOperand(field.value, 0);
  2616. // // a.type = alloc_type_pointer(field.type);
  2617. // // irInstr *b = &a->Instr;
  2618. // // GB_ASSERT(b->kind == irInstr_StructElementPtr);
  2619. // // lbValue base_struct = b->StructElementPtr.address;
  2620. // // GB_ASSERT(is_type_soa_struct(type_deref(ir_type(base_struct))));
  2621. // // lbValue len = ir_soa_struct_len(p, base_struct);
  2622. // // lb_emit_bounds_check(p, ast_token(ie->index), index, len);
  2623. // }
  2624. // lbValue val = lb_emit_ptr_offset(p, field, index);
  2625. // return lb_addr(val);
  2626. }
  2627. GB_ASSERT_MSG(is_type_indexable(t), "%s %s", type_to_string(t), expr_to_string(expr));
  2628. if (is_type_map(t)) {
  2629. GB_PANIC("TODO(bill): map indexing");
  2630. // lbAddr map_addr = lb_build_addr(p, ie->expr);
  2631. // lbValue key = lb_build_expr(p, ie->index);
  2632. // key = lb_emit_conv(p, key, t->Map.key);
  2633. // Type *result_type = type_of_expr(expr);
  2634. // lbValue map_ptr = lb_addr_get_ptr(p, map_addr);
  2635. // if (is_type_pointer(type_deref(map_ptr.type))) {
  2636. // map_ptr = lb_emit_load(p, map_ptr);
  2637. // }
  2638. // return lb_addr_map(map_ptr, key, t, result_type);
  2639. }
  2640. switch (t->kind) {
  2641. case Type_Array: {
  2642. cgValue array = {};
  2643. array = cg_build_addr_ptr(p, ie->expr);
  2644. if (deref) {
  2645. array = cg_emit_load(p, array);
  2646. }
  2647. cgValue index = cg_build_expr(p, ie->index);
  2648. index = cg_emit_conv(p, index, t_int);
  2649. cgValue elem = cg_emit_array_ep(p, array, index);
  2650. auto index_tv = type_and_value_of_expr(ie->index);
  2651. if (index_tv.mode != Addressing_Constant) {
  2652. // cgValue len = cg_const_int(p->module, t_int, t->Array.count);
  2653. // cg_emit_bounds_check(p, ast_token(ie->index), index, len);
  2654. }
  2655. return cg_addr(elem);
  2656. }
  2657. case Type_EnumeratedArray: {
  2658. cgValue array = {};
  2659. array = cg_build_addr_ptr(p, ie->expr);
  2660. if (deref) {
  2661. array = cg_emit_load(p, array);
  2662. }
  2663. Type *index_type = t->EnumeratedArray.index;
  2664. auto index_tv = type_and_value_of_expr(ie->index);
  2665. cgValue index = {};
  2666. if (compare_exact_values(Token_NotEq, *t->EnumeratedArray.min_value, exact_value_i64(0))) {
  2667. if (index_tv.mode == Addressing_Constant) {
  2668. ExactValue idx = exact_value_sub(index_tv.value, *t->EnumeratedArray.min_value);
  2669. index = cg_const_value(p, index_type, idx);
  2670. } else {
  2671. index = cg_emit_arith(p, Token_Sub,
  2672. cg_build_expr(p, ie->index),
  2673. cg_const_value(p, index_type, *t->EnumeratedArray.min_value),
  2674. index_type);
  2675. index = cg_emit_conv(p, index, t_int);
  2676. }
  2677. } else {
  2678. index = cg_emit_conv(p, cg_build_expr(p, ie->index), t_int);
  2679. }
  2680. cgValue elem = cg_emit_array_ep(p, array, index);
  2681. if (index_tv.mode != Addressing_Constant) {
  2682. // cgValue len = cg_const_int(p->module, t_int, t->EnumeratedArray.count);
  2683. // cg_emit_bounds_check(p, ast_token(ie->index), index, len);
  2684. }
  2685. return cg_addr(elem);
  2686. }
  2687. case Type_Slice: {
  2688. cgValue slice = {};
  2689. slice = cg_build_expr(p, ie->expr);
  2690. if (deref) {
  2691. slice = cg_emit_load(p, slice);
  2692. }
  2693. cgValue elem = cg_builtin_raw_data(p, slice);
  2694. cgValue index = cg_emit_conv(p, cg_build_expr(p, ie->index), t_int);
  2695. // cgValue len = cg_builtin_len(p, slice);
  2696. // cg_emit_bounds_check(p, ast_token(ie->index), index, len);
  2697. cgValue v = cg_emit_ptr_offset(p, elem, index);
  2698. v.type = alloc_type_pointer(type_deref(v.type, true));
  2699. return cg_addr(v);
  2700. }
  2701. case Type_MultiPointer: {
  2702. cgValue multi_ptr = {};
  2703. multi_ptr = cg_build_expr(p, ie->expr);
  2704. if (deref) {
  2705. multi_ptr = cg_emit_load(p, multi_ptr);
  2706. }
  2707. cgValue index = cg_build_expr(p, ie->index);
  2708. index = cg_emit_conv(p, index, t_int);
  2709. cgValue v = cg_emit_ptr_offset(p, multi_ptr, index);
  2710. v.type = alloc_type_pointer(type_deref(v.type, true));
  2711. return cg_addr(v);
  2712. }
  2713. case Type_RelativeSlice: {
  2714. GB_PANIC("TODO(bill): relative slice");
  2715. // lbAddr slice_addr = {};
  2716. // if (deref) {
  2717. // slice_addr = lb_addr(lb_build_expr(p, ie->expr));
  2718. // } else {
  2719. // slice_addr = lb_build_addr(p, ie->expr);
  2720. // }
  2721. // lbValue slice = lb_addr_load(p, slice_addr);
  2722. // lbValue elem = lb_slice_elem(p, slice);
  2723. // lbValue index = lb_emit_conv(p, lb_build_expr(p, ie->index), t_int);
  2724. // lbValue len = lb_slice_len(p, slice);
  2725. // lb_emit_bounds_check(p, ast_token(ie->index), index, len);
  2726. // lbValue v = lb_emit_ptr_offset(p, elem, index);
  2727. // return lb_addr(v);
  2728. }
  2729. case Type_DynamicArray: {
  2730. cgValue dynamic_array = {};
  2731. dynamic_array = cg_build_expr(p, ie->expr);
  2732. if (deref) {
  2733. dynamic_array = cg_emit_load(p, dynamic_array);
  2734. }
  2735. cgValue elem = cg_builtin_raw_data(p, dynamic_array);
  2736. cgValue index = cg_emit_conv(p, cg_build_expr(p, ie->index), t_int);
  2737. // cgValue len = cg_dynamic_array_len(p, dynamic_array);
  2738. // cg_emit_bounds_check(p, ast_token(ie->index), index, len);
  2739. cgValue v = cg_emit_ptr_offset(p, elem, index);
  2740. v.type = alloc_type_pointer(type_deref(v.type, true));
  2741. return cg_addr(v);
  2742. }
  2743. case Type_Matrix: {
  2744. GB_PANIC("TODO(bill): matrix");
  2745. // lbValue matrix = {};
  2746. // matrix = lb_build_addr_ptr(p, ie->expr);
  2747. // if (deref) {
  2748. // matrix = lb_emit_load(p, matrix);
  2749. // }
  2750. // lbValue index = lb_build_expr(p, ie->index);
  2751. // index = lb_emit_conv(p, index, t_int);
  2752. // lbValue elem = lb_emit_matrix_ep(p, matrix, lb_const_int(p->module, t_int, 0), index);
  2753. // elem = lb_emit_conv(p, elem, alloc_type_pointer(type_of_expr(expr)));
  2754. // auto index_tv = type_and_value_of_expr(ie->index);
  2755. // if (index_tv.mode != Addressing_Constant) {
  2756. // lbValue len = lb_const_int(p->module, t_int, t->Matrix.column_count);
  2757. // lb_emit_bounds_check(p, ast_token(ie->index), index, len);
  2758. // }
  2759. // return lb_addr(elem);
  2760. }
  2761. case Type_Basic: { // Basic_string
  2762. cgValue str;
  2763. cgValue elem;
  2764. cgValue len;
  2765. cgValue index;
  2766. str = cg_build_expr(p, ie->expr);
  2767. if (deref) {
  2768. str = cg_emit_load(p, str);
  2769. }
  2770. elem = cg_builtin_raw_data(p, str);
  2771. len = cg_builtin_len(p, str);
  2772. index = cg_emit_conv(p, cg_build_expr(p, ie->index), t_int);
  2773. // cg_emit_bounds_check(p, ast_token(ie->index), index, len);
  2774. cgValue v = cg_emit_ptr_offset(p, elem, index);
  2775. v.type = alloc_type_pointer(type_deref(v.type, true));
  2776. return cg_addr(v);
  2777. }
  2778. }
  2779. return {};
  2780. }
  2781. gb_internal cgAddr cg_build_addr_internal(cgProcedure *p, Ast *expr) {
  2782. switch (expr->kind) {
  2783. case_ast_node(i, Implicit, expr);
  2784. cgAddr v = {};
  2785. switch (i->kind) {
  2786. case Token_context:
  2787. v = cg_find_or_generate_context_ptr(p);
  2788. break;
  2789. }
  2790. GB_ASSERT(v.addr.node != nullptr);
  2791. return v;
  2792. case_end;
  2793. case_ast_node(i, Ident, expr);
  2794. if (is_blank_ident(expr)) {
  2795. cgAddr val = {};
  2796. return val;
  2797. }
  2798. String name = i->token.string;
  2799. Entity *e = entity_of_node(expr);
  2800. return cg_build_addr_from_entity(p, e, expr);
  2801. case_end;
  2802. case_ast_node(de, DerefExpr, expr);
  2803. Type *t = type_of_expr(de->expr);
  2804. if (is_type_relative_pointer(t)) {
  2805. cgAddr addr = cg_build_addr(p, de->expr);
  2806. addr.relative.deref = true;
  2807. return addr;
  2808. } else if (is_type_soa_pointer(t)) {
  2809. cgValue value = cg_build_expr(p, de->expr);
  2810. cgValue ptr = cg_emit_struct_ev(p, value, 0);
  2811. cgValue idx = cg_emit_struct_ev(p, value, 1);
  2812. GB_PANIC("TODO(bill): cg_addr_soa_variable");
  2813. // return cg_addr_soa_variable(ptr, idx, nullptr);
  2814. }
  2815. cgValue addr = cg_build_expr(p, de->expr);
  2816. return cg_addr(addr);
  2817. case_end;
  2818. case_ast_node(ie, IndexExpr, expr);
  2819. return cg_build_addr_index_expr(p, expr);
  2820. case_end;
  2821. case_ast_node(se, SliceExpr, expr);
  2822. return cg_build_addr_slice_expr(p, expr);
  2823. case_end;
  2824. case_ast_node(se, SelectorExpr, expr);
  2825. Ast *sel_node = unparen_expr(se->selector);
  2826. if (sel_node->kind != Ast_Ident) {
  2827. GB_PANIC("Unsupported selector expression");
  2828. }
  2829. String selector = sel_node->Ident.token.string;
  2830. TypeAndValue tav = type_and_value_of_expr(se->expr);
  2831. if (tav.mode == Addressing_Invalid) {
  2832. // NOTE(bill): Imports
  2833. Entity *imp = entity_of_node(se->expr);
  2834. if (imp != nullptr) {
  2835. GB_ASSERT(imp->kind == Entity_ImportName);
  2836. }
  2837. return cg_build_addr(p, unparen_expr(se->selector));
  2838. }
  2839. Type *type = base_type(tav.type);
  2840. if (tav.mode == Addressing_Type) { // Addressing_Type
  2841. Selection sel = lookup_field(tav.type, selector, true);
  2842. if (sel.pseudo_field) {
  2843. GB_ASSERT(sel.entity->kind == Entity_Procedure);
  2844. return cg_addr(cg_find_value_from_entity(p->module, sel.entity));
  2845. }
  2846. GB_PANIC("Unreachable %.*s", LIT(selector));
  2847. }
  2848. if (se->swizzle_count > 0) {
  2849. Type *array_type = base_type(type_deref(tav.type));
  2850. GB_ASSERT(array_type->kind == Type_Array);
  2851. u8 swizzle_count = se->swizzle_count;
  2852. u8 swizzle_indices_raw = se->swizzle_indices;
  2853. u8 swizzle_indices[4] = {};
  2854. for (u8 i = 0; i < swizzle_count; i++) {
  2855. u8 index = swizzle_indices_raw>>(i*2) & 3;
  2856. swizzle_indices[i] = index;
  2857. }
  2858. cgValue a = {};
  2859. if (is_type_pointer(tav.type)) {
  2860. a = cg_build_expr(p, se->expr);
  2861. } else {
  2862. cgAddr addr = cg_build_addr(p, se->expr);
  2863. a = cg_addr_get_ptr(p, addr);
  2864. }
  2865. GB_ASSERT(is_type_array(expr->tav.type));
  2866. GB_PANIC("TODO(bill): cg_addr_swizzle");
  2867. // return cg_addr_swizzle(a, expr->tav.type, swizzle_count, swizzle_indices);
  2868. }
  2869. Selection sel = lookup_field(type, selector, false);
  2870. GB_ASSERT(sel.entity != nullptr);
  2871. if (sel.pseudo_field) {
  2872. GB_ASSERT(sel.entity->kind == Entity_Procedure);
  2873. Entity *e = entity_of_node(sel_node);
  2874. return cg_addr(cg_find_value_from_entity(p->module, e));
  2875. }
  2876. {
  2877. cgAddr addr = cg_build_addr(p, se->expr);
  2878. if (addr.kind == cgAddr_Map) {
  2879. cgValue v = cg_addr_load(p, addr);
  2880. cgValue a = cg_address_from_load_or_generate_local(p, v);
  2881. a = cg_emit_deep_field_gep(p, a, sel);
  2882. return cg_addr(a);
  2883. } else if (addr.kind == cgAddr_Context) {
  2884. GB_ASSERT(sel.index.count > 0);
  2885. if (addr.ctx.sel.index.count >= 0) {
  2886. sel = selection_combine(addr.ctx.sel, sel);
  2887. }
  2888. addr.ctx.sel = sel;
  2889. addr.kind = cgAddr_Context;
  2890. return addr;
  2891. } else if (addr.kind == cgAddr_SoaVariable) {
  2892. cgValue index = addr.soa.index;
  2893. i64 first_index = sel.index[0];
  2894. Selection sub_sel = sel;
  2895. sub_sel.index.data += 1;
  2896. sub_sel.index.count -= 1;
  2897. cgValue arr = cg_emit_struct_ep(p, addr.addr, first_index);
  2898. Type *t = base_type(type_deref(addr.addr.type));
  2899. GB_ASSERT(is_type_soa_struct(t));
  2900. // TODO(bill): bounds checking for soa variable
  2901. // if (addr.soa.index_expr != nullptr && (!cg_is_const(addr.soa.index) || t->Struct.soa_kind != StructSoa_Fixed)) {
  2902. // cgValue len = cg_soa_struct_len(p, addr.addr);
  2903. // cg_emit_bounds_check(p, ast_token(addr.soa.index_expr), addr.soa.index, len);
  2904. // }
  2905. cgValue item = {};
  2906. if (t->Struct.soa_kind == StructSoa_Fixed) {
  2907. item = cg_emit_array_ep(p, arr, index);
  2908. } else {
  2909. item = cg_emit_ptr_offset(p, cg_emit_load(p, arr), index);
  2910. }
  2911. if (sub_sel.index.count > 0) {
  2912. item = cg_emit_deep_field_gep(p, item, sub_sel);
  2913. }
  2914. item.type = alloc_type_pointer(type_deref(item.type, true));
  2915. return cg_addr(item);
  2916. } else if (addr.kind == cgAddr_Swizzle) {
  2917. GB_ASSERT(sel.index.count > 0);
  2918. // NOTE(bill): just patch the index in place
  2919. sel.index[0] = addr.swizzle.indices[sel.index[0]];
  2920. } else if (addr.kind == cgAddr_SwizzleLarge) {
  2921. GB_ASSERT(sel.index.count > 0);
  2922. // NOTE(bill): just patch the index in place
  2923. sel.index[0] = addr.swizzle.indices[sel.index[0]];
  2924. }
  2925. cgValue a = cg_addr_get_ptr(p, addr);
  2926. a = cg_emit_deep_field_gep(p, a, sel);
  2927. return cg_addr(a);
  2928. }
  2929. case_end;
  2930. case_ast_node(ce, CallExpr, expr);
  2931. cgValue res = cg_build_expr(p, expr);
  2932. switch (res.kind) {
  2933. case cgValue_Value:
  2934. return cg_addr(cg_address_from_load_or_generate_local(p, res));
  2935. case cgValue_Addr:
  2936. return cg_addr(res);
  2937. case cgValue_Multi:
  2938. GB_PANIC("cannot address a multi-valued expression");
  2939. break;
  2940. }
  2941. case_end;
  2942. case_ast_node(cl, CompoundLit, expr);
  2943. return cg_build_addr_compound_lit(p, expr);
  2944. case_end;
  2945. }
  2946. TokenPos token_pos = ast_token(expr).pos;
  2947. GB_PANIC("Unexpected address expression\n"
  2948. "\tAst: %.*s @ "
  2949. "%s\n",
  2950. LIT(ast_strings[expr->kind]),
  2951. token_pos_to_string(token_pos));
  2952. return {};
  2953. }