llvm_backend_expr.cpp 148 KB

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