2
0

llvm_backend_expr.cpp 148 KB

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