llvm_backend_expr.cpp 154 KB

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