llvm_backend_expr.cpp 142 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609
  1. lbValue lb_emit_logical_binary_expr(lbProcedure *p, TokenKind op, Ast *left, Ast *right, Type *type) {
  2. lbModule *m = p->module;
  3. lbBlock *rhs = lb_create_block(p, "logical.cmp.rhs");
  4. lbBlock *done = lb_create_block(p, "logical.cmp.done");
  5. type = default_type(type);
  6. lbValue short_circuit = {};
  7. if (op == Token_CmpAnd) {
  8. lb_build_cond(p, left, rhs, done);
  9. short_circuit = lb_const_bool(m, type, false);
  10. } else if (op == Token_CmpOr) {
  11. lb_build_cond(p, left, done, rhs);
  12. short_circuit = lb_const_bool(m, type, true);
  13. }
  14. if (rhs->preds.count == 0) {
  15. lb_start_block(p, done);
  16. return short_circuit;
  17. }
  18. if (done->preds.count == 0) {
  19. lb_start_block(p, rhs);
  20. if (lb_is_expr_untyped_const(right)) {
  21. return lb_expr_untyped_const_to_typed(m, right, type);
  22. }
  23. return lb_build_expr(p, right);
  24. }
  25. Array<LLVMValueRef> incoming_values = {};
  26. Array<LLVMBasicBlockRef> incoming_blocks = {};
  27. array_init(&incoming_values, heap_allocator(), done->preds.count+1);
  28. array_init(&incoming_blocks, heap_allocator(), done->preds.count+1);
  29. for_array(i, done->preds) {
  30. incoming_values[i] = short_circuit.value;
  31. incoming_blocks[i] = done->preds[i]->block;
  32. }
  33. lb_start_block(p, rhs);
  34. lbValue edge = {};
  35. if (lb_is_expr_untyped_const(right)) {
  36. edge = lb_expr_untyped_const_to_typed(m, right, type);
  37. } else {
  38. edge = lb_build_expr(p, right);
  39. }
  40. incoming_values[done->preds.count] = edge.value;
  41. incoming_blocks[done->preds.count] = p->curr_block->block;
  42. lb_emit_jump(p, done);
  43. lb_start_block(p, done);
  44. LLVMTypeRef dst_type = lb_type(m, type);
  45. LLVMValueRef phi = nullptr;
  46. GB_ASSERT(incoming_values.count == incoming_blocks.count);
  47. GB_ASSERT(incoming_values.count > 0);
  48. LLVMTypeRef phi_type = nullptr;
  49. for_array(i, incoming_values) {
  50. LLVMValueRef incoming_value = incoming_values[i];
  51. if (!LLVMIsConstant(incoming_value)) {
  52. phi_type = LLVMTypeOf(incoming_value);
  53. break;
  54. }
  55. }
  56. if (phi_type == nullptr) {
  57. phi = LLVMBuildPhi(p->builder, dst_type, "");
  58. LLVMAddIncoming(phi, incoming_values.data, incoming_blocks.data, cast(unsigned)incoming_values.count);
  59. lbValue res = {};
  60. res.type = type;
  61. res.value = phi;
  62. return res;
  63. }
  64. for_array(i, incoming_values) {
  65. LLVMValueRef incoming_value = incoming_values[i];
  66. LLVMTypeRef incoming_type = LLVMTypeOf(incoming_value);
  67. if (phi_type != incoming_type) {
  68. GB_ASSERT_MSG(LLVMIsConstant(incoming_value), "%s vs %s", LLVMPrintTypeToString(phi_type), LLVMPrintTypeToString(incoming_type));
  69. bool ok = !!LLVMConstIntGetZExtValue(incoming_value);
  70. incoming_values[i] = LLVMConstInt(phi_type, ok, false);
  71. }
  72. }
  73. phi = LLVMBuildPhi(p->builder, phi_type, "");
  74. LLVMAddIncoming(phi, incoming_values.data, incoming_blocks.data, cast(unsigned)incoming_values.count);
  75. LLVMTypeRef i1 = LLVMInt1TypeInContext(m->ctx);
  76. if ((phi_type == i1) ^ (dst_type == i1)) {
  77. if (phi_type == i1) {
  78. phi = LLVMBuildZExt(p->builder, phi, dst_type, "");
  79. } else {
  80. phi = LLVMBuildTruncOrBitCast(p->builder, phi, dst_type, "");
  81. }
  82. } else if (lb_sizeof(phi_type) < lb_sizeof(dst_type)) {
  83. phi = LLVMBuildZExt(p->builder, phi, dst_type, "");
  84. } else {
  85. phi = LLVMBuildTruncOrBitCast(p->builder, phi, dst_type, "");
  86. }
  87. lbValue res = {};
  88. res.type = type;
  89. res.value = phi;
  90. return res;
  91. }
  92. lbValue lb_emit_unary_arith(lbProcedure *p, TokenKind op, lbValue x, Type *type) {
  93. switch (op) {
  94. case Token_Add:
  95. return x;
  96. case Token_Not: // Boolean not
  97. case Token_Xor: // Bitwise not
  98. case Token_Sub: // Number negation
  99. break;
  100. case Token_Pointer:
  101. GB_PANIC("This should be handled elsewhere");
  102. break;
  103. }
  104. if (is_type_array_like(x.type)) {
  105. // IMPORTANT TODO(bill): This is very wasteful with regards to stack memory
  106. Type *tl = base_type(x.type);
  107. lbValue val = lb_address_from_load_or_generate_local(p, x);
  108. GB_ASSERT(is_type_array_like(type));
  109. Type *elem_type = base_array_type(type);
  110. // NOTE(bill): Doesn't need to be zero because it will be initialized in the loops
  111. lbAddr res_addr = lb_add_local(p, type, nullptr, false, 0, true);
  112. lbValue res = lb_addr_get_ptr(p, res_addr);
  113. bool inline_array_arith = type_size_of(type) <= build_context.max_align;
  114. i32 count = cast(i32)get_array_type_count(tl);
  115. LLVMTypeRef vector_type = nullptr;
  116. if (op != Token_Not && lb_try_vector_cast(p->module, val, &vector_type)) {
  117. LLVMValueRef vp = LLVMBuildPointerCast(p->builder, val.value, LLVMPointerType(vector_type, 0), "");
  118. LLVMValueRef v = LLVMBuildLoad2(p->builder, vector_type, vp, "");
  119. LLVMValueRef opv = nullptr;
  120. switch (op) {
  121. case Token_Xor:
  122. opv = LLVMBuildNot(p->builder, v, "");
  123. break;
  124. case Token_Sub:
  125. if (is_type_float(elem_type)) {
  126. opv = LLVMBuildFNeg(p->builder, v, "");
  127. } else {
  128. opv = LLVMBuildNeg(p->builder, v, "");
  129. }
  130. break;
  131. }
  132. if (opv != nullptr) {
  133. LLVMSetAlignment(res.value, cast(unsigned)lb_alignof(vector_type));
  134. LLVMValueRef res_ptr = LLVMBuildPointerCast(p->builder, res.value, LLVMPointerType(vector_type, 0), "");
  135. LLVMBuildStore(p->builder, opv, res_ptr);
  136. return lb_emit_conv(p, lb_emit_load(p, res), type);
  137. }
  138. }
  139. if (inline_array_arith) {
  140. // inline
  141. for (i32 i = 0; i < count; i++) {
  142. lbValue e = lb_emit_load(p, lb_emit_array_epi(p, val, i));
  143. lbValue z = lb_emit_unary_arith(p, op, e, elem_type);
  144. lb_emit_store(p, lb_emit_array_epi(p, res, i), z);
  145. }
  146. } else {
  147. auto loop_data = lb_loop_start(p, count, t_i32);
  148. lbValue e = lb_emit_load(p, lb_emit_array_ep(p, val, loop_data.idx));
  149. lbValue z = lb_emit_unary_arith(p, op, e, elem_type);
  150. lb_emit_store(p, lb_emit_array_ep(p, res, loop_data.idx), z);
  151. lb_loop_end(p, loop_data);
  152. }
  153. return lb_emit_load(p, res);
  154. }
  155. if (op == Token_Xor) {
  156. lbValue cmp = {};
  157. cmp.value = LLVMBuildNot(p->builder, x.value, "");
  158. cmp.type = x.type;
  159. return lb_emit_conv(p, cmp, type);
  160. }
  161. if (op == Token_Not) {
  162. lbValue cmp = {};
  163. LLVMValueRef zero = LLVMConstInt(lb_type(p->module, x.type), 0, false);
  164. cmp.value = LLVMBuildICmp(p->builder, LLVMIntEQ, x.value, zero, "");
  165. cmp.type = t_llvm_bool;
  166. return lb_emit_conv(p, cmp, type);
  167. }
  168. if (op == Token_Sub && is_type_integer(type) && is_type_different_to_arch_endianness(type)) {
  169. Type *platform_type = integer_endian_type_to_platform_type(type);
  170. lbValue v = lb_emit_byte_swap(p, x, platform_type);
  171. lbValue res = {};
  172. res.value = LLVMBuildNeg(p->builder, v.value, "");
  173. res.type = platform_type;
  174. return lb_emit_byte_swap(p, res, type);
  175. }
  176. if (op == Token_Sub && is_type_float(type) && is_type_different_to_arch_endianness(type)) {
  177. Type *platform_type = integer_endian_type_to_platform_type(type);
  178. lbValue v = lb_emit_byte_swap(p, x, platform_type);
  179. lbValue res = {};
  180. res.value = LLVMBuildFNeg(p->builder, v.value, "");
  181. res.type = platform_type;
  182. return lb_emit_byte_swap(p, res, type);
  183. }
  184. lbValue res = {};
  185. switch (op) {
  186. case Token_Not: // Boolean not
  187. case Token_Xor: // Bitwise not
  188. res.value = LLVMBuildNot(p->builder, x.value, "");
  189. res.type = x.type;
  190. return res;
  191. case Token_Sub: // Number negation
  192. if (is_type_integer(x.type)) {
  193. res.value = LLVMBuildNeg(p->builder, x.value, "");
  194. } else if (is_type_float(x.type)) {
  195. res.value = LLVMBuildFNeg(p->builder, x.value, "");
  196. } else if (is_type_complex(x.type)) {
  197. LLVMValueRef v0 = LLVMBuildFNeg(p->builder, LLVMBuildExtractValue(p->builder, x.value, 0, ""), "");
  198. LLVMValueRef v1 = LLVMBuildFNeg(p->builder, LLVMBuildExtractValue(p->builder, x.value, 1, ""), "");
  199. lbAddr addr = lb_add_local_generated(p, x.type, false);
  200. LLVMBuildStore(p->builder, v0, LLVMBuildStructGEP(p->builder, addr.addr.value, 0, ""));
  201. LLVMBuildStore(p->builder, v1, LLVMBuildStructGEP(p->builder, addr.addr.value, 1, ""));
  202. return lb_addr_load(p, addr);
  203. } else if (is_type_quaternion(x.type)) {
  204. LLVMValueRef v0 = LLVMBuildFNeg(p->builder, LLVMBuildExtractValue(p->builder, x.value, 0, ""), "");
  205. LLVMValueRef v1 = LLVMBuildFNeg(p->builder, LLVMBuildExtractValue(p->builder, x.value, 1, ""), "");
  206. LLVMValueRef v2 = LLVMBuildFNeg(p->builder, LLVMBuildExtractValue(p->builder, x.value, 2, ""), "");
  207. LLVMValueRef v3 = LLVMBuildFNeg(p->builder, LLVMBuildExtractValue(p->builder, x.value, 3, ""), "");
  208. lbAddr addr = lb_add_local_generated(p, x.type, false);
  209. LLVMBuildStore(p->builder, v0, LLVMBuildStructGEP(p->builder, addr.addr.value, 0, ""));
  210. LLVMBuildStore(p->builder, v1, LLVMBuildStructGEP(p->builder, addr.addr.value, 1, ""));
  211. LLVMBuildStore(p->builder, v2, LLVMBuildStructGEP(p->builder, addr.addr.value, 2, ""));
  212. LLVMBuildStore(p->builder, v3, LLVMBuildStructGEP(p->builder, addr.addr.value, 3, ""));
  213. return lb_addr_load(p, addr);
  214. } else {
  215. GB_PANIC("Unhandled type %s", type_to_string(x.type));
  216. }
  217. res.type = x.type;
  218. return res;
  219. }
  220. return res;
  221. }
  222. bool lb_try_direct_vector_arith(lbProcedure *p, TokenKind op, lbValue lhs, lbValue rhs, Type *type, lbValue *res_) {
  223. GB_ASSERT(is_type_array_like(type));
  224. Type *elem_type = base_array_type(type);
  225. // NOTE(bill): Shift operations cannot be easily dealt with due to Odin's semantics
  226. if (op == Token_Shl || op == Token_Shr) {
  227. return false;
  228. }
  229. if (!LLVMIsALoadInst(lhs.value) || !LLVMIsALoadInst(rhs.value)) {
  230. return false;
  231. }
  232. lbValue lhs_ptr = {};
  233. lbValue rhs_ptr = {};
  234. lhs_ptr.value = LLVMGetOperand(lhs.value, 0);
  235. lhs_ptr.type = alloc_type_pointer(lhs.type);
  236. rhs_ptr.value = LLVMGetOperand(rhs.value, 0);
  237. rhs_ptr.type = alloc_type_pointer(rhs.type);
  238. LLVMTypeRef vector_type0 = nullptr;
  239. LLVMTypeRef vector_type1 = nullptr;
  240. if (lb_try_vector_cast(p->module, lhs_ptr, &vector_type0) &&
  241. lb_try_vector_cast(p->module, rhs_ptr, &vector_type1)) {
  242. GB_ASSERT(vector_type0 == vector_type1);
  243. LLVMTypeRef vector_type = vector_type0;
  244. LLVMValueRef lhs_vp = LLVMBuildPointerCast(p->builder, lhs_ptr.value, LLVMPointerType(vector_type, 0), "");
  245. LLVMValueRef rhs_vp = LLVMBuildPointerCast(p->builder, rhs_ptr.value, LLVMPointerType(vector_type, 0), "");
  246. LLVMValueRef x = LLVMBuildLoad2(p->builder, vector_type, lhs_vp, "");
  247. LLVMValueRef y = LLVMBuildLoad2(p->builder, vector_type, rhs_vp, "");
  248. LLVMValueRef z = nullptr;
  249. Type *integral_type = base_type(elem_type);
  250. if (is_type_simd_vector(integral_type)) {
  251. integral_type = core_array_type(integral_type);
  252. }
  253. if (is_type_bit_set(integral_type)) {
  254. switch (op) {
  255. case Token_Add: op = Token_Or; break;
  256. case Token_Sub: op = Token_AndNot; break;
  257. }
  258. }
  259. if (is_type_float(integral_type)) {
  260. switch (op) {
  261. case Token_Add:
  262. z = LLVMBuildFAdd(p->builder, x, y, "");
  263. break;
  264. case Token_Sub:
  265. z = LLVMBuildFSub(p->builder, x, y, "");
  266. break;
  267. case Token_Mul:
  268. z = LLVMBuildFMul(p->builder, x, y, "");
  269. break;
  270. case Token_Quo:
  271. z = LLVMBuildFDiv(p->builder, x, y, "");
  272. break;
  273. case Token_Mod:
  274. z = LLVMBuildFRem(p->builder, x, y, "");
  275. break;
  276. default:
  277. GB_PANIC("Unsupported vector operation %.*s", LIT(token_strings[op]));
  278. break;
  279. }
  280. } else {
  281. switch (op) {
  282. case Token_Add:
  283. z = LLVMBuildAdd(p->builder, x, y, "");
  284. break;
  285. case Token_Sub:
  286. z = LLVMBuildSub(p->builder, x, y, "");
  287. break;
  288. case Token_Mul:
  289. z = LLVMBuildMul(p->builder, x, y, "");
  290. break;
  291. case Token_Quo:
  292. if (is_type_unsigned(integral_type)) {
  293. z = LLVMBuildUDiv(p->builder, x, y, "");
  294. } else {
  295. z = LLVMBuildSDiv(p->builder, x, y, "");
  296. }
  297. break;
  298. case Token_Mod:
  299. if (is_type_unsigned(integral_type)) {
  300. z = LLVMBuildURem(p->builder, x, y, "");
  301. } else {
  302. z = LLVMBuildSRem(p->builder, x, y, "");
  303. }
  304. break;
  305. case Token_ModMod:
  306. if (is_type_unsigned(integral_type)) {
  307. z = LLVMBuildURem(p->builder, x, y, "");
  308. } else {
  309. LLVMValueRef a = LLVMBuildSRem(p->builder, x, y, "");
  310. LLVMValueRef b = LLVMBuildAdd(p->builder, a, y, "");
  311. z = LLVMBuildSRem(p->builder, b, y, "");
  312. }
  313. break;
  314. case Token_And:
  315. z = LLVMBuildAnd(p->builder, x, y, "");
  316. break;
  317. case Token_AndNot:
  318. z = LLVMBuildAnd(p->builder, x, LLVMBuildNot(p->builder, y, ""), "");
  319. break;
  320. case Token_Or:
  321. z = LLVMBuildOr(p->builder, x, y, "");
  322. break;
  323. case Token_Xor:
  324. z = LLVMBuildXor(p->builder, x, y, "");
  325. break;
  326. default:
  327. GB_PANIC("Unsupported vector operation");
  328. break;
  329. }
  330. }
  331. if (z != nullptr) {
  332. lbAddr res = lb_add_local_generated_temp(p, type, lb_alignof(vector_type));
  333. LLVMValueRef vp = LLVMBuildPointerCast(p->builder, res.addr.value, LLVMPointerType(vector_type, 0), "");
  334. LLVMBuildStore(p->builder, z, vp);
  335. lbValue v = lb_addr_load(p, res);
  336. if (res_) *res_ = v;
  337. return true;
  338. }
  339. }
  340. return false;
  341. }
  342. lbValue lb_emit_arith_array(lbProcedure *p, TokenKind op, lbValue lhs, lbValue rhs, Type *type) {
  343. GB_ASSERT(is_type_array_like(lhs.type) || is_type_array_like(rhs.type));
  344. lhs = lb_emit_conv(p, lhs, type);
  345. rhs = lb_emit_conv(p, rhs, type);
  346. GB_ASSERT(is_type_array_like(type));
  347. Type *elem_type = base_array_type(type);
  348. i64 count = get_array_type_count(type);
  349. unsigned n = cast(unsigned)count;
  350. // NOTE(bill, 2021-06-12): Try to do a direct operation as a vector, if possible
  351. lbValue direct_vector_res = {};
  352. if (lb_try_direct_vector_arith(p, op, lhs, rhs, type, &direct_vector_res)) {
  353. return direct_vector_res;
  354. }
  355. bool inline_array_arith = type_size_of(type) <= build_context.max_align;
  356. if (inline_array_arith) {
  357. auto dst_ptrs = slice_make<lbValue>(temporary_allocator(), n);
  358. auto a_loads = slice_make<lbValue>(temporary_allocator(), n);
  359. auto b_loads = slice_make<lbValue>(temporary_allocator(), n);
  360. auto c_ops = slice_make<lbValue>(temporary_allocator(), n);
  361. for (unsigned i = 0; i < n; i++) {
  362. a_loads[i].value = LLVMBuildExtractValue(p->builder, lhs.value, i, "");
  363. a_loads[i].type = elem_type;
  364. }
  365. for (unsigned i = 0; i < n; i++) {
  366. b_loads[i].value = LLVMBuildExtractValue(p->builder, rhs.value, i, "");
  367. b_loads[i].type = elem_type;
  368. }
  369. for (unsigned i = 0; i < n; i++) {
  370. c_ops[i] = lb_emit_arith(p, op, a_loads[i], b_loads[i], elem_type);
  371. }
  372. lbAddr res = lb_add_local_generated(p, type, false);
  373. for (unsigned i = 0; i < n; i++) {
  374. dst_ptrs[i] = lb_emit_array_epi(p, res.addr, i);
  375. }
  376. for (unsigned i = 0; i < n; i++) {
  377. lb_emit_store(p, dst_ptrs[i], c_ops[i]);
  378. }
  379. return lb_addr_load(p, res);
  380. } else {
  381. lbValue x = lb_address_from_load_or_generate_local(p, lhs);
  382. lbValue y = lb_address_from_load_or_generate_local(p, rhs);
  383. lbAddr res = lb_add_local_generated(p, type, false);
  384. auto loop_data = lb_loop_start(p, cast(isize)count, t_i32);
  385. lbValue a_ptr = lb_emit_array_ep(p, x, loop_data.idx);
  386. lbValue b_ptr = lb_emit_array_ep(p, y, loop_data.idx);
  387. lbValue dst_ptr = lb_emit_array_ep(p, res.addr, loop_data.idx);
  388. lbValue a = lb_emit_load(p, a_ptr);
  389. lbValue b = lb_emit_load(p, b_ptr);
  390. lbValue c = lb_emit_arith(p, op, a, b, elem_type);
  391. lb_emit_store(p, dst_ptr, c);
  392. lb_loop_end(p, loop_data);
  393. return lb_addr_load(p, res);
  394. }
  395. }
  396. bool lb_is_matrix_simdable(Type *t) {
  397. Type *mt = base_type(t);
  398. GB_ASSERT(mt->kind == Type_Matrix);
  399. Type *elem = core_type(mt->Matrix.elem);
  400. if (is_type_complex(elem)) {
  401. return false;
  402. }
  403. if (is_type_different_to_arch_endianness(elem)) {
  404. return false;
  405. }
  406. switch (build_context.metrics.arch) {
  407. case TargetArch_amd64:
  408. case TargetArch_arm64:
  409. // possible
  410. break;
  411. case TargetArch_386:
  412. case TargetArch_wasm32:
  413. // nope
  414. return false;
  415. }
  416. if (elem->kind == Type_Basic) {
  417. switch (elem->Basic.kind) {
  418. case Basic_f16:
  419. case Basic_f16le:
  420. case Basic_f16be:
  421. switch (build_context.metrics.arch) {
  422. case TargetArch_amd64:
  423. return false;
  424. case TargetArch_arm64:
  425. // TODO(bill): determine when this is fine
  426. return true;
  427. case TargetArch_386:
  428. case TargetArch_wasm32:
  429. return false;
  430. }
  431. }
  432. }
  433. return true;
  434. }
  435. LLVMValueRef lb_matrix_to_vector(lbProcedure *p, lbValue matrix) {
  436. Type *mt = base_type(matrix.type);
  437. GB_ASSERT(mt->kind == Type_Matrix);
  438. LLVMTypeRef elem_type = lb_type(p->module, mt->Matrix.elem);
  439. unsigned total_count = cast(unsigned)matrix_type_total_internal_elems(mt);
  440. LLVMTypeRef total_matrix_type = LLVMVectorType(elem_type, total_count);
  441. #if 1
  442. LLVMValueRef ptr = lb_address_from_load_or_generate_local(p, matrix).value;
  443. LLVMValueRef matrix_vector_ptr = LLVMBuildPointerCast(p->builder, ptr, LLVMPointerType(total_matrix_type, 0), "");
  444. LLVMValueRef matrix_vector = LLVMBuildLoad(p->builder, matrix_vector_ptr, "");
  445. LLVMSetAlignment(matrix_vector, cast(unsigned)type_align_of(mt));
  446. return matrix_vector;
  447. #else
  448. LLVMValueRef matrix_vector = LLVMBuildBitCast(p->builder, matrix.value, total_matrix_type, "");
  449. return matrix_vector;
  450. #endif
  451. }
  452. LLVMValueRef lb_matrix_trimmed_vector_mask(lbProcedure *p, Type *mt) {
  453. mt = base_type(mt);
  454. GB_ASSERT(mt->kind == Type_Matrix);
  455. unsigned stride = cast(unsigned)matrix_type_stride_in_elems(mt);
  456. unsigned row_count = cast(unsigned)mt->Matrix.row_count;
  457. unsigned column_count = cast(unsigned)mt->Matrix.column_count;
  458. unsigned mask_elems_index = 0;
  459. auto mask_elems = slice_make<LLVMValueRef>(permanent_allocator(), row_count*column_count);
  460. for (unsigned j = 0; j < column_count; j++) {
  461. for (unsigned i = 0; i < row_count; i++) {
  462. unsigned offset = stride*j + i;
  463. mask_elems[mask_elems_index++] = lb_const_int(p->module, t_u32, offset).value;
  464. }
  465. }
  466. LLVMValueRef mask = LLVMConstVector(mask_elems.data, cast(unsigned)mask_elems.count);
  467. return mask;
  468. }
  469. LLVMValueRef lb_matrix_to_trimmed_vector(lbProcedure *p, lbValue m) {
  470. LLVMValueRef vector = lb_matrix_to_vector(p, m);
  471. Type *mt = base_type(m.type);
  472. GB_ASSERT(mt->kind == Type_Matrix);
  473. unsigned stride = cast(unsigned)matrix_type_stride_in_elems(mt);
  474. unsigned row_count = cast(unsigned)mt->Matrix.row_count;
  475. if (stride == row_count) {
  476. return vector;
  477. }
  478. LLVMValueRef mask = lb_matrix_trimmed_vector_mask(p, mt);
  479. LLVMValueRef trimmed_vector = LLVMBuildShuffleVector(p->builder, vector, LLVMGetUndef(LLVMTypeOf(vector)), mask, "");
  480. return trimmed_vector;
  481. }
  482. lbValue lb_emit_matrix_tranpose(lbProcedure *p, lbValue m, Type *type) {
  483. if (is_type_array(m.type)) {
  484. // no-op
  485. m.type = type;
  486. return m;
  487. }
  488. Type *mt = base_type(m.type);
  489. GB_ASSERT(mt->kind == Type_Matrix);
  490. if (lb_is_matrix_simdable(mt)) {
  491. unsigned stride = cast(unsigned)matrix_type_stride_in_elems(mt);
  492. unsigned row_count = cast(unsigned)mt->Matrix.row_count;
  493. unsigned column_count = cast(unsigned)mt->Matrix.column_count;
  494. auto rows = slice_make<LLVMValueRef>(permanent_allocator(), row_count);
  495. auto mask_elems = slice_make<LLVMValueRef>(permanent_allocator(), column_count);
  496. LLVMValueRef vector = lb_matrix_to_vector(p, m);
  497. for (unsigned i = 0; i < row_count; i++) {
  498. for (unsigned j = 0; j < column_count; j++) {
  499. unsigned offset = stride*j + i;
  500. mask_elems[j] = lb_const_int(p->module, t_u32, offset).value;
  501. }
  502. // transpose mask
  503. LLVMValueRef mask = LLVMConstVector(mask_elems.data, column_count);
  504. LLVMValueRef row = LLVMBuildShuffleVector(p->builder, vector, LLVMGetUndef(LLVMTypeOf(vector)), mask, "");
  505. rows[i] = row;
  506. }
  507. lbAddr res = lb_add_local_generated(p, type, true);
  508. for_array(i, rows) {
  509. LLVMValueRef row = rows[i];
  510. lbValue dst_row_ptr = lb_emit_matrix_epi(p, res.addr, 0, i);
  511. LLVMValueRef ptr = dst_row_ptr.value;
  512. ptr = LLVMBuildPointerCast(p->builder, ptr, LLVMPointerType(LLVMTypeOf(row), 0), "");
  513. LLVMBuildStore(p->builder, row, ptr);
  514. }
  515. return lb_addr_load(p, res);
  516. }
  517. lbAddr res = lb_add_local_generated(p, type, true);
  518. i64 row_count = mt->Matrix.row_count;
  519. i64 column_count = mt->Matrix.column_count;
  520. for (i64 j = 0; j < column_count; j++) {
  521. for (i64 i = 0; i < row_count; i++) {
  522. lbValue src = lb_emit_matrix_ev(p, m, i, j);
  523. lbValue dst = lb_emit_matrix_epi(p, res.addr, j, i);
  524. lb_emit_store(p, dst, src);
  525. }
  526. }
  527. return lb_addr_load(p, res);
  528. }
  529. lbValue lb_matrix_cast_vector_to_type(lbProcedure *p, LLVMValueRef vector, Type *type) {
  530. lbAddr res = lb_add_local_generated(p, type, true);
  531. LLVMValueRef res_ptr = res.addr.value;
  532. unsigned alignment = cast(unsigned)gb_max(type_align_of(type), lb_alignof(LLVMTypeOf(vector)));
  533. LLVMSetAlignment(res_ptr, alignment);
  534. res_ptr = LLVMBuildPointerCast(p->builder, res_ptr, LLVMPointerType(LLVMTypeOf(vector), 0), "");
  535. LLVMBuildStore(p->builder, vector, res_ptr);
  536. return lb_addr_load(p, res);
  537. }
  538. lbValue lb_emit_matrix_flatten(lbProcedure *p, lbValue m, Type *type) {
  539. if (is_type_array(m.type)) {
  540. // no-op
  541. m.type = type;
  542. return m;
  543. }
  544. Type *mt = base_type(m.type);
  545. GB_ASSERT(mt->kind == Type_Matrix);
  546. if (lb_is_matrix_simdable(mt)) {
  547. LLVMValueRef vector = lb_matrix_to_trimmed_vector(p, m);
  548. return lb_matrix_cast_vector_to_type(p, vector, type);
  549. }
  550. lbAddr res = lb_add_local_generated(p, type, true);
  551. i64 row_count = mt->Matrix.row_count;
  552. i64 column_count = mt->Matrix.column_count;
  553. for (i64 j = 0; j < column_count; j++) {
  554. for (i64 i = 0; i < row_count; i++) {
  555. lbValue src = lb_emit_matrix_ev(p, m, i, j);
  556. lbValue dst = lb_emit_matrix_epi(p, res.addr, i, j);
  557. lb_emit_store(p, dst, src);
  558. }
  559. }
  560. return lb_addr_load(p, res);
  561. }
  562. lbValue lb_emit_outer_product(lbProcedure *p, lbValue a, lbValue b, Type *type) {
  563. Type *mt = base_type(type);
  564. Type *at = base_type(a.type);
  565. Type *bt = base_type(b.type);
  566. GB_ASSERT(mt->kind == Type_Matrix);
  567. GB_ASSERT(at->kind == Type_Array);
  568. GB_ASSERT(bt->kind == Type_Array);
  569. i64 row_count = mt->Matrix.row_count;
  570. i64 column_count = mt->Matrix.column_count;
  571. GB_ASSERT(row_count == at->Array.count);
  572. GB_ASSERT(column_count == bt->Array.count);
  573. lbAddr res = lb_add_local_generated(p, type, true);
  574. for (i64 j = 0; j < column_count; j++) {
  575. for (i64 i = 0; i < row_count; i++) {
  576. lbValue x = lb_emit_struct_ev(p, a, cast(i32)i);
  577. lbValue y = lb_emit_struct_ev(p, b, cast(i32)j);
  578. lbValue src = lb_emit_arith(p, Token_Mul, x, y, mt->Matrix.elem);
  579. lbValue dst = lb_emit_matrix_epi(p, res.addr, i, j);
  580. lb_emit_store(p, dst, src);
  581. }
  582. }
  583. return lb_addr_load(p, res);
  584. }
  585. lbValue lb_emit_matrix_mul(lbProcedure *p, lbValue lhs, lbValue rhs, Type *type) {
  586. // TODO(bill): Handle edge case for f16 types on x86(-64) platforms
  587. Type *xt = base_type(lhs.type);
  588. Type *yt = base_type(rhs.type);
  589. GB_ASSERT(is_type_matrix(type));
  590. GB_ASSERT(is_type_matrix(xt));
  591. GB_ASSERT(is_type_matrix(yt));
  592. GB_ASSERT(xt->Matrix.column_count == yt->Matrix.row_count);
  593. GB_ASSERT(are_types_identical(xt->Matrix.elem, yt->Matrix.elem));
  594. Type *elem = xt->Matrix.elem;
  595. unsigned outer_rows = cast(unsigned)xt->Matrix.row_count;
  596. unsigned inner = cast(unsigned)xt->Matrix.column_count;
  597. unsigned outer_columns = cast(unsigned)yt->Matrix.column_count;
  598. if (lb_is_matrix_simdable(xt)) {
  599. unsigned x_stride = cast(unsigned)matrix_type_stride_in_elems(xt);
  600. unsigned y_stride = cast(unsigned)matrix_type_stride_in_elems(yt);
  601. auto x_rows = slice_make<LLVMValueRef>(permanent_allocator(), outer_rows);
  602. auto y_columns = slice_make<LLVMValueRef>(permanent_allocator(), outer_columns);
  603. LLVMValueRef x_vector = lb_matrix_to_vector(p, lhs);
  604. LLVMValueRef y_vector = lb_matrix_to_vector(p, rhs);
  605. auto mask_elems = slice_make<LLVMValueRef>(permanent_allocator(), inner);
  606. for (unsigned i = 0; i < outer_rows; i++) {
  607. for (unsigned j = 0; j < inner; j++) {
  608. unsigned offset = x_stride*j + i;
  609. mask_elems[j] = lb_const_int(p->module, t_u32, offset).value;
  610. }
  611. // transpose mask
  612. LLVMValueRef mask = LLVMConstVector(mask_elems.data, inner);
  613. LLVMValueRef row = LLVMBuildShuffleVector(p->builder, x_vector, LLVMGetUndef(LLVMTypeOf(x_vector)), mask, "");
  614. x_rows[i] = row;
  615. }
  616. for (unsigned i = 0; i < outer_columns; i++) {
  617. LLVMValueRef mask = llvm_mask_iota(p->module, y_stride*i, inner);
  618. LLVMValueRef column = LLVMBuildShuffleVector(p->builder, y_vector, LLVMGetUndef(LLVMTypeOf(y_vector)), mask, "");
  619. y_columns[i] = column;
  620. }
  621. lbAddr res = lb_add_local_generated(p, type, true);
  622. for_array(i, x_rows) {
  623. LLVMValueRef x_row = x_rows[i];
  624. for_array(j, y_columns) {
  625. LLVMValueRef y_column = y_columns[j];
  626. LLVMValueRef elem = llvm_vector_dot(p, x_row, y_column);
  627. lbValue dst = lb_emit_matrix_epi(p, res.addr, i, j);
  628. LLVMBuildStore(p->builder, elem, dst.value);
  629. }
  630. }
  631. return lb_addr_load(p, res);
  632. }
  633. {
  634. lbAddr res = lb_add_local_generated(p, type, true);
  635. auto inners = slice_make<lbValue[2]>(permanent_allocator(), inner);
  636. for (unsigned j = 0; j < outer_columns; j++) {
  637. for (unsigned i = 0; i < outer_rows; i++) {
  638. lbValue dst = lb_emit_matrix_epi(p, res.addr, i, j);
  639. for (unsigned k = 0; k < inner; k++) {
  640. inners[k][0] = lb_emit_matrix_ev(p, lhs, i, k);
  641. inners[k][1] = lb_emit_matrix_ev(p, rhs, k, j);
  642. }
  643. lbValue sum = lb_const_nil(p->module, elem);
  644. for (unsigned k = 0; k < inner; k++) {
  645. lbValue a = inners[k][0];
  646. lbValue b = inners[k][1];
  647. sum = lb_emit_mul_add(p, a, b, sum, elem);
  648. }
  649. lb_emit_store(p, dst, sum);
  650. }
  651. }
  652. return lb_addr_load(p, res);
  653. }
  654. }
  655. lbValue lb_emit_matrix_mul_vector(lbProcedure *p, lbValue lhs, lbValue rhs, Type *type) {
  656. // TODO(bill): Handle edge case for f16 types on x86(-64) platforms
  657. Type *mt = base_type(lhs.type);
  658. Type *vt = base_type(rhs.type);
  659. GB_ASSERT(is_type_matrix(mt));
  660. GB_ASSERT(is_type_array_like(vt));
  661. i64 vector_count = get_array_type_count(vt);
  662. GB_ASSERT(mt->Matrix.column_count == vector_count);
  663. GB_ASSERT(are_types_identical(mt->Matrix.elem, base_array_type(vt)));
  664. Type *elem = mt->Matrix.elem;
  665. if (lb_is_matrix_simdable(mt)) {
  666. unsigned stride = cast(unsigned)matrix_type_stride_in_elems(mt);
  667. unsigned row_count = cast(unsigned)mt->Matrix.row_count;
  668. unsigned column_count = cast(unsigned)mt->Matrix.column_count;
  669. auto m_columns = slice_make<LLVMValueRef>(permanent_allocator(), column_count);
  670. auto v_rows = slice_make<LLVMValueRef>(permanent_allocator(), column_count);
  671. LLVMValueRef matrix_vector = lb_matrix_to_vector(p, lhs);
  672. for (unsigned column_index = 0; column_index < column_count; column_index++) {
  673. LLVMValueRef mask = llvm_mask_iota(p->module, stride*column_index, row_count);
  674. LLVMValueRef column = LLVMBuildShuffleVector(p->builder, matrix_vector, LLVMGetUndef(LLVMTypeOf(matrix_vector)), mask, "");
  675. m_columns[column_index] = column;
  676. }
  677. for (unsigned row_index = 0; row_index < column_count; row_index++) {
  678. LLVMValueRef value = lb_emit_struct_ev(p, rhs, row_index).value;
  679. LLVMValueRef row = llvm_vector_broadcast(p, value, row_count);
  680. v_rows[row_index] = row;
  681. }
  682. GB_ASSERT(column_count > 0);
  683. LLVMValueRef vector = nullptr;
  684. for (i64 i = 0; i < column_count; i++) {
  685. if (i == 0) {
  686. vector = llvm_vector_mul(p, m_columns[i], v_rows[i]);
  687. } else {
  688. vector = llvm_vector_mul_add(p, m_columns[i], v_rows[i], vector);
  689. }
  690. }
  691. return lb_matrix_cast_vector_to_type(p, vector, type);
  692. }
  693. lbAddr res = lb_add_local_generated(p, type, true);
  694. for (i64 i = 0; i < mt->Matrix.row_count; i++) {
  695. for (i64 j = 0; j < mt->Matrix.column_count; j++) {
  696. lbValue dst = lb_emit_matrix_epi(p, res.addr, i, 0);
  697. lbValue d0 = lb_emit_load(p, dst);
  698. lbValue a = lb_emit_matrix_ev(p, lhs, i, j);
  699. lbValue b = lb_emit_struct_ev(p, rhs, cast(i32)j);
  700. lbValue c = lb_emit_mul_add(p, a, b, d0, elem);
  701. lb_emit_store(p, dst, c);
  702. }
  703. }
  704. return lb_addr_load(p, res);
  705. }
  706. lbValue lb_emit_vector_mul_matrix(lbProcedure *p, lbValue lhs, lbValue rhs, Type *type) {
  707. // TODO(bill): Handle edge case for f16 types on x86(-64) platforms
  708. Type *mt = base_type(rhs.type);
  709. Type *vt = base_type(lhs.type);
  710. GB_ASSERT(is_type_matrix(mt));
  711. GB_ASSERT(is_type_array_like(vt));
  712. i64 vector_count = get_array_type_count(vt);
  713. GB_ASSERT(vector_count == mt->Matrix.row_count);
  714. GB_ASSERT(are_types_identical(mt->Matrix.elem, base_array_type(vt)));
  715. Type *elem = mt->Matrix.elem;
  716. if (lb_is_matrix_simdable(mt)) {
  717. unsigned stride = cast(unsigned)matrix_type_stride_in_elems(mt);
  718. unsigned row_count = cast(unsigned)mt->Matrix.row_count;
  719. unsigned column_count = cast(unsigned)mt->Matrix.column_count; gb_unused(column_count);
  720. auto m_columns = slice_make<LLVMValueRef>(permanent_allocator(), row_count);
  721. auto v_rows = slice_make<LLVMValueRef>(permanent_allocator(), row_count);
  722. LLVMValueRef matrix_vector = lb_matrix_to_vector(p, rhs);
  723. auto mask_elems = slice_make<LLVMValueRef>(permanent_allocator(), column_count);
  724. for (unsigned row_index = 0; row_index < row_count; row_index++) {
  725. for (unsigned column_index = 0; column_index < column_count; column_index++) {
  726. unsigned offset = row_index + column_index*stride;
  727. mask_elems[column_index] = lb_const_int(p->module, t_u32, offset).value;
  728. }
  729. // transpose mask
  730. LLVMValueRef mask = LLVMConstVector(mask_elems.data, column_count);
  731. LLVMValueRef column = LLVMBuildShuffleVector(p->builder, matrix_vector, LLVMGetUndef(LLVMTypeOf(matrix_vector)), mask, "");
  732. m_columns[row_index] = column;
  733. }
  734. for (unsigned column_index = 0; column_index < row_count; column_index++) {
  735. LLVMValueRef value = lb_emit_struct_ev(p, lhs, column_index).value;
  736. LLVMValueRef row = llvm_vector_broadcast(p, value, column_count);
  737. v_rows[column_index] = row;
  738. }
  739. GB_ASSERT(row_count > 0);
  740. LLVMValueRef vector = nullptr;
  741. for (i64 i = 0; i < row_count; i++) {
  742. if (i == 0) {
  743. vector = llvm_vector_mul(p, v_rows[i], m_columns[i]);
  744. } else {
  745. vector = llvm_vector_mul_add(p, v_rows[i], m_columns[i], vector);
  746. }
  747. }
  748. lbAddr res = lb_add_local_generated(p, type, true);
  749. LLVMValueRef res_ptr = res.addr.value;
  750. unsigned alignment = cast(unsigned)gb_max(type_align_of(type), lb_alignof(LLVMTypeOf(vector)));
  751. LLVMSetAlignment(res_ptr, alignment);
  752. res_ptr = LLVMBuildPointerCast(p->builder, res_ptr, LLVMPointerType(LLVMTypeOf(vector), 0), "");
  753. LLVMBuildStore(p->builder, vector, res_ptr);
  754. return lb_addr_load(p, res);
  755. }
  756. lbAddr res = lb_add_local_generated(p, type, true);
  757. for (i64 j = 0; j < mt->Matrix.column_count; j++) {
  758. for (i64 k = 0; k < mt->Matrix.row_count; k++) {
  759. lbValue dst = lb_emit_matrix_epi(p, res.addr, 0, j);
  760. lbValue d0 = lb_emit_load(p, dst);
  761. lbValue a = lb_emit_struct_ev(p, lhs, cast(i32)k);
  762. lbValue b = lb_emit_matrix_ev(p, rhs, k, j);
  763. lbValue c = lb_emit_mul_add(p, a, b, d0, elem);
  764. lb_emit_store(p, dst, c);
  765. }
  766. }
  767. return lb_addr_load(p, res);
  768. }
  769. lbValue lb_emit_arith_matrix(lbProcedure *p, TokenKind op, lbValue lhs, lbValue rhs, Type *type, bool component_wise=false) {
  770. GB_ASSERT(is_type_matrix(lhs.type) || is_type_matrix(rhs.type));
  771. if (op == Token_Mul && !component_wise) {
  772. Type *xt = base_type(lhs.type);
  773. Type *yt = base_type(rhs.type);
  774. if (xt->kind == Type_Matrix) {
  775. if (yt->kind == Type_Matrix) {
  776. return lb_emit_matrix_mul(p, lhs, rhs, type);
  777. } else if (is_type_array_like(yt)) {
  778. return lb_emit_matrix_mul_vector(p, lhs, rhs, type);
  779. }
  780. } else if (is_type_array_like(xt)) {
  781. GB_ASSERT(yt->kind == Type_Matrix);
  782. return lb_emit_vector_mul_matrix(p, lhs, rhs, type);
  783. }
  784. } else {
  785. if (is_type_matrix(lhs.type)) {
  786. rhs = lb_emit_conv(p, rhs, lhs.type);
  787. } else {
  788. lhs = lb_emit_conv(p, lhs, rhs.type);
  789. }
  790. Type *xt = base_type(lhs.type);
  791. Type *yt = base_type(rhs.type);
  792. GB_ASSERT_MSG(are_types_identical(xt, yt), "%s %.*s %s", type_to_string(lhs.type), LIT(token_strings[op]), type_to_string(rhs.type));
  793. GB_ASSERT(xt->kind == Type_Matrix);
  794. // element-wise arithmetic
  795. // pretend it is an array
  796. lbValue array_lhs = lhs;
  797. lbValue array_rhs = rhs;
  798. Type *array_type = alloc_type_array(xt->Matrix.elem, matrix_type_total_internal_elems(xt));
  799. GB_ASSERT(type_size_of(array_type) == type_size_of(xt));
  800. array_lhs.type = array_type;
  801. array_rhs.type = array_type;
  802. if (token_is_comparison(op)) {
  803. lbValue res = lb_emit_comp(p, op, array_lhs, array_rhs);
  804. return lb_emit_conv(p, res, type);
  805. } else {
  806. lbValue array = lb_emit_arith(p, op, array_lhs, array_rhs, array_type);
  807. array.type = type;
  808. return array;
  809. }
  810. }
  811. GB_PANIC("TODO: lb_emit_arith_matrix");
  812. return {};
  813. }
  814. lbValue lb_emit_arith(lbProcedure *p, TokenKind op, lbValue lhs, lbValue rhs, Type *type) {
  815. if (is_type_array_like(lhs.type) || is_type_array_like(rhs.type)) {
  816. return lb_emit_arith_array(p, op, lhs, rhs, type);
  817. } else if (is_type_matrix(lhs.type) || is_type_matrix(rhs.type)) {
  818. return lb_emit_arith_matrix(p, op, lhs, rhs, type);
  819. } else if (is_type_complex(type)) {
  820. lhs = lb_emit_conv(p, lhs, type);
  821. rhs = lb_emit_conv(p, rhs, type);
  822. Type *ft = base_complex_elem_type(type);
  823. if (op == Token_Quo) {
  824. auto args = array_make<lbValue>(permanent_allocator(), 2);
  825. args[0] = lhs;
  826. args[1] = rhs;
  827. switch (type_size_of(ft)) {
  828. case 4: return lb_emit_runtime_call(p, "quo_complex64", args);
  829. case 8: return lb_emit_runtime_call(p, "quo_complex128", args);
  830. default: GB_PANIC("Unknown float type"); break;
  831. }
  832. }
  833. lbAddr res = lb_add_local_generated(p, type, false); // NOTE: initialized in full later
  834. lbValue a = lb_emit_struct_ev(p, lhs, 0);
  835. lbValue b = lb_emit_struct_ev(p, lhs, 1);
  836. lbValue c = lb_emit_struct_ev(p, rhs, 0);
  837. lbValue d = lb_emit_struct_ev(p, rhs, 1);
  838. lbValue real = {};
  839. lbValue imag = {};
  840. switch (op) {
  841. case Token_Add:
  842. real = lb_emit_arith(p, Token_Add, a, c, ft);
  843. imag = lb_emit_arith(p, Token_Add, b, d, ft);
  844. break;
  845. case Token_Sub:
  846. real = lb_emit_arith(p, Token_Sub, a, c, ft);
  847. imag = lb_emit_arith(p, Token_Sub, b, d, ft);
  848. break;
  849. case Token_Mul: {
  850. lbValue x = lb_emit_arith(p, Token_Mul, a, c, ft);
  851. lbValue y = lb_emit_arith(p, Token_Mul, b, d, ft);
  852. real = lb_emit_arith(p, Token_Sub, x, y, ft);
  853. lbValue z = lb_emit_arith(p, Token_Mul, b, c, ft);
  854. lbValue w = lb_emit_arith(p, Token_Mul, a, d, ft);
  855. imag = lb_emit_arith(p, Token_Add, z, w, ft);
  856. break;
  857. }
  858. }
  859. lb_emit_store(p, lb_emit_struct_ep(p, res.addr, 0), real);
  860. lb_emit_store(p, lb_emit_struct_ep(p, res.addr, 1), imag);
  861. return lb_addr_load(p, res);
  862. } else if (is_type_quaternion(type)) {
  863. lhs = lb_emit_conv(p, lhs, type);
  864. rhs = lb_emit_conv(p, rhs, type);
  865. Type *ft = base_complex_elem_type(type);
  866. if (op == Token_Add || op == Token_Sub) {
  867. lbAddr res = lb_add_local_generated(p, type, false); // NOTE: initialized in full later
  868. lbValue x0 = lb_emit_struct_ev(p, lhs, 0);
  869. lbValue x1 = lb_emit_struct_ev(p, lhs, 1);
  870. lbValue x2 = lb_emit_struct_ev(p, lhs, 2);
  871. lbValue x3 = lb_emit_struct_ev(p, lhs, 3);
  872. lbValue y0 = lb_emit_struct_ev(p, rhs, 0);
  873. lbValue y1 = lb_emit_struct_ev(p, rhs, 1);
  874. lbValue y2 = lb_emit_struct_ev(p, rhs, 2);
  875. lbValue y3 = lb_emit_struct_ev(p, rhs, 3);
  876. lbValue z0 = lb_emit_arith(p, op, x0, y0, ft);
  877. lbValue z1 = lb_emit_arith(p, op, x1, y1, ft);
  878. lbValue z2 = lb_emit_arith(p, op, x2, y2, ft);
  879. lbValue z3 = lb_emit_arith(p, op, x3, y3, ft);
  880. lb_emit_store(p, lb_emit_struct_ep(p, res.addr, 0), z0);
  881. lb_emit_store(p, lb_emit_struct_ep(p, res.addr, 1), z1);
  882. lb_emit_store(p, lb_emit_struct_ep(p, res.addr, 2), z2);
  883. lb_emit_store(p, lb_emit_struct_ep(p, res.addr, 3), z3);
  884. return lb_addr_load(p, res);
  885. } else if (op == Token_Mul) {
  886. auto args = array_make<lbValue>(permanent_allocator(), 2);
  887. args[0] = lhs;
  888. args[1] = rhs;
  889. switch (8*type_size_of(ft)) {
  890. case 32: return lb_emit_runtime_call(p, "mul_quaternion128", args);
  891. case 64: return lb_emit_runtime_call(p, "mul_quaternion256", args);
  892. default: GB_PANIC("Unknown float type"); break;
  893. }
  894. } else if (op == Token_Quo) {
  895. auto args = array_make<lbValue>(permanent_allocator(), 2);
  896. args[0] = lhs;
  897. args[1] = rhs;
  898. switch (8*type_size_of(ft)) {
  899. case 32: return lb_emit_runtime_call(p, "quo_quaternion128", args);
  900. case 64: return lb_emit_runtime_call(p, "quo_quaternion256", args);
  901. default: GB_PANIC("Unknown float type"); break;
  902. }
  903. }
  904. }
  905. if (is_type_integer(type) && is_type_different_to_arch_endianness(type)) {
  906. switch (op) {
  907. case Token_AndNot:
  908. case Token_And:
  909. case Token_Or:
  910. case Token_Xor:
  911. goto handle_op;
  912. }
  913. Type *platform_type = integer_endian_type_to_platform_type(type);
  914. lbValue x = lb_emit_byte_swap(p, lhs, integer_endian_type_to_platform_type(lhs.type));
  915. lbValue y = lb_emit_byte_swap(p, rhs, integer_endian_type_to_platform_type(rhs.type));
  916. lbValue res = lb_emit_arith(p, op, x, y, platform_type);
  917. return lb_emit_byte_swap(p, res, type);
  918. }
  919. if (is_type_float(type) && is_type_different_to_arch_endianness(type)) {
  920. Type *platform_type = integer_endian_type_to_platform_type(type);
  921. lbValue x = lb_emit_conv(p, lhs, integer_endian_type_to_platform_type(lhs.type));
  922. lbValue y = lb_emit_conv(p, rhs, integer_endian_type_to_platform_type(rhs.type));
  923. lbValue res = lb_emit_arith(p, op, x, y, platform_type);
  924. return lb_emit_byte_swap(p, res, type);
  925. }
  926. handle_op:
  927. lhs = lb_emit_conv(p, lhs, type);
  928. rhs = lb_emit_conv(p, rhs, type);
  929. lbValue res = {};
  930. res.type = type;
  931. // NOTE(bill): Bit Set Aliases for + and -
  932. if (is_type_bit_set(type)) {
  933. switch (op) {
  934. case Token_Add: op = Token_Or; break;
  935. case Token_Sub: op = Token_AndNot; break;
  936. }
  937. }
  938. Type *integral_type = type;
  939. if (is_type_simd_vector(integral_type)) {
  940. integral_type = core_array_type(integral_type);
  941. }
  942. switch (op) {
  943. case Token_Add:
  944. if (is_type_float(integral_type)) {
  945. res.value = LLVMBuildFAdd(p->builder, lhs.value, rhs.value, "");
  946. return res;
  947. }
  948. res.value = LLVMBuildAdd(p->builder, lhs.value, rhs.value, "");
  949. return res;
  950. case Token_Sub:
  951. if (is_type_float(integral_type)) {
  952. res.value = LLVMBuildFSub(p->builder, lhs.value, rhs.value, "");
  953. return res;
  954. }
  955. res.value = LLVMBuildSub(p->builder, lhs.value, rhs.value, "");
  956. return res;
  957. case Token_Mul:
  958. if (is_type_float(integral_type)) {
  959. res.value = LLVMBuildFMul(p->builder, lhs.value, rhs.value, "");
  960. return res;
  961. }
  962. res.value = LLVMBuildMul(p->builder, lhs.value, rhs.value, "");
  963. return res;
  964. case Token_Quo:
  965. if (is_type_float(integral_type)) {
  966. res.value = LLVMBuildFDiv(p->builder, lhs.value, rhs.value, "");
  967. return res;
  968. } else if (is_type_unsigned(integral_type)) {
  969. res.value = LLVMBuildUDiv(p->builder, lhs.value, rhs.value, "");
  970. return res;
  971. }
  972. res.value = LLVMBuildSDiv(p->builder, lhs.value, rhs.value, "");
  973. return res;
  974. case Token_Mod:
  975. if (is_type_float(integral_type)) {
  976. res.value = LLVMBuildFRem(p->builder, lhs.value, rhs.value, "");
  977. return res;
  978. } else if (is_type_unsigned(integral_type)) {
  979. res.value = LLVMBuildURem(p->builder, lhs.value, rhs.value, "");
  980. return res;
  981. }
  982. res.value = LLVMBuildSRem(p->builder, lhs.value, rhs.value, "");
  983. return res;
  984. case Token_ModMod:
  985. if (is_type_unsigned(integral_type)) {
  986. res.value = LLVMBuildURem(p->builder, lhs.value, rhs.value, "");
  987. return res;
  988. } else {
  989. LLVMValueRef a = LLVMBuildSRem(p->builder, lhs.value, rhs.value, "");
  990. LLVMValueRef b = LLVMBuildAdd(p->builder, a, rhs.value, "");
  991. LLVMValueRef c = LLVMBuildSRem(p->builder, b, rhs.value, "");
  992. res.value = c;
  993. return res;
  994. }
  995. case Token_And:
  996. res.value = LLVMBuildAnd(p->builder, lhs.value, rhs.value, "");
  997. return res;
  998. case Token_Or:
  999. res.value = LLVMBuildOr(p->builder, lhs.value, rhs.value, "");
  1000. return res;
  1001. case Token_Xor:
  1002. res.value = LLVMBuildXor(p->builder, lhs.value, rhs.value, "");
  1003. return res;
  1004. case Token_Shl:
  1005. {
  1006. rhs = lb_emit_conv(p, rhs, lhs.type);
  1007. LLVMValueRef lhsval = lhs.value;
  1008. LLVMValueRef bits = rhs.value;
  1009. LLVMValueRef bit_size = LLVMConstInt(lb_type(p->module, rhs.type), 8*type_size_of(lhs.type), false);
  1010. LLVMValueRef width_test = LLVMBuildICmp(p->builder, LLVMIntULT, bits, bit_size, "");
  1011. res.value = LLVMBuildShl(p->builder, lhsval, bits, "");
  1012. LLVMValueRef zero = LLVMConstNull(lb_type(p->module, lhs.type));
  1013. res.value = LLVMBuildSelect(p->builder, width_test, res.value, zero, "");
  1014. return res;
  1015. }
  1016. case Token_Shr:
  1017. {
  1018. rhs = lb_emit_conv(p, rhs, lhs.type);
  1019. LLVMValueRef lhsval = lhs.value;
  1020. LLVMValueRef bits = rhs.value;
  1021. bool is_unsigned = is_type_unsigned(integral_type);
  1022. LLVMValueRef bit_size = LLVMConstInt(lb_type(p->module, rhs.type), 8*type_size_of(lhs.type), false);
  1023. LLVMValueRef width_test = LLVMBuildICmp(p->builder, LLVMIntULT, bits, bit_size, "");
  1024. if (is_unsigned) {
  1025. res.value = LLVMBuildLShr(p->builder, lhsval, bits, "");
  1026. } else {
  1027. res.value = LLVMBuildAShr(p->builder, lhsval, bits, "");
  1028. }
  1029. LLVMValueRef zero = LLVMConstNull(lb_type(p->module, lhs.type));
  1030. res.value = LLVMBuildSelect(p->builder, width_test, res.value, zero, "");
  1031. return res;
  1032. }
  1033. case Token_AndNot:
  1034. {
  1035. LLVMValueRef new_rhs = LLVMBuildNot(p->builder, rhs.value, "");
  1036. res.value = LLVMBuildAnd(p->builder, lhs.value, new_rhs, "");
  1037. return res;
  1038. }
  1039. break;
  1040. }
  1041. GB_PANIC("unhandled operator of lb_emit_arith");
  1042. return {};
  1043. }
  1044. lbValue lb_build_binary_expr(lbProcedure *p, Ast *expr) {
  1045. ast_node(be, BinaryExpr, expr);
  1046. TypeAndValue tv = type_and_value_of_expr(expr);
  1047. if (is_type_matrix(be->left->tav.type) || is_type_matrix(be->right->tav.type)) {
  1048. lbValue left = lb_build_expr(p, be->left);
  1049. lbValue right = lb_build_expr(p, be->right);
  1050. return lb_emit_arith_matrix(p, be->op.kind, left, right, default_type(tv.type));
  1051. }
  1052. switch (be->op.kind) {
  1053. case Token_Add:
  1054. case Token_Sub:
  1055. case Token_Mul:
  1056. case Token_Quo:
  1057. case Token_Mod:
  1058. case Token_ModMod:
  1059. case Token_And:
  1060. case Token_Or:
  1061. case Token_Xor:
  1062. case Token_AndNot: {
  1063. Type *type = default_type(tv.type);
  1064. lbValue left = lb_build_expr(p, be->left);
  1065. lbValue right = lb_build_expr(p, be->right);
  1066. return lb_emit_arith(p, be->op.kind, left, right, type);
  1067. }
  1068. case Token_Shl:
  1069. case Token_Shr: {
  1070. lbValue left, right;
  1071. Type *type = default_type(tv.type);
  1072. left = lb_build_expr(p, be->left);
  1073. if (lb_is_expr_untyped_const(be->right)) {
  1074. // NOTE(bill): RHS shift operands can still be untyped
  1075. // Just bypass the standard lb_build_expr
  1076. right = lb_expr_untyped_const_to_typed(p->module, be->right, type);
  1077. } else {
  1078. right = lb_build_expr(p, be->right);
  1079. }
  1080. return lb_emit_arith(p, be->op.kind, left, right, type);
  1081. }
  1082. case Token_CmpEq:
  1083. case Token_NotEq:
  1084. if (is_type_untyped_nil(be->right->tav.type)) {
  1085. lbValue left = lb_build_expr(p, be->left);
  1086. lbValue cmp = lb_emit_comp_against_nil(p, be->op.kind, left);
  1087. Type *type = default_type(tv.type);
  1088. return lb_emit_conv(p, cmp, type);
  1089. } else if (is_type_untyped_nil(be->left->tav.type)) {
  1090. lbValue right = lb_build_expr(p, be->right);
  1091. lbValue cmp = lb_emit_comp_against_nil(p, be->op.kind, right);
  1092. Type *type = default_type(tv.type);
  1093. return lb_emit_conv(p, cmp, type);
  1094. }
  1095. /*fallthrough*/
  1096. case Token_Lt:
  1097. case Token_LtEq:
  1098. case Token_Gt:
  1099. case Token_GtEq:
  1100. {
  1101. lbValue left = {};
  1102. lbValue right = {};
  1103. if (be->left->tav.mode == Addressing_Type) {
  1104. left = lb_typeid(p->module, be->left->tav.type);
  1105. }
  1106. if (be->right->tav.mode == Addressing_Type) {
  1107. right = lb_typeid(p->module, be->right->tav.type);
  1108. }
  1109. if (left.value == nullptr) left = lb_build_expr(p, be->left);
  1110. if (right.value == nullptr) right = lb_build_expr(p, be->right);
  1111. lbValue cmp = lb_emit_comp(p, be->op.kind, left, right);
  1112. Type *type = default_type(tv.type);
  1113. return lb_emit_conv(p, cmp, type);
  1114. }
  1115. case Token_CmpAnd:
  1116. case Token_CmpOr:
  1117. return lb_emit_logical_binary_expr(p, be->op.kind, be->left, be->right, tv.type);
  1118. case Token_in:
  1119. case Token_not_in:
  1120. {
  1121. lbValue left = lb_build_expr(p, be->left);
  1122. lbValue right = lb_build_expr(p, be->right);
  1123. Type *rt = base_type(right.type);
  1124. if (is_type_pointer(rt)) {
  1125. right = lb_emit_load(p, right);
  1126. rt = type_deref(rt);
  1127. }
  1128. switch (rt->kind) {
  1129. case Type_Map:
  1130. {
  1131. lbValue addr = lb_address_from_load_or_generate_local(p, right);
  1132. lbValue h = lb_gen_map_header(p, addr, rt);
  1133. lbValue key = lb_gen_map_hash(p, left, rt->Map.key);
  1134. auto args = array_make<lbValue>(permanent_allocator(), 2);
  1135. args[0] = h;
  1136. args[1] = key;
  1137. lbValue ptr = lb_emit_runtime_call(p, "__dynamic_map_get", args);
  1138. if (be->op.kind == Token_in) {
  1139. return lb_emit_conv(p, lb_emit_comp_against_nil(p, Token_NotEq, ptr), t_bool);
  1140. } else {
  1141. return lb_emit_conv(p, lb_emit_comp_against_nil(p, Token_CmpEq, ptr), t_bool);
  1142. }
  1143. }
  1144. break;
  1145. case Type_BitSet:
  1146. {
  1147. Type *key_type = rt->BitSet.elem;
  1148. GB_ASSERT(are_types_identical(left.type, key_type));
  1149. Type *it = bit_set_to_int(rt);
  1150. left = lb_emit_conv(p, left, it);
  1151. lbValue lower = lb_const_value(p->module, it, exact_value_i64(rt->BitSet.lower));
  1152. lbValue key = lb_emit_arith(p, Token_Sub, left, lower, it);
  1153. lbValue bit = lb_emit_arith(p, Token_Shl, lb_const_int(p->module, it, 1), key, it);
  1154. bit = lb_emit_conv(p, bit, it);
  1155. lbValue old_value = lb_emit_transmute(p, right, it);
  1156. lbValue new_value = lb_emit_arith(p, Token_And, old_value, bit, it);
  1157. if (be->op.kind == Token_in) {
  1158. return lb_emit_conv(p, lb_emit_comp(p, Token_NotEq, new_value, lb_const_int(p->module, new_value.type, 0)), t_bool);
  1159. } else {
  1160. return lb_emit_conv(p, lb_emit_comp(p, Token_CmpEq, new_value, lb_const_int(p->module, new_value.type, 0)), t_bool);
  1161. }
  1162. }
  1163. break;
  1164. default:
  1165. GB_PANIC("Invalid 'in' type");
  1166. }
  1167. break;
  1168. }
  1169. break;
  1170. default:
  1171. GB_PANIC("Invalid binary expression");
  1172. break;
  1173. }
  1174. return {};
  1175. }
  1176. lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t) {
  1177. lbModule *m = p->module;
  1178. t = reduce_tuple_to_single_type(t);
  1179. Type *src_type = value.type;
  1180. if (are_types_identical(t, src_type)) {
  1181. return value;
  1182. }
  1183. Type *src = core_type(src_type);
  1184. Type *dst = core_type(t);
  1185. GB_ASSERT(src != nullptr);
  1186. GB_ASSERT(dst != nullptr);
  1187. if (is_type_untyped_nil(src)) {
  1188. return lb_const_nil(m, t);
  1189. }
  1190. if (is_type_untyped_undef(src)) {
  1191. return lb_const_undef(m, t);
  1192. }
  1193. if (LLVMIsConstant(value.value)) {
  1194. if (is_type_any(dst)) {
  1195. Type *st = default_type(src_type);
  1196. lbAddr default_value = lb_add_local_generated(p, st, false);
  1197. lb_addr_store(p, default_value, value);
  1198. lbValue data = lb_emit_conv(p, default_value.addr, t_rawptr);
  1199. lbValue id = lb_typeid(m, st);
  1200. lbAddr res = lb_add_local_generated(p, t, false);
  1201. lbValue a0 = lb_emit_struct_ep(p, res.addr, 0);
  1202. lbValue a1 = lb_emit_struct_ep(p, res.addr, 1);
  1203. lb_emit_store(p, a0, data);
  1204. lb_emit_store(p, a1, id);
  1205. return lb_addr_load(p, res);
  1206. } else if (dst->kind == Type_Basic) {
  1207. if (src->Basic.kind == Basic_string && dst->Basic.kind == Basic_cstring) {
  1208. String str = lb_get_const_string(m, value);
  1209. lbValue res = {};
  1210. res.type = t;
  1211. res.value = llvm_cstring(m, str);
  1212. return res;
  1213. }
  1214. // if (is_type_float(dst)) {
  1215. // return value;
  1216. // } else if (is_type_integer(dst)) {
  1217. // return value;
  1218. // }
  1219. // ExactValue ev = value->Constant.value;
  1220. // if (is_type_float(dst)) {
  1221. // ev = exact_value_to_float(ev);
  1222. // } else if (is_type_complex(dst)) {
  1223. // ev = exact_value_to_complex(ev);
  1224. // } else if (is_type_quaternion(dst)) {
  1225. // ev = exact_value_to_quaternion(ev);
  1226. // } else if (is_type_string(dst)) {
  1227. // // Handled elsewhere
  1228. // GB_ASSERT_MSG(ev.kind == ExactValue_String, "%d", ev.kind);
  1229. // } else if (is_type_integer(dst)) {
  1230. // ev = exact_value_to_integer(ev);
  1231. // } else if (is_type_pointer(dst)) {
  1232. // // IMPORTANT NOTE(bill): LLVM doesn't support pointer constants expect 'null'
  1233. // lbValue i = lb_add_module_constant(p->module, t_uintptr, ev);
  1234. // return lb_emit(p, lb_instr_conv(p, irConv_inttoptr, i, t_uintptr, dst));
  1235. // }
  1236. // return lb_const_value(p->module, t, ev);
  1237. }
  1238. }
  1239. if (are_types_identical(src, dst)) {
  1240. if (!are_types_identical(src_type, t)) {
  1241. return lb_emit_transmute(p, value, t);
  1242. }
  1243. return value;
  1244. }
  1245. // bool <-> llvm bool
  1246. if (is_type_boolean(src) && dst == t_llvm_bool) {
  1247. lbValue res = {};
  1248. res.value = LLVMBuildTrunc(p->builder, value.value, lb_type(m, dst), "");
  1249. res.type = dst;
  1250. return res;
  1251. }
  1252. if (src == t_llvm_bool && is_type_boolean(dst)) {
  1253. lbValue res = {};
  1254. res.value = LLVMBuildZExt(p->builder, value.value, lb_type(m, dst), "");
  1255. res.type = dst;
  1256. return res;
  1257. }
  1258. // integer -> integer
  1259. if (is_type_integer(src) && is_type_integer(dst)) {
  1260. GB_ASSERT(src->kind == Type_Basic &&
  1261. dst->kind == Type_Basic);
  1262. i64 sz = type_size_of(default_type(src));
  1263. i64 dz = type_size_of(default_type(dst));
  1264. if (sz == dz) {
  1265. if (dz > 1 && !types_have_same_internal_endian(src, dst)) {
  1266. return lb_emit_byte_swap(p, value, t);
  1267. }
  1268. lbValue res = {};
  1269. res.value = value.value;
  1270. res.type = t;
  1271. return res;
  1272. }
  1273. if (sz > 1 && is_type_different_to_arch_endianness(src)) {
  1274. Type *platform_src_type = integer_endian_type_to_platform_type(src);
  1275. value = lb_emit_byte_swap(p, value, platform_src_type);
  1276. }
  1277. LLVMOpcode op = LLVMTrunc;
  1278. if (dz < sz) {
  1279. op = LLVMTrunc;
  1280. } else if (dz == sz) {
  1281. // NOTE(bill): In LLVM, all integers are signed and rely upon 2's compliment
  1282. // NOTE(bill): Copy the value just for type correctness
  1283. op = LLVMBitCast;
  1284. } else if (dz > sz) {
  1285. op = is_type_unsigned(src) ? LLVMZExt : LLVMSExt; // zero extent
  1286. }
  1287. if (dz > 1 && is_type_different_to_arch_endianness(dst)) {
  1288. Type *platform_dst_type = integer_endian_type_to_platform_type(dst);
  1289. lbValue res = {};
  1290. res.value = LLVMBuildCast(p->builder, op, value.value, lb_type(m, platform_dst_type), "");
  1291. res.type = t;
  1292. return lb_emit_byte_swap(p, res, t);
  1293. } else {
  1294. lbValue res = {};
  1295. res.value = LLVMBuildCast(p->builder, op, value.value, lb_type(m, t), "");
  1296. res.type = t;
  1297. return res;
  1298. }
  1299. }
  1300. // boolean -> boolean/integer
  1301. if (is_type_boolean(src) && (is_type_boolean(dst) || is_type_integer(dst))) {
  1302. LLVMValueRef b = LLVMBuildICmp(p->builder, LLVMIntNE, value.value, LLVMConstNull(lb_type(m, value.type)), "");
  1303. lbValue res = {};
  1304. res.value = LLVMBuildIntCast2(p->builder, b, lb_type(m, t), false, "");
  1305. res.type = t;
  1306. return res;
  1307. }
  1308. if (is_type_cstring(src) && is_type_u8_ptr(dst)) {
  1309. return lb_emit_transmute(p, value, dst);
  1310. }
  1311. if (is_type_u8_ptr(src) && is_type_cstring(dst)) {
  1312. return lb_emit_transmute(p, value, dst);
  1313. }
  1314. if (is_type_cstring(src) && is_type_u8_multi_ptr(dst)) {
  1315. return lb_emit_transmute(p, value, dst);
  1316. }
  1317. if (is_type_u8_multi_ptr(src) && is_type_cstring(dst)) {
  1318. return lb_emit_transmute(p, value, dst);
  1319. }
  1320. if (is_type_cstring(src) && is_type_rawptr(dst)) {
  1321. return lb_emit_transmute(p, value, dst);
  1322. }
  1323. if (is_type_rawptr(src) && is_type_cstring(dst)) {
  1324. return lb_emit_transmute(p, value, dst);
  1325. }
  1326. if (are_types_identical(src, t_cstring) && are_types_identical(dst, t_string)) {
  1327. lbValue c = lb_emit_conv(p, value, t_cstring);
  1328. auto args = array_make<lbValue>(permanent_allocator(), 1);
  1329. args[0] = c;
  1330. lbValue s = lb_emit_runtime_call(p, "cstring_to_string", args);
  1331. return lb_emit_conv(p, s, dst);
  1332. }
  1333. // integer -> boolean
  1334. if (is_type_integer(src) && is_type_boolean(dst)) {
  1335. lbValue res = {};
  1336. res.value = LLVMBuildICmp(p->builder, LLVMIntNE, value.value, LLVMConstNull(lb_type(m, value.type)), "");
  1337. res.type = t_llvm_bool;
  1338. return lb_emit_conv(p, res, t);
  1339. }
  1340. // float -> float
  1341. if (is_type_float(src) && is_type_float(dst)) {
  1342. i64 sz = type_size_of(src);
  1343. i64 dz = type_size_of(dst);
  1344. if (dz == sz) {
  1345. if (types_have_same_internal_endian(src, dst)) {
  1346. lbValue res = {};
  1347. res.type = t;
  1348. res.value = value.value;
  1349. return res;
  1350. } else {
  1351. return lb_emit_byte_swap(p, value, t);
  1352. }
  1353. }
  1354. if (is_type_different_to_arch_endianness(src) || is_type_different_to_arch_endianness(dst)) {
  1355. Type *platform_src_type = integer_endian_type_to_platform_type(src);
  1356. Type *platform_dst_type = integer_endian_type_to_platform_type(dst);
  1357. lbValue res = {};
  1358. res = lb_emit_conv(p, value, platform_src_type);
  1359. res = lb_emit_conv(p, res, platform_dst_type);
  1360. if (is_type_different_to_arch_endianness(dst)) {
  1361. res = lb_emit_byte_swap(p, res, t);
  1362. }
  1363. return lb_emit_conv(p, res, t);
  1364. }
  1365. lbValue res = {};
  1366. res.type = t;
  1367. if (dz >= sz) {
  1368. res.value = LLVMBuildFPExt(p->builder, value.value, lb_type(m, t), "");
  1369. } else {
  1370. res.value = LLVMBuildFPTrunc(p->builder, value.value, lb_type(m, t), "");
  1371. }
  1372. return res;
  1373. }
  1374. if (is_type_complex(src) && is_type_complex(dst)) {
  1375. Type *ft = base_complex_elem_type(dst);
  1376. lbAddr gen = lb_add_local_generated(p, dst, false);
  1377. lbValue gp = lb_addr_get_ptr(p, gen);
  1378. lbValue real = lb_emit_conv(p, lb_emit_struct_ev(p, value, 0), ft);
  1379. lbValue imag = lb_emit_conv(p, lb_emit_struct_ev(p, value, 1), ft);
  1380. lb_emit_store(p, lb_emit_struct_ep(p, gp, 0), real);
  1381. lb_emit_store(p, lb_emit_struct_ep(p, gp, 1), imag);
  1382. return lb_addr_load(p, gen);
  1383. }
  1384. if (is_type_quaternion(src) && is_type_quaternion(dst)) {
  1385. // @QuaternionLayout
  1386. Type *ft = base_complex_elem_type(dst);
  1387. lbAddr gen = lb_add_local_generated(p, dst, false);
  1388. lbValue gp = lb_addr_get_ptr(p, gen);
  1389. lbValue q0 = lb_emit_conv(p, lb_emit_struct_ev(p, value, 0), ft);
  1390. lbValue q1 = lb_emit_conv(p, lb_emit_struct_ev(p, value, 1), ft);
  1391. lbValue q2 = lb_emit_conv(p, lb_emit_struct_ev(p, value, 2), ft);
  1392. lbValue q3 = lb_emit_conv(p, lb_emit_struct_ev(p, value, 3), ft);
  1393. lb_emit_store(p, lb_emit_struct_ep(p, gp, 0), q0);
  1394. lb_emit_store(p, lb_emit_struct_ep(p, gp, 1), q1);
  1395. lb_emit_store(p, lb_emit_struct_ep(p, gp, 2), q2);
  1396. lb_emit_store(p, lb_emit_struct_ep(p, gp, 3), q3);
  1397. return lb_addr_load(p, gen);
  1398. }
  1399. if (is_type_integer(src) && is_type_complex(dst)) {
  1400. Type *ft = base_complex_elem_type(dst);
  1401. lbAddr gen = lb_add_local_generated(p, dst, true);
  1402. lbValue gp = lb_addr_get_ptr(p, gen);
  1403. lbValue real = lb_emit_conv(p, value, ft);
  1404. lb_emit_store(p, lb_emit_struct_ep(p, gp, 0), real);
  1405. return lb_addr_load(p, gen);
  1406. }
  1407. if (is_type_float(src) && is_type_complex(dst)) {
  1408. Type *ft = base_complex_elem_type(dst);
  1409. lbAddr gen = lb_add_local_generated(p, dst, true);
  1410. lbValue gp = lb_addr_get_ptr(p, gen);
  1411. lbValue real = lb_emit_conv(p, value, ft);
  1412. lb_emit_store(p, lb_emit_struct_ep(p, gp, 0), real);
  1413. return lb_addr_load(p, gen);
  1414. }
  1415. if (is_type_integer(src) && is_type_quaternion(dst)) {
  1416. Type *ft = base_complex_elem_type(dst);
  1417. lbAddr gen = lb_add_local_generated(p, dst, true);
  1418. lbValue gp = lb_addr_get_ptr(p, gen);
  1419. lbValue real = lb_emit_conv(p, value, ft);
  1420. // @QuaternionLayout
  1421. lb_emit_store(p, lb_emit_struct_ep(p, gp, 3), real);
  1422. return lb_addr_load(p, gen);
  1423. }
  1424. if (is_type_float(src) && is_type_quaternion(dst)) {
  1425. Type *ft = base_complex_elem_type(dst);
  1426. lbAddr gen = lb_add_local_generated(p, dst, true);
  1427. lbValue gp = lb_addr_get_ptr(p, gen);
  1428. lbValue real = lb_emit_conv(p, value, ft);
  1429. // @QuaternionLayout
  1430. lb_emit_store(p, lb_emit_struct_ep(p, gp, 3), real);
  1431. return lb_addr_load(p, gen);
  1432. }
  1433. if (is_type_complex(src) && is_type_quaternion(dst)) {
  1434. Type *ft = base_complex_elem_type(dst);
  1435. lbAddr gen = lb_add_local_generated(p, dst, true);
  1436. lbValue gp = lb_addr_get_ptr(p, gen);
  1437. lbValue real = lb_emit_conv(p, lb_emit_struct_ev(p, value, 0), ft);
  1438. lbValue imag = lb_emit_conv(p, lb_emit_struct_ev(p, value, 1), ft);
  1439. // @QuaternionLayout
  1440. lb_emit_store(p, lb_emit_struct_ep(p, gp, 3), real);
  1441. lb_emit_store(p, lb_emit_struct_ep(p, gp, 0), imag);
  1442. return lb_addr_load(p, gen);
  1443. }
  1444. // float <-> integer
  1445. if (is_type_float(src) && is_type_integer(dst)) {
  1446. if (is_type_different_to_arch_endianness(src) || is_type_different_to_arch_endianness(dst)) {
  1447. Type *platform_src_type = integer_endian_type_to_platform_type(src);
  1448. Type *platform_dst_type = integer_endian_type_to_platform_type(dst);
  1449. lbValue res = {};
  1450. res = lb_emit_conv(p, value, platform_src_type);
  1451. res = lb_emit_conv(p, res, platform_dst_type);
  1452. if (is_type_different_to_arch_endianness(dst)) {
  1453. res = lb_emit_byte_swap(p, res, platform_dst_type);
  1454. }
  1455. return lb_emit_conv(p, res, t);
  1456. }
  1457. if (is_type_integer_128bit(dst)) {
  1458. auto args = array_make<lbValue>(temporary_allocator(), 1);
  1459. args[0] = value;
  1460. char const *call = "fixunsdfdi";
  1461. if (is_type_unsigned(dst)) {
  1462. call = "fixunsdfti";
  1463. }
  1464. lbValue res_i128 = lb_emit_runtime_call(p, call, args);
  1465. return lb_emit_conv(p, res_i128, t);
  1466. }
  1467. lbValue res = {};
  1468. res.type = t;
  1469. if (is_type_unsigned(dst)) {
  1470. res.value = LLVMBuildFPToUI(p->builder, value.value, lb_type(m, t), "");
  1471. } else {
  1472. res.value = LLVMBuildFPToSI(p->builder, value.value, lb_type(m, t), "");
  1473. }
  1474. return res;
  1475. }
  1476. if (is_type_integer(src) && is_type_float(dst)) {
  1477. if (is_type_different_to_arch_endianness(src) || is_type_different_to_arch_endianness(dst)) {
  1478. Type *platform_src_type = integer_endian_type_to_platform_type(src);
  1479. Type *platform_dst_type = integer_endian_type_to_platform_type(dst);
  1480. lbValue res = {};
  1481. res = lb_emit_conv(p, value, platform_src_type);
  1482. res = lb_emit_conv(p, res, platform_dst_type);
  1483. if (is_type_different_to_arch_endianness(dst)) {
  1484. res = lb_emit_byte_swap(p, res, t);
  1485. }
  1486. return lb_emit_conv(p, res, t);
  1487. }
  1488. if (is_type_integer_128bit(src)) {
  1489. auto args = array_make<lbValue>(temporary_allocator(), 1);
  1490. args[0] = value;
  1491. char const *call = "floattidf";
  1492. if (is_type_unsigned(src)) {
  1493. call = "floattidf_unsigned";
  1494. }
  1495. lbValue res_f64 = lb_emit_runtime_call(p, call, args);
  1496. return lb_emit_conv(p, res_f64, t);
  1497. }
  1498. lbValue res = {};
  1499. res.type = t;
  1500. if (is_type_unsigned(src)) {
  1501. res.value = LLVMBuildUIToFP(p->builder, value.value, lb_type(m, t), "");
  1502. } else {
  1503. res.value = LLVMBuildSIToFP(p->builder, value.value, lb_type(m, t), "");
  1504. }
  1505. return res;
  1506. }
  1507. // Pointer <-> uintptr
  1508. if (is_type_pointer(src) && is_type_uintptr(dst)) {
  1509. lbValue res = {};
  1510. res.type = t;
  1511. res.value = LLVMBuildPtrToInt(p->builder, value.value, lb_type(m, t), "");
  1512. return res;
  1513. }
  1514. if (is_type_uintptr(src) && is_type_pointer(dst)) {
  1515. lbValue res = {};
  1516. res.type = t;
  1517. res.value = LLVMBuildIntToPtr(p->builder, value.value, lb_type(m, t), "");
  1518. return res;
  1519. }
  1520. if (is_type_multi_pointer(src) && is_type_uintptr(dst)) {
  1521. lbValue res = {};
  1522. res.type = t;
  1523. res.value = LLVMBuildPtrToInt(p->builder, value.value, lb_type(m, t), "");
  1524. return res;
  1525. }
  1526. if (is_type_uintptr(src) && is_type_multi_pointer(dst)) {
  1527. lbValue res = {};
  1528. res.type = t;
  1529. res.value = LLVMBuildIntToPtr(p->builder, value.value, lb_type(m, t), "");
  1530. return res;
  1531. }
  1532. #if 1
  1533. if (is_type_union(dst)) {
  1534. for_array(i, dst->Union.variants) {
  1535. Type *vt = dst->Union.variants[i];
  1536. if (are_types_identical(vt, src_type)) {
  1537. lbAddr parent = lb_add_local_generated(p, t, true);
  1538. lb_emit_store_union_variant(p, parent.addr, value, vt);
  1539. return lb_addr_load(p, parent);
  1540. }
  1541. }
  1542. }
  1543. #endif
  1544. // NOTE(bill): This has to be done before 'Pointer <-> Pointer' as it's
  1545. // subtype polymorphism casting
  1546. if (check_is_assignable_to_using_subtype(src_type, t)) {
  1547. Type *st = type_deref(src_type);
  1548. st = type_deref(st);
  1549. bool st_is_ptr = is_type_pointer(src_type);
  1550. st = base_type(st);
  1551. Type *dt = t;
  1552. GB_ASSERT(is_type_struct(st) || is_type_raw_union(st));
  1553. String field_name = lookup_subtype_polymorphic_field(t, src_type);
  1554. if (field_name.len > 0) {
  1555. // NOTE(bill): It can be casted
  1556. Selection sel = lookup_field(st, field_name, false, true);
  1557. if (sel.entity != nullptr) {
  1558. if (st_is_ptr) {
  1559. lbValue res = lb_emit_deep_field_gep(p, value, sel);
  1560. Type *rt = res.type;
  1561. if (!are_types_identical(rt, dt) && are_types_identical(type_deref(rt), dt)) {
  1562. res = lb_emit_load(p, res);
  1563. }
  1564. return res;
  1565. } else {
  1566. if (is_type_pointer(value.type)) {
  1567. Type *rt = value.type;
  1568. if (!are_types_identical(rt, dt) && are_types_identical(type_deref(rt), dt)) {
  1569. value = lb_emit_load(p, value);
  1570. } else {
  1571. value = lb_emit_deep_field_gep(p, value, sel);
  1572. return lb_emit_load(p, value);
  1573. }
  1574. }
  1575. return lb_emit_deep_field_ev(p, value, sel);
  1576. }
  1577. } else {
  1578. GB_PANIC("invalid subtype cast %s.%.*s", type_to_string(src_type), LIT(field_name));
  1579. }
  1580. }
  1581. }
  1582. // Pointer <-> Pointer
  1583. if (is_type_pointer(src) && is_type_pointer(dst)) {
  1584. lbValue res = {};
  1585. res.type = t;
  1586. res.value = LLVMBuildPointerCast(p->builder, value.value, lb_type(m, t), "");
  1587. return res;
  1588. }
  1589. if (is_type_multi_pointer(src) && is_type_pointer(dst)) {
  1590. lbValue res = {};
  1591. res.type = t;
  1592. res.value = LLVMBuildPointerCast(p->builder, value.value, lb_type(m, t), "");
  1593. return res;
  1594. }
  1595. if (is_type_pointer(src) && is_type_multi_pointer(dst)) {
  1596. lbValue res = {};
  1597. res.type = t;
  1598. res.value = LLVMBuildPointerCast(p->builder, value.value, lb_type(m, t), "");
  1599. return res;
  1600. }
  1601. if (is_type_multi_pointer(src) && is_type_multi_pointer(dst)) {
  1602. lbValue res = {};
  1603. res.type = t;
  1604. res.value = LLVMBuildPointerCast(p->builder, value.value, lb_type(m, t), "");
  1605. return res;
  1606. }
  1607. // proc <-> proc
  1608. if (is_type_proc(src) && is_type_proc(dst)) {
  1609. lbValue res = {};
  1610. res.type = t;
  1611. res.value = LLVMBuildPointerCast(p->builder, value.value, lb_type(m, t), "");
  1612. return res;
  1613. }
  1614. // pointer -> proc
  1615. if (is_type_pointer(src) && is_type_proc(dst)) {
  1616. lbValue res = {};
  1617. res.type = t;
  1618. res.value = LLVMBuildPointerCast(p->builder, value.value, lb_type(m, t), "");
  1619. return res;
  1620. }
  1621. // proc -> pointer
  1622. if (is_type_proc(src) && is_type_pointer(dst)) {
  1623. lbValue res = {};
  1624. res.type = t;
  1625. res.value = LLVMBuildPointerCast(p->builder, value.value, lb_type(m, t), "");
  1626. return res;
  1627. }
  1628. // []byte/[]u8 <-> string
  1629. if (is_type_u8_slice(src) && is_type_string(dst)) {
  1630. return lb_emit_transmute(p, value, t);
  1631. }
  1632. if (is_type_string(src) && is_type_u8_slice(dst)) {
  1633. return lb_emit_transmute(p, value, t);
  1634. }
  1635. if (is_type_array_like(dst)) {
  1636. Type *elem = base_array_type(dst);
  1637. lbValue e = lb_emit_conv(p, value, elem);
  1638. // NOTE(bill): Doesn't need to be zero because it will be initialized in the loops
  1639. lbAddr v = lb_add_local_generated(p, t, false);
  1640. isize index_count = cast(isize)get_array_type_count(dst);
  1641. for (isize i = 0; i < index_count; i++) {
  1642. lbValue elem = lb_emit_array_epi(p, v.addr, i);
  1643. lb_emit_store(p, elem, e);
  1644. }
  1645. return lb_addr_load(p, v);
  1646. }
  1647. if (is_type_matrix(dst) && !is_type_matrix(src)) {
  1648. GB_ASSERT_MSG(dst->Matrix.row_count == dst->Matrix.column_count, "%s <- %s", type_to_string(dst), type_to_string(src));
  1649. Type *elem = base_array_type(dst);
  1650. lbValue e = lb_emit_conv(p, value, elem);
  1651. lbAddr v = lb_add_local_generated(p, t, false);
  1652. for (i64 i = 0; i < dst->Matrix.row_count; i++) {
  1653. isize j = cast(isize)i;
  1654. lbValue ptr = lb_emit_matrix_epi(p, v.addr, j, j);
  1655. lb_emit_store(p, ptr, e);
  1656. }
  1657. return lb_addr_load(p, v);
  1658. }
  1659. if (is_type_matrix(dst) && is_type_matrix(src)) {
  1660. GB_ASSERT(dst->kind == Type_Matrix);
  1661. GB_ASSERT(src->kind == Type_Matrix);
  1662. lbAddr v = lb_add_local_generated(p, t, true);
  1663. if (is_matrix_square(dst) && is_matrix_square(dst)) {
  1664. for (i64 j = 0; j < dst->Matrix.column_count; j++) {
  1665. for (i64 i = 0; i < dst->Matrix.row_count; i++) {
  1666. if (i < src->Matrix.row_count && j < src->Matrix.column_count) {
  1667. lbValue d = lb_emit_matrix_epi(p, v.addr, i, j);
  1668. lbValue s = lb_emit_matrix_ev(p, value, i, j);
  1669. lb_emit_store(p, d, s);
  1670. } else if (i == j) {
  1671. lbValue d = lb_emit_matrix_epi(p, v.addr, i, j);
  1672. lbValue s = lb_const_value(p->module, dst->Matrix.elem, exact_value_i64(1), true);
  1673. lb_emit_store(p, d, s);
  1674. }
  1675. }
  1676. }
  1677. } else {
  1678. i64 dst_count = dst->Matrix.row_count*dst->Matrix.column_count;
  1679. i64 src_count = src->Matrix.row_count*src->Matrix.column_count;
  1680. GB_ASSERT(dst_count == src_count);
  1681. for (i64 j = 0; j < src->Matrix.column_count; j++) {
  1682. for (i64 i = 0; i < src->Matrix.row_count; i++) {
  1683. lbValue s = lb_emit_matrix_ev(p, value, i, j);
  1684. i64 index = i + j*src->Matrix.row_count;
  1685. i64 dst_i = index%dst->Matrix.row_count;
  1686. i64 dst_j = index/dst->Matrix.row_count;
  1687. lbValue d = lb_emit_matrix_epi(p, v.addr, dst_i, dst_j);
  1688. lb_emit_store(p, d, s);
  1689. }
  1690. }
  1691. }
  1692. return lb_addr_load(p, v);
  1693. }
  1694. if (is_type_any(dst)) {
  1695. if (is_type_untyped_nil(src)) {
  1696. return lb_const_nil(p->module, t);
  1697. }
  1698. if (is_type_untyped_undef(src)) {
  1699. return lb_const_undef(p->module, t);
  1700. }
  1701. lbAddr result = lb_add_local_generated(p, t, true);
  1702. Type *st = default_type(src_type);
  1703. lbValue data = lb_address_from_load_or_generate_local(p, value);
  1704. GB_ASSERT_MSG(is_type_pointer(data.type), "%s", type_to_string(data.type));
  1705. GB_ASSERT_MSG(is_type_typed(st), "%s", type_to_string(st));
  1706. data = lb_emit_conv(p, data, t_rawptr);
  1707. lbValue id = lb_typeid(p->module, st);
  1708. lbValue any_data = lb_emit_struct_ep(p, result.addr, 0);
  1709. lbValue any_id = lb_emit_struct_ep(p, result.addr, 1);
  1710. lb_emit_store(p, any_data, data);
  1711. lb_emit_store(p, any_id, id);
  1712. return lb_addr_load(p, result);
  1713. }
  1714. i64 src_sz = type_size_of(src);
  1715. i64 dst_sz = type_size_of(dst);
  1716. if (src_sz == dst_sz) {
  1717. // bit_set <-> integer
  1718. if (is_type_integer(src) && is_type_bit_set(dst)) {
  1719. lbValue res = lb_emit_conv(p, value, bit_set_to_int(dst));
  1720. res.type = dst;
  1721. return res;
  1722. }
  1723. if (is_type_bit_set(src) && is_type_integer(dst)) {
  1724. lbValue bs = value;
  1725. bs.type = bit_set_to_int(src);
  1726. return lb_emit_conv(p, bs, dst);
  1727. }
  1728. // typeid <-> integer
  1729. if (is_type_integer(src) && is_type_typeid(dst)) {
  1730. return lb_emit_transmute(p, value, dst);
  1731. }
  1732. if (is_type_typeid(src) && is_type_integer(dst)) {
  1733. return lb_emit_transmute(p, value, dst);
  1734. }
  1735. }
  1736. if (is_type_untyped(src)) {
  1737. if (is_type_string(src) && is_type_string(dst)) {
  1738. lbAddr result = lb_add_local_generated(p, t, false);
  1739. lb_addr_store(p, result, value);
  1740. return lb_addr_load(p, result);
  1741. }
  1742. }
  1743. gb_printf_err("%.*s\n", LIT(p->name));
  1744. gb_printf_err("lb_emit_conv: src -> dst\n");
  1745. gb_printf_err("Not Identical %s != %s\n", type_to_string(src_type), type_to_string(t));
  1746. gb_printf_err("Not Identical %s != %s\n", type_to_string(src), type_to_string(dst));
  1747. gb_printf_err("Not Identical %p != %p\n", src_type, t);
  1748. gb_printf_err("Not Identical %p != %p\n", src, dst);
  1749. GB_PANIC("Invalid type conversion: '%s' to '%s' for procedure '%.*s'",
  1750. type_to_string(src_type), type_to_string(t),
  1751. LIT(p->name));
  1752. return {};
  1753. }
  1754. lbValue lb_compare_records(lbProcedure *p, TokenKind op_kind, lbValue left, lbValue right, Type *type) {
  1755. GB_ASSERT((is_type_struct(type) || is_type_union(type)) && is_type_comparable(type));
  1756. lbValue left_ptr = lb_address_from_load_or_generate_local(p, left);
  1757. lbValue right_ptr = lb_address_from_load_or_generate_local(p, right);
  1758. lbValue res = {};
  1759. if (is_type_simple_compare(type)) {
  1760. // TODO(bill): Test to see if this is actually faster!!!!
  1761. auto args = array_make<lbValue>(permanent_allocator(), 3);
  1762. args[0] = lb_emit_conv(p, left_ptr, t_rawptr);
  1763. args[1] = lb_emit_conv(p, right_ptr, t_rawptr);
  1764. args[2] = lb_const_int(p->module, t_int, type_size_of(type));
  1765. res = lb_emit_runtime_call(p, "memory_equal", args);
  1766. } else {
  1767. lbValue value = lb_get_equal_proc_for_type(p->module, type);
  1768. auto args = array_make<lbValue>(permanent_allocator(), 2);
  1769. args[0] = lb_emit_conv(p, left_ptr, t_rawptr);
  1770. args[1] = lb_emit_conv(p, right_ptr, t_rawptr);
  1771. res = lb_emit_call(p, value, args);
  1772. }
  1773. if (op_kind == Token_NotEq) {
  1774. res = lb_emit_unary_arith(p, Token_Not, res, res.type);
  1775. }
  1776. return res;
  1777. }
  1778. lbValue lb_emit_comp(lbProcedure *p, TokenKind op_kind, lbValue left, lbValue right) {
  1779. Type *a = core_type(left.type);
  1780. Type *b = core_type(right.type);
  1781. GB_ASSERT(gb_is_between(op_kind, Token__ComparisonBegin+1, Token__ComparisonEnd-1));
  1782. lbValue nil_check = {};
  1783. if (is_type_untyped_nil(left.type)) {
  1784. nil_check = lb_emit_comp_against_nil(p, op_kind, right);
  1785. } else if (is_type_untyped_nil(right.type)) {
  1786. nil_check = lb_emit_comp_against_nil(p, op_kind, left);
  1787. }
  1788. if (nil_check.value != nullptr) {
  1789. return nil_check;
  1790. }
  1791. if (are_types_identical(a, b)) {
  1792. // NOTE(bill): No need for a conversion
  1793. } else if (lb_is_const(left) || lb_is_const_nil(left)) {
  1794. left = lb_emit_conv(p, left, right.type);
  1795. } else if (lb_is_const(right) || lb_is_const_nil(right)) {
  1796. right = lb_emit_conv(p, right, left.type);
  1797. } else {
  1798. Type *lt = left.type;
  1799. Type *rt = right.type;
  1800. lt = left.type;
  1801. rt = right.type;
  1802. i64 ls = type_size_of(lt);
  1803. i64 rs = type_size_of(rt);
  1804. // NOTE(bill): Quick heuristic, larger types are usually the target type
  1805. if (ls < rs) {
  1806. left = lb_emit_conv(p, left, rt);
  1807. } else if (ls > rs) {
  1808. right = lb_emit_conv(p, right, lt);
  1809. } else {
  1810. if (is_type_union(rt)) {
  1811. left = lb_emit_conv(p, left, rt);
  1812. } else {
  1813. right = lb_emit_conv(p, right, lt);
  1814. }
  1815. }
  1816. }
  1817. if (is_type_array(a) || is_type_enumerated_array(a)) {
  1818. Type *tl = base_type(a);
  1819. lbValue lhs = lb_address_from_load_or_generate_local(p, left);
  1820. lbValue rhs = lb_address_from_load_or_generate_local(p, right);
  1821. TokenKind cmp_op = Token_And;
  1822. lbValue res = lb_const_bool(p->module, t_llvm_bool, true);
  1823. if (op_kind == Token_NotEq) {
  1824. res = lb_const_bool(p->module, t_llvm_bool, false);
  1825. cmp_op = Token_Or;
  1826. } else if (op_kind == Token_CmpEq) {
  1827. res = lb_const_bool(p->module, t_llvm_bool, true);
  1828. cmp_op = Token_And;
  1829. }
  1830. bool inline_array_arith = type_size_of(tl) <= build_context.max_align;
  1831. i32 count = 0;
  1832. switch (tl->kind) {
  1833. case Type_Array: count = cast(i32)tl->Array.count; break;
  1834. case Type_EnumeratedArray: count = cast(i32)tl->EnumeratedArray.count; break;
  1835. }
  1836. if (inline_array_arith) {
  1837. // inline
  1838. lbAddr val = lb_add_local_generated(p, t_bool, false);
  1839. lb_addr_store(p, val, res);
  1840. for (i32 i = 0; i < count; i++) {
  1841. lbValue x = lb_emit_load(p, lb_emit_array_epi(p, lhs, i));
  1842. lbValue y = lb_emit_load(p, lb_emit_array_epi(p, rhs, i));
  1843. lbValue cmp = lb_emit_comp(p, op_kind, x, y);
  1844. lbValue new_res = lb_emit_arith(p, cmp_op, lb_addr_load(p, val), cmp, t_bool);
  1845. lb_addr_store(p, val, lb_emit_conv(p, new_res, t_bool));
  1846. }
  1847. return lb_addr_load(p, val);
  1848. } else {
  1849. if (is_type_simple_compare(tl) && (op_kind == Token_CmpEq || op_kind == Token_NotEq)) {
  1850. // TODO(bill): Test to see if this is actually faster!!!!
  1851. auto args = array_make<lbValue>(permanent_allocator(), 3);
  1852. args[0] = lb_emit_conv(p, lhs, t_rawptr);
  1853. args[1] = lb_emit_conv(p, rhs, t_rawptr);
  1854. args[2] = lb_const_int(p->module, t_int, type_size_of(tl));
  1855. lbValue val = lb_emit_runtime_call(p, "memory_compare", args);
  1856. lbValue res = lb_emit_comp(p, op_kind, val, lb_const_nil(p->module, val.type));
  1857. return lb_emit_conv(p, res, t_bool);
  1858. } else {
  1859. lbAddr val = lb_add_local_generated(p, t_bool, false);
  1860. lb_addr_store(p, val, res);
  1861. auto loop_data = lb_loop_start(p, count, t_i32);
  1862. {
  1863. lbValue i = loop_data.idx;
  1864. lbValue x = lb_emit_load(p, lb_emit_array_ep(p, lhs, i));
  1865. lbValue y = lb_emit_load(p, lb_emit_array_ep(p, rhs, i));
  1866. lbValue cmp = lb_emit_comp(p, op_kind, x, y);
  1867. lbValue new_res = lb_emit_arith(p, cmp_op, lb_addr_load(p, val), cmp, t_bool);
  1868. lb_addr_store(p, val, lb_emit_conv(p, new_res, t_bool));
  1869. }
  1870. lb_loop_end(p, loop_data);
  1871. return lb_addr_load(p, val);
  1872. }
  1873. }
  1874. }
  1875. if ((is_type_struct(a) || is_type_union(a)) && is_type_comparable(a)) {
  1876. return lb_compare_records(p, op_kind, left, right, a);
  1877. }
  1878. if ((is_type_struct(b) || is_type_union(b)) && is_type_comparable(b)) {
  1879. return lb_compare_records(p, op_kind, left, right, b);
  1880. }
  1881. if (is_type_string(a)) {
  1882. if (is_type_cstring(a)) {
  1883. left = lb_emit_conv(p, left, t_string);
  1884. right = lb_emit_conv(p, right, t_string);
  1885. }
  1886. char const *runtime_procedure = nullptr;
  1887. switch (op_kind) {
  1888. case Token_CmpEq: runtime_procedure = "string_eq"; break;
  1889. case Token_NotEq: runtime_procedure = "string_ne"; break;
  1890. case Token_Lt: runtime_procedure = "string_lt"; break;
  1891. case Token_Gt: runtime_procedure = "string_gt"; break;
  1892. case Token_LtEq: runtime_procedure = "string_le"; break;
  1893. case Token_GtEq: runtime_procedure = "string_gt"; break;
  1894. }
  1895. GB_ASSERT(runtime_procedure != nullptr);
  1896. auto args = array_make<lbValue>(permanent_allocator(), 2);
  1897. args[0] = left;
  1898. args[1] = right;
  1899. return lb_emit_runtime_call(p, runtime_procedure, args);
  1900. }
  1901. if (is_type_complex(a)) {
  1902. char const *runtime_procedure = "";
  1903. i64 sz = 8*type_size_of(a);
  1904. switch (sz) {
  1905. case 32:
  1906. switch (op_kind) {
  1907. case Token_CmpEq: runtime_procedure = "complex32_eq"; break;
  1908. case Token_NotEq: runtime_procedure = "complex32_ne"; break;
  1909. }
  1910. break;
  1911. case 64:
  1912. switch (op_kind) {
  1913. case Token_CmpEq: runtime_procedure = "complex64_eq"; break;
  1914. case Token_NotEq: runtime_procedure = "complex64_ne"; break;
  1915. }
  1916. break;
  1917. case 128:
  1918. switch (op_kind) {
  1919. case Token_CmpEq: runtime_procedure = "complex128_eq"; break;
  1920. case Token_NotEq: runtime_procedure = "complex128_ne"; break;
  1921. }
  1922. break;
  1923. }
  1924. GB_ASSERT(runtime_procedure != nullptr);
  1925. auto args = array_make<lbValue>(permanent_allocator(), 2);
  1926. args[0] = left;
  1927. args[1] = right;
  1928. return lb_emit_runtime_call(p, runtime_procedure, args);
  1929. }
  1930. if (is_type_quaternion(a)) {
  1931. char const *runtime_procedure = "";
  1932. i64 sz = 8*type_size_of(a);
  1933. switch (sz) {
  1934. case 64:
  1935. switch (op_kind) {
  1936. case Token_CmpEq: runtime_procedure = "quaternion64_eq"; break;
  1937. case Token_NotEq: runtime_procedure = "quaternion64_ne"; break;
  1938. }
  1939. break;
  1940. case 128:
  1941. switch (op_kind) {
  1942. case Token_CmpEq: runtime_procedure = "quaternion128_eq"; break;
  1943. case Token_NotEq: runtime_procedure = "quaternion128_ne"; break;
  1944. }
  1945. break;
  1946. case 256:
  1947. switch (op_kind) {
  1948. case Token_CmpEq: runtime_procedure = "quaternion256_eq"; break;
  1949. case Token_NotEq: runtime_procedure = "quaternion256_ne"; break;
  1950. }
  1951. break;
  1952. }
  1953. GB_ASSERT(runtime_procedure != nullptr);
  1954. auto args = array_make<lbValue>(permanent_allocator(), 2);
  1955. args[0] = left;
  1956. args[1] = right;
  1957. return lb_emit_runtime_call(p, runtime_procedure, args);
  1958. }
  1959. if (is_type_bit_set(a)) {
  1960. switch (op_kind) {
  1961. case Token_Lt:
  1962. case Token_LtEq:
  1963. case Token_Gt:
  1964. case Token_GtEq:
  1965. {
  1966. Type *it = bit_set_to_int(a);
  1967. lbValue lhs = lb_emit_transmute(p, left, it);
  1968. lbValue rhs = lb_emit_transmute(p, right, it);
  1969. lbValue res = lb_emit_arith(p, Token_And, lhs, rhs, it);
  1970. if (op_kind == Token_Lt || op_kind == Token_LtEq) {
  1971. // (lhs & rhs) == lhs
  1972. res.value = LLVMBuildICmp(p->builder, LLVMIntEQ, res.value, lhs.value, "");
  1973. res.type = t_llvm_bool;
  1974. } else if (op_kind == Token_Gt || op_kind == Token_GtEq) {
  1975. // (lhs & rhs) == rhs
  1976. res.value = LLVMBuildICmp(p->builder, LLVMIntEQ, res.value, rhs.value, "");
  1977. res.type = t_llvm_bool;
  1978. }
  1979. // NOTE(bill): Strict subsets
  1980. if (op_kind == Token_Lt || op_kind == Token_Gt) {
  1981. // res &~ (lhs == rhs)
  1982. lbValue eq = {};
  1983. eq.value = LLVMBuildICmp(p->builder, LLVMIntEQ, lhs.value, rhs.value, "");
  1984. eq.type = t_llvm_bool;
  1985. res = lb_emit_arith(p, Token_AndNot, res, eq, t_llvm_bool);
  1986. }
  1987. return res;
  1988. }
  1989. case Token_CmpEq:
  1990. case Token_NotEq:
  1991. {
  1992. LLVMIntPredicate pred = {};
  1993. switch (op_kind) {
  1994. case Token_CmpEq: pred = LLVMIntEQ; break;
  1995. case Token_NotEq: pred = LLVMIntNE; break;
  1996. }
  1997. lbValue res = {};
  1998. res.type = t_llvm_bool;
  1999. res.value = LLVMBuildICmp(p->builder, pred, left.value, right.value, "");
  2000. return res;
  2001. }
  2002. }
  2003. }
  2004. if (op_kind != Token_CmpEq && op_kind != Token_NotEq) {
  2005. Type *t = left.type;
  2006. if (is_type_integer(t) && is_type_different_to_arch_endianness(t)) {
  2007. Type *platform_type = integer_endian_type_to_platform_type(t);
  2008. lbValue x = lb_emit_byte_swap(p, left, platform_type);
  2009. lbValue y = lb_emit_byte_swap(p, right, platform_type);
  2010. left = x;
  2011. right = y;
  2012. } else if (is_type_float(t) && is_type_different_to_arch_endianness(t)) {
  2013. Type *platform_type = integer_endian_type_to_platform_type(t);
  2014. lbValue x = lb_emit_conv(p, left, platform_type);
  2015. lbValue y = lb_emit_conv(p, right, platform_type);
  2016. left = x;
  2017. right = y;
  2018. }
  2019. }
  2020. a = core_type(left.type);
  2021. b = core_type(right.type);
  2022. lbValue res = {};
  2023. res.type = t_llvm_bool;
  2024. if (is_type_integer(a) ||
  2025. is_type_boolean(a) ||
  2026. is_type_pointer(a) ||
  2027. is_type_multi_pointer(a) ||
  2028. is_type_proc(a) ||
  2029. is_type_enum(a)) {
  2030. LLVMIntPredicate pred = {};
  2031. if (is_type_unsigned(left.type)) {
  2032. switch (op_kind) {
  2033. case Token_Gt: pred = LLVMIntUGT; break;
  2034. case Token_GtEq: pred = LLVMIntUGE; break;
  2035. case Token_Lt: pred = LLVMIntULT; break;
  2036. case Token_LtEq: pred = LLVMIntULE; break;
  2037. }
  2038. } else {
  2039. switch (op_kind) {
  2040. case Token_Gt: pred = LLVMIntSGT; break;
  2041. case Token_GtEq: pred = LLVMIntSGE; break;
  2042. case Token_Lt: pred = LLVMIntSLT; break;
  2043. case Token_LtEq: pred = LLVMIntSLE; break;
  2044. }
  2045. }
  2046. switch (op_kind) {
  2047. case Token_CmpEq: pred = LLVMIntEQ; break;
  2048. case Token_NotEq: pred = LLVMIntNE; break;
  2049. }
  2050. LLVMValueRef lhs = left.value;
  2051. LLVMValueRef rhs = right.value;
  2052. if (LLVMTypeOf(lhs) != LLVMTypeOf(rhs)) {
  2053. if (lb_is_type_kind(LLVMTypeOf(lhs), LLVMPointerTypeKind)) {
  2054. rhs = LLVMBuildPointerCast(p->builder, rhs, LLVMTypeOf(lhs), "");
  2055. }
  2056. }
  2057. res.value = LLVMBuildICmp(p->builder, pred, lhs, rhs, "");
  2058. } else if (is_type_float(a)) {
  2059. LLVMRealPredicate pred = {};
  2060. switch (op_kind) {
  2061. case Token_CmpEq: pred = LLVMRealOEQ; break;
  2062. case Token_Gt: pred = LLVMRealOGT; break;
  2063. case Token_GtEq: pred = LLVMRealOGE; break;
  2064. case Token_Lt: pred = LLVMRealOLT; break;
  2065. case Token_LtEq: pred = LLVMRealOLE; break;
  2066. case Token_NotEq: pred = LLVMRealONE; break;
  2067. }
  2068. res.value = LLVMBuildFCmp(p->builder, pred, left.value, right.value, "");
  2069. } else if (is_type_typeid(a)) {
  2070. LLVMIntPredicate pred = {};
  2071. switch (op_kind) {
  2072. case Token_Gt: pred = LLVMIntUGT; break;
  2073. case Token_GtEq: pred = LLVMIntUGE; break;
  2074. case Token_Lt: pred = LLVMIntULT; break;
  2075. case Token_LtEq: pred = LLVMIntULE; break;
  2076. case Token_CmpEq: pred = LLVMIntEQ; break;
  2077. case Token_NotEq: pred = LLVMIntNE; break;
  2078. }
  2079. res.value = LLVMBuildICmp(p->builder, pred, left.value, right.value, "");
  2080. } else {
  2081. 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)));
  2082. }
  2083. return res;
  2084. }
  2085. lbValue lb_emit_comp_against_nil(lbProcedure *p, TokenKind op_kind, lbValue x) {
  2086. lbValue res = {};
  2087. res.type = t_llvm_bool;
  2088. Type *t = x.type;
  2089. Type *bt = base_type(t);
  2090. TypeKind type_kind = bt->kind;
  2091. switch (type_kind) {
  2092. case Type_Basic:
  2093. switch (bt->Basic.kind) {
  2094. case Basic_rawptr:
  2095. case Basic_cstring:
  2096. if (op_kind == Token_CmpEq) {
  2097. res.value = LLVMBuildIsNull(p->builder, x.value, "");
  2098. } else if (op_kind == Token_NotEq) {
  2099. res.value = LLVMBuildIsNotNull(p->builder, x.value, "");
  2100. }
  2101. return res;
  2102. case Basic_any:
  2103. {
  2104. // TODO(bill): is this correct behaviour for nil comparison for any?
  2105. lbValue data = lb_emit_struct_ev(p, x, 0);
  2106. lbValue ti = lb_emit_struct_ev(p, x, 1);
  2107. if (op_kind == Token_CmpEq) {
  2108. LLVMValueRef a = LLVMBuildIsNull(p->builder, data.value, "");
  2109. LLVMValueRef b = LLVMBuildIsNull(p->builder, ti.value, "");
  2110. res.value = LLVMBuildOr(p->builder, a, b, "");
  2111. return res;
  2112. } else if (op_kind == Token_NotEq) {
  2113. LLVMValueRef a = LLVMBuildIsNotNull(p->builder, data.value, "");
  2114. LLVMValueRef b = LLVMBuildIsNotNull(p->builder, ti.value, "");
  2115. res.value = LLVMBuildAnd(p->builder, a, b, "");
  2116. return res;
  2117. }
  2118. }
  2119. break;
  2120. case Basic_typeid:
  2121. lbValue invalid_typeid = lb_const_value(p->module, t_typeid, exact_value_i64(0));
  2122. return lb_emit_comp(p, op_kind, x, invalid_typeid);
  2123. }
  2124. break;
  2125. case Type_Enum:
  2126. case Type_Pointer:
  2127. case Type_MultiPointer:
  2128. case Type_Proc:
  2129. case Type_BitSet:
  2130. if (op_kind == Token_CmpEq) {
  2131. res.value = LLVMBuildIsNull(p->builder, x.value, "");
  2132. } else if (op_kind == Token_NotEq) {
  2133. res.value = LLVMBuildIsNotNull(p->builder, x.value, "");
  2134. }
  2135. return res;
  2136. case Type_Slice:
  2137. {
  2138. lbValue data = lb_emit_struct_ev(p, x, 0);
  2139. if (op_kind == Token_CmpEq) {
  2140. res.value = LLVMBuildIsNull(p->builder, data.value, "");
  2141. return res;
  2142. } else if (op_kind == Token_NotEq) {
  2143. res.value = LLVMBuildIsNotNull(p->builder, data.value, "");
  2144. return res;
  2145. }
  2146. }
  2147. break;
  2148. case Type_DynamicArray:
  2149. {
  2150. lbValue data = lb_emit_struct_ev(p, x, 0);
  2151. if (op_kind == Token_CmpEq) {
  2152. res.value = LLVMBuildIsNull(p->builder, data.value, "");
  2153. return res;
  2154. } else if (op_kind == Token_NotEq) {
  2155. res.value = LLVMBuildIsNotNull(p->builder, data.value, "");
  2156. return res;
  2157. }
  2158. }
  2159. break;
  2160. case Type_Map:
  2161. {
  2162. lbValue map_ptr = lb_address_from_load_or_generate_local(p, x);
  2163. unsigned indices[2] = {0, 0};
  2164. lbValue hashes_data = lb_emit_struct_ep(p, map_ptr, 0);
  2165. lbValue hashes_data_ptr_ptr = lb_emit_struct_ep(p, hashes_data, 0);
  2166. LLVMValueRef hashes_data_ptr = LLVMBuildLoad(p->builder, hashes_data_ptr_ptr.value, "");
  2167. if (op_kind == Token_CmpEq) {
  2168. res.value = LLVMBuildIsNull(p->builder, hashes_data_ptr, "");
  2169. return res;
  2170. } else {
  2171. res.value = LLVMBuildIsNotNull(p->builder, hashes_data_ptr, "");
  2172. return res;
  2173. }
  2174. }
  2175. break;
  2176. case Type_Union:
  2177. {
  2178. if (type_size_of(t) == 0) {
  2179. if (op_kind == Token_CmpEq) {
  2180. return lb_const_bool(p->module, t_llvm_bool, true);
  2181. } else if (op_kind == Token_NotEq) {
  2182. return lb_const_bool(p->module, t_llvm_bool, false);
  2183. }
  2184. } else if (is_type_union_maybe_pointer(t)) {
  2185. lbValue tag = lb_emit_transmute(p, x, t_rawptr);
  2186. return lb_emit_comp_against_nil(p, op_kind, tag);
  2187. } else {
  2188. lbValue tag = lb_emit_union_tag_value(p, x);
  2189. return lb_emit_comp(p, op_kind, tag, lb_zero(p->module, tag.type));
  2190. }
  2191. }
  2192. case Type_Struct:
  2193. if (is_type_soa_struct(t)) {
  2194. Type *bt = base_type(t);
  2195. if (bt->Struct.soa_kind == StructSoa_Slice) {
  2196. LLVMValueRef the_value = {};
  2197. if (bt->Struct.fields.count == 0) {
  2198. lbValue len = lb_soa_struct_len(p, x);
  2199. the_value = len.value;
  2200. } else {
  2201. lbValue first_field = lb_emit_struct_ev(p, x, 0);
  2202. the_value = first_field.value;
  2203. }
  2204. if (op_kind == Token_CmpEq) {
  2205. res.value = LLVMBuildIsNull(p->builder, the_value, "");
  2206. return res;
  2207. } else if (op_kind == Token_NotEq) {
  2208. res.value = LLVMBuildIsNotNull(p->builder, the_value, "");
  2209. return res;
  2210. }
  2211. } else if (bt->Struct.soa_kind == StructSoa_Dynamic) {
  2212. LLVMValueRef the_value = {};
  2213. if (bt->Struct.fields.count == 0) {
  2214. lbValue cap = lb_soa_struct_cap(p, x);
  2215. the_value = cap.value;
  2216. } else {
  2217. lbValue first_field = lb_emit_struct_ev(p, x, 0);
  2218. the_value = first_field.value;
  2219. }
  2220. if (op_kind == Token_CmpEq) {
  2221. res.value = LLVMBuildIsNull(p->builder, the_value, "");
  2222. return res;
  2223. } else if (op_kind == Token_NotEq) {
  2224. res.value = LLVMBuildIsNotNull(p->builder, the_value, "");
  2225. return res;
  2226. }
  2227. }
  2228. } else if (is_type_struct(t) && type_has_nil(t)) {
  2229. auto args = array_make<lbValue>(permanent_allocator(), 2);
  2230. lbValue lhs = lb_address_from_load_or_generate_local(p, x);
  2231. args[0] = lb_emit_conv(p, lhs, t_rawptr);
  2232. args[1] = lb_const_int(p->module, t_int, type_size_of(t));
  2233. lbValue val = lb_emit_runtime_call(p, "memory_compare_zero", args);
  2234. lbValue res = lb_emit_comp(p, op_kind, val, lb_const_int(p->module, t_int, 0));
  2235. return res;
  2236. }
  2237. break;
  2238. }
  2239. GB_PANIC("Unknown handled type: %s -> %s", type_to_string(t), type_to_string(bt));
  2240. return {};
  2241. }
  2242. lbValue lb_build_unary_and(lbProcedure *p, Ast *expr) {
  2243. ast_node(ue, UnaryExpr, expr);
  2244. auto tv = type_and_value_of_expr(expr);
  2245. Ast *ue_expr = unparen_expr(ue->expr);
  2246. if (ue_expr->kind == Ast_IndexExpr && tv.mode == Addressing_OptionalOkPtr && is_type_tuple(tv.type)) {
  2247. Type *tuple = tv.type;
  2248. Type *map_type = type_of_expr(ue_expr->IndexExpr.expr);
  2249. Type *ot = base_type(map_type);
  2250. Type *t = base_type(type_deref(ot));
  2251. bool deref = t != ot;
  2252. GB_ASSERT(t->kind == Type_Map);
  2253. ast_node(ie, IndexExpr, ue_expr);
  2254. lbValue map_val = lb_build_addr_ptr(p, ie->expr);
  2255. if (deref) {
  2256. map_val = lb_emit_load(p, map_val);
  2257. }
  2258. lbValue key = lb_build_expr(p, ie->index);
  2259. key = lb_emit_conv(p, key, t->Map.key);
  2260. lbAddr addr = lb_addr_map(map_val, key, t, alloc_type_pointer(t->Map.value));
  2261. lbValue ptr = lb_addr_get_ptr(p, addr);
  2262. lbValue ok = lb_emit_comp_against_nil(p, Token_NotEq, ptr);
  2263. ok = lb_emit_conv(p, ok, tuple->Tuple.variables[1]->type);
  2264. lbAddr res = lb_add_local_generated(p, tuple, false);
  2265. lbValue gep0 = lb_emit_struct_ep(p, res.addr, 0);
  2266. lbValue gep1 = lb_emit_struct_ep(p, res.addr, 1);
  2267. lb_emit_store(p, gep0, ptr);
  2268. lb_emit_store(p, gep1, ok);
  2269. return lb_addr_load(p, res);
  2270. } if (ue_expr->kind == Ast_CompoundLit) {
  2271. lbValue v = lb_build_expr(p, ue->expr);
  2272. Type *type = v.type;
  2273. lbAddr addr = {};
  2274. if (p->is_startup) {
  2275. addr = lb_add_global_generated(p->module, type, v);
  2276. } else {
  2277. addr = lb_add_local_generated(p, type, false);
  2278. }
  2279. lb_addr_store(p, addr, v);
  2280. return addr.addr;
  2281. } else if (ue_expr->kind == Ast_TypeAssertion) {
  2282. if (is_type_tuple(tv.type)) {
  2283. Type *tuple = tv.type;
  2284. Type *ptr_type = tuple->Tuple.variables[0]->type;
  2285. Type *ok_type = tuple->Tuple.variables[1]->type;
  2286. ast_node(ta, TypeAssertion, ue_expr);
  2287. TokenPos pos = ast_token(expr).pos;
  2288. Type *type = type_of_expr(ue_expr);
  2289. GB_ASSERT(!is_type_tuple(type));
  2290. lbValue e = lb_build_expr(p, ta->expr);
  2291. Type *t = type_deref(e.type);
  2292. if (is_type_union(t)) {
  2293. lbValue v = e;
  2294. if (!is_type_pointer(v.type)) {
  2295. v = lb_address_from_load_or_generate_local(p, v);
  2296. }
  2297. Type *src_type = type_deref(v.type);
  2298. Type *dst_type = type;
  2299. lbValue src_tag = {};
  2300. lbValue dst_tag = {};
  2301. if (is_type_union_maybe_pointer(src_type)) {
  2302. src_tag = lb_emit_comp_against_nil(p, Token_NotEq, v);
  2303. dst_tag = lb_const_bool(p->module, t_bool, true);
  2304. } else {
  2305. src_tag = lb_emit_load(p, lb_emit_union_tag_ptr(p, v));
  2306. dst_tag = lb_const_union_tag(p->module, src_type, dst_type);
  2307. }
  2308. lbValue ok = lb_emit_comp(p, Token_CmpEq, src_tag, dst_tag);
  2309. lbValue data_ptr = lb_emit_conv(p, v, ptr_type);
  2310. lbAddr res = lb_add_local_generated(p, tuple, true);
  2311. lbValue gep0 = lb_emit_struct_ep(p, res.addr, 0);
  2312. lbValue gep1 = lb_emit_struct_ep(p, res.addr, 1);
  2313. lb_emit_store(p, gep0, lb_emit_select(p, ok, data_ptr, lb_const_nil(p->module, ptr_type)));
  2314. lb_emit_store(p, gep1, lb_emit_conv(p, ok, ok_type));
  2315. return lb_addr_load(p, res);
  2316. } else if (is_type_any(t)) {
  2317. lbValue v = e;
  2318. if (is_type_pointer(v.type)) {
  2319. v = lb_emit_load(p, v);
  2320. }
  2321. lbValue data_ptr = lb_emit_conv(p, lb_emit_struct_ev(p, v, 0), ptr_type);
  2322. lbValue any_id = lb_emit_struct_ev(p, v, 1);
  2323. lbValue id = lb_typeid(p->module, type);
  2324. lbValue ok = lb_emit_comp(p, Token_CmpEq, any_id, id);
  2325. lbAddr res = lb_add_local_generated(p, tuple, false);
  2326. lbValue gep0 = lb_emit_struct_ep(p, res.addr, 0);
  2327. lbValue gep1 = lb_emit_struct_ep(p, res.addr, 1);
  2328. lb_emit_store(p, gep0, lb_emit_select(p, ok, data_ptr, lb_const_nil(p->module, ptr_type)));
  2329. lb_emit_store(p, gep1, lb_emit_conv(p, ok, ok_type));
  2330. return lb_addr_load(p, res);
  2331. } else {
  2332. GB_PANIC("TODO(bill): type assertion %s", type_to_string(type));
  2333. }
  2334. } else {
  2335. GB_ASSERT(is_type_pointer(tv.type));
  2336. ast_node(ta, TypeAssertion, ue_expr);
  2337. TokenPos pos = ast_token(expr).pos;
  2338. Type *type = type_of_expr(ue_expr);
  2339. GB_ASSERT(!is_type_tuple(type));
  2340. lbValue e = lb_build_expr(p, ta->expr);
  2341. Type *t = type_deref(e.type);
  2342. if (is_type_union(t)) {
  2343. lbValue v = e;
  2344. if (!is_type_pointer(v.type)) {
  2345. v = lb_address_from_load_or_generate_local(p, v);
  2346. }
  2347. Type *src_type = type_deref(v.type);
  2348. Type *dst_type = type;
  2349. lbValue src_tag = {};
  2350. lbValue dst_tag = {};
  2351. if (is_type_union_maybe_pointer(src_type)) {
  2352. src_tag = lb_emit_comp_against_nil(p, Token_NotEq, v);
  2353. dst_tag = lb_const_bool(p->module, t_bool, true);
  2354. } else {
  2355. src_tag = lb_emit_load(p, lb_emit_union_tag_ptr(p, v));
  2356. dst_tag = lb_const_union_tag(p->module, src_type, dst_type);
  2357. }
  2358. lbValue ok = lb_emit_comp(p, Token_CmpEq, src_tag, dst_tag);
  2359. auto args = array_make<lbValue>(permanent_allocator(), 6);
  2360. args[0] = ok;
  2361. args[1] = lb_find_or_add_entity_string(p->module, get_file_path_string(pos.file_id));
  2362. args[2] = lb_const_int(p->module, t_i32, pos.line);
  2363. args[3] = lb_const_int(p->module, t_i32, pos.column);
  2364. args[4] = lb_typeid(p->module, src_type);
  2365. args[5] = lb_typeid(p->module, dst_type);
  2366. lb_emit_runtime_call(p, "type_assertion_check", args);
  2367. lbValue data_ptr = v;
  2368. return lb_emit_conv(p, data_ptr, tv.type);
  2369. } else if (is_type_any(t)) {
  2370. lbValue v = e;
  2371. if (is_type_pointer(v.type)) {
  2372. v = lb_emit_load(p, v);
  2373. }
  2374. lbValue data_ptr = lb_emit_struct_ev(p, v, 0);
  2375. lbValue any_id = lb_emit_struct_ev(p, v, 1);
  2376. lbValue id = lb_typeid(p->module, type);
  2377. lbValue ok = lb_emit_comp(p, Token_CmpEq, any_id, id);
  2378. auto args = array_make<lbValue>(permanent_allocator(), 6);
  2379. args[0] = ok;
  2380. args[1] = lb_find_or_add_entity_string(p->module, get_file_path_string(pos.file_id));
  2381. args[2] = lb_const_int(p->module, t_i32, pos.line);
  2382. args[3] = lb_const_int(p->module, t_i32, pos.column);
  2383. args[4] = any_id;
  2384. args[5] = id;
  2385. lb_emit_runtime_call(p, "type_assertion_check", args);
  2386. return lb_emit_conv(p, data_ptr, tv.type);
  2387. } else {
  2388. GB_PANIC("TODO(bill): type assertion %s", type_to_string(type));
  2389. }
  2390. }
  2391. }
  2392. return lb_build_addr_ptr(p, ue->expr);
  2393. }
  2394. lbValue lb_build_expr(lbProcedure *p, Ast *expr) {
  2395. lbModule *m = p->module;
  2396. u16 prev_state_flags = p->state_flags;
  2397. defer (p->state_flags = prev_state_flags);
  2398. if (expr->state_flags != 0) {
  2399. u16 in = expr->state_flags;
  2400. u16 out = p->state_flags;
  2401. if (in & StateFlag_bounds_check) {
  2402. out |= StateFlag_bounds_check;
  2403. out &= ~StateFlag_no_bounds_check;
  2404. } else if (in & StateFlag_no_bounds_check) {
  2405. out |= StateFlag_no_bounds_check;
  2406. out &= ~StateFlag_bounds_check;
  2407. }
  2408. p->state_flags = out;
  2409. }
  2410. expr = unparen_expr(expr);
  2411. TokenPos expr_pos = ast_token(expr).pos;
  2412. TypeAndValue tv = type_and_value_of_expr(expr);
  2413. Type *type = type_of_expr(expr);
  2414. 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));
  2415. if (tv.value.kind != ExactValue_Invalid) {
  2416. // NOTE(bill): The commented out code below is just for debug purposes only
  2417. // if (is_type_untyped(type)) {
  2418. // 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);
  2419. // GB_PANIC("%s\n", type_to_string(tv.type));
  2420. // }
  2421. // NOTE(bill): Short on constant values
  2422. return lb_const_value(p->module, type, tv.value);
  2423. }
  2424. #if 0
  2425. LLVMMetadataRef prev_debug_location = nullptr;
  2426. if (p->debug_info != nullptr) {
  2427. prev_debug_location = LLVMGetCurrentDebugLocation2(p->builder);
  2428. LLVMSetCurrentDebugLocation2(p->builder, lb_debug_location_from_ast(p, expr));
  2429. }
  2430. defer (if (prev_debug_location != nullptr) {
  2431. LLVMSetCurrentDebugLocation2(p->builder, prev_debug_location);
  2432. });
  2433. #endif
  2434. switch (expr->kind) {
  2435. case_ast_node(bl, BasicLit, expr);
  2436. TokenPos pos = bl->token.pos;
  2437. GB_PANIC("Non-constant basic literal %s - %.*s", token_pos_to_string(pos), LIT(token_strings[bl->token.kind]));
  2438. case_end;
  2439. case_ast_node(bd, BasicDirective, expr);
  2440. TokenPos pos = bd->token.pos;
  2441. GB_PANIC("Non-constant basic literal %s - %.*s", token_pos_to_string(pos), LIT(bd->name.string));
  2442. case_end;
  2443. case_ast_node(i, Implicit, expr);
  2444. return lb_addr_load(p, lb_build_addr(p, expr));
  2445. case_end;
  2446. case_ast_node(u, Undef, expr)
  2447. lbValue res = {};
  2448. if (is_type_untyped(type)) {
  2449. res.value = nullptr;
  2450. res.type = t_untyped_undef;
  2451. } else {
  2452. res.value = LLVMGetUndef(lb_type(m, type));
  2453. res.type = type;
  2454. }
  2455. return res;
  2456. case_end;
  2457. case_ast_node(i, Ident, expr);
  2458. Entity *e = entity_from_expr(expr);
  2459. e = strip_entity_wrapping(e);
  2460. GB_ASSERT_MSG(e != nullptr, "%s", expr_to_string(expr));
  2461. if (e->kind == Entity_Builtin) {
  2462. Token token = ast_token(expr);
  2463. GB_PANIC("TODO(bill): lb_build_expr Entity_Builtin '%.*s'\n"
  2464. "\t at %s", LIT(builtin_procs[e->Builtin.id].name),
  2465. token_pos_to_string(token.pos));
  2466. return {};
  2467. } else if (e->kind == Entity_Nil) {
  2468. lbValue res = {};
  2469. res.value = nullptr;
  2470. res.type = e->type;
  2471. return res;
  2472. }
  2473. GB_ASSERT(e->kind != Entity_ProcGroup);
  2474. return lb_find_ident(p, m, e, expr);
  2475. case_end;
  2476. case_ast_node(de, DerefExpr, expr);
  2477. return lb_addr_load(p, lb_build_addr(p, expr));
  2478. case_end;
  2479. case_ast_node(se, SelectorExpr, expr);
  2480. TypeAndValue tav = type_and_value_of_expr(expr);
  2481. GB_ASSERT(tav.mode != Addressing_Invalid);
  2482. return lb_addr_load(p, lb_build_addr(p, expr));
  2483. case_end;
  2484. case_ast_node(ise, ImplicitSelectorExpr, expr);
  2485. TypeAndValue tav = type_and_value_of_expr(expr);
  2486. GB_ASSERT(tav.mode == Addressing_Constant);
  2487. return lb_const_value(p->module, type, tv.value);
  2488. case_end;
  2489. case_ast_node(se, SelectorCallExpr, expr);
  2490. GB_ASSERT(se->modified_call);
  2491. TypeAndValue tav = type_and_value_of_expr(expr);
  2492. GB_ASSERT(tav.mode != Addressing_Invalid);
  2493. lbValue res = lb_build_call_expr(p, se->call);
  2494. ast_node(ce, CallExpr, se->call);
  2495. ce->sce_temp_data = gb_alloc_copy(permanent_allocator(), &res, gb_size_of(res));
  2496. return res;
  2497. case_end;
  2498. case_ast_node(te, TernaryIfExpr, expr);
  2499. LLVMValueRef incoming_values[2] = {};
  2500. LLVMBasicBlockRef incoming_blocks[2] = {};
  2501. GB_ASSERT(te->y != nullptr);
  2502. lbBlock *then = lb_create_block(p, "if.then");
  2503. lbBlock *done = lb_create_block(p, "if.done"); // NOTE(bill): Append later
  2504. lbBlock *else_ = lb_create_block(p, "if.else");
  2505. lbValue cond = lb_build_cond(p, te->cond, then, else_);
  2506. lb_start_block(p, then);
  2507. Type *type = default_type(type_of_expr(expr));
  2508. incoming_values[0] = lb_emit_conv(p, lb_build_expr(p, te->x), type).value;
  2509. lb_emit_jump(p, done);
  2510. lb_start_block(p, else_);
  2511. incoming_values[1] = lb_emit_conv(p, lb_build_expr(p, te->y), type).value;
  2512. lb_emit_jump(p, done);
  2513. lb_start_block(p, done);
  2514. lbValue res = {};
  2515. res.value = LLVMBuildPhi(p->builder, lb_type(p->module, type), "");
  2516. res.type = type;
  2517. GB_ASSERT(p->curr_block->preds.count >= 2);
  2518. incoming_blocks[0] = p->curr_block->preds[0]->block;
  2519. incoming_blocks[1] = p->curr_block->preds[1]->block;
  2520. LLVMAddIncoming(res.value, incoming_values, incoming_blocks, 2);
  2521. return res;
  2522. case_end;
  2523. case_ast_node(te, TernaryWhenExpr, expr);
  2524. TypeAndValue tav = type_and_value_of_expr(te->cond);
  2525. GB_ASSERT(tav.mode == Addressing_Constant);
  2526. GB_ASSERT(tav.value.kind == ExactValue_Bool);
  2527. if (tav.value.value_bool) {
  2528. return lb_build_expr(p, te->x);
  2529. } else {
  2530. return lb_build_expr(p, te->y);
  2531. }
  2532. case_end;
  2533. case_ast_node(oe, OrElseExpr, expr);
  2534. return lb_emit_or_else(p, oe->x, oe->y, tv);
  2535. case_end;
  2536. case_ast_node(oe, OrReturnExpr, expr);
  2537. return lb_emit_or_return(p, oe->expr, tv);
  2538. case_end;
  2539. case_ast_node(ta, TypeAssertion, expr);
  2540. TokenPos pos = ast_token(expr).pos;
  2541. lbValue e = lb_build_expr(p, ta->expr);
  2542. Type *t = type_deref(e.type);
  2543. if (is_type_union(t)) {
  2544. if (ta->ignores[0]) {
  2545. // NOTE(bill): This is not needed for optimization levels other than 0
  2546. return lb_emit_union_cast_only_ok_check(p, e, type, pos);
  2547. }
  2548. return lb_emit_union_cast(p, e, type, pos);
  2549. } else if (is_type_any(t)) {
  2550. return lb_emit_any_cast(p, e, type, pos);
  2551. } else {
  2552. GB_PANIC("TODO(bill): type assertion %s", type_to_string(e.type));
  2553. }
  2554. case_end;
  2555. case_ast_node(tc, TypeCast, expr);
  2556. lbValue e = lb_build_expr(p, tc->expr);
  2557. switch (tc->token.kind) {
  2558. case Token_cast:
  2559. return lb_emit_conv(p, e, type);
  2560. case Token_transmute:
  2561. return lb_emit_transmute(p, e, type);
  2562. }
  2563. GB_PANIC("Invalid AST TypeCast");
  2564. case_end;
  2565. case_ast_node(ac, AutoCast, expr);
  2566. lbValue value = lb_build_expr(p, ac->expr);
  2567. return lb_emit_conv(p, value, type);
  2568. case_end;
  2569. case_ast_node(ue, UnaryExpr, expr);
  2570. switch (ue->op.kind) {
  2571. case Token_And:
  2572. return lb_build_unary_and(p, expr);
  2573. default:
  2574. {
  2575. lbValue v = lb_build_expr(p, ue->expr);
  2576. return lb_emit_unary_arith(p, ue->op.kind, v, type);
  2577. }
  2578. }
  2579. case_end;
  2580. case_ast_node(be, BinaryExpr, expr);
  2581. return lb_build_binary_expr(p, expr);
  2582. case_end;
  2583. case_ast_node(pl, ProcLit, expr);
  2584. return lb_generate_anonymous_proc_lit(p->module, p->name, expr, p);
  2585. case_end;
  2586. case_ast_node(cl, CompoundLit, expr);
  2587. return lb_addr_load(p, lb_build_addr(p, expr));
  2588. case_end;
  2589. case_ast_node(ce, CallExpr, expr);
  2590. return lb_build_call_expr(p, expr);
  2591. case_end;
  2592. case_ast_node(se, SliceExpr, expr);
  2593. if (is_type_slice(type_of_expr(se->expr))) {
  2594. // NOTE(bill): Quick optimization
  2595. if (se->high == nullptr &&
  2596. (se->low == nullptr || lb_is_expr_constant_zero(se->low))) {
  2597. return lb_build_expr(p, se->expr);
  2598. }
  2599. }
  2600. return lb_addr_load(p, lb_build_addr(p, expr));
  2601. case_end;
  2602. case_ast_node(ie, IndexExpr, expr);
  2603. return lb_addr_load(p, lb_build_addr(p, expr));
  2604. case_end;
  2605. case_ast_node(ie, MatrixIndexExpr, expr);
  2606. return lb_addr_load(p, lb_build_addr(p, expr));
  2607. case_end;
  2608. case_ast_node(ia, InlineAsmExpr, expr);
  2609. Type *t = type_of_expr(expr);
  2610. GB_ASSERT(is_type_asm_proc(t));
  2611. String asm_string = {};
  2612. String constraints_string = {};
  2613. TypeAndValue tav;
  2614. tav = type_and_value_of_expr(ia->asm_string);
  2615. GB_ASSERT(is_type_string(tav.type));
  2616. GB_ASSERT(tav.value.kind == ExactValue_String);
  2617. asm_string = tav.value.value_string;
  2618. tav = type_and_value_of_expr(ia->constraints_string);
  2619. GB_ASSERT(is_type_string(tav.type));
  2620. GB_ASSERT(tav.value.kind == ExactValue_String);
  2621. constraints_string = tav.value.value_string;
  2622. LLVMInlineAsmDialect dialect = LLVMInlineAsmDialectATT;
  2623. switch (ia->dialect) {
  2624. case InlineAsmDialect_Default: dialect = LLVMInlineAsmDialectATT; break;
  2625. case InlineAsmDialect_ATT: dialect = LLVMInlineAsmDialectATT; break;
  2626. case InlineAsmDialect_Intel: dialect = LLVMInlineAsmDialectIntel; break;
  2627. default: GB_PANIC("Unhandled inline asm dialect"); break;
  2628. }
  2629. LLVMTypeRef func_type = LLVMGetElementType(lb_type(p->module, t));
  2630. LLVMValueRef the_asm = LLVMGetInlineAsm(func_type,
  2631. cast(char *)asm_string.text, cast(size_t)asm_string.len,
  2632. cast(char *)constraints_string.text, cast(size_t)constraints_string.len,
  2633. ia->has_side_effects, ia->is_align_stack, dialect
  2634. );
  2635. GB_ASSERT(the_asm != nullptr);
  2636. return {the_asm, t};
  2637. case_end;
  2638. }
  2639. GB_PANIC("lb_build_expr: %.*s", LIT(ast_strings[expr->kind]));
  2640. return {};
  2641. }
  2642. lbAddr lb_get_soa_variable_addr(lbProcedure *p, Entity *e) {
  2643. return map_must_get(&p->module->soa_values, hash_entity(e));
  2644. }
  2645. lbValue lb_get_using_variable(lbProcedure *p, Entity *e) {
  2646. GB_ASSERT(e->kind == Entity_Variable && e->flags & EntityFlag_Using);
  2647. String name = e->token.string;
  2648. Entity *parent = e->using_parent;
  2649. Selection sel = lookup_field(parent->type, name, false);
  2650. GB_ASSERT(sel.entity != nullptr);
  2651. lbValue *pv = map_get(&p->module->values, hash_entity(parent));
  2652. lbValue v = {};
  2653. if (pv == nullptr && parent->flags & EntityFlag_SoaPtrField) {
  2654. // NOTE(bill): using SOA value (probably from for-in statement)
  2655. lbAddr parent_addr = lb_get_soa_variable_addr(p, parent);
  2656. v = lb_addr_get_ptr(p, parent_addr);
  2657. } else if (pv != nullptr) {
  2658. v = *pv;
  2659. } else {
  2660. GB_ASSERT_MSG(e->using_expr != nullptr, "%.*s", LIT(name));
  2661. v = lb_build_addr_ptr(p, e->using_expr);
  2662. }
  2663. GB_ASSERT(v.value != nullptr);
  2664. GB_ASSERT_MSG(parent->type == type_deref(v.type), "%s %s", type_to_string(parent->type), type_to_string(v.type));
  2665. lbValue ptr = lb_emit_deep_field_gep(p, v, sel);
  2666. if (parent->scope) {
  2667. if ((parent->scope->flags & (ScopeFlag_File|ScopeFlag_Pkg)) == 0) {
  2668. lb_add_debug_local_variable(p, ptr.value, e->type, e->token);
  2669. }
  2670. } else {
  2671. lb_add_debug_local_variable(p, ptr.value, e->type, e->token);
  2672. }
  2673. return ptr;
  2674. }
  2675. lbAddr lb_build_addr_from_entity(lbProcedure *p, Entity *e, Ast *expr) {
  2676. GB_ASSERT(e != nullptr);
  2677. if (e->kind == Entity_Constant) {
  2678. Type *t = default_type(type_of_expr(expr));
  2679. lbValue v = lb_const_value(p->module, t, e->Constant.value);
  2680. lbAddr g = lb_add_global_generated(p->module, t, v);
  2681. return g;
  2682. }
  2683. lbValue v = {};
  2684. lbValue *found = map_get(&p->module->values, hash_entity(e));
  2685. if (found) {
  2686. v = *found;
  2687. } else if (e->kind == Entity_Variable && e->flags & EntityFlag_Using) {
  2688. // NOTE(bill): Calculate the using variable every time
  2689. v = lb_get_using_variable(p, e);
  2690. } else if (e->flags & EntityFlag_SoaPtrField) {
  2691. return lb_get_soa_variable_addr(p, e);
  2692. }
  2693. if (v.value == nullptr) {
  2694. return lb_addr(lb_find_value_from_entity(p->module, e));
  2695. // error(expr, "%.*s Unknown value: %.*s, entity: %p %.*s",
  2696. // LIT(p->name),
  2697. // LIT(e->token.string), e, LIT(entity_strings[e->kind]));
  2698. // GB_PANIC("Unknown value");
  2699. }
  2700. return lb_addr(v);
  2701. }
  2702. lbAddr lb_build_array_swizzle_addr(lbProcedure *p, AstCallExpr *ce, TypeAndValue const &tv) {
  2703. isize index_count = ce->args.count-1;
  2704. lbAddr addr = lb_build_addr(p, ce->args[0]);
  2705. if (index_count == 0) {
  2706. return addr;
  2707. }
  2708. Type *type = base_type(lb_addr_type(addr));
  2709. GB_ASSERT(type->kind == Type_Array);
  2710. i64 count = type->Array.count;
  2711. if (count <= 4) {
  2712. u8 indices[4] = {};
  2713. u8 index_count = 0;
  2714. for (i32 i = 1; i < ce->args.count; i++) {
  2715. TypeAndValue tv = type_and_value_of_expr(ce->args[i]);
  2716. GB_ASSERT(is_type_integer(tv.type));
  2717. GB_ASSERT(tv.value.kind == ExactValue_Integer);
  2718. i64 src_index = big_int_to_i64(&tv.value.value_integer);
  2719. indices[index_count++] = cast(u8)src_index;
  2720. }
  2721. return lb_addr_swizzle(lb_addr_get_ptr(p, addr), tv.type, index_count, indices);
  2722. }
  2723. auto indices = slice_make<i32>(permanent_allocator(), ce->args.count-1);
  2724. isize index_index = 0;
  2725. for (i32 i = 1; i < ce->args.count; i++) {
  2726. TypeAndValue tv = type_and_value_of_expr(ce->args[i]);
  2727. GB_ASSERT(is_type_integer(tv.type));
  2728. GB_ASSERT(tv.value.kind == ExactValue_Integer);
  2729. i64 src_index = big_int_to_i64(&tv.value.value_integer);
  2730. indices[index_index++] = cast(i32)src_index;
  2731. }
  2732. return lb_addr_swizzle_large(lb_addr_get_ptr(p, addr), tv.type, indices);
  2733. }
  2734. lbAddr lb_build_addr(lbProcedure *p, Ast *expr) {
  2735. expr = unparen_expr(expr);
  2736. switch (expr->kind) {
  2737. case_ast_node(i, Implicit, expr);
  2738. lbAddr v = {};
  2739. switch (i->kind) {
  2740. case Token_context:
  2741. v = lb_find_or_generate_context_ptr(p);
  2742. break;
  2743. }
  2744. GB_ASSERT(v.addr.value != nullptr);
  2745. return v;
  2746. case_end;
  2747. case_ast_node(i, Ident, expr);
  2748. if (is_blank_ident(expr)) {
  2749. lbAddr val = {};
  2750. return val;
  2751. }
  2752. String name = i->token.string;
  2753. Entity *e = entity_of_node(expr);
  2754. return lb_build_addr_from_entity(p, e, expr);
  2755. case_end;
  2756. case_ast_node(se, SelectorExpr, expr);
  2757. Ast *sel = unparen_expr(se->selector);
  2758. if (sel->kind == Ast_Ident) {
  2759. String selector = sel->Ident.token.string;
  2760. TypeAndValue tav = type_and_value_of_expr(se->expr);
  2761. if (tav.mode == Addressing_Invalid) {
  2762. // NOTE(bill): Imports
  2763. Entity *imp = entity_of_node(se->expr);
  2764. if (imp != nullptr) {
  2765. GB_ASSERT(imp->kind == Entity_ImportName);
  2766. }
  2767. return lb_build_addr(p, unparen_expr(se->selector));
  2768. }
  2769. Type *type = base_type(tav.type);
  2770. if (tav.mode == Addressing_Type) { // Addressing_Type
  2771. GB_PANIC("Unreachable");
  2772. }
  2773. if (se->swizzle_count > 0) {
  2774. Type *array_type = base_type(type_deref(tav.type));
  2775. GB_ASSERT(array_type->kind == Type_Array);
  2776. u8 swizzle_count = se->swizzle_count;
  2777. u8 swizzle_indices_raw = se->swizzle_indices;
  2778. u8 swizzle_indices[4] = {};
  2779. for (u8 i = 0; i < swizzle_count; i++) {
  2780. u8 index = swizzle_indices_raw>>(i*2) & 3;
  2781. swizzle_indices[i] = index;
  2782. }
  2783. lbValue a = {};
  2784. if (is_type_pointer(tav.type)) {
  2785. a = lb_build_expr(p, se->expr);
  2786. } else {
  2787. lbAddr addr = lb_build_addr(p, se->expr);
  2788. a = lb_addr_get_ptr(p, addr);
  2789. }
  2790. GB_ASSERT(is_type_array(expr->tav.type));
  2791. return lb_addr_swizzle(a, expr->tav.type, swizzle_count, swizzle_indices);
  2792. }
  2793. Selection sel = lookup_field(type, selector, false);
  2794. GB_ASSERT(sel.entity != nullptr);
  2795. {
  2796. lbAddr addr = lb_build_addr(p, se->expr);
  2797. if (addr.kind == lbAddr_Map) {
  2798. lbValue v = lb_addr_load(p, addr);
  2799. lbValue a = lb_address_from_load_or_generate_local(p, v);
  2800. a = lb_emit_deep_field_gep(p, a, sel);
  2801. return lb_addr(a);
  2802. } else if (addr.kind == lbAddr_Context) {
  2803. GB_ASSERT(sel.index.count > 0);
  2804. if (addr.ctx.sel.index.count >= 0) {
  2805. sel = selection_combine(addr.ctx.sel, sel);
  2806. }
  2807. addr.ctx.sel = sel;
  2808. addr.kind = lbAddr_Context;
  2809. return addr;
  2810. } else if (addr.kind == lbAddr_SoaVariable) {
  2811. lbValue index = addr.soa.index;
  2812. i32 first_index = sel.index[0];
  2813. Selection sub_sel = sel;
  2814. sub_sel.index.data += 1;
  2815. sub_sel.index.count -= 1;
  2816. lbValue arr = lb_emit_struct_ep(p, addr.addr, first_index);
  2817. Type *t = base_type(type_deref(addr.addr.type));
  2818. GB_ASSERT(is_type_soa_struct(t));
  2819. if (addr.soa.index_expr != nullptr && (!lb_is_const(addr.soa.index) || t->Struct.soa_kind != StructSoa_Fixed)) {
  2820. lbValue len = lb_soa_struct_len(p, addr.addr);
  2821. lb_emit_bounds_check(p, ast_token(addr.soa.index_expr), addr.soa.index, len);
  2822. }
  2823. lbValue item = {};
  2824. if (t->Struct.soa_kind == StructSoa_Fixed) {
  2825. item = lb_emit_array_ep(p, arr, index);
  2826. } else {
  2827. item = lb_emit_ptr_offset(p, lb_emit_load(p, arr), index);
  2828. }
  2829. if (sub_sel.index.count > 0) {
  2830. item = lb_emit_deep_field_gep(p, item, sub_sel);
  2831. }
  2832. return lb_addr(item);
  2833. } else if (addr.kind == lbAddr_Swizzle) {
  2834. GB_ASSERT(sel.index.count > 0);
  2835. // NOTE(bill): just patch the index in place
  2836. sel.index[0] = addr.swizzle.indices[sel.index[0]];
  2837. } else if (addr.kind == lbAddr_SwizzleLarge) {
  2838. GB_ASSERT(sel.index.count > 0);
  2839. // NOTE(bill): just patch the index in place
  2840. sel.index[0] = addr.swizzle.indices[sel.index[0]];
  2841. }
  2842. lbValue a = lb_addr_get_ptr(p, addr);
  2843. a = lb_emit_deep_field_gep(p, a, sel);
  2844. return lb_addr(a);
  2845. }
  2846. } else {
  2847. GB_PANIC("Unsupported selector expression");
  2848. }
  2849. case_end;
  2850. case_ast_node(se, SelectorCallExpr, expr);
  2851. GB_ASSERT(se->modified_call);
  2852. TypeAndValue tav = type_and_value_of_expr(expr);
  2853. GB_ASSERT(tav.mode != Addressing_Invalid);
  2854. lbValue e = lb_build_expr(p, expr);
  2855. return lb_addr(lb_address_from_load_or_generate_local(p, e));
  2856. case_end;
  2857. case_ast_node(ta, TypeAssertion, expr);
  2858. TokenPos pos = ast_token(expr).pos;
  2859. lbValue e = lb_build_expr(p, ta->expr);
  2860. Type *t = type_deref(e.type);
  2861. if (is_type_union(t)) {
  2862. Type *type = type_of_expr(expr);
  2863. lbAddr v = lb_add_local_generated(p, type, false);
  2864. lb_addr_store(p, v, lb_emit_union_cast(p, lb_build_expr(p, ta->expr), type, pos));
  2865. return v;
  2866. } else if (is_type_any(t)) {
  2867. Type *type = type_of_expr(expr);
  2868. return lb_emit_any_cast_addr(p, lb_build_expr(p, ta->expr), type, pos);
  2869. } else {
  2870. GB_PANIC("TODO(bill): type assertion %s", type_to_string(e.type));
  2871. }
  2872. case_end;
  2873. case_ast_node(ue, UnaryExpr, expr);
  2874. switch (ue->op.kind) {
  2875. case Token_And: {
  2876. lbValue ptr = lb_build_expr(p, expr);
  2877. return lb_addr(lb_address_from_load_or_generate_local(p, ptr));
  2878. }
  2879. default:
  2880. GB_PANIC("Invalid unary expression for lb_build_addr");
  2881. }
  2882. case_end;
  2883. case_ast_node(be, BinaryExpr, expr);
  2884. lbValue v = lb_build_expr(p, expr);
  2885. Type *t = v.type;
  2886. if (is_type_pointer(t)) {
  2887. return lb_addr(v);
  2888. }
  2889. return lb_addr(lb_address_from_load_or_generate_local(p, v));
  2890. case_end;
  2891. case_ast_node(ie, IndexExpr, expr);
  2892. Type *t = base_type(type_of_expr(ie->expr));
  2893. bool deref = is_type_pointer(t);
  2894. t = base_type(type_deref(t));
  2895. if (is_type_soa_struct(t)) {
  2896. // SOA STRUCTURES!!!!
  2897. lbValue val = lb_build_addr_ptr(p, ie->expr);
  2898. if (deref) {
  2899. val = lb_emit_load(p, val);
  2900. }
  2901. lbValue index = lb_build_expr(p, ie->index);
  2902. return lb_addr_soa_variable(val, index, ie->index);
  2903. }
  2904. if (ie->expr->tav.mode == Addressing_SoaVariable) {
  2905. // SOA Structures for slices/dynamic arrays
  2906. GB_ASSERT(is_type_pointer(type_of_expr(ie->expr)));
  2907. lbValue field = lb_build_expr(p, ie->expr);
  2908. lbValue index = lb_build_expr(p, ie->index);
  2909. if (!build_context.no_bounds_check) {
  2910. // TODO HACK(bill): Clean up this hack to get the length for bounds checking
  2911. // GB_ASSERT(LLVMIsALoadInst(field.value));
  2912. // lbValue a = {};
  2913. // a.value = LLVMGetOperand(field.value, 0);
  2914. // a.type = alloc_type_pointer(field.type);
  2915. // irInstr *b = &a->Instr;
  2916. // GB_ASSERT(b->kind == irInstr_StructElementPtr);
  2917. // lbValue base_struct = b->StructElementPtr.address;
  2918. // GB_ASSERT(is_type_soa_struct(type_deref(ir_type(base_struct))));
  2919. // lbValue len = ir_soa_struct_len(p, base_struct);
  2920. // lb_emit_bounds_check(p, ast_token(ie->index), index, len);
  2921. }
  2922. lbValue val = lb_emit_ptr_offset(p, field, index);
  2923. return lb_addr(val);
  2924. }
  2925. GB_ASSERT_MSG(is_type_indexable(t), "%s %s", type_to_string(t), expr_to_string(expr));
  2926. if (is_type_map(t)) {
  2927. lbValue map_val = lb_build_addr_ptr(p, ie->expr);
  2928. if (deref) {
  2929. map_val = lb_emit_load(p, map_val);
  2930. }
  2931. lbValue key = lb_build_expr(p, ie->index);
  2932. key = lb_emit_conv(p, key, t->Map.key);
  2933. Type *result_type = type_of_expr(expr);
  2934. return lb_addr_map(map_val, key, t, result_type);
  2935. }
  2936. switch (t->kind) {
  2937. case Type_Array: {
  2938. lbValue array = {};
  2939. array = lb_build_addr_ptr(p, ie->expr);
  2940. if (deref) {
  2941. array = lb_emit_load(p, array);
  2942. }
  2943. lbValue index = lb_build_expr(p, ie->index);
  2944. index = lb_emit_conv(p, index, t_int);
  2945. lbValue elem = lb_emit_array_ep(p, array, index);
  2946. auto index_tv = type_and_value_of_expr(ie->index);
  2947. if (index_tv.mode != Addressing_Constant) {
  2948. lbValue len = lb_const_int(p->module, t_int, t->Array.count);
  2949. lb_emit_bounds_check(p, ast_token(ie->index), index, len);
  2950. }
  2951. return lb_addr(elem);
  2952. }
  2953. case Type_EnumeratedArray: {
  2954. lbValue array = {};
  2955. array = lb_build_addr_ptr(p, ie->expr);
  2956. if (deref) {
  2957. array = lb_emit_load(p, array);
  2958. }
  2959. Type *index_type = t->EnumeratedArray.index;
  2960. auto index_tv = type_and_value_of_expr(ie->index);
  2961. lbValue index = {};
  2962. if (compare_exact_values(Token_NotEq, *t->EnumeratedArray.min_value, exact_value_i64(0))) {
  2963. if (index_tv.mode == Addressing_Constant) {
  2964. ExactValue idx = exact_value_sub(index_tv.value, *t->EnumeratedArray.min_value);
  2965. index = lb_const_value(p->module, index_type, idx);
  2966. } else {
  2967. index = lb_emit_conv(p, lb_build_expr(p, ie->index), t_int);
  2968. index = lb_emit_arith(p, Token_Sub, index, lb_const_value(p->module, index_type, *t->EnumeratedArray.min_value), index_type);
  2969. }
  2970. } else {
  2971. index = lb_emit_conv(p, lb_build_expr(p, ie->index), t_int);
  2972. }
  2973. lbValue elem = lb_emit_array_ep(p, array, index);
  2974. if (index_tv.mode != Addressing_Constant) {
  2975. lbValue len = lb_const_int(p->module, t_int, t->EnumeratedArray.count);
  2976. lb_emit_bounds_check(p, ast_token(ie->index), index, len);
  2977. }
  2978. return lb_addr(elem);
  2979. }
  2980. case Type_Slice: {
  2981. lbValue slice = {};
  2982. slice = lb_build_expr(p, ie->expr);
  2983. if (deref) {
  2984. slice = lb_emit_load(p, slice);
  2985. }
  2986. lbValue elem = lb_slice_elem(p, slice);
  2987. lbValue index = lb_emit_conv(p, lb_build_expr(p, ie->index), t_int);
  2988. lbValue len = lb_slice_len(p, slice);
  2989. lb_emit_bounds_check(p, ast_token(ie->index), index, len);
  2990. lbValue v = lb_emit_ptr_offset(p, elem, index);
  2991. return lb_addr(v);
  2992. }
  2993. case Type_MultiPointer: {
  2994. lbValue multi_ptr = {};
  2995. multi_ptr = lb_build_expr(p, ie->expr);
  2996. if (deref) {
  2997. multi_ptr = lb_emit_load(p, multi_ptr);
  2998. }
  2999. lbValue index = lb_build_expr(p, ie->index);
  3000. lbValue v = {};
  3001. LLVMValueRef indices[1] = {index.value};
  3002. v.value = LLVMBuildGEP(p->builder, multi_ptr.value, indices, 1, "");
  3003. v.type = alloc_type_pointer(t->MultiPointer.elem);
  3004. return lb_addr(v);
  3005. }
  3006. case Type_RelativeSlice: {
  3007. lbAddr slice_addr = {};
  3008. if (deref) {
  3009. slice_addr = lb_addr(lb_build_expr(p, ie->expr));
  3010. } else {
  3011. slice_addr = lb_build_addr(p, ie->expr);
  3012. }
  3013. lbValue slice = lb_addr_load(p, slice_addr);
  3014. lbValue elem = lb_slice_elem(p, slice);
  3015. lbValue index = lb_emit_conv(p, lb_build_expr(p, ie->index), t_int);
  3016. lbValue len = lb_slice_len(p, slice);
  3017. lb_emit_bounds_check(p, ast_token(ie->index), index, len);
  3018. lbValue v = lb_emit_ptr_offset(p, elem, index);
  3019. return lb_addr(v);
  3020. }
  3021. case Type_DynamicArray: {
  3022. lbValue dynamic_array = {};
  3023. dynamic_array = lb_build_expr(p, ie->expr);
  3024. if (deref) {
  3025. dynamic_array = lb_emit_load(p, dynamic_array);
  3026. }
  3027. lbValue elem = lb_dynamic_array_elem(p, dynamic_array);
  3028. lbValue len = lb_dynamic_array_len(p, dynamic_array);
  3029. lbValue index = lb_emit_conv(p, lb_build_expr(p, ie->index), t_int);
  3030. lb_emit_bounds_check(p, ast_token(ie->index), index, len);
  3031. lbValue v = lb_emit_ptr_offset(p, elem, index);
  3032. return lb_addr(v);
  3033. }
  3034. case Type_Matrix: {
  3035. lbValue matrix = {};
  3036. matrix = lb_build_addr_ptr(p, ie->expr);
  3037. if (deref) {
  3038. matrix = lb_emit_load(p, matrix);
  3039. }
  3040. lbValue index = lb_build_expr(p, ie->index);
  3041. index = lb_emit_conv(p, index, t_int);
  3042. lbValue elem = lb_emit_matrix_ep(p, matrix, lb_const_int(p->module, t_int, 0), index);
  3043. elem = lb_emit_conv(p, elem, alloc_type_pointer(type_of_expr(expr)));
  3044. auto index_tv = type_and_value_of_expr(ie->index);
  3045. if (index_tv.mode != Addressing_Constant) {
  3046. lbValue len = lb_const_int(p->module, t_int, t->Matrix.column_count);
  3047. lb_emit_bounds_check(p, ast_token(ie->index), index, len);
  3048. }
  3049. return lb_addr(elem);
  3050. }
  3051. case Type_Basic: { // Basic_string
  3052. lbValue str;
  3053. lbValue elem;
  3054. lbValue len;
  3055. lbValue index;
  3056. str = lb_build_expr(p, ie->expr);
  3057. if (deref) {
  3058. str = lb_emit_load(p, str);
  3059. }
  3060. elem = lb_string_elem(p, str);
  3061. len = lb_string_len(p, str);
  3062. index = lb_emit_conv(p, lb_build_expr(p, ie->index), t_int);
  3063. lb_emit_bounds_check(p, ast_token(ie->index), index, len);
  3064. return lb_addr(lb_emit_ptr_offset(p, elem, index));
  3065. }
  3066. }
  3067. case_end;
  3068. case_ast_node(ie, MatrixIndexExpr, expr);
  3069. Type *t = base_type(type_of_expr(ie->expr));
  3070. bool deref = is_type_pointer(t);
  3071. t = base_type(type_deref(t));
  3072. lbValue m = {};
  3073. m = lb_build_addr_ptr(p, ie->expr);
  3074. if (deref) {
  3075. m = lb_emit_load(p, m);
  3076. }
  3077. lbValue row_index = lb_build_expr(p, ie->row_index);
  3078. lbValue column_index = lb_build_expr(p, ie->column_index);
  3079. row_index = lb_emit_conv(p, row_index, t_int);
  3080. column_index = lb_emit_conv(p, column_index, t_int);
  3081. lbValue elem = lb_emit_matrix_ep(p, m, row_index, column_index);
  3082. auto row_index_tv = type_and_value_of_expr(ie->row_index);
  3083. auto column_index_tv = type_and_value_of_expr(ie->column_index);
  3084. if (row_index_tv.mode != Addressing_Constant || column_index_tv.mode != Addressing_Constant) {
  3085. lbValue row_count = lb_const_int(p->module, t_int, t->Matrix.row_count);
  3086. lbValue column_count = lb_const_int(p->module, t_int, t->Matrix.column_count);
  3087. lb_emit_matrix_bounds_check(p, ast_token(ie->row_index), row_index, column_index, row_count, column_count);
  3088. }
  3089. return lb_addr(elem);
  3090. case_end;
  3091. case_ast_node(se, SliceExpr, expr);
  3092. lbValue low = lb_const_int(p->module, t_int, 0);
  3093. lbValue high = {};
  3094. if (se->low != nullptr) {
  3095. low = lb_correct_endianness(p, lb_build_expr(p, se->low));
  3096. }
  3097. if (se->high != nullptr) {
  3098. high = lb_correct_endianness(p, lb_build_expr(p, se->high));
  3099. }
  3100. bool no_indices = se->low == nullptr && se->high == nullptr;
  3101. lbAddr addr = lb_build_addr(p, se->expr);
  3102. lbValue base = lb_addr_load(p, addr);
  3103. Type *type = base_type(base.type);
  3104. if (is_type_pointer(type)) {
  3105. type = base_type(type_deref(type));
  3106. addr = lb_addr(base);
  3107. base = lb_addr_load(p, addr);
  3108. }
  3109. switch (type->kind) {
  3110. case Type_Slice: {
  3111. Type *slice_type = type;
  3112. lbValue len = lb_slice_len(p, base);
  3113. if (high.value == nullptr) high = len;
  3114. if (!no_indices) {
  3115. lb_emit_slice_bounds_check(p, se->open, low, high, len, se->low != nullptr);
  3116. }
  3117. lbValue elem = lb_emit_ptr_offset(p, lb_slice_elem(p, base), low);
  3118. lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int);
  3119. lbAddr slice = lb_add_local_generated(p, slice_type, false);
  3120. lb_fill_slice(p, slice, elem, new_len);
  3121. return slice;
  3122. }
  3123. case Type_RelativeSlice:
  3124. GB_PANIC("TODO(bill): Type_RelativeSlice should be handled above already on the lb_addr_load");
  3125. break;
  3126. case Type_DynamicArray: {
  3127. Type *elem_type = type->DynamicArray.elem;
  3128. Type *slice_type = alloc_type_slice(elem_type);
  3129. lbValue len = lb_dynamic_array_len(p, base);
  3130. if (high.value == nullptr) high = len;
  3131. if (!no_indices) {
  3132. lb_emit_slice_bounds_check(p, se->open, low, high, len, se->low != nullptr);
  3133. }
  3134. lbValue elem = lb_emit_ptr_offset(p, lb_dynamic_array_elem(p, base), low);
  3135. lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int);
  3136. lbAddr slice = lb_add_local_generated(p, slice_type, false);
  3137. lb_fill_slice(p, slice, elem, new_len);
  3138. return slice;
  3139. }
  3140. case Type_MultiPointer: {
  3141. lbAddr res = lb_add_local_generated(p, type_of_expr(expr), false);
  3142. if (se->high == nullptr) {
  3143. lbValue offset = base;
  3144. LLVMValueRef indices[1] = {low.value};
  3145. offset.value = LLVMBuildGEP(p->builder, offset.value, indices, 1, "");
  3146. lb_addr_store(p, res, offset);
  3147. } else {
  3148. low = lb_emit_conv(p, low, t_int);
  3149. high = lb_emit_conv(p, high, t_int);
  3150. lb_emit_multi_pointer_slice_bounds_check(p, se->open, low, high);
  3151. LLVMValueRef indices[1] = {low.value};
  3152. LLVMValueRef ptr = LLVMBuildGEP(p->builder, base.value, indices, 1, "");
  3153. LLVMValueRef len = LLVMBuildSub(p->builder, high.value, low.value, "");
  3154. LLVMValueRef gep0 = lb_emit_struct_ep(p, res.addr, 0).value;
  3155. LLVMValueRef gep1 = lb_emit_struct_ep(p, res.addr, 1).value;
  3156. LLVMBuildStore(p->builder, ptr, gep0);
  3157. LLVMBuildStore(p->builder, len, gep1);
  3158. }
  3159. return res;
  3160. }
  3161. case Type_Array: {
  3162. Type *slice_type = alloc_type_slice(type->Array.elem);
  3163. lbValue len = lb_const_int(p->module, t_int, type->Array.count);
  3164. if (high.value == nullptr) high = len;
  3165. bool low_const = type_and_value_of_expr(se->low).mode == Addressing_Constant;
  3166. bool high_const = type_and_value_of_expr(se->high).mode == Addressing_Constant;
  3167. if (!low_const || !high_const) {
  3168. if (!no_indices) {
  3169. lb_emit_slice_bounds_check(p, se->open, low, high, len, se->low != nullptr);
  3170. }
  3171. }
  3172. lbValue elem = lb_emit_ptr_offset(p, lb_array_elem(p, lb_addr_get_ptr(p, addr)), low);
  3173. lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int);
  3174. lbAddr slice = lb_add_local_generated(p, slice_type, false);
  3175. lb_fill_slice(p, slice, elem, new_len);
  3176. return slice;
  3177. }
  3178. case Type_Basic: {
  3179. GB_ASSERT(type == t_string);
  3180. lbValue len = lb_string_len(p, base);
  3181. if (high.value == nullptr) high = len;
  3182. if (!no_indices) {
  3183. lb_emit_slice_bounds_check(p, se->open, low, high, len, se->low != nullptr);
  3184. }
  3185. lbValue elem = lb_emit_ptr_offset(p, lb_string_elem(p, base), low);
  3186. lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int);
  3187. lbAddr str = lb_add_local_generated(p, t_string, false);
  3188. lb_fill_string(p, str, elem, new_len);
  3189. return str;
  3190. }
  3191. case Type_Struct:
  3192. if (is_type_soa_struct(type)) {
  3193. lbValue len = lb_soa_struct_len(p, lb_addr_get_ptr(p, addr));
  3194. if (high.value == nullptr) high = len;
  3195. if (!no_indices) {
  3196. lb_emit_slice_bounds_check(p, se->open, low, high, len, se->low != nullptr);
  3197. }
  3198. #if 1
  3199. lbAddr dst = lb_add_local_generated(p, type_of_expr(expr), true);
  3200. if (type->Struct.soa_kind == StructSoa_Fixed) {
  3201. i32 field_count = cast(i32)type->Struct.fields.count;
  3202. for (i32 i = 0; i < field_count; i++) {
  3203. lbValue field_dst = lb_emit_struct_ep(p, dst.addr, i);
  3204. lbValue field_src = lb_emit_struct_ep(p, lb_addr_get_ptr(p, addr), i);
  3205. field_src = lb_emit_array_ep(p, field_src, low);
  3206. lb_emit_store(p, field_dst, field_src);
  3207. }
  3208. lbValue len_dst = lb_emit_struct_ep(p, dst.addr, field_count);
  3209. lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int);
  3210. lb_emit_store(p, len_dst, new_len);
  3211. } else if (type->Struct.soa_kind == StructSoa_Slice) {
  3212. if (no_indices) {
  3213. lb_addr_store(p, dst, base);
  3214. } else {
  3215. i32 field_count = cast(i32)type->Struct.fields.count - 1;
  3216. for (i32 i = 0; i < field_count; i++) {
  3217. lbValue field_dst = lb_emit_struct_ep(p, dst.addr, i);
  3218. lbValue field_src = lb_emit_struct_ev(p, base, i);
  3219. field_src = lb_emit_ptr_offset(p, field_src, low);
  3220. lb_emit_store(p, field_dst, field_src);
  3221. }
  3222. lbValue len_dst = lb_emit_struct_ep(p, dst.addr, field_count);
  3223. lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int);
  3224. lb_emit_store(p, len_dst, new_len);
  3225. }
  3226. } else if (type->Struct.soa_kind == StructSoa_Dynamic) {
  3227. i32 field_count = cast(i32)type->Struct.fields.count - 3;
  3228. for (i32 i = 0; i < field_count; i++) {
  3229. lbValue field_dst = lb_emit_struct_ep(p, dst.addr, i);
  3230. lbValue field_src = lb_emit_struct_ev(p, base, i);
  3231. field_src = lb_emit_ptr_offset(p, field_src, low);
  3232. lb_emit_store(p, field_dst, field_src);
  3233. }
  3234. lbValue len_dst = lb_emit_struct_ep(p, dst.addr, field_count);
  3235. lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int);
  3236. lb_emit_store(p, len_dst, new_len);
  3237. }
  3238. return dst;
  3239. #endif
  3240. }
  3241. break;
  3242. }
  3243. GB_PANIC("Unknown slicable type");
  3244. case_end;
  3245. case_ast_node(de, DerefExpr, expr);
  3246. if (is_type_relative_pointer(type_of_expr(de->expr))) {
  3247. lbAddr addr = lb_build_addr(p, de->expr);
  3248. addr.relative.deref = true;
  3249. return addr;\
  3250. }
  3251. lbValue addr = lb_build_expr(p, de->expr);
  3252. return lb_addr(addr);
  3253. case_end;
  3254. case_ast_node(ce, CallExpr, expr);
  3255. BuiltinProcId builtin_id = BuiltinProc_Invalid;
  3256. if (ce->proc->tav.mode == Addressing_Builtin) {
  3257. Entity *e = entity_of_node(ce->proc);
  3258. if (e != nullptr) {
  3259. builtin_id = cast(BuiltinProcId)e->Builtin.id;
  3260. } else {
  3261. builtin_id = BuiltinProc_DIRECTIVE;
  3262. }
  3263. }
  3264. auto const &tv = expr->tav;
  3265. if (builtin_id == BuiltinProc_swizzle &&
  3266. is_type_array(tv.type)) {
  3267. // NOTE(bill, 2021-08-09): `swizzle` has some bizarre semantics so it needs to be
  3268. // specialized here for to be addressable
  3269. return lb_build_array_swizzle_addr(p, ce, tv);
  3270. }
  3271. // NOTE(bill): This is make sure you never need to have an 'array_ev'
  3272. lbValue e = lb_build_expr(p, expr);
  3273. #if 1
  3274. return lb_addr(lb_address_from_load_or_generate_local(p, e));
  3275. #else
  3276. lbAddr v = lb_add_local_generated(p, e.type, false);
  3277. lb_addr_store(p, v, e);
  3278. return v;
  3279. #endif
  3280. case_end;
  3281. case_ast_node(cl, CompoundLit, expr);
  3282. Type *type = type_of_expr(expr);
  3283. Type *bt = base_type(type);
  3284. lbAddr v = lb_add_local_generated(p, type, true);
  3285. Type *et = nullptr;
  3286. switch (bt->kind) {
  3287. case Type_Array: et = bt->Array.elem; break;
  3288. case Type_EnumeratedArray: et = bt->EnumeratedArray.elem; break;
  3289. case Type_Slice: et = bt->Slice.elem; break;
  3290. case Type_BitSet: et = bt->BitSet.elem; break;
  3291. case Type_SimdVector: et = bt->SimdVector.elem; break;
  3292. case Type_Matrix: et = bt->Matrix.elem; break;
  3293. }
  3294. String proc_name = {};
  3295. if (p->entity) {
  3296. proc_name = p->entity->token.string;
  3297. }
  3298. TokenPos pos = ast_token(expr).pos;
  3299. switch (bt->kind) {
  3300. default: GB_PANIC("Unknown CompoundLit type: %s", type_to_string(type)); break;
  3301. case Type_Struct: {
  3302. // TODO(bill): "constant" '#raw_union's are not initialized constantly at the moment.
  3303. // NOTE(bill): This is due to the layout of the unions when printed to LLVM-IR
  3304. bool is_raw_union = is_type_raw_union(bt);
  3305. GB_ASSERT(is_type_struct(bt) || is_raw_union);
  3306. TypeStruct *st = &bt->Struct;
  3307. if (cl->elems.count > 0) {
  3308. lb_addr_store(p, v, lb_const_value(p->module, type, exact_value_compound(expr)));
  3309. lbValue comp_lit_ptr = lb_addr_get_ptr(p, v);
  3310. for_array(field_index, cl->elems) {
  3311. Ast *elem = cl->elems[field_index];
  3312. lbValue field_expr = {};
  3313. Entity *field = nullptr;
  3314. isize index = field_index;
  3315. if (elem->kind == Ast_FieldValue) {
  3316. ast_node(fv, FieldValue, elem);
  3317. String name = fv->field->Ident.token.string;
  3318. Selection sel = lookup_field(bt, name, false);
  3319. index = sel.index[0];
  3320. elem = fv->value;
  3321. TypeAndValue tav = type_and_value_of_expr(elem);
  3322. } else {
  3323. TypeAndValue tav = type_and_value_of_expr(elem);
  3324. Selection sel = lookup_field_from_index(bt, st->fields[field_index]->Variable.field_index);
  3325. index = sel.index[0];
  3326. }
  3327. field = st->fields[index];
  3328. Type *ft = field->type;
  3329. if (!is_raw_union && !is_type_typeid(ft) && lb_is_elem_const(elem, ft)) {
  3330. continue;
  3331. }
  3332. field_expr = lb_build_expr(p, elem);
  3333. lbValue gep = {};
  3334. if (is_raw_union) {
  3335. gep = lb_emit_conv(p, comp_lit_ptr, alloc_type_pointer(ft));
  3336. } else {
  3337. gep = lb_emit_struct_ep(p, comp_lit_ptr, cast(i32)index);
  3338. }
  3339. Type *fet = field_expr.type;
  3340. GB_ASSERT(fet->kind != Type_Tuple);
  3341. // HACK TODO(bill): THIS IS A MASSIVE HACK!!!!
  3342. if (is_type_union(ft) && !are_types_identical(fet, ft) && !is_type_untyped(fet)) {
  3343. GB_ASSERT_MSG(union_variant_index(ft, fet) > 0, "%s", type_to_string(fet));
  3344. lb_emit_store_union_variant(p, gep, field_expr, fet);
  3345. } else {
  3346. lbValue fv = lb_emit_conv(p, field_expr, ft);
  3347. lb_emit_store(p, gep, fv);
  3348. }
  3349. }
  3350. }
  3351. break;
  3352. }
  3353. case Type_Map: {
  3354. if (cl->elems.count == 0) {
  3355. break;
  3356. }
  3357. {
  3358. auto args = array_make<lbValue>(permanent_allocator(), 3);
  3359. args[0] = lb_gen_map_header(p, v.addr, type);
  3360. args[1] = lb_const_int(p->module, t_int, 2*cl->elems.count);
  3361. args[2] = lb_emit_source_code_location(p, proc_name, pos);
  3362. lb_emit_runtime_call(p, "__dynamic_map_reserve", args);
  3363. }
  3364. for_array(field_index, cl->elems) {
  3365. Ast *elem = cl->elems[field_index];
  3366. ast_node(fv, FieldValue, elem);
  3367. lbValue key = lb_build_expr(p, fv->field);
  3368. lbValue value = lb_build_expr(p, fv->value);
  3369. lb_insert_dynamic_map_key_and_value(p, v, type, key, value, elem);
  3370. }
  3371. break;
  3372. }
  3373. case Type_Array: {
  3374. if (cl->elems.count > 0) {
  3375. lb_addr_store(p, v, lb_const_value(p->module, type, exact_value_compound(expr)));
  3376. auto temp_data = array_make<lbCompoundLitElemTempData>(temporary_allocator(), 0, cl->elems.count);
  3377. // NOTE(bill): Separate value, gep, store into their own chunks
  3378. for_array(i, cl->elems) {
  3379. Ast *elem = cl->elems[i];
  3380. if (elem->kind == Ast_FieldValue) {
  3381. ast_node(fv, FieldValue, elem);
  3382. if (lb_is_elem_const(fv->value, et)) {
  3383. continue;
  3384. }
  3385. if (is_ast_range(fv->field)) {
  3386. ast_node(ie, BinaryExpr, fv->field);
  3387. TypeAndValue lo_tav = ie->left->tav;
  3388. TypeAndValue hi_tav = ie->right->tav;
  3389. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  3390. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  3391. TokenKind op = ie->op.kind;
  3392. i64 lo = exact_value_to_i64(lo_tav.value);
  3393. i64 hi = exact_value_to_i64(hi_tav.value);
  3394. if (op != Token_RangeHalf) {
  3395. hi += 1;
  3396. }
  3397. lbValue value = lb_build_expr(p, fv->value);
  3398. for (i64 k = lo; k < hi; k++) {
  3399. lbCompoundLitElemTempData data = {};
  3400. data.value = value;
  3401. data.elem_index = cast(i32)k;
  3402. array_add(&temp_data, data);
  3403. }
  3404. } else {
  3405. auto tav = fv->field->tav;
  3406. GB_ASSERT(tav.mode == Addressing_Constant);
  3407. i64 index = exact_value_to_i64(tav.value);
  3408. lbValue value = lb_build_expr(p, fv->value);
  3409. lbCompoundLitElemTempData data = {};
  3410. data.value = lb_emit_conv(p, value, et);
  3411. data.expr = fv->value;
  3412. data.elem_index = cast(i32)index;
  3413. array_add(&temp_data, data);
  3414. }
  3415. } else {
  3416. if (lb_is_elem_const(elem, et)) {
  3417. continue;
  3418. }
  3419. lbCompoundLitElemTempData data = {};
  3420. data.expr = elem;
  3421. data.elem_index = cast(i32)i;
  3422. array_add(&temp_data, data);
  3423. }
  3424. }
  3425. for_array(i, temp_data) {
  3426. temp_data[i].gep = lb_emit_array_epi(p, lb_addr_get_ptr(p, v), temp_data[i].elem_index);
  3427. }
  3428. for_array(i, temp_data) {
  3429. lbValue field_expr = temp_data[i].value;
  3430. Ast *expr = temp_data[i].expr;
  3431. auto prev_hint = lb_set_copy_elision_hint(p, lb_addr(temp_data[i].gep), expr);
  3432. if (field_expr.value == nullptr) {
  3433. field_expr = lb_build_expr(p, expr);
  3434. }
  3435. Type *t = field_expr.type;
  3436. GB_ASSERT(t->kind != Type_Tuple);
  3437. lbValue ev = lb_emit_conv(p, field_expr, et);
  3438. if (!p->copy_elision_hint.used) {
  3439. temp_data[i].value = ev;
  3440. }
  3441. lb_reset_copy_elision_hint(p, prev_hint);
  3442. }
  3443. for_array(i, temp_data) {
  3444. if (temp_data[i].value.value != nullptr) {
  3445. lb_emit_store(p, temp_data[i].gep, temp_data[i].value);
  3446. }
  3447. }
  3448. }
  3449. break;
  3450. }
  3451. case Type_EnumeratedArray: {
  3452. if (cl->elems.count > 0) {
  3453. lb_addr_store(p, v, lb_const_value(p->module, type, exact_value_compound(expr)));
  3454. auto temp_data = array_make<lbCompoundLitElemTempData>(temporary_allocator(), 0, cl->elems.count);
  3455. // NOTE(bill): Separate value, gep, store into their own chunks
  3456. for_array(i, cl->elems) {
  3457. Ast *elem = cl->elems[i];
  3458. if (elem->kind == Ast_FieldValue) {
  3459. ast_node(fv, FieldValue, elem);
  3460. if (lb_is_elem_const(fv->value, et)) {
  3461. continue;
  3462. }
  3463. if (is_ast_range(fv->field)) {
  3464. ast_node(ie, BinaryExpr, fv->field);
  3465. TypeAndValue lo_tav = ie->left->tav;
  3466. TypeAndValue hi_tav = ie->right->tav;
  3467. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  3468. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  3469. TokenKind op = ie->op.kind;
  3470. i64 lo = exact_value_to_i64(lo_tav.value);
  3471. i64 hi = exact_value_to_i64(hi_tav.value);
  3472. if (op != Token_RangeHalf) {
  3473. hi += 1;
  3474. }
  3475. lbValue value = lb_build_expr(p, fv->value);
  3476. for (i64 k = lo; k < hi; k++) {
  3477. lbCompoundLitElemTempData data = {};
  3478. data.value = value;
  3479. data.elem_index = cast(i32)k;
  3480. array_add(&temp_data, data);
  3481. }
  3482. } else {
  3483. auto tav = fv->field->tav;
  3484. GB_ASSERT(tav.mode == Addressing_Constant);
  3485. i64 index = exact_value_to_i64(tav.value);
  3486. lbValue value = lb_build_expr(p, fv->value);
  3487. lbCompoundLitElemTempData data = {};
  3488. data.value = lb_emit_conv(p, value, et);
  3489. data.expr = fv->value;
  3490. data.elem_index = cast(i32)index;
  3491. array_add(&temp_data, data);
  3492. }
  3493. } else {
  3494. if (lb_is_elem_const(elem, et)) {
  3495. continue;
  3496. }
  3497. lbCompoundLitElemTempData data = {};
  3498. data.expr = elem;
  3499. data.elem_index = cast(i32)i;
  3500. array_add(&temp_data, data);
  3501. }
  3502. }
  3503. i32 index_offset = cast(i32)exact_value_to_i64(*bt->EnumeratedArray.min_value);
  3504. for_array(i, temp_data) {
  3505. i32 index = temp_data[i].elem_index - index_offset;
  3506. temp_data[i].gep = lb_emit_array_epi(p, lb_addr_get_ptr(p, v), index);
  3507. }
  3508. for_array(i, temp_data) {
  3509. lbValue field_expr = temp_data[i].value;
  3510. Ast *expr = temp_data[i].expr;
  3511. auto prev_hint = lb_set_copy_elision_hint(p, lb_addr(temp_data[i].gep), expr);
  3512. if (field_expr.value == nullptr) {
  3513. field_expr = lb_build_expr(p, expr);
  3514. }
  3515. Type *t = field_expr.type;
  3516. GB_ASSERT(t->kind != Type_Tuple);
  3517. lbValue ev = lb_emit_conv(p, field_expr, et);
  3518. if (!p->copy_elision_hint.used) {
  3519. temp_data[i].value = ev;
  3520. }
  3521. lb_reset_copy_elision_hint(p, prev_hint);
  3522. }
  3523. for_array(i, temp_data) {
  3524. if (temp_data[i].value.value != nullptr) {
  3525. lb_emit_store(p, temp_data[i].gep, temp_data[i].value);
  3526. }
  3527. }
  3528. }
  3529. break;
  3530. }
  3531. case Type_Slice: {
  3532. if (cl->elems.count > 0) {
  3533. lbValue slice = lb_const_value(p->module, type, exact_value_compound(expr));
  3534. lbValue data = lb_slice_elem(p, slice);
  3535. auto temp_data = array_make<lbCompoundLitElemTempData>(temporary_allocator(), 0, cl->elems.count);
  3536. for_array(i, cl->elems) {
  3537. Ast *elem = cl->elems[i];
  3538. if (elem->kind == Ast_FieldValue) {
  3539. ast_node(fv, FieldValue, elem);
  3540. if (lb_is_elem_const(fv->value, et)) {
  3541. continue;
  3542. }
  3543. if (is_ast_range(fv->field)) {
  3544. ast_node(ie, BinaryExpr, fv->field);
  3545. TypeAndValue lo_tav = ie->left->tav;
  3546. TypeAndValue hi_tav = ie->right->tav;
  3547. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  3548. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  3549. TokenKind op = ie->op.kind;
  3550. i64 lo = exact_value_to_i64(lo_tav.value);
  3551. i64 hi = exact_value_to_i64(hi_tav.value);
  3552. if (op != Token_RangeHalf) {
  3553. hi += 1;
  3554. }
  3555. lbValue value = lb_emit_conv(p, lb_build_expr(p, fv->value), et);
  3556. for (i64 k = lo; k < hi; k++) {
  3557. lbCompoundLitElemTempData data = {};
  3558. data.value = value;
  3559. data.elem_index = cast(i32)k;
  3560. array_add(&temp_data, data);
  3561. }
  3562. } else {
  3563. GB_ASSERT(fv->field->tav.mode == Addressing_Constant);
  3564. i64 index = exact_value_to_i64(fv->field->tav.value);
  3565. lbValue field_expr = lb_build_expr(p, fv->value);
  3566. GB_ASSERT(!is_type_tuple(field_expr.type));
  3567. lbValue ev = lb_emit_conv(p, field_expr, et);
  3568. lbCompoundLitElemTempData data = {};
  3569. data.value = ev;
  3570. data.elem_index = cast(i32)index;
  3571. array_add(&temp_data, data);
  3572. }
  3573. } else {
  3574. if (lb_is_elem_const(elem, et)) {
  3575. continue;
  3576. }
  3577. lbValue field_expr = lb_build_expr(p, elem);
  3578. GB_ASSERT(!is_type_tuple(field_expr.type));
  3579. lbValue ev = lb_emit_conv(p, field_expr, et);
  3580. lbCompoundLitElemTempData data = {};
  3581. data.value = ev;
  3582. data.elem_index = cast(i32)i;
  3583. array_add(&temp_data, data);
  3584. }
  3585. }
  3586. for_array(i, temp_data) {
  3587. temp_data[i].gep = lb_emit_ptr_offset(p, data, lb_const_int(p->module, t_int, temp_data[i].elem_index));
  3588. }
  3589. for_array(i, temp_data) {
  3590. lb_emit_store(p, temp_data[i].gep, temp_data[i].value);
  3591. }
  3592. {
  3593. lbValue count = {};
  3594. count.type = t_int;
  3595. if (lb_is_const(slice)) {
  3596. unsigned indices[1] = {1};
  3597. count.value = LLVMConstExtractValue(slice.value, indices, gb_count_of(indices));
  3598. } else {
  3599. count.value = LLVMBuildExtractValue(p->builder, slice.value, 1, "");
  3600. }
  3601. lb_fill_slice(p, v, data, count);
  3602. }
  3603. }
  3604. break;
  3605. }
  3606. case Type_DynamicArray: {
  3607. if (cl->elems.count == 0) {
  3608. break;
  3609. }
  3610. Type *et = bt->DynamicArray.elem;
  3611. lbValue size = lb_const_int(p->module, t_int, type_size_of(et));
  3612. lbValue align = lb_const_int(p->module, t_int, type_align_of(et));
  3613. i64 item_count = gb_max(cl->max_count, cl->elems.count);
  3614. {
  3615. auto args = array_make<lbValue>(permanent_allocator(), 5);
  3616. args[0] = lb_emit_conv(p, lb_addr_get_ptr(p, v), t_rawptr);
  3617. args[1] = size;
  3618. args[2] = align;
  3619. args[3] = lb_const_int(p->module, t_int, 2*item_count); // TODO(bill): Is this too much waste?
  3620. args[4] = lb_emit_source_code_location(p, proc_name, pos);
  3621. lb_emit_runtime_call(p, "__dynamic_array_reserve", args);
  3622. }
  3623. lbValue items = lb_generate_local_array(p, et, item_count);
  3624. // lbValue items = lb_generate_global_array(p->module, et, item_count, str_lit("dacl$"), cast(i64)cast(intptr)expr);
  3625. for_array(i, cl->elems) {
  3626. Ast *elem = cl->elems[i];
  3627. if (elem->kind == Ast_FieldValue) {
  3628. ast_node(fv, FieldValue, elem);
  3629. if (is_ast_range(fv->field)) {
  3630. ast_node(ie, BinaryExpr, fv->field);
  3631. TypeAndValue lo_tav = ie->left->tav;
  3632. TypeAndValue hi_tav = ie->right->tav;
  3633. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  3634. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  3635. TokenKind op = ie->op.kind;
  3636. i64 lo = exact_value_to_i64(lo_tav.value);
  3637. i64 hi = exact_value_to_i64(hi_tav.value);
  3638. if (op != Token_RangeHalf) {
  3639. hi += 1;
  3640. }
  3641. lbValue value = lb_emit_conv(p, lb_build_expr(p, fv->value), et);
  3642. for (i64 k = lo; k < hi; k++) {
  3643. lbValue ep = lb_emit_array_epi(p, items, cast(i32)k);
  3644. lb_emit_store(p, ep, value);
  3645. }
  3646. } else {
  3647. GB_ASSERT(fv->field->tav.mode == Addressing_Constant);
  3648. i64 field_index = exact_value_to_i64(fv->field->tav.value);
  3649. lbValue ev = lb_build_expr(p, fv->value);
  3650. lbValue value = lb_emit_conv(p, ev, et);
  3651. lbValue ep = lb_emit_array_epi(p, items, cast(i32)field_index);
  3652. lb_emit_store(p, ep, value);
  3653. }
  3654. } else {
  3655. lbValue value = lb_emit_conv(p, lb_build_expr(p, elem), et);
  3656. lbValue ep = lb_emit_array_epi(p, items, cast(i32)i);
  3657. lb_emit_store(p, ep, value);
  3658. }
  3659. }
  3660. {
  3661. auto args = array_make<lbValue>(permanent_allocator(), 6);
  3662. args[0] = lb_emit_conv(p, v.addr, t_rawptr);
  3663. args[1] = size;
  3664. args[2] = align;
  3665. args[3] = lb_emit_conv(p, items, t_rawptr);
  3666. args[4] = lb_const_int(p->module, t_int, item_count);
  3667. args[5] = lb_emit_source_code_location(p, proc_name, pos);
  3668. lb_emit_runtime_call(p, "__dynamic_array_append", args);
  3669. }
  3670. break;
  3671. }
  3672. case Type_Basic: {
  3673. GB_ASSERT(is_type_any(bt));
  3674. if (cl->elems.count > 0) {
  3675. lb_addr_store(p, v, lb_const_value(p->module, type, exact_value_compound(expr)));
  3676. String field_names[2] = {
  3677. str_lit("data"),
  3678. str_lit("id"),
  3679. };
  3680. Type *field_types[2] = {
  3681. t_rawptr,
  3682. t_typeid,
  3683. };
  3684. for_array(field_index, cl->elems) {
  3685. Ast *elem = cl->elems[field_index];
  3686. lbValue field_expr = {};
  3687. isize index = field_index;
  3688. if (elem->kind == Ast_FieldValue) {
  3689. ast_node(fv, FieldValue, elem);
  3690. Selection sel = lookup_field(bt, fv->field->Ident.token.string, false);
  3691. index = sel.index[0];
  3692. elem = fv->value;
  3693. } else {
  3694. TypeAndValue tav = type_and_value_of_expr(elem);
  3695. Selection sel = lookup_field(bt, field_names[field_index], false);
  3696. index = sel.index[0];
  3697. }
  3698. field_expr = lb_build_expr(p, elem);
  3699. GB_ASSERT(field_expr.type->kind != Type_Tuple);
  3700. Type *ft = field_types[index];
  3701. lbValue fv = lb_emit_conv(p, field_expr, ft);
  3702. lbValue gep = lb_emit_struct_ep(p, lb_addr_get_ptr(p, v), cast(i32)index);
  3703. lb_emit_store(p, gep, fv);
  3704. }
  3705. }
  3706. break;
  3707. }
  3708. case Type_BitSet: {
  3709. i64 sz = type_size_of(type);
  3710. if (cl->elems.count > 0 && sz > 0) {
  3711. lb_addr_store(p, v, lb_const_value(p->module, type, exact_value_compound(expr)));
  3712. lbValue lower = lb_const_value(p->module, t_int, exact_value_i64(bt->BitSet.lower));
  3713. for_array(i, cl->elems) {
  3714. Ast *elem = cl->elems[i];
  3715. GB_ASSERT(elem->kind != Ast_FieldValue);
  3716. if (lb_is_elem_const(elem, et)) {
  3717. continue;
  3718. }
  3719. lbValue expr = lb_build_expr(p, elem);
  3720. GB_ASSERT(expr.type->kind != Type_Tuple);
  3721. Type *it = bit_set_to_int(bt);
  3722. lbValue one = lb_const_value(p->module, it, exact_value_i64(1));
  3723. lbValue e = lb_emit_conv(p, expr, it);
  3724. e = lb_emit_arith(p, Token_Sub, e, lower, it);
  3725. e = lb_emit_arith(p, Token_Shl, one, e, it);
  3726. lbValue old_value = lb_emit_transmute(p, lb_addr_load(p, v), it);
  3727. lbValue new_value = lb_emit_arith(p, Token_Or, old_value, e, it);
  3728. new_value = lb_emit_transmute(p, new_value, type);
  3729. lb_addr_store(p, v, new_value);
  3730. }
  3731. }
  3732. break;
  3733. }
  3734. case Type_Matrix: {
  3735. if (cl->elems.count > 0) {
  3736. lb_addr_store(p, v, lb_const_value(p->module, type, exact_value_compound(expr)));
  3737. auto temp_data = array_make<lbCompoundLitElemTempData>(temporary_allocator(), 0, cl->elems.count);
  3738. // NOTE(bill): Separate value, gep, store into their own chunks
  3739. for_array(i, cl->elems) {
  3740. Ast *elem = cl->elems[i];
  3741. if (elem->kind == Ast_FieldValue) {
  3742. ast_node(fv, FieldValue, elem);
  3743. if (lb_is_elem_const(fv->value, et)) {
  3744. continue;
  3745. }
  3746. if (is_ast_range(fv->field)) {
  3747. ast_node(ie, BinaryExpr, fv->field);
  3748. TypeAndValue lo_tav = ie->left->tav;
  3749. TypeAndValue hi_tav = ie->right->tav;
  3750. GB_ASSERT(lo_tav.mode == Addressing_Constant);
  3751. GB_ASSERT(hi_tav.mode == Addressing_Constant);
  3752. TokenKind op = ie->op.kind;
  3753. i64 lo = exact_value_to_i64(lo_tav.value);
  3754. i64 hi = exact_value_to_i64(hi_tav.value);
  3755. if (op != Token_RangeHalf) {
  3756. hi += 1;
  3757. }
  3758. lbValue value = lb_build_expr(p, fv->value);
  3759. for (i64 k = lo; k < hi; k++) {
  3760. lbCompoundLitElemTempData data = {};
  3761. data.value = value;
  3762. data.elem_index = cast(i32)matrix_index_to_offset(bt, k);
  3763. array_add(&temp_data, data);
  3764. }
  3765. } else {
  3766. auto tav = fv->field->tav;
  3767. GB_ASSERT(tav.mode == Addressing_Constant);
  3768. i64 index = exact_value_to_i64(tav.value);
  3769. lbValue value = lb_build_expr(p, fv->value);
  3770. lbCompoundLitElemTempData data = {};
  3771. data.value = lb_emit_conv(p, value, et);
  3772. data.expr = fv->value;
  3773. data.elem_index = cast(i32)matrix_index_to_offset(bt, index);
  3774. array_add(&temp_data, data);
  3775. }
  3776. } else {
  3777. if (lb_is_elem_const(elem, et)) {
  3778. continue;
  3779. }
  3780. lbCompoundLitElemTempData data = {};
  3781. data.expr = elem;
  3782. data.elem_index = cast(i32)matrix_index_to_offset(bt, i);
  3783. array_add(&temp_data, data);
  3784. }
  3785. }
  3786. for_array(i, temp_data) {
  3787. temp_data[i].gep = lb_emit_array_epi(p, lb_addr_get_ptr(p, v), temp_data[i].elem_index);
  3788. }
  3789. for_array(i, temp_data) {
  3790. lbValue field_expr = temp_data[i].value;
  3791. Ast *expr = temp_data[i].expr;
  3792. auto prev_hint = lb_set_copy_elision_hint(p, lb_addr(temp_data[i].gep), expr);
  3793. if (field_expr.value == nullptr) {
  3794. field_expr = lb_build_expr(p, expr);
  3795. }
  3796. Type *t = field_expr.type;
  3797. GB_ASSERT(t->kind != Type_Tuple);
  3798. lbValue ev = lb_emit_conv(p, field_expr, et);
  3799. if (!p->copy_elision_hint.used) {
  3800. temp_data[i].value = ev;
  3801. }
  3802. lb_reset_copy_elision_hint(p, prev_hint);
  3803. }
  3804. for_array(i, temp_data) {
  3805. if (temp_data[i].value.value != nullptr) {
  3806. lb_emit_store(p, temp_data[i].gep, temp_data[i].value);
  3807. }
  3808. }
  3809. }
  3810. break;
  3811. }
  3812. }
  3813. return v;
  3814. case_end;
  3815. case_ast_node(tc, TypeCast, expr);
  3816. Type *type = type_of_expr(expr);
  3817. lbValue x = lb_build_expr(p, tc->expr);
  3818. lbValue e = {};
  3819. switch (tc->token.kind) {
  3820. case Token_cast:
  3821. e = lb_emit_conv(p, x, type);
  3822. break;
  3823. case Token_transmute:
  3824. e = lb_emit_transmute(p, x, type);
  3825. break;
  3826. default:
  3827. GB_PANIC("Invalid AST TypeCast");
  3828. }
  3829. lbAddr v = lb_add_local_generated(p, type, false);
  3830. lb_addr_store(p, v, e);
  3831. return v;
  3832. case_end;
  3833. case_ast_node(ac, AutoCast, expr);
  3834. return lb_build_addr(p, ac->expr);
  3835. case_end;
  3836. case_ast_node(te, TernaryIfExpr, expr);
  3837. LLVMValueRef incoming_values[2] = {};
  3838. LLVMBasicBlockRef incoming_blocks[2] = {};
  3839. GB_ASSERT(te->y != nullptr);
  3840. lbBlock *then = lb_create_block(p, "if.then");
  3841. lbBlock *done = lb_create_block(p, "if.done"); // NOTE(bill): Append later
  3842. lbBlock *else_ = lb_create_block(p, "if.else");
  3843. lbValue cond = lb_build_cond(p, te->cond, then, else_);
  3844. lb_start_block(p, then);
  3845. Type *ptr_type = alloc_type_pointer(default_type(type_of_expr(expr)));
  3846. incoming_values[0] = lb_emit_conv(p, lb_build_addr_ptr(p, te->x), ptr_type).value;
  3847. lb_emit_jump(p, done);
  3848. lb_start_block(p, else_);
  3849. incoming_values[1] = lb_emit_conv(p, lb_build_addr_ptr(p, te->y), ptr_type).value;
  3850. lb_emit_jump(p, done);
  3851. lb_start_block(p, done);
  3852. lbValue res = {};
  3853. res.value = LLVMBuildPhi(p->builder, lb_type(p->module, ptr_type), "");
  3854. res.type = ptr_type;
  3855. GB_ASSERT(p->curr_block->preds.count >= 2);
  3856. incoming_blocks[0] = p->curr_block->preds[0]->block;
  3857. incoming_blocks[1] = p->curr_block->preds[1]->block;
  3858. LLVMAddIncoming(res.value, incoming_values, incoming_blocks, 2);
  3859. return lb_addr(res);
  3860. case_end;
  3861. }
  3862. TokenPos token_pos = ast_token(expr).pos;
  3863. GB_PANIC("Unexpected address expression\n"
  3864. "\tAst: %.*s @ "
  3865. "%s\n",
  3866. LIT(ast_strings[expr->kind]),
  3867. token_pos_to_string(token_pos));
  3868. return {};
  3869. }