llvm_backend_expr.cpp 144 KB

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