llvm_backend_expr.cpp 147 KB

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