llvm_backend_expr.cpp 147 KB

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