llvm_backend_proc.cpp 144 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407
  1. gb_internal LLVMValueRef lb_call_intrinsic(lbProcedure *p, const char *name, LLVMValueRef* args, unsigned arg_count, LLVMTypeRef* types, unsigned type_count) {
  2. unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name));
  3. GB_ASSERT_MSG(id != 0, "Unable to find %s", name);
  4. LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, type_count);
  5. LLVMTypeRef call_type = LLVMIntrinsicGetType(p->module->ctx, id, types, type_count);
  6. return LLVMBuildCall2(p->builder, call_type, ip, args, arg_count, "");
  7. }
  8. gb_internal void lb_mem_copy_overlapping(lbProcedure *p, lbValue dst, lbValue src, lbValue len, bool is_volatile) {
  9. dst = lb_emit_conv(p, dst, t_rawptr);
  10. src = lb_emit_conv(p, src, t_rawptr);
  11. len = lb_emit_conv(p, len, t_int);
  12. char const *name = "llvm.memmove";
  13. if (LLVMIsConstant(len.value)) {
  14. i64 const_len = cast(i64)LLVMConstIntGetSExtValue(len.value);
  15. if (const_len <= 4*build_context.int_size) {
  16. name = "llvm.memmove.inline";
  17. }
  18. }
  19. LLVMTypeRef types[3] = {
  20. lb_type(p->module, t_rawptr),
  21. lb_type(p->module, t_rawptr),
  22. lb_type(p->module, t_int)
  23. };
  24. LLVMValueRef args[4] = {
  25. dst.value,
  26. src.value,
  27. len.value,
  28. LLVMConstInt(LLVMInt1TypeInContext(p->module->ctx), 0, is_volatile)
  29. };
  30. lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types));
  31. }
  32. gb_internal void lb_mem_copy_non_overlapping(lbProcedure *p, lbValue dst, lbValue src, lbValue len, bool is_volatile) {
  33. dst = lb_emit_conv(p, dst, t_rawptr);
  34. src = lb_emit_conv(p, src, t_rawptr);
  35. len = lb_emit_conv(p, len, t_int);
  36. char const *name = "llvm.memcpy";
  37. if (LLVMIsConstant(len.value)) {
  38. i64 const_len = cast(i64)LLVMConstIntGetSExtValue(len.value);
  39. if (const_len <= 4*build_context.int_size) {
  40. name = "llvm.memcpy.inline";
  41. }
  42. }
  43. LLVMTypeRef types[3] = {
  44. lb_type(p->module, t_rawptr),
  45. lb_type(p->module, t_rawptr),
  46. lb_type(p->module, t_int)
  47. };
  48. LLVMValueRef args[4] = {
  49. dst.value,
  50. src.value,
  51. len.value,
  52. LLVMConstInt(LLVMInt1TypeInContext(p->module->ctx), 0, is_volatile) };
  53. lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types));
  54. }
  55. gb_internal lbProcedure *lb_create_procedure(lbModule *m, Entity *entity, bool ignore_body) {
  56. GB_ASSERT(entity != nullptr);
  57. GB_ASSERT(entity->kind == Entity_Procedure);
  58. // Skip codegen for unspecialized polymorphic procedures
  59. if (is_type_polymorphic(entity->type) && !entity->Procedure.is_foreign) {
  60. Type *bt = base_type(entity->type);
  61. if (bt->kind == Type_Proc && bt->Proc.is_polymorphic && !bt->Proc.is_poly_specialized) {
  62. // Do not generate code for unspecialized polymorphic procedures
  63. return nullptr;
  64. }
  65. }
  66. if (!entity->Procedure.is_foreign) {
  67. if ((entity->flags & EntityFlag_ProcBodyChecked) == 0) {
  68. GB_PANIC("%.*s :: %s (was parapoly: %d %d)", LIT(entity->token.string), type_to_string(entity->type), is_type_polymorphic(entity->type, true), is_type_polymorphic(entity->type, false));
  69. }
  70. }
  71. String link_name = {};
  72. if (ignore_body) {
  73. lbModule *other_module = lb_module_of_entity(m->gen, entity, m);
  74. link_name = lb_get_entity_name(other_module, entity);
  75. } else {
  76. link_name = lb_get_entity_name(m, entity);
  77. }
  78. {
  79. StringHashKey key = string_hash_string(link_name);
  80. lbValue *found = string_map_get(&m->members, key);
  81. if (found) {
  82. lb_add_entity(m, entity, *found);
  83. return string_map_must_get(&m->procedures, key);
  84. }
  85. }
  86. lbProcedure *p = gb_alloc_item(permanent_allocator(), lbProcedure);
  87. p->module = m;
  88. entity->code_gen_module = m;
  89. entity->code_gen_procedure = p;
  90. p->entity = entity;
  91. p->name = link_name;
  92. DeclInfo *decl = entity->decl_info;
  93. ast_node(pl, ProcLit, decl->proc_lit);
  94. Type *pt = base_type(entity->type);
  95. GB_ASSERT(pt->kind == Type_Proc);
  96. p->type = entity->type;
  97. p->type_expr = decl->type_expr;
  98. p->body = pl->body;
  99. p->inlining = pl->inlining;
  100. p->is_foreign = entity->Procedure.is_foreign;
  101. p->is_export = entity->Procedure.is_export;
  102. p->is_entry_point = false;
  103. gbAllocator a = heap_allocator();
  104. p->children.allocator = a;
  105. p->defer_stmts.allocator = a;
  106. p->blocks.allocator = a;
  107. p->branch_blocks.allocator = a;
  108. p->context_stack.allocator = a;
  109. p->scope_stack.allocator = a;
  110. p->asan_stack_locals.allocator = a;
  111. // map_init(&p->selector_values, 0);
  112. // map_init(&p->selector_addr, 0);
  113. // map_init(&p->tuple_fix_map, 0);
  114. if (p->entity != nullptr && p->entity->Procedure.uses_branch_location) {
  115. p->uses_branch_location = true;
  116. }
  117. if (p->is_foreign) {
  118. lb_add_foreign_library_path(p->module, entity->Procedure.foreign_library);
  119. }
  120. LLVMTypeRef func_type = lb_get_procedure_raw_type(m, p->type);
  121. {
  122. TEMPORARY_ALLOCATOR_GUARD();
  123. char *c_link_name = alloc_cstring(temporary_allocator(), p->name);
  124. p->value = LLVMAddFunction(m->mod, c_link_name, func_type);
  125. }
  126. lb_ensure_abi_function_type(m, p);
  127. lb_add_function_type_attributes(p->value, p->abi_function_type, p->abi_function_type->calling_convention);
  128. if (pt->Proc.diverging) {
  129. lb_add_attribute_to_proc(m, p->value, "noreturn");
  130. }
  131. if (pt->Proc.calling_convention == ProcCC_Naked) {
  132. lb_add_attribute_to_proc(m, p->value, "naked");
  133. }
  134. if (!entity->Procedure.is_foreign && build_context.disable_red_zone) {
  135. lb_add_attribute_to_proc(m, p->value, "noredzone");
  136. }
  137. switch (p->inlining) {
  138. case ProcInlining_inline:
  139. lb_add_attribute_to_proc(m, p->value, "alwaysinline");
  140. break;
  141. case ProcInlining_no_inline:
  142. lb_add_attribute_to_proc(m, p->value, "noinline");
  143. break;
  144. default:
  145. if (build_context.internal_no_inline) {
  146. lb_add_attribute_to_proc(m, p->value, "noinline");
  147. break;
  148. }
  149. }
  150. switch (entity->Procedure.optimization_mode) {
  151. case ProcedureOptimizationMode_None:
  152. lb_add_attribute_to_proc(m, p->value, "optnone");
  153. lb_add_attribute_to_proc(m, p->value, "noinline");
  154. break;
  155. case ProcedureOptimizationMode_FavorSize:
  156. lb_add_attribute_to_proc(m, p->value, "optsize");
  157. break;
  158. }
  159. if (pt->Proc.enable_target_feature.len != 0) {
  160. gbString feature_str = gb_string_make(temporary_allocator(), "");
  161. String_Iterator it = {pt->Proc.enable_target_feature, 0};
  162. bool first = true;
  163. for (;;) {
  164. String str = string_split_iterator(&it, ',');
  165. if (str == "") break;
  166. if (!first) {
  167. feature_str = gb_string_appendc(feature_str, ",");
  168. }
  169. first = false;
  170. feature_str = gb_string_appendc(feature_str, "+");
  171. feature_str = gb_string_append_length(feature_str, str.text, str.len);
  172. }
  173. lb_add_attribute_to_proc_with_string(m, p->value, make_string_c("target-features"), make_string_c(feature_str));
  174. }
  175. if (entity->flags & EntityFlag_Cold) {
  176. lb_add_attribute_to_proc(m, p->value, "cold");
  177. }
  178. if (p->is_export) {
  179. LLVMSetLinkage(p->value, LLVMDLLExportLinkage);
  180. LLVMSetDLLStorageClass(p->value, LLVMDLLExportStorageClass);
  181. LLVMSetVisibility(p->value, LLVMDefaultVisibility);
  182. lb_set_wasm_export_attributes(p->value, p->name);
  183. } else if (!p->is_foreign) {
  184. if (USE_SEPARATE_MODULES) {
  185. LLVMSetLinkage(p->value, LLVMExternalLinkage);
  186. } else {
  187. LLVMSetLinkage(p->value, LLVMInternalLinkage);
  188. // NOTE(bill): if a procedure is defined in package runtime and uses a custom link name,
  189. // then it is very likely it is required by LLVM and thus cannot have internal linkage
  190. if (entity->pkg != nullptr && entity->pkg->kind == Package_Runtime && p->body != nullptr) {
  191. GB_ASSERT(entity->kind == Entity_Procedure);
  192. String link_name = entity->Procedure.link_name;
  193. if (entity->flags & EntityFlag_CustomLinkName &&
  194. link_name != "") {
  195. if (string_starts_with(link_name, str_lit("__"))) {
  196. LLVMSetLinkage(p->value, LLVMExternalLinkage);
  197. } else {
  198. LLVMSetLinkage(p->value, LLVMInternalLinkage);
  199. }
  200. }
  201. }
  202. }
  203. }
  204. lb_set_linkage_from_entity_flags(p->module, p->value, entity->flags);
  205. if (p->is_foreign) {
  206. lb_set_wasm_procedure_import_attributes(p->value, entity, p->name);
  207. }
  208. // NOTE(bill): offset==0 is the return value
  209. isize offset = 1;
  210. if (pt->Proc.return_by_pointer) {
  211. offset = 2;
  212. }
  213. isize parameter_index = 0;
  214. if (pt->Proc.param_count) {
  215. TypeTuple *params = &pt->Proc.params->Tuple;
  216. for (isize i = 0; i < pt->Proc.param_count; i++) {
  217. Entity *e = params->variables[i];
  218. if (e->kind != Entity_Variable) {
  219. continue;
  220. }
  221. if (i+1 == params->variables.count && pt->Proc.c_vararg) {
  222. continue;
  223. }
  224. if (e->flags&EntityFlag_NoAlias) {
  225. lb_add_proc_attribute_at_index(p, offset+parameter_index, "noalias");
  226. }
  227. if (e->flags&EntityFlag_NoCapture) {
  228. if (is_type_internally_pointer_like(e->type)) {
  229. lb_add_proc_attribute_at_index(p, offset+parameter_index, "nocapture");
  230. }
  231. }
  232. parameter_index += 1;
  233. }
  234. }
  235. if (ignore_body) {
  236. p->body = nullptr;
  237. LLVMSetLinkage(p->value, LLVMExternalLinkage);
  238. }
  239. if (m->debug_builder) { // Debug Information
  240. Type *bt = base_type(p->type);
  241. unsigned line = cast(unsigned)entity->token.pos.line;
  242. LLVMMetadataRef scope = nullptr;
  243. LLVMMetadataRef file = nullptr;
  244. LLVMMetadataRef type = nullptr;
  245. scope = p->module->debug_compile_unit;
  246. type = lb_debug_type_internal_proc(m, bt);
  247. Ast *ident = entity->identifier.load();
  248. if (entity->file != nullptr) {
  249. file = lb_get_llvm_metadata(m, entity->file);
  250. scope = file;
  251. } else if (ident != nullptr && ident->file_id != 0) {
  252. file = lb_get_llvm_metadata(m, ident->file());
  253. scope = file;
  254. } else if (entity->scope != nullptr) {
  255. file = lb_get_llvm_metadata(m, entity->scope->file);
  256. scope = file;
  257. }
  258. GB_ASSERT_MSG(file != nullptr, "%.*s", LIT(entity->token.string));
  259. // LLVMBool is_local_to_unit = !entity->Procedure.is_export;
  260. LLVMBool is_local_to_unit = false;
  261. LLVMBool is_definition = p->body != nullptr;
  262. unsigned scope_line = line;
  263. u32 flags = LLVMDIFlagStaticMember;
  264. LLVMBool is_optimized = false;
  265. if (bt->Proc.diverging) {
  266. flags |= LLVMDIFlagNoReturn;
  267. }
  268. if (p->body == nullptr) {
  269. flags |= LLVMDIFlagPrototyped;
  270. is_optimized = false;
  271. }
  272. if (p->body != nullptr) {
  273. // String debug_name = entity->token.string.text;
  274. String debug_name = p->name;
  275. p->debug_info = LLVMDIBuilderCreateFunction(m->debug_builder, scope,
  276. cast(char const *)debug_name.text, debug_name.len,
  277. cast(char const *)p->name.text, p->name.len,
  278. file, line, type,
  279. is_local_to_unit, is_definition,
  280. scope_line, cast(LLVMDIFlags)flags, is_optimized
  281. );
  282. GB_ASSERT(p->debug_info != nullptr);
  283. LLVMSetSubprogram(p->value, p->debug_info);
  284. lb_set_llvm_metadata(m, p, p->debug_info);
  285. }
  286. }
  287. if (p->body && entity->pkg && ((entity->pkg->kind == Package_Normal) || (entity->pkg->kind == Package_Init))) {
  288. if (build_context.sanitizer_flags & SanitizerFlag_Address && !entity->Procedure.no_sanitize_address) {
  289. lb_add_attribute_to_proc(m, p->value, "sanitize_address");
  290. }
  291. if (build_context.sanitizer_flags & SanitizerFlag_Memory && !entity->Procedure.no_sanitize_memory) {
  292. lb_add_attribute_to_proc(m, p->value, "sanitize_memory");
  293. }
  294. if (build_context.sanitizer_flags & SanitizerFlag_Thread) {
  295. lb_add_attribute_to_proc(m, p->value, "sanitize_thread");
  296. }
  297. }
  298. if (p->body && entity->Procedure.has_instrumentation) {
  299. Entity *instrumentation_enter = m->info->instrumentation_enter_entity;
  300. Entity *instrumentation_exit = m->info->instrumentation_exit_entity;
  301. if (instrumentation_enter && instrumentation_exit) {
  302. String enter = lb_get_entity_name(m, instrumentation_enter);
  303. String exit = lb_get_entity_name(m, instrumentation_exit);
  304. lb_add_attribute_to_proc_with_string(m, p->value, make_string_c("instrument-function-entry"), enter);
  305. lb_add_attribute_to_proc_with_string(m, p->value, make_string_c("instrument-function-exit"), exit);
  306. }
  307. }
  308. lbValue proc_value = {p->value, p->type};
  309. lb_add_entity(m, entity, proc_value);
  310. lb_add_member(m, p->name, proc_value);
  311. lb_add_procedure_value(m, p);
  312. return p;
  313. }
  314. gb_internal lbProcedure *lb_create_dummy_procedure(lbModule *m, String link_name, Type *type) {
  315. {
  316. lbValue *found = string_map_get(&m->members, link_name);
  317. GB_ASSERT_MSG(found == nullptr, "failed to create dummy procedure for: %.*s", LIT(link_name));
  318. }
  319. lbProcedure *p = gb_alloc_item(permanent_allocator(), lbProcedure);
  320. p->module = m;
  321. p->name = link_name;
  322. p->type = type;
  323. p->type_expr = nullptr;
  324. p->body = nullptr;
  325. p->tags = 0;
  326. p->inlining = ProcInlining_none;
  327. p->is_foreign = false;
  328. p->is_export = false;
  329. p->is_entry_point = false;
  330. gbAllocator a = permanent_allocator();
  331. p->children.allocator = a;
  332. p->defer_stmts.allocator = a;
  333. p->blocks.allocator = a;
  334. p->branch_blocks.allocator = a;
  335. p->context_stack.allocator = a;
  336. p->asan_stack_locals.allocator = a;
  337. map_init(&p->tuple_fix_map, 0);
  338. char *c_link_name = alloc_cstring(permanent_allocator(), p->name);
  339. LLVMTypeRef func_type = lb_get_procedure_raw_type(m, p->type);
  340. p->value = LLVMAddFunction(m->mod, c_link_name, func_type);
  341. Type *pt = p->type;
  342. lbCallingConventionKind cc_kind = lbCallingConvention_C;
  343. if (!is_arch_wasm()) {
  344. cc_kind = lb_calling_convention_map[pt->Proc.calling_convention];
  345. }
  346. LLVMSetFunctionCallConv(p->value, cc_kind);
  347. lbValue proc_value = {p->value, p->type};
  348. lb_add_member(m, p->name, proc_value);
  349. lb_add_procedure_value(m, p);
  350. // NOTE(bill): offset==0 is the return value
  351. isize offset = 1;
  352. if (pt->Proc.return_by_pointer) {
  353. lb_add_proc_attribute_at_index(p, 1, "sret");
  354. lb_add_proc_attribute_at_index(p, 1, "noalias");
  355. offset = 2;
  356. }
  357. isize parameter_index = 0;
  358. if (pt->Proc.calling_convention == ProcCC_Odin) {
  359. lb_add_proc_attribute_at_index(p, offset+parameter_index, "noalias");
  360. lb_add_proc_attribute_at_index(p, offset+parameter_index, "nonnull");
  361. lb_add_proc_attribute_at_index(p, offset+parameter_index, "nocapture");
  362. }
  363. return p;
  364. }
  365. // gb_internal lbValue lb_value_param(lbProcedure *p, Entity *e, Type *abi_type, i32 index, lbParamPasskind *kind_) {
  366. // lbParamPasskind kind = lbParamPass_Value;
  367. // if (e != nullptr && !are_types_identical(abi_type, e->type)) {
  368. // if (is_type_pointer(abi_type)) {
  369. // GB_ASSERT(e->kind == Entity_Variable);
  370. // Type *av = core_type(type_deref(abi_type));
  371. // if (are_types_identical(av, core_type(e->type))) {
  372. // kind = lbParamPass_Pointer;
  373. // if (e->flags&EntityFlag_Value) {
  374. // kind = lbParamPass_ConstRef;
  375. // }
  376. // } else {
  377. // kind = lbParamPass_BitCast;
  378. // }
  379. // } else if (is_type_integer(abi_type)) {
  380. // kind = lbParamPass_Integer;
  381. // } else if (abi_type == t_llvm_bool) {
  382. // kind = lbParamPass_Value;
  383. // } else if (is_type_boolean(abi_type)) {
  384. // kind = lbParamPass_Integer;
  385. // } else if (is_type_simd_vector(abi_type)) {
  386. // kind = lbParamPass_BitCast;
  387. // } else if (is_type_float(abi_type)) {
  388. // kind = lbParamPass_BitCast;
  389. // } else if (is_type_tuple(abi_type)) {
  390. // kind = lbParamPass_Tuple;
  391. // } else if (is_type_proc(abi_type)) {
  392. // kind = lbParamPass_Value;
  393. // } else {
  394. // GB_PANIC("Invalid abi type pass kind %s", type_to_string(abi_type));
  395. // }
  396. // }
  397. // if (kind_) *kind_ = kind;
  398. // lbValue res = {};
  399. // res.value = LLVMGetParam(p->value, cast(unsigned)index);
  400. // res.type = abi_type;
  401. // return res;
  402. // }
  403. gb_internal void lb_start_block(lbProcedure *p, lbBlock *b) {
  404. GB_ASSERT(b != nullptr);
  405. if (!b->appended) {
  406. b->appended = true;
  407. LLVMAppendExistingBasicBlock(p->value, b->block);
  408. }
  409. LLVMPositionBuilderAtEnd(p->builder, b->block);
  410. p->curr_block = b;
  411. }
  412. gb_internal void lb_set_debug_position_to_procedure_begin(lbProcedure *p) {
  413. if (p->debug_info == nullptr) {
  414. return;
  415. }
  416. TokenPos pos = {};
  417. if (p->body != nullptr) {
  418. pos = ast_token(p->body).pos;
  419. } else if (p->type_expr != nullptr) {
  420. pos = ast_token(p->type_expr).pos;
  421. } else if (p->entity != nullptr) {
  422. pos = p->entity->token.pos;
  423. }
  424. if (pos.file_id != 0) {
  425. LLVMSetCurrentDebugLocation2(p->builder, lb_debug_location_from_token_pos(p, pos));
  426. }
  427. }
  428. gb_internal void lb_set_debug_position_to_procedure_end(lbProcedure *p) {
  429. if (p->debug_info == nullptr) {
  430. return;
  431. }
  432. TokenPos pos = {};
  433. if (p->body != nullptr) {
  434. pos = ast_end_token(p->body).pos;
  435. } else if (p->type_expr != nullptr) {
  436. pos = ast_end_token(p->type_expr).pos;
  437. } else if (p->entity != nullptr) {
  438. pos = p->entity->token.pos;
  439. }
  440. if (pos.file_id != 0) {
  441. LLVMSetCurrentDebugLocation2(p->builder, lb_debug_location_from_token_pos(p, pos));
  442. }
  443. }
  444. gb_internal void lb_begin_procedure_body(lbProcedure *p) {
  445. DeclInfo *decl = decl_info_of_entity(p->entity);
  446. if (decl != nullptr) {
  447. for_array(i, decl->labels) {
  448. BlockLabel bl = decl->labels[i];
  449. lbBranchBlocks bb = {bl.label, nullptr, nullptr};
  450. array_add(&p->branch_blocks, bb);
  451. }
  452. }
  453. p->builder = LLVMCreateBuilderInContext(p->module->ctx);
  454. p->decl_block = lb_create_block(p, "decls", true);
  455. p->entry_block = lb_create_block(p, "entry", true);
  456. lb_start_block(p, p->entry_block);
  457. map_init(&p->direct_parameters);
  458. p->variadic_reuses.allocator = heap_allocator();
  459. GB_ASSERT(p->type != nullptr);
  460. lb_ensure_abi_function_type(p->module, p);
  461. if (p->type->Proc.calling_convention == ProcCC_Odin) {
  462. lb_push_context_onto_stack_from_implicit_parameter(p);
  463. }
  464. {
  465. lbFunctionType *ft = p->abi_function_type;
  466. unsigned param_offset = 0;
  467. lbValue return_ptr_value = {};
  468. if (ft->ret.kind == lbArg_Indirect) {
  469. // NOTE(bill): this must be parameter 0
  470. String name = str_lit("agg.result");
  471. if (ft->multiple_return_original_type &&
  472. p->type->Proc.has_named_results) {
  473. auto const &variables = p->type->Proc.results->Tuple.variables;
  474. Entity *e = variables[variables.count-1];
  475. if (!is_blank_ident(e->token)) {
  476. name = e->token.string;
  477. }
  478. }
  479. Type *return_ptr_type = reduce_tuple_to_single_type(p->type->Proc.results);
  480. bool split_returns = ft->multiple_return_original_type != nullptr;
  481. if (split_returns) {
  482. GB_ASSERT(is_type_tuple(return_ptr_type));
  483. auto const &variables = return_ptr_type->Tuple.variables;
  484. return_ptr_type = variables[variables.count-1]->type;
  485. }
  486. Type *ptr_type = alloc_type_pointer(return_ptr_type);
  487. Entity *e = alloc_entity_param(nullptr, make_token_ident(name), ptr_type, false, false);
  488. e->flags |= EntityFlag_NoAlias;
  489. return_ptr_value.value = LLVMGetParam(p->value, 0);
  490. LLVMSetValueName2(return_ptr_value.value, cast(char const *)name.text, name.len);
  491. return_ptr_value.type = ptr_type;
  492. p->return_ptr = lb_addr(return_ptr_value);
  493. lb_add_entity(p->module, e, return_ptr_value);
  494. param_offset += 1;
  495. }
  496. if (p->type->Proc.params != nullptr) {
  497. TypeTuple *params = &p->type->Proc.params->Tuple;
  498. unsigned raw_input_parameters_count = LLVMCountParams(p->value);
  499. p->raw_input_parameters = array_make<LLVMValueRef>(permanent_allocator(), raw_input_parameters_count);
  500. LLVMGetParams(p->value, p->raw_input_parameters.data);
  501. bool is_odin_cc = is_calling_convention_odin(ft->calling_convention);
  502. unsigned param_index = 0;
  503. for_array(i, params->variables) {
  504. Entity *e = params->variables[i];
  505. if (e->kind != Entity_Variable) {
  506. continue;
  507. }
  508. lbArgType *arg_type = &ft->args[param_index];
  509. defer (param_index += 1);
  510. if (arg_type->kind == lbArg_Ignore) {
  511. // Even though it is an ignored argument, it might still be referenced in the
  512. // body.
  513. lbValue dummy = lb_add_local_generated(p, e->type, false).addr;
  514. lb_add_entity(p->module, e, dummy);
  515. } else if (arg_type->kind == lbArg_Direct) {
  516. if (e->token.string.len != 0 && !is_blank_ident(e->token.string)) {
  517. LLVMTypeRef param_type = lb_type(p->module, e->type);
  518. LLVMValueRef original_value = LLVMGetParam(p->value, param_offset+param_index);
  519. LLVMValueRef value = OdinLLVMBuildTransmute(p, original_value, param_type);
  520. lbValue param = {};
  521. param.value = value;
  522. param.type = e->type;
  523. map_set(&p->direct_parameters, e, param);
  524. lbValue ptr = lb_address_from_load_or_generate_local(p, param);
  525. GB_ASSERT(LLVMIsAAllocaInst(ptr.value));
  526. lb_add_entity(p->module, e, ptr);
  527. lb_add_debug_param_variable(p, ptr.value, e->type, e->token, param_index+1, p->curr_block);
  528. }
  529. } else if (arg_type->kind == lbArg_Indirect) {
  530. if (e->token.string.len != 0 && !is_blank_ident(e->token.string)) {
  531. i64 sz = type_size_of(e->type);
  532. bool do_callee_copy = false;
  533. if (is_odin_cc) {
  534. do_callee_copy = sz <= 16;
  535. if (build_context.internal_by_value) {
  536. do_callee_copy = true;
  537. }
  538. }
  539. lbValue ptr = {};
  540. ptr.value = LLVMGetParam(p->value, param_offset+param_index);
  541. ptr.type = alloc_type_pointer(e->type);
  542. if (do_callee_copy) {
  543. lbValue new_ptr = lb_add_local_generated(p, e->type, false).addr;
  544. lb_mem_copy_non_overlapping(p, new_ptr, ptr, lb_const_int(p->module, t_uint, sz));
  545. ptr = new_ptr;
  546. }
  547. lb_add_entity(p->module, e, ptr);
  548. lb_add_debug_param_variable(p, ptr.value, e->type, e->token, param_index+1, p->decl_block);
  549. }
  550. }
  551. }
  552. }
  553. if (p->type->Proc.has_named_results) {
  554. GB_ASSERT(p->type->Proc.result_count > 0);
  555. TypeTuple *results = &p->type->Proc.results->Tuple;
  556. for_array(i, results->variables) {
  557. Entity *e = results->variables[i];
  558. GB_ASSERT(e->kind == Entity_Variable);
  559. if (e->token.string != "") {
  560. GB_ASSERT(!is_blank_ident(e->token));
  561. lbAddr res = {};
  562. if (p->entity && p->entity->decl_info &&
  563. p->entity->decl_info->defer_use_checked &&
  564. p->entity->decl_info->defer_used == 0) {
  565. // NOTE(bill): this is a bodge to get around the issue of the problem BELOW
  566. // We check to see if we ever use a defer statement ever within a procedure and if it
  567. // if it never happens, see if you can possibly do take the return value pointer
  568. //
  569. // NOTE(bill): this could be buggy in that I have missed a case where `defer` was used
  570. //
  571. // TODO(bill): This could be optimized to check to see where a `defer` only uses
  572. // the variable in question
  573. bool has_return_ptr = p->return_ptr.addr.value != nullptr;
  574. lbValue ptr = {};
  575. if (ft->multiple_return_original_type != nullptr) {
  576. isize the_offset = -1;
  577. if (i+1 < results->variables.count) {
  578. the_offset = cast(isize)param_offset + ft->original_arg_count + i;
  579. } else if (has_return_ptr) {
  580. GB_ASSERT(i+1 == results->variables.count);
  581. the_offset = 0;
  582. }
  583. if (the_offset >= 0) {
  584. lbValue ptr = {};
  585. ptr.value = LLVMGetParam(p->value, cast(unsigned)the_offset);
  586. ptr.type = alloc_type_pointer(e->type);
  587. }
  588. } else if (has_return_ptr) {
  589. lbValue ptr = p->return_ptr.addr;
  590. if (results->variables.count > 1) {
  591. ptr = lb_emit_tuple_ep(p, ptr, cast(i32)i);
  592. }
  593. GB_ASSERT(is_type_pointer(ptr.type));
  594. GB_ASSERT(are_types_identical(type_deref(ptr.type), e->type));
  595. }
  596. if (ptr.value != nullptr) {
  597. lb_add_entity(p->module, e, ptr);
  598. lb_add_debug_local_variable(p, ptr.value, e->type, e->token);
  599. // NOTE(bill): no need to zero on the callee side as it is zeroed on the caller side
  600. res = lb_addr(ptr);
  601. }
  602. }
  603. if (res.addr.type == nullptr) {
  604. // NOTE(bill): Don't even bother trying to optimize this with the return ptr value
  605. // This will violate the defer rules if you do:
  606. // foo :: proc() -> (x, y: T) {
  607. // defer x = ... // defer is executed after the `defer`
  608. // return // the values returned should be zeroed
  609. // }
  610. // NOTE(bill): REALLY, don't even bother.
  611. //
  612. // IMPORTANT NOTE(bill): REALLY, don't even bother!!!!!!
  613. res = lb_add_local(p, e->type, e);
  614. }
  615. if (e->Variable.param_value.kind != ParameterValue_Invalid) {
  616. GB_ASSERT(e->Variable.param_value.kind != ParameterValue_Location);
  617. GB_ASSERT(e->Variable.param_value.kind != ParameterValue_Expression);
  618. lbValue c = lb_handle_param_value(p, e->type, e->Variable.param_value, nullptr, nullptr);
  619. lb_addr_store(p, res, c);
  620. }
  621. }
  622. }
  623. }
  624. }
  625. lb_set_debug_position_to_procedure_begin(p);
  626. if (p->debug_info != nullptr) {
  627. if (p->context_stack.count != 0) {
  628. lbBlock *prev_block = p->curr_block;
  629. p->curr_block = p->decl_block;
  630. lb_add_debug_context_variable(p, lb_find_or_generate_context_ptr(p));
  631. p->curr_block = prev_block;
  632. }
  633. }
  634. }
  635. gb_internal void lb_end_procedure_body(lbProcedure *p) {
  636. lb_set_debug_position_to_procedure_begin(p);
  637. LLVMPositionBuilderAtEnd(p->builder, p->decl_block->block);
  638. LLVMBuildBr(p->builder, p->entry_block->block);
  639. LLVMPositionBuilderAtEnd(p->builder, p->curr_block->block);
  640. LLVMValueRef instr = nullptr;
  641. // Make sure there is a "ret void" at the end of a procedure with no return type
  642. if (p->type->Proc.result_count == 0) {
  643. instr = LLVMGetLastInstruction(p->curr_block->block);
  644. if (!lb_is_instr_terminating(instr)) {
  645. lb_emit_defer_stmts(p, lbDeferExit_Return, nullptr, p->body);
  646. lb_set_debug_position_to_procedure_end(p);
  647. LLVMBuildRetVoid(p->builder);
  648. }
  649. }
  650. LLVMBasicBlockRef first_block = LLVMGetFirstBasicBlock(p->value);
  651. LLVMBasicBlockRef block = nullptr;
  652. // Make sure every block terminates, and if not, make it unreachable
  653. for (block = first_block; block != nullptr; block = LLVMGetNextBasicBlock(block)) {
  654. instr = LLVMGetLastInstruction(block);
  655. if (instr == nullptr || !lb_is_instr_terminating(instr)) {
  656. LLVMPositionBuilderAtEnd(p->builder, block);
  657. LLVMBuildUnreachable(p->builder);
  658. }
  659. }
  660. p->curr_block = nullptr;
  661. p->state_flags = 0;
  662. LLVMDisposeBuilder(p->builder);
  663. }
  664. gb_internal void lb_build_nested_proc(lbProcedure *p, AstProcLit *pd, Entity *e) {
  665. GB_ASSERT(pd->body != nullptr);
  666. lbModule *m = p->module;
  667. if (e->min_dep_count.load(std::memory_order_relaxed) == 0) {
  668. // NOTE(bill): Nothing depends upon it so doesn't need to be built
  669. return;
  670. }
  671. // NOTE(bill): Generate a new name
  672. // parent.name-guid
  673. String original_name = e->token.string;
  674. String pd_name = original_name;
  675. if (e->Procedure.link_name.len > 0) {
  676. pd_name = e->Procedure.link_name;
  677. }
  678. isize name_len = p->name.len + 1 + pd_name.len + 1 + 10 + 1;
  679. char *name_text = gb_alloc_array(permanent_allocator(), char, name_len);
  680. i32 guid = cast(i32)p->children.count;
  681. name_len = gb_snprintf(name_text, name_len, "%.*s" ABI_PKG_NAME_SEPARATOR "%.*s-%d", LIT(p->name), LIT(pd_name), guid);
  682. String name = make_string(cast(u8 *)name_text, name_len-1);
  683. e->Procedure.link_name = name;
  684. lbProcedure *nested_proc = lb_create_procedure(p->module, e);
  685. if (nested_proc == nullptr) {
  686. // This is an unspecialized polymorphic procedure, skip codegen
  687. return;
  688. }
  689. e->code_gen_procedure = nested_proc;
  690. lbValue value = {};
  691. value.value = nested_proc->value;
  692. value.type = nested_proc->type;
  693. lb_add_entity(m, e, value);
  694. array_add(&p->children, nested_proc);
  695. mpsc_enqueue(&m->procedures_to_generate, nested_proc);
  696. }
  697. gb_internal Array<lbValue> lb_value_to_array(lbProcedure *p, gbAllocator const &allocator, lbValue value) {
  698. Array<lbValue> array = {};
  699. Type *t = base_type(value.type);
  700. if (t == nullptr) {
  701. // Do nothing
  702. } else if (is_type_tuple(t)) {
  703. array = array_make<lbValue>(allocator, 0, t->Tuple.variables.count);
  704. lb_append_tuple_values(p, &array, value);
  705. } else {
  706. array = array_make<lbValue>(allocator, 1);
  707. array[0] = value;
  708. }
  709. return array;
  710. }
  711. gb_internal lbValue lb_emit_call_internal(lbProcedure *p, lbValue value, lbValue return_ptr, Array<lbValue> const &processed_args, Type *abi_rt, lbAddr context_ptr, ProcInlining inlining) {
  712. GB_ASSERT(p->module->ctx == LLVMGetTypeContext(LLVMTypeOf(value.value)));
  713. unsigned arg_count = cast(unsigned)processed_args.count;
  714. if (return_ptr.value != nullptr) {
  715. arg_count += 1;
  716. }
  717. if (context_ptr.addr.value != nullptr) {
  718. arg_count += 1;
  719. }
  720. LLVMValueRef *args = gb_alloc_array(permanent_allocator(), LLVMValueRef, arg_count);
  721. isize arg_index = 0;
  722. if (return_ptr.value != nullptr) {
  723. args[arg_index++] = return_ptr.value;
  724. }
  725. for_array(i, processed_args) {
  726. lbValue arg = processed_args[i];
  727. if (is_type_proc(arg.type)) {
  728. arg.value = LLVMBuildPointerCast(p->builder, arg.value, lb_type(p->module, arg.type), "");
  729. }
  730. args[arg_index++] = arg.value;
  731. }
  732. if (context_ptr.addr.value != nullptr) {
  733. LLVMValueRef cp = context_ptr.addr.value;
  734. cp = LLVMBuildPointerCast(p->builder, cp, lb_type(p->module, t_rawptr), "");
  735. args[arg_index++] = cp;
  736. }
  737. GB_ASSERT(arg_index == arg_count);
  738. LLVMBasicBlockRef curr_block = LLVMGetInsertBlock(p->builder);
  739. GB_ASSERT(curr_block != p->decl_block->block);
  740. {
  741. Type *proc_type = base_type(value.type);
  742. GB_ASSERT(proc_type->kind == Type_Proc);
  743. LLVMTypeRef fnp = lb_type_internal_for_procedures_raw(p->module, proc_type);
  744. LLVMTypeRef ftp = LLVMPointerType(fnp, 0);
  745. LLVMValueRef fn = value.value;
  746. if (!lb_is_type_kind(LLVMTypeOf(value.value), LLVMFunctionTypeKind)) {
  747. fn = LLVMBuildPointerCast(p->builder, fn, ftp, "");
  748. }
  749. GB_ASSERT_MSG(lb_is_type_kind(fnp, LLVMFunctionTypeKind), "%s", LLVMPrintTypeToString(fnp));
  750. lbFunctionType *ft = map_must_get(&p->module->function_type_map, base_type(value.type));
  751. {
  752. unsigned param_count = LLVMCountParamTypes(fnp);
  753. GB_ASSERT(arg_count >= param_count);
  754. LLVMTypeRef *param_types = gb_alloc_array(temporary_allocator(), LLVMTypeRef, param_count);
  755. LLVMGetParamTypes(fnp, param_types);
  756. for (unsigned i = 0; i < param_count; i++) {
  757. LLVMTypeRef param_type = param_types[i];
  758. LLVMTypeRef arg_type = LLVMTypeOf(args[i]);
  759. if (LB_USE_NEW_PASS_SYSTEM &&
  760. arg_type != param_type) {
  761. LLVMTypeKind arg_kind = LLVMGetTypeKind(arg_type);
  762. LLVMTypeKind param_kind = LLVMGetTypeKind(param_type);
  763. if (arg_kind == param_kind) {
  764. if (arg_kind == LLVMPointerTypeKind) {
  765. // NOTE(bill): LLVM's newer `ptr` only type system seems to fail at times
  766. // I don't know why...
  767. args[i] = LLVMBuildPointerCast(p->builder, args[i], param_type, "");
  768. arg_type = param_type;
  769. continue;
  770. }
  771. }
  772. }
  773. GB_ASSERT_MSG(
  774. arg_type == param_type,
  775. "Parameter types do not match: %s != %s, argument: %s\n\t%s",
  776. LLVMPrintTypeToString(arg_type),
  777. LLVMPrintTypeToString(param_type),
  778. LLVMPrintValueToString(args[i]),
  779. LLVMPrintTypeToString(fnp)
  780. );
  781. }
  782. }
  783. LLVMValueRef ret = LLVMBuildCall2(p->builder, fnp, fn, args, arg_count, "");
  784. auto llvm_cc = lb_calling_convention_map[proc_type->Proc.calling_convention];
  785. LLVMSetInstructionCallConv(ret, llvm_cc);
  786. LLVMAttributeIndex param_offset = LLVMAttributeIndex_FirstArgIndex;
  787. if (return_ptr.value != nullptr) {
  788. param_offset += 1;
  789. LLVMAddCallSiteAttribute(ret, 1, lb_create_enum_attribute_with_type(p->module->ctx, "sret", LLVMTypeOf(args[0])));
  790. }
  791. for_array(i, ft->args) {
  792. LLVMAttributeRef attribute = ft->args[i].attribute;
  793. if (attribute != nullptr) {
  794. LLVMAddCallSiteAttribute(ret, param_offset + cast(LLVMAttributeIndex)i, attribute);
  795. }
  796. }
  797. switch (inlining) {
  798. case ProcInlining_none:
  799. break;
  800. case ProcInlining_inline:
  801. LLVMAddCallSiteAttribute(ret, LLVMAttributeIndex_FunctionIndex, lb_create_enum_attribute(p->module->ctx, "alwaysinline"));
  802. break;
  803. case ProcInlining_no_inline:
  804. LLVMAddCallSiteAttribute(ret, LLVMAttributeIndex_FunctionIndex, lb_create_enum_attribute(p->module->ctx, "noinline"));
  805. break;
  806. }
  807. lbValue res = {};
  808. res.value = ret;
  809. res.type = abi_rt;
  810. return res;
  811. }
  812. }
  813. gb_internal lbValue lb_lookup_runtime_procedure(lbModule *m, String const &name) {
  814. AstPackage *pkg = m->info->runtime_package;
  815. Entity *e = scope_lookup_current(pkg->scope, name);
  816. GB_ASSERT_MSG(e != nullptr, "Runtime procedure not found: %s", name);
  817. return lb_find_procedure_value_from_entity(m, e);
  818. }
  819. gb_internal lbValue lb_emit_runtime_call(lbProcedure *p, char const *c_name, Array<lbValue> const &args) {
  820. String name = make_string_c(c_name);
  821. lbValue proc = lb_lookup_runtime_procedure(p->module, name);
  822. return lb_emit_call(p, proc, args);
  823. }
  824. gb_internal lbValue lb_emit_conjugate(lbProcedure *p, lbValue val, Type *type) {
  825. lbValue res = {};
  826. Type *t = val.type;
  827. if (is_type_complex(t)) {
  828. res = lb_addr_get_ptr(p, lb_add_local_generated(p, type, false));
  829. lbValue real = lb_emit_struct_ev(p, val, 0);
  830. lbValue imag = lb_emit_struct_ev(p, val, 1);
  831. imag = lb_emit_unary_arith(p, Token_Sub, imag, imag.type);
  832. lb_emit_store(p, lb_emit_struct_ep(p, res, 0), real);
  833. lb_emit_store(p, lb_emit_struct_ep(p, res, 1), imag);
  834. } else if (is_type_quaternion(t)) {
  835. // @QuaternionLayout
  836. res = lb_addr_get_ptr(p, lb_add_local_generated(p, type, false));
  837. lbValue real = lb_emit_struct_ev(p, val, 3);
  838. lbValue imag = lb_emit_struct_ev(p, val, 0);
  839. lbValue jmag = lb_emit_struct_ev(p, val, 1);
  840. lbValue kmag = lb_emit_struct_ev(p, val, 2);
  841. imag = lb_emit_unary_arith(p, Token_Sub, imag, imag.type);
  842. jmag = lb_emit_unary_arith(p, Token_Sub, jmag, jmag.type);
  843. kmag = lb_emit_unary_arith(p, Token_Sub, kmag, kmag.type);
  844. lb_emit_store(p, lb_emit_struct_ep(p, res, 3), real);
  845. lb_emit_store(p, lb_emit_struct_ep(p, res, 0), imag);
  846. lb_emit_store(p, lb_emit_struct_ep(p, res, 1), jmag);
  847. lb_emit_store(p, lb_emit_struct_ep(p, res, 2), kmag);
  848. } else if (is_type_array_like(t)) {
  849. res = lb_addr_get_ptr(p, lb_add_local_generated(p, type, true));
  850. Type *elem_type = base_array_type(t);
  851. i64 count = get_array_type_count(t);
  852. for (i64 i = 0; i < count; i++) {
  853. lbValue dst = lb_emit_array_epi(p, res, i);
  854. lbValue elem = lb_emit_struct_ev(p, val, cast(i32)i);
  855. elem = lb_emit_conjugate(p, elem, elem_type);
  856. lb_emit_store(p, dst, elem);
  857. }
  858. } else if (is_type_matrix(t)) {
  859. Type *mt = base_type(t);
  860. GB_ASSERT(mt->kind == Type_Matrix);
  861. Type *elem_type = mt->Matrix.elem;
  862. res = lb_addr_get_ptr(p, lb_add_local_generated(p, type, true));
  863. for (i64 j = 0; j < mt->Matrix.column_count; j++) {
  864. for (i64 i = 0; i < mt->Matrix.row_count; i++) {
  865. lbValue dst = lb_emit_matrix_epi(p, res, i, j);
  866. lbValue elem = lb_emit_matrix_ev(p, val, i, j);
  867. elem = lb_emit_conjugate(p, elem, elem_type);
  868. lb_emit_store(p, dst, elem);
  869. }
  870. }
  871. }
  872. return lb_emit_load(p, res);
  873. }
  874. gb_internal lbValue lb_emit_call(lbProcedure *p, lbValue value, Array<lbValue> const &args, ProcInlining inlining) {
  875. lbModule *m = p->module;
  876. Type *pt = base_type(value.type);
  877. GB_ASSERT(pt->kind == Type_Proc);
  878. Type *results = pt->Proc.results;
  879. lbAddr context_ptr = {};
  880. if (pt->Proc.calling_convention == ProcCC_Odin) {
  881. context_ptr = lb_find_or_generate_context_ptr(p);
  882. }
  883. defer (if (pt->Proc.diverging) {
  884. LLVMBuildUnreachable(p->builder);
  885. });
  886. bool is_c_vararg = pt->Proc.c_vararg;
  887. isize param_count = pt->Proc.param_count;
  888. if (is_c_vararg) {
  889. GB_ASSERT(param_count-1 <= args.count);
  890. param_count -= 1;
  891. } else {
  892. GB_ASSERT_MSG(param_count == args.count, "%td == %td (%s)", param_count, args.count, LLVMPrintValueToString(value.value));
  893. }
  894. lbValue result = {};
  895. isize ignored_args = 0;
  896. auto processed_args = array_make<lbValue>(permanent_allocator(), 0, args.count);
  897. {
  898. bool is_odin_cc = is_calling_convention_odin(pt->Proc.calling_convention);
  899. lbFunctionType *ft = lb_get_function_type(m, pt);
  900. bool return_by_pointer = ft->ret.kind == lbArg_Indirect;
  901. bool split_returns = ft->multiple_return_original_type != nullptr;
  902. unsigned param_index = 0;
  903. for (isize i = 0; i < param_count; i++) {
  904. Entity *e = pt->Proc.params->Tuple.variables[i];
  905. if (e->kind != Entity_Variable) {
  906. continue;
  907. }
  908. GB_ASSERT(e->flags & EntityFlag_Param);
  909. Type *original_type = e->type;
  910. lbArgType *arg = &ft->args[param_index];
  911. if (arg->kind == lbArg_Ignore) {
  912. param_index += 1;
  913. ignored_args += 1;
  914. continue;
  915. }
  916. lbValue x = lb_emit_conv(p, args[i], original_type);
  917. LLVMTypeRef xt = lb_type(p->module, x.type);
  918. if (arg->kind == lbArg_Direct) {
  919. LLVMTypeRef abi_type = arg->cast_type;
  920. if (!abi_type) {
  921. abi_type = arg->type;
  922. }
  923. if (xt == abi_type) {
  924. array_add(&processed_args, x);
  925. } else {
  926. x.value = OdinLLVMBuildTransmute(p, x.value, abi_type);
  927. array_add(&processed_args, x);
  928. }
  929. } else if (arg->kind == lbArg_Indirect) {
  930. lbValue ptr = {};
  931. if (arg->is_byval) {
  932. if (is_odin_cc) {
  933. if (are_types_identical(original_type, t_source_code_location)) {
  934. ptr = lb_address_from_load_or_generate_local(p, x);
  935. // } else {
  936. // ptr = lb_address_from_load_if_readonly_parameter(p, x);
  937. }
  938. }
  939. if (ptr.value == nullptr) {
  940. ptr = lb_copy_value_to_ptr(p, x, original_type, arg->byval_alignment);
  941. }
  942. } else if (is_odin_cc) {
  943. // NOTE(bill): Odin parameters are immutable so the original value can be passed if possible
  944. // i.e. `T const &` in C++
  945. if (LLVMIsConstant(x.value)) {
  946. // NOTE(bill): if the value is already constant, then just it as a global variable
  947. // and pass it by pointer
  948. lbAddr addr = lb_add_global_generated_from_procedure(p, original_type, x);
  949. lb_make_global_private_const(addr);
  950. ptr = addr.addr;
  951. } else {
  952. ptr = lb_address_from_load_or_generate_local(p, x);
  953. }
  954. } else {
  955. ptr = lb_copy_value_to_ptr(p, x, original_type, 16);
  956. }
  957. array_add(&processed_args, ptr);
  958. }
  959. param_index += 1;
  960. }
  961. if (is_c_vararg) {
  962. for (isize i = processed_args.count; i < args.count; i++) {
  963. array_add(&processed_args, args[i]);
  964. }
  965. }
  966. Type *rt = reduce_tuple_to_single_type(results);
  967. Type *original_rt = rt;
  968. if (split_returns) {
  969. GB_ASSERT(rt->kind == Type_Tuple);
  970. for (isize j = 0; j < rt->Tuple.variables.count-1; j++) {
  971. Type *partial_return_type = rt->Tuple.variables[j]->type;
  972. lbValue partial_return_ptr = lb_add_local(p, partial_return_type, nullptr, true, false).addr;
  973. array_add(&processed_args, partial_return_ptr);
  974. }
  975. rt = reduce_tuple_to_single_type(rt->Tuple.variables[rt->Tuple.variables.count-1]->type);
  976. }
  977. if (return_by_pointer) {
  978. lbValue return_ptr = lb_add_local_generated(p, rt, true).addr;
  979. lb_emit_call_internal(p, value, return_ptr, processed_args, nullptr, context_ptr, inlining);
  980. result = lb_emit_load(p, return_ptr);
  981. } else if (rt != nullptr) {
  982. result = lb_emit_call_internal(p, value, {}, processed_args, rt, context_ptr, inlining);
  983. if (ft->ret.cast_type) {
  984. result.value = OdinLLVMBuildTransmute(p, result.value, ft->ret.cast_type);
  985. }
  986. result.value = OdinLLVMBuildTransmute(p, result.value, ft->ret.type);
  987. result.type = rt;
  988. if (LLVMTypeOf(result.value) == LLVMInt1TypeInContext(p->module->ctx)) {
  989. result.type = t_llvm_bool;
  990. }
  991. if (!is_type_tuple(rt)) {
  992. result = lb_emit_conv(p, result, rt);
  993. }
  994. } else {
  995. lb_emit_call_internal(p, value, {}, processed_args, nullptr, context_ptr, inlining);
  996. }
  997. if (original_rt != rt) {
  998. GB_ASSERT(split_returns);
  999. GB_ASSERT(is_type_tuple(original_rt));
  1000. // IMPORTANT NOTE(bill, 2022-11-24)
  1001. // result_ptr is a dummy value which is only used to reference a tuple
  1002. // value for the "tuple-fix"
  1003. //
  1004. // The reason for the fake stack allocation is to have a unique pointer
  1005. // for the value to be used as a key within the procedure itself
  1006. lbValue result_ptr = lb_add_local_generated(p, original_rt, false).addr;
  1007. isize ret_count = original_rt->Tuple.variables.count;
  1008. auto tuple_fix_values = slice_make<lbValue>(permanent_allocator(), ret_count);
  1009. auto tuple_geps = slice_make<lbValue>(permanent_allocator(), ret_count);
  1010. isize offset = ft->original_arg_count - ignored_args;
  1011. for (isize j = 0; j < ret_count-1; j++) {
  1012. lbValue ret_arg_ptr = processed_args[offset + j];
  1013. lbValue ret_arg = lb_emit_load(p, ret_arg_ptr);
  1014. tuple_fix_values[j] = ret_arg;
  1015. }
  1016. tuple_fix_values[ret_count-1] = result;
  1017. #if 0
  1018. for (isize j = 0; j < ret_count; j++) {
  1019. tuple_geps[j] = lb_emit_struct_ep(p, result_ptr, cast(i32)j);
  1020. }
  1021. for (isize j = 0; j < ret_count; j++) {
  1022. lb_emit_store(p, tuple_geps[j], tuple_fix_values[j]);
  1023. }
  1024. #endif
  1025. result = lb_emit_load(p, result_ptr);
  1026. lbTupleFix tf = {tuple_fix_values};
  1027. map_set(&p->tuple_fix_map, result_ptr.value, tf);
  1028. map_set(&p->tuple_fix_map, result.value, tf);
  1029. }
  1030. }
  1031. LLVMValueRef the_proc_value = value.value;
  1032. if (LLVMIsAConstantExpr(the_proc_value)) {
  1033. // NOTE(bill): it's a bit cast
  1034. the_proc_value = LLVMGetOperand(the_proc_value, 0);
  1035. }
  1036. Entity **found = map_get(&p->module->procedure_values, the_proc_value);
  1037. if (found != nullptr) {
  1038. Entity *e = *found;
  1039. if (e != nullptr && entity_has_deferred_procedure(e)) {
  1040. DeferredProcedureKind kind = e->Procedure.deferred_procedure.kind;
  1041. Entity *deferred_entity = e->Procedure.deferred_procedure.entity;
  1042. lbValue deferred = lb_find_procedure_value_from_entity(p->module, deferred_entity);
  1043. bool by_ptr = false;
  1044. auto in_args = args;
  1045. Array<lbValue> result_as_args = {};
  1046. switch (kind) {
  1047. case DeferredProcedure_none:
  1048. break;
  1049. case DeferredProcedure_in_by_ptr:
  1050. by_ptr = true;
  1051. /*fallthrough*/
  1052. case DeferredProcedure_in:
  1053. result_as_args = array_clone(heap_allocator(), in_args);
  1054. break;
  1055. case DeferredProcedure_out_by_ptr:
  1056. by_ptr = true;
  1057. /*fallthrough*/
  1058. case DeferredProcedure_out:
  1059. result_as_args = lb_value_to_array(p, heap_allocator(), result);
  1060. break;
  1061. case DeferredProcedure_in_out_by_ptr:
  1062. by_ptr = true;
  1063. /*fallthrough*/
  1064. case DeferredProcedure_in_out:
  1065. {
  1066. auto out_args = lb_value_to_array(p, heap_allocator(), result);
  1067. array_init(&result_as_args, heap_allocator(), in_args.count + out_args.count);
  1068. array_copy(&result_as_args, in_args, 0);
  1069. array_copy(&result_as_args, out_args, in_args.count);
  1070. }
  1071. break;
  1072. }
  1073. if (by_ptr) {
  1074. for_array(i, result_as_args) {
  1075. lbValue arg_ptr = lb_address_from_load_or_generate_local(p, result_as_args[i]);
  1076. result_as_args[i] = arg_ptr;
  1077. }
  1078. }
  1079. lb_add_defer_proc(p, p->scope_index, deferred, result_as_args);
  1080. }
  1081. }
  1082. return result;
  1083. }
  1084. gb_internal LLVMValueRef llvm_splat_int(i64 count, LLVMTypeRef type, i64 value, bool is_signed=false) {
  1085. LLVMValueRef v = LLVMConstInt(type, value, is_signed);
  1086. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, count);
  1087. for (i64 i = 0; i < count; i++) {
  1088. values[i] = v;
  1089. }
  1090. return LLVMConstVector(values, cast(unsigned)count);
  1091. }
  1092. gb_internal lbValue lb_build_builtin_simd_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, BuiltinProcId builtin_id) {
  1093. ast_node(ce, CallExpr, expr);
  1094. lbModule *m = p->module;
  1095. lbValue res = {};
  1096. res.type = tv.type;
  1097. switch (builtin_id) {
  1098. case BuiltinProc_simd_indices: {
  1099. Type *type = base_type(res.type);
  1100. GB_ASSERT(type->kind == Type_SimdVector);
  1101. Type *elem = type->SimdVector.elem;
  1102. i64 count = type->SimdVector.count;
  1103. LLVMValueRef *scalars = gb_alloc_array(temporary_allocator(), LLVMValueRef, count);
  1104. for (i64 i = 0; i < count; i++) {
  1105. scalars[i] = lb_const_value(m, elem, exact_value_i64(i)).value;
  1106. }
  1107. res.value = LLVMConstVector(scalars, cast(unsigned)count);
  1108. return res;
  1109. }
  1110. }
  1111. lbValue arg0 = {}; if (ce->args.count > 0) arg0 = lb_build_expr(p, ce->args[0]);
  1112. lbValue arg1 = {}; if (ce->args.count > 1) arg1 = lb_build_expr(p, ce->args[1]);
  1113. lbValue arg2 = {}; if (ce->args.count > 2) arg2 = lb_build_expr(p, ce->args[2]);
  1114. Type *elem = base_array_type(arg0.type);
  1115. bool is_float = is_type_float(elem);
  1116. bool is_signed = !is_type_unsigned(elem);
  1117. LLVMOpcode op_code = cast(LLVMOpcode)0;
  1118. switch (builtin_id) {
  1119. case BuiltinProc_simd_add:
  1120. case BuiltinProc_simd_sub:
  1121. case BuiltinProc_simd_mul:
  1122. case BuiltinProc_simd_div:
  1123. case BuiltinProc_simd_rem:
  1124. if (is_float) {
  1125. switch (builtin_id) {
  1126. case BuiltinProc_simd_add: op_code = LLVMFAdd; break;
  1127. case BuiltinProc_simd_sub: op_code = LLVMFSub; break;
  1128. case BuiltinProc_simd_mul: op_code = LLVMFMul; break;
  1129. case BuiltinProc_simd_div: op_code = LLVMFDiv; break;
  1130. }
  1131. } else {
  1132. switch (builtin_id) {
  1133. case BuiltinProc_simd_add: op_code = LLVMAdd; break;
  1134. case BuiltinProc_simd_sub: op_code = LLVMSub; break;
  1135. case BuiltinProc_simd_mul: op_code = LLVMMul; break;
  1136. case BuiltinProc_simd_div:
  1137. if (is_signed) {
  1138. op_code = LLVMSDiv;
  1139. } else {
  1140. op_code = LLVMUDiv;
  1141. }
  1142. break;
  1143. case BuiltinProc_simd_rem:
  1144. if (is_signed) {
  1145. op_code = LLVMSRem;
  1146. } else {
  1147. op_code = LLVMURem;
  1148. }
  1149. break;
  1150. }
  1151. }
  1152. if (op_code) {
  1153. res.value = LLVMBuildBinOp(p->builder, op_code, arg0.value, arg1.value, "");
  1154. return res;
  1155. }
  1156. break;
  1157. case BuiltinProc_simd_shl: // Odin logic
  1158. case BuiltinProc_simd_shr: // Odin logic
  1159. case BuiltinProc_simd_shl_masked: // C logic
  1160. case BuiltinProc_simd_shr_masked: // C logic
  1161. {
  1162. i64 sz = type_size_of(elem);
  1163. GB_ASSERT(arg0.type->kind == Type_SimdVector);
  1164. i64 count = arg0.type->SimdVector.count;
  1165. Type *elem1 = base_array_type(arg1.type);
  1166. bool is_masked = false;
  1167. switch (builtin_id) {
  1168. case BuiltinProc_simd_shl: op_code = LLVMShl; is_masked = false; break;
  1169. case BuiltinProc_simd_shr: op_code = is_signed ? LLVMAShr : LLVMLShr; is_masked = false; break;
  1170. case BuiltinProc_simd_shl_masked: op_code = LLVMShl; is_masked = true; break;
  1171. case BuiltinProc_simd_shr_masked: op_code = is_signed ? LLVMAShr : LLVMLShr; is_masked = true; break;
  1172. }
  1173. if (op_code) {
  1174. LLVMValueRef bits = llvm_splat_int(count, lb_type(m, elem1), sz*8 - 1);
  1175. if (is_masked) {
  1176. // C logic
  1177. LLVMValueRef shift = LLVMBuildAnd(p->builder, arg1.value, bits, "");
  1178. res.value = LLVMBuildBinOp(p->builder, op_code, arg0.value, shift, "");
  1179. } else {
  1180. // Odin logic
  1181. LLVMValueRef zero = lb_const_nil(m, arg1.type).value;
  1182. LLVMValueRef mask = LLVMBuildICmp(p->builder, LLVMIntULE, arg1.value, bits, "");
  1183. LLVMValueRef shift = LLVMBuildBinOp(p->builder, op_code, arg0.value, arg1.value, "");
  1184. res.value = LLVMBuildSelect(p->builder, mask, shift, zero, "");
  1185. }
  1186. return res;
  1187. }
  1188. }
  1189. break;
  1190. case BuiltinProc_simd_bit_and:
  1191. case BuiltinProc_simd_bit_or:
  1192. case BuiltinProc_simd_bit_xor:
  1193. case BuiltinProc_simd_bit_and_not:
  1194. switch (builtin_id) {
  1195. case BuiltinProc_simd_bit_and: op_code = LLVMAnd; break;
  1196. case BuiltinProc_simd_bit_or: op_code = LLVMOr; break;
  1197. case BuiltinProc_simd_bit_xor: op_code = LLVMXor; break;
  1198. case BuiltinProc_simd_bit_and_not:
  1199. op_code = LLVMAnd;
  1200. arg1.value = LLVMBuildNot(p->builder, arg1.value, "");
  1201. break;
  1202. }
  1203. if (op_code) {
  1204. res.value = LLVMBuildBinOp(p->builder, op_code, arg0.value, arg1.value, "");
  1205. return res;
  1206. }
  1207. break;
  1208. case BuiltinProc_simd_neg:
  1209. if (is_float) {
  1210. res.value = LLVMBuildFNeg(p->builder, arg0.value, "");
  1211. } else {
  1212. res.value = LLVMBuildNeg(p->builder, arg0.value, "");
  1213. }
  1214. return res;
  1215. case BuiltinProc_simd_abs:
  1216. if (is_float) {
  1217. LLVMValueRef pos = arg0.value;
  1218. LLVMValueRef neg = LLVMBuildFNeg(p->builder, pos, "");
  1219. LLVMValueRef cond = LLVMBuildFCmp(p->builder, LLVMRealOGT, pos, neg, "");
  1220. res.value = LLVMBuildSelect(p->builder, cond, pos, neg, "");
  1221. } else {
  1222. LLVMValueRef pos = arg0.value;
  1223. LLVMValueRef neg = LLVMBuildNeg(p->builder, pos, "");
  1224. LLVMValueRef cond = LLVMBuildICmp(p->builder, is_signed ? LLVMIntSGT : LLVMIntUGT, pos, neg, "");
  1225. res.value = LLVMBuildSelect(p->builder, cond, pos, neg, "");
  1226. }
  1227. return res;
  1228. case BuiltinProc_simd_min:
  1229. if (is_float) {
  1230. return lb_emit_min(p, res.type, arg0, arg1);
  1231. } else {
  1232. LLVMValueRef cond = LLVMBuildICmp(p->builder, is_signed ? LLVMIntSLT : LLVMIntULT, arg0.value, arg1.value, "");
  1233. res.value = LLVMBuildSelect(p->builder, cond, arg0.value, arg1.value, "");
  1234. }
  1235. return res;
  1236. case BuiltinProc_simd_max:
  1237. if (is_float) {
  1238. return lb_emit_max(p, res.type, arg0, arg1);
  1239. } else {
  1240. LLVMValueRef cond = LLVMBuildICmp(p->builder, is_signed ? LLVMIntSGT : LLVMIntUGT, arg0.value, arg1.value, "");
  1241. res.value = LLVMBuildSelect(p->builder, cond, arg0.value, arg1.value, "");
  1242. }
  1243. return res;
  1244. case BuiltinProc_simd_lanes_eq:
  1245. case BuiltinProc_simd_lanes_ne:
  1246. case BuiltinProc_simd_lanes_lt:
  1247. case BuiltinProc_simd_lanes_le:
  1248. case BuiltinProc_simd_lanes_gt:
  1249. case BuiltinProc_simd_lanes_ge:
  1250. if (is_float) {
  1251. LLVMRealPredicate pred = cast(LLVMRealPredicate)0;
  1252. switch (builtin_id) {
  1253. case BuiltinProc_simd_lanes_eq: pred = LLVMRealOEQ; break;
  1254. case BuiltinProc_simd_lanes_ne: pred = LLVMRealUNE; break;
  1255. case BuiltinProc_simd_lanes_lt: pred = LLVMRealOLT; break;
  1256. case BuiltinProc_simd_lanes_le: pred = LLVMRealOLE; break;
  1257. case BuiltinProc_simd_lanes_gt: pred = LLVMRealOGT; break;
  1258. case BuiltinProc_simd_lanes_ge: pred = LLVMRealOGE; break;
  1259. }
  1260. if (pred) {
  1261. res.value = LLVMBuildFCmp(p->builder, pred, arg0.value, arg1.value, "");
  1262. res.value = LLVMBuildSExtOrBitCast(p->builder, res.value, lb_type(m, tv.type), "");
  1263. return res;
  1264. }
  1265. } else {
  1266. LLVMIntPredicate pred = cast(LLVMIntPredicate)0;
  1267. switch (builtin_id) {
  1268. case BuiltinProc_simd_lanes_eq: pred = LLVMIntEQ; break;
  1269. case BuiltinProc_simd_lanes_ne: pred = LLVMIntNE; break;
  1270. case BuiltinProc_simd_lanes_lt: pred = is_signed ? LLVMIntSLT :LLVMIntULT; break;
  1271. case BuiltinProc_simd_lanes_le: pred = is_signed ? LLVMIntSLE :LLVMIntULE; break;
  1272. case BuiltinProc_simd_lanes_gt: pred = is_signed ? LLVMIntSGT :LLVMIntUGT; break;
  1273. case BuiltinProc_simd_lanes_ge: pred = is_signed ? LLVMIntSGE :LLVMIntUGE; break;
  1274. }
  1275. if (pred) {
  1276. res.value = LLVMBuildICmp(p->builder, pred, arg0.value, arg1.value, "");
  1277. res.value = LLVMBuildSExtOrBitCast(p->builder, res.value, lb_type(m, tv.type), "");
  1278. return res;
  1279. }
  1280. }
  1281. break;
  1282. case BuiltinProc_simd_extract:
  1283. res.value = LLVMBuildExtractElement(p->builder, arg0.value, arg1.value, "");
  1284. return res;
  1285. case BuiltinProc_simd_replace:
  1286. res.value = LLVMBuildInsertElement(p->builder, arg0.value, arg2.value, arg1.value, "");
  1287. return res;
  1288. case BuiltinProc_simd_reduce_add_bisect:
  1289. case BuiltinProc_simd_reduce_mul_bisect:
  1290. {
  1291. GB_ASSERT(arg0.type->kind == Type_SimdVector);
  1292. i64 num_elems = arg0.type->SimdVector.count;
  1293. LLVMValueRef *indices = gb_alloc_array(temporary_allocator(), LLVMValueRef, num_elems);
  1294. for (i64 i = 0; i < num_elems; i++) {
  1295. indices[i] = lb_const_int(m, t_uint, cast(u64)i).value;
  1296. }
  1297. switch (builtin_id) {
  1298. case BuiltinProc_simd_reduce_add_bisect: op_code = is_float ? LLVMFAdd : LLVMAdd; break;
  1299. case BuiltinProc_simd_reduce_mul_bisect: op_code = is_float ? LLVMFMul : LLVMMul; break;
  1300. }
  1301. LLVMValueRef remaining = arg0.value;
  1302. i64 num_remaining = num_elems;
  1303. while (num_remaining > 1) {
  1304. num_remaining /= 2;
  1305. LLVMValueRef left_indices = LLVMConstVector(&indices[0], cast(unsigned)num_remaining);
  1306. LLVMValueRef left_value = LLVMBuildShuffleVector(p->builder, remaining, remaining, left_indices, "");
  1307. LLVMValueRef right_indices = LLVMConstVector(&indices[num_remaining], cast(unsigned)num_remaining);
  1308. LLVMValueRef right_value = LLVMBuildShuffleVector(p->builder, remaining, remaining, right_indices, "");
  1309. remaining = LLVMBuildBinOp(p->builder, op_code, left_value, right_value, "");
  1310. }
  1311. res.value = LLVMBuildExtractElement(p->builder, remaining, indices[0], "");
  1312. return res;
  1313. }
  1314. case BuiltinProc_simd_reduce_add_ordered:
  1315. case BuiltinProc_simd_reduce_mul_ordered:
  1316. {
  1317. LLVMTypeRef llvm_elem = lb_type(m, elem);
  1318. LLVMValueRef args[2] = {};
  1319. isize args_count = 0;
  1320. char const *name = nullptr;
  1321. switch (builtin_id) {
  1322. case BuiltinProc_simd_reduce_add_ordered:
  1323. if (is_float) {
  1324. name = "llvm.vector.reduce.fadd";
  1325. args[args_count++] = LLVMConstReal(llvm_elem, 0.0);
  1326. } else {
  1327. name = "llvm.vector.reduce.add";
  1328. }
  1329. break;
  1330. case BuiltinProc_simd_reduce_mul_ordered:
  1331. if (is_float) {
  1332. name = "llvm.vector.reduce.fmul";
  1333. args[args_count++] = LLVMConstReal(llvm_elem, 1.0);
  1334. } else {
  1335. name = "llvm.vector.reduce.mul";
  1336. }
  1337. break;
  1338. }
  1339. args[args_count++] = arg0.value;
  1340. LLVMTypeRef types[1] = {lb_type(p->module, arg0.type)};
  1341. res.value = lb_call_intrinsic(p, name, args, cast(unsigned)args_count, types, gb_count_of(types));
  1342. return res;
  1343. }
  1344. case BuiltinProc_simd_reduce_add_pairs:
  1345. case BuiltinProc_simd_reduce_mul_pairs:
  1346. {
  1347. GB_ASSERT(arg0.type->kind == Type_SimdVector);
  1348. i64 num_elems = arg0.type->SimdVector.count;
  1349. LLVMValueRef *indices = gb_alloc_array(temporary_allocator(), LLVMValueRef, num_elems);
  1350. for (i64 i = 0; i < num_elems/2; i++) {
  1351. indices[i] = lb_const_int(m, t_uint, cast(u64)(2*i)).value;
  1352. indices[i+num_elems/2] = lb_const_int(m, t_uint, cast(u64)(2*i+1)).value;
  1353. }
  1354. switch (builtin_id) {
  1355. case BuiltinProc_simd_reduce_add_pairs: op_code = is_float ? LLVMFAdd : LLVMAdd; break;
  1356. case BuiltinProc_simd_reduce_mul_pairs: op_code = is_float ? LLVMFMul : LLVMMul; break;
  1357. }
  1358. LLVMValueRef remaining = arg0.value;
  1359. i64 num_remaining = num_elems;
  1360. while (num_remaining > 1) {
  1361. num_remaining /= 2;
  1362. LLVMValueRef left_indices = LLVMConstVector(&indices[0], cast(unsigned)num_remaining);
  1363. LLVMValueRef left_value = LLVMBuildShuffleVector(p->builder, remaining, remaining, left_indices, "");
  1364. LLVMValueRef right_indices = LLVMConstVector(&indices[num_elems/2], cast(unsigned)num_remaining);
  1365. LLVMValueRef right_value = LLVMBuildShuffleVector(p->builder, remaining, remaining, right_indices, "");
  1366. remaining = LLVMBuildBinOp(p->builder, op_code, left_value, right_value, "");
  1367. }
  1368. res.value = LLVMBuildExtractElement(p->builder, remaining, indices[0], "");
  1369. return res;
  1370. }
  1371. case BuiltinProc_simd_reduce_min:
  1372. case BuiltinProc_simd_reduce_max:
  1373. case BuiltinProc_simd_reduce_and:
  1374. case BuiltinProc_simd_reduce_or:
  1375. case BuiltinProc_simd_reduce_xor:
  1376. {
  1377. char const *name = nullptr;
  1378. switch (builtin_id) {
  1379. case BuiltinProc_simd_reduce_min:
  1380. if (is_float) {
  1381. name = "llvm.vector.reduce.fmin";
  1382. } else if (is_signed) {
  1383. name = "llvm.vector.reduce.smin";
  1384. } else {
  1385. name = "llvm.vector.reduce.umin";
  1386. }
  1387. break;
  1388. case BuiltinProc_simd_reduce_max:
  1389. if (is_float) {
  1390. name = "llvm.vector.reduce.fmax";
  1391. } else if (is_signed) {
  1392. name = "llvm.vector.reduce.smax";
  1393. } else {
  1394. name = "llvm.vector.reduce.umax";
  1395. }
  1396. break;
  1397. case BuiltinProc_simd_reduce_and: name = "llvm.vector.reduce.and"; break;
  1398. case BuiltinProc_simd_reduce_or: name = "llvm.vector.reduce.or"; break;
  1399. case BuiltinProc_simd_reduce_xor: name = "llvm.vector.reduce.xor"; break;
  1400. }
  1401. LLVMTypeRef types[1] = { lb_type(p->module, arg0.type) };
  1402. LLVMValueRef args[1] = { arg0.value };
  1403. res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types));
  1404. return res;
  1405. }
  1406. case BuiltinProc_simd_reduce_any:
  1407. case BuiltinProc_simd_reduce_all:
  1408. {
  1409. char const *name = nullptr;
  1410. switch (builtin_id) {
  1411. case BuiltinProc_simd_reduce_any: name = "llvm.vector.reduce.or"; break;
  1412. case BuiltinProc_simd_reduce_all: name = "llvm.vector.reduce.and"; break;
  1413. }
  1414. LLVMTypeRef types[1] = { lb_type(p->module, arg0.type) };
  1415. LLVMValueRef args[1] = { arg0.value };
  1416. res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types));
  1417. return res;
  1418. }
  1419. case BuiltinProc_simd_extract_lsbs:
  1420. case BuiltinProc_simd_extract_msbs:
  1421. {
  1422. Type *vt = arg0.type;
  1423. GB_ASSERT(vt->kind == Type_SimdVector);
  1424. i64 elem_bits = 8*type_size_of(elem);
  1425. i64 num_elems = get_array_type_count(vt);
  1426. LLVMValueRef broadcast_value = arg0.value;
  1427. if (builtin_id == BuiltinProc_simd_extract_msbs) {
  1428. LLVMTypeRef word_type = lb_type(m, elem);
  1429. LLVMValueRef shift_value = llvm_splat_int(num_elems, word_type, elem_bits - 1);
  1430. broadcast_value = LLVMBuildAShr(p->builder, broadcast_value, shift_value, "");
  1431. }
  1432. LLVMTypeRef bitvec_type = LLVMVectorType(LLVMInt1TypeInContext(m->ctx), (unsigned)num_elems);
  1433. LLVMValueRef bitvec_value = LLVMBuildTrunc(p->builder, broadcast_value, bitvec_type, "");
  1434. LLVMTypeRef mask_type = LLVMIntTypeInContext(m->ctx, (unsigned)num_elems);
  1435. LLVMValueRef mask_value = LLVMBuildBitCast(p->builder, bitvec_value, mask_type, "");
  1436. LLVMTypeRef result_type = lb_type(m, res.type);
  1437. res.value = LLVMBuildZExtOrBitCast(p->builder, mask_value, result_type, "");
  1438. return res;
  1439. }
  1440. case BuiltinProc_simd_shuffle:
  1441. {
  1442. Type *vt = arg0.type;
  1443. GB_ASSERT(vt->kind == Type_SimdVector);
  1444. i64 indices_count = ce->args.count-2;
  1445. i64 max_count = vt->SimdVector.count*2;
  1446. GB_ASSERT(indices_count <= max_count);
  1447. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, indices_count);
  1448. for (isize i = 0; i < indices_count; i++) {
  1449. lbValue idx = lb_build_expr(p, ce->args[i+2]);
  1450. GB_ASSERT(LLVMIsConstant(idx.value));
  1451. values[i] = idx.value;
  1452. }
  1453. LLVMValueRef indices = LLVMConstVector(values, cast(unsigned)indices_count);
  1454. res.value = LLVMBuildShuffleVector(p->builder, arg0.value, arg1.value, indices, "");
  1455. return res;
  1456. }
  1457. case BuiltinProc_simd_select:
  1458. {
  1459. LLVMValueRef cond = arg0.value;
  1460. LLVMValueRef x = lb_build_expr(p, ce->args[1]).value;
  1461. LLVMValueRef y = lb_build_expr(p, ce->args[2]).value;
  1462. cond = LLVMBuildICmp(p->builder, LLVMIntNE, cond, LLVMConstNull(LLVMTypeOf(cond)), "");
  1463. res.value = LLVMBuildSelect(p->builder, cond, x, y, "");
  1464. return res;
  1465. }
  1466. case BuiltinProc_simd_runtime_swizzle:
  1467. {
  1468. LLVMValueRef src = arg0.value;
  1469. LLVMValueRef indices = lb_build_expr(p, ce->args[1]).value;
  1470. Type *vt = arg0.type;
  1471. GB_ASSERT(vt->kind == Type_SimdVector);
  1472. i64 count = vt->SimdVector.count;
  1473. Type *elem_type = vt->SimdVector.elem;
  1474. i64 elem_size = type_size_of(elem_type);
  1475. // Determine strategy based on element size and target architecture
  1476. char const *intrinsic_name = nullptr;
  1477. bool use_hardware_runtime_swizzle = false;
  1478. // 8-bit elements: Use dedicated table lookup instructions
  1479. if (elem_size == 1) {
  1480. use_hardware_runtime_swizzle = true;
  1481. if (build_context.metrics.arch == TargetArch_amd64 || build_context.metrics.arch == TargetArch_i386) {
  1482. // x86/x86-64: Use pshufb intrinsics
  1483. switch (count) {
  1484. case 16:
  1485. intrinsic_name = "llvm.x86.ssse3.pshuf.b.128";
  1486. break;
  1487. case 32:
  1488. intrinsic_name = "llvm.x86.avx2.pshuf.b";
  1489. break;
  1490. case 64:
  1491. intrinsic_name = "llvm.x86.avx512.pshuf.b.512";
  1492. break;
  1493. default:
  1494. use_hardware_runtime_swizzle = false;
  1495. break;
  1496. }
  1497. } else if (build_context.metrics.arch == TargetArch_arm64) {
  1498. // ARM64: Use NEON tbl intrinsics with automatic table splitting
  1499. switch (count) {
  1500. case 16:
  1501. intrinsic_name = "llvm.aarch64.neon.tbl1";
  1502. break;
  1503. case 32:
  1504. intrinsic_name = "llvm.aarch64.neon.tbl2";
  1505. break;
  1506. case 48:
  1507. intrinsic_name = "llvm.aarch64.neon.tbl3";
  1508. break;
  1509. case 64:
  1510. intrinsic_name = "llvm.aarch64.neon.tbl4";
  1511. break;
  1512. default:
  1513. use_hardware_runtime_swizzle = false;
  1514. break;
  1515. }
  1516. } else if (build_context.metrics.arch == TargetArch_arm32) {
  1517. // ARM32: Use NEON vtbl intrinsics with automatic table splitting
  1518. switch (count) {
  1519. case 8:
  1520. intrinsic_name = "llvm.arm.neon.vtbl1";
  1521. break;
  1522. case 16:
  1523. intrinsic_name = "llvm.arm.neon.vtbl2";
  1524. break;
  1525. case 24:
  1526. intrinsic_name = "llvm.arm.neon.vtbl3";
  1527. break;
  1528. case 32:
  1529. intrinsic_name = "llvm.arm.neon.vtbl4";
  1530. break;
  1531. default:
  1532. use_hardware_runtime_swizzle = false;
  1533. break;
  1534. }
  1535. } else if (build_context.metrics.arch == TargetArch_wasm32 || build_context.metrics.arch == TargetArch_wasm64p32) {
  1536. // WebAssembly: Use swizzle (only supports 16-byte vectors)
  1537. if (count == 16) {
  1538. intrinsic_name = "llvm.wasm.swizzle";
  1539. } else {
  1540. use_hardware_runtime_swizzle = false;
  1541. }
  1542. } else {
  1543. use_hardware_runtime_swizzle = false;
  1544. }
  1545. }
  1546. if (use_hardware_runtime_swizzle && intrinsic_name != nullptr) {
  1547. // Use dedicated hardware swizzle instruction
  1548. // Check if required target features are enabled
  1549. bool features_enabled = true;
  1550. if (build_context.metrics.arch == TargetArch_amd64 || build_context.metrics.arch == TargetArch_i386) {
  1551. // x86/x86-64 feature checking
  1552. if (count == 16) {
  1553. // SSE/SSSE3 for 128-bit vectors
  1554. if (!check_target_feature_is_enabled(str_lit("ssse3"), nullptr)) {
  1555. features_enabled = false;
  1556. }
  1557. } else if (count == 32) {
  1558. // AVX2 requires ssse3 + avx2 features
  1559. if (!check_target_feature_is_enabled(str_lit("ssse3"), nullptr) ||
  1560. !check_target_feature_is_enabled(str_lit("avx2"), nullptr)) {
  1561. features_enabled = false;
  1562. }
  1563. } else if (count == 64) {
  1564. // AVX512 requires ssse3 + avx2 + avx512f + avx512bw features
  1565. if (!check_target_feature_is_enabled(str_lit("ssse3"), nullptr) ||
  1566. !check_target_feature_is_enabled(str_lit("avx2"), nullptr) ||
  1567. !check_target_feature_is_enabled(str_lit("avx512f"), nullptr) ||
  1568. !check_target_feature_is_enabled(str_lit("avx512bw"), nullptr)) {
  1569. features_enabled = false;
  1570. }
  1571. }
  1572. } else if (build_context.metrics.arch == TargetArch_arm64 || build_context.metrics.arch == TargetArch_arm32) {
  1573. // ARM/ARM64 feature checking - NEON is required for all table/swizzle ops
  1574. if (!check_target_feature_is_enabled(str_lit("neon"), nullptr)) {
  1575. features_enabled = false;
  1576. }
  1577. }
  1578. if (features_enabled) {
  1579. // Add target features to function attributes for LLVM instruction selection
  1580. if (build_context.metrics.arch == TargetArch_amd64 || build_context.metrics.arch == TargetArch_i386) {
  1581. // x86/x86-64 function attributes
  1582. if (count == 16) {
  1583. // SSE/SSSE3 for 128-bit vectors
  1584. lb_add_attribute_to_proc_with_string(p->module, p->value, str_lit("target-features"), str_lit("+ssse3"));
  1585. lb_add_attribute_to_proc_with_string(p->module, p->value, str_lit("min-legal-vector-width"), str_lit("128"));
  1586. } else if (count == 32) {
  1587. lb_add_attribute_to_proc_with_string(p->module, p->value, str_lit("target-features"), str_lit("+avx,+avx2,+ssse3"));
  1588. lb_add_attribute_to_proc_with_string(p->module, p->value, str_lit("min-legal-vector-width"), str_lit("256"));
  1589. } else if (count == 64) {
  1590. lb_add_attribute_to_proc_with_string(p->module, p->value, str_lit("target-features"), str_lit("+avx,+avx2,+avx512f,+avx512bw,+ssse3"));
  1591. lb_add_attribute_to_proc_with_string(p->module, p->value, str_lit("min-legal-vector-width"), str_lit("512"));
  1592. }
  1593. } else if (build_context.metrics.arch == TargetArch_arm64) {
  1594. // ARM64 function attributes - enable NEON for swizzle instructions
  1595. lb_add_attribute_to_proc_with_string(p->module, p->value, str_lit("target-features"), str_lit("+neon"));
  1596. // Set appropriate vector width for multi-swizzle operations
  1597. if (count >= 32) {
  1598. lb_add_attribute_to_proc_with_string(p->module, p->value, str_lit("min-legal-vector-width"), str_lit("256"));
  1599. }
  1600. } else if (build_context.metrics.arch == TargetArch_arm32) {
  1601. // ARM32 function attributes - enable NEON for swizzle instructions
  1602. lb_add_attribute_to_proc_with_string(p->module, p->value, str_lit("target-features"), str_lit("+neon"));
  1603. }
  1604. // Handle ARM's multi-swizzle intrinsics by splitting the src vector
  1605. if (build_context.metrics.arch == TargetArch_arm64 && count > 16) {
  1606. // ARM64 TBL2/TBL3/TBL4: Split src into multiple 16-byte vectors
  1607. int num_tables = cast(int)(count / 16);
  1608. GB_ASSERT_MSG(count % 16 == 0, "ARM64 src size must be multiple of 16 bytes, got %lld bytes", count);
  1609. GB_ASSERT_MSG(num_tables <= 4, "ARM64 NEON supports maximum 4 tables (tbl4), got %d tables for %lld-byte vector", num_tables, count);
  1610. LLVMValueRef src_parts[4]; // Max 4 tables for tbl4
  1611. for (int i = 0; i < num_tables; i++) {
  1612. // Extract 16-byte slice from the larger src
  1613. LLVMValueRef indices_for_extract[16];
  1614. for (int j = 0; j < 16; j++) {
  1615. indices_for_extract[j] = LLVMConstInt(LLVMInt32TypeInContext(p->module->ctx), i * 16 + j, false);
  1616. }
  1617. LLVMValueRef extract_mask = LLVMConstVector(indices_for_extract, 16);
  1618. src_parts[i] = LLVMBuildShuffleVector(p->builder, src, LLVMGetUndef(LLVMTypeOf(src)), extract_mask, "");
  1619. }
  1620. // Call appropriate ARM64 tbl intrinsic
  1621. if (count == 32) {
  1622. LLVMValueRef args[3] = { src_parts[0], src_parts[1], indices };
  1623. res.value = lb_call_intrinsic(p, intrinsic_name, args, 3, nullptr, 0);
  1624. } else if (count == 48) {
  1625. LLVMValueRef args[4] = { src_parts[0], src_parts[1], src_parts[2], indices };
  1626. res.value = lb_call_intrinsic(p, intrinsic_name, args, 4, nullptr, 0);
  1627. } else if (count == 64) {
  1628. LLVMValueRef args[5] = { src_parts[0], src_parts[1], src_parts[2], src_parts[3], indices };
  1629. res.value = lb_call_intrinsic(p, intrinsic_name, args, 5, nullptr, 0);
  1630. }
  1631. } else if (build_context.metrics.arch == TargetArch_arm32 && count > 8) {
  1632. // ARM32 VTBL2/VTBL3/VTBL4: Split src into multiple 8-byte vectors
  1633. int num_tables = cast(int)count / 8;
  1634. GB_ASSERT_MSG(count % 8 == 0, "ARM32 src size must be multiple of 8 bytes, got %lld bytes", count);
  1635. GB_ASSERT_MSG(num_tables <= 4, "ARM32 NEON supports maximum 4 tables (vtbl4), got %d tables for %lld-byte vector", num_tables, count);
  1636. LLVMValueRef src_parts[4]; // Max 4 tables for vtbl4
  1637. for (int i = 0; i < num_tables; i++) {
  1638. // Extract 8-byte slice from the larger src
  1639. LLVMValueRef indices_for_extract[8];
  1640. for (int j = 0; j < 8; j++) {
  1641. indices_for_extract[j] = LLVMConstInt(LLVMInt32TypeInContext(p->module->ctx), i * 8 + j, false);
  1642. }
  1643. LLVMValueRef extract_mask = LLVMConstVector(indices_for_extract, 8);
  1644. src_parts[i] = LLVMBuildShuffleVector(p->builder, src, LLVMGetUndef(LLVMTypeOf(src)), extract_mask, "");
  1645. }
  1646. // Call appropriate ARM32 vtbl intrinsic
  1647. if (count == 16) {
  1648. LLVMValueRef args[3] = { src_parts[0], src_parts[1], indices };
  1649. res.value = lb_call_intrinsic(p, intrinsic_name, args, 3, nullptr, 0);
  1650. } else if (count == 24) {
  1651. LLVMValueRef args[4] = { src_parts[0], src_parts[1], src_parts[2], indices };
  1652. res.value = lb_call_intrinsic(p, intrinsic_name, args, 4, nullptr, 0);
  1653. } else if (count == 32) {
  1654. LLVMValueRef args[5] = { src_parts[0], src_parts[1], src_parts[2], src_parts[3], indices };
  1655. res.value = lb_call_intrinsic(p, intrinsic_name, args, 5, nullptr, 0);
  1656. }
  1657. } else {
  1658. // Single runtime swizzle case (x86, WebAssembly, ARM single-table)
  1659. LLVMValueRef args[2] = { src, indices };
  1660. res.value = lb_call_intrinsic(p, intrinsic_name, args, gb_count_of(args), nullptr, 0);
  1661. }
  1662. return res;
  1663. } else {
  1664. // Features not enabled, fall back to emulation
  1665. use_hardware_runtime_swizzle = false;
  1666. }
  1667. }
  1668. // Fallback: Emulate with extracts and inserts for all element sizes
  1669. GB_ASSERT(count > 0 && count <= 64); // Sanity check
  1670. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, count);
  1671. LLVMTypeRef i32_type = LLVMInt32TypeInContext(p->module->ctx);
  1672. LLVMTypeRef elem_llvm_type = lb_type(p->module, elem_type);
  1673. // Calculate mask based on element size and vector count
  1674. i64 max_index = count - 1;
  1675. LLVMValueRef index_mask;
  1676. if (elem_size == 1) {
  1677. // 8-bit: mask to src size (like pshufb behavior)
  1678. index_mask = LLVMConstInt(elem_llvm_type, max_index, false);
  1679. } else if (elem_size == 2) {
  1680. // 16-bit: mask to src size
  1681. index_mask = LLVMConstInt(elem_llvm_type, max_index, false);
  1682. } else if (elem_size == 4) {
  1683. // 32-bit: mask to src size
  1684. index_mask = LLVMConstInt(elem_llvm_type, max_index, false);
  1685. } else {
  1686. // 64-bit: mask to src size
  1687. index_mask = LLVMConstInt(elem_llvm_type, max_index, false);
  1688. }
  1689. for (i64 i = 0; i < count; i++) {
  1690. LLVMValueRef idx_i = LLVMConstInt(i32_type, cast(unsigned)i, false);
  1691. LLVMValueRef index_elem = LLVMBuildExtractElement(p->builder, indices, idx_i, "");
  1692. // Mask index to valid range
  1693. LLVMValueRef masked_index = LLVMBuildAnd(p->builder, index_elem, index_mask, "");
  1694. // Convert to i32 for extractelement
  1695. LLVMValueRef index_i32;
  1696. if (LLVMGetIntTypeWidth(LLVMTypeOf(masked_index)) < 32) {
  1697. index_i32 = LLVMBuildZExt(p->builder, masked_index, i32_type, "");
  1698. } else if (LLVMGetIntTypeWidth(LLVMTypeOf(masked_index)) > 32) {
  1699. index_i32 = LLVMBuildTrunc(p->builder, masked_index, i32_type, "");
  1700. } else {
  1701. index_i32 = masked_index;
  1702. }
  1703. values[i] = LLVMBuildExtractElement(p->builder, src, index_i32, "");
  1704. }
  1705. // Build result vector
  1706. res.value = LLVMGetUndef(LLVMTypeOf(src));
  1707. for (i64 i = 0; i < count; i++) {
  1708. LLVMValueRef idx_i = LLVMConstInt(i32_type, cast(unsigned)i, false);
  1709. res.value = LLVMBuildInsertElement(p->builder, res.value, values[i], idx_i, "");
  1710. }
  1711. return res;
  1712. }
  1713. case BuiltinProc_simd_ceil:
  1714. case BuiltinProc_simd_floor:
  1715. case BuiltinProc_simd_trunc:
  1716. case BuiltinProc_simd_nearest:
  1717. {
  1718. char const *name = nullptr;
  1719. switch (builtin_id) {
  1720. case BuiltinProc_simd_ceil: name = "llvm.ceil"; break;
  1721. case BuiltinProc_simd_floor: name = "llvm.floor"; break;
  1722. case BuiltinProc_simd_trunc: name = "llvm.trunc"; break;
  1723. case BuiltinProc_simd_nearest: name = "llvm.nearbyint"; break;
  1724. }
  1725. LLVMTypeRef types[1] = { lb_type(p->module, arg0.type) };
  1726. LLVMValueRef args[1] = { arg0.value };
  1727. res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types));
  1728. return res;
  1729. }
  1730. case BuiltinProc_simd_lanes_reverse:
  1731. {
  1732. i64 count = get_array_type_count(arg0.type);
  1733. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, count);
  1734. LLVMTypeRef llvm_u32 = lb_type(m, t_u32);
  1735. for (i64 i = 0; i < count; i++) {
  1736. values[i] = LLVMConstInt(llvm_u32, count-1-i, false);
  1737. }
  1738. LLVMValueRef mask = LLVMConstVector(values, cast(unsigned)count);
  1739. LLVMValueRef v = arg0.value;
  1740. res.value = LLVMBuildShuffleVector(p->builder, v, v, mask, "");
  1741. return res;
  1742. }
  1743. case BuiltinProc_simd_lanes_rotate_left:
  1744. case BuiltinProc_simd_lanes_rotate_right:
  1745. {
  1746. i64 count = get_array_type_count(arg0.type);
  1747. GB_ASSERT(is_power_of_two(count));
  1748. BigInt bi_count = {};
  1749. big_int_from_i64(&bi_count, count);
  1750. TypeAndValue const &tv = ce->args[1]->tav;
  1751. ExactValue val = exact_value_to_integer(tv.value);
  1752. GB_ASSERT(val.kind == ExactValue_Integer);
  1753. BigInt *bi = &val.value_integer;
  1754. if (builtin_id == BuiltinProc_simd_lanes_rotate_right) {
  1755. big_int_neg(bi, bi);
  1756. }
  1757. big_int_rem(bi, bi, &bi_count);
  1758. big_int_dealloc(&bi_count);
  1759. i64 left = big_int_to_i64(bi);
  1760. LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, count);
  1761. LLVMTypeRef llvm_u32 = lb_type(m, t_u32);
  1762. for (i64 i = 0; i < count; i++) {
  1763. u64 idx = cast(u64)(i+left) & cast(u64)(count-1);
  1764. values[i] = LLVMConstInt(llvm_u32, idx, false);
  1765. }
  1766. LLVMValueRef mask = LLVMConstVector(values, cast(unsigned)count);
  1767. LLVMValueRef v = arg0.value;
  1768. res.value = LLVMBuildShuffleVector(p->builder, v, v, mask, "");
  1769. return res;
  1770. }
  1771. case BuiltinProc_simd_saturating_add:
  1772. case BuiltinProc_simd_saturating_sub:
  1773. {
  1774. char const *name = nullptr;
  1775. switch (builtin_id) {
  1776. case BuiltinProc_simd_saturating_add: name = is_signed ? "llvm.sadd.sat" : "llvm.uadd.sat"; break;
  1777. case BuiltinProc_simd_saturating_sub: name = is_signed ? "llvm.ssub.sat" : "llvm.usub.sat"; break;
  1778. }
  1779. LLVMTypeRef types[1] = {lb_type(p->module, arg0.type)};
  1780. LLVMValueRef args[2] = { arg0.value, arg1.value };
  1781. res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types));
  1782. return res;
  1783. }
  1784. case BuiltinProc_simd_clamp:
  1785. {
  1786. LLVMValueRef v = arg0.value;
  1787. LLVMValueRef min = arg1.value;
  1788. LLVMValueRef max = arg2.value;
  1789. if (is_float) {
  1790. v = LLVMBuildSelect(p->builder, LLVMBuildFCmp(p->builder, LLVMRealOLT, v, min, ""), min, v, "");
  1791. res.value = LLVMBuildSelect(p->builder, LLVMBuildFCmp(p->builder, LLVMRealOGT, v, max, ""), max, v, "");
  1792. } else if (is_signed) {
  1793. v = LLVMBuildSelect(p->builder, LLVMBuildICmp(p->builder, LLVMIntSLT, v, min, ""), min, v, "");
  1794. res.value = LLVMBuildSelect(p->builder, LLVMBuildICmp(p->builder, LLVMIntSGT, v, max, ""), max, v, "");
  1795. } else {
  1796. v = LLVMBuildSelect(p->builder, LLVMBuildICmp(p->builder, LLVMIntULT, v, min, ""), min, v, "");
  1797. res.value = LLVMBuildSelect(p->builder, LLVMBuildICmp(p->builder, LLVMIntUGT, v, max, ""), max, v, "");
  1798. }
  1799. return res;
  1800. }
  1801. case BuiltinProc_simd_to_bits:
  1802. {
  1803. res.value = LLVMBuildBitCast(p->builder, arg0.value, lb_type(m, tv.type), "");
  1804. return res;
  1805. }
  1806. case BuiltinProc_simd_gather:
  1807. case BuiltinProc_simd_scatter:
  1808. case BuiltinProc_simd_masked_load:
  1809. case BuiltinProc_simd_masked_store:
  1810. case BuiltinProc_simd_masked_expand_load:
  1811. case BuiltinProc_simd_masked_compress_store:
  1812. {
  1813. LLVMValueRef ptr = arg0.value;
  1814. LLVMValueRef val = arg1.value;
  1815. LLVMValueRef mask = arg2.value;
  1816. unsigned count = cast(unsigned)get_array_type_count(arg1.type);
  1817. LLVMTypeRef mask_type = LLVMVectorType(LLVMInt1TypeInContext(p->module->ctx), count);
  1818. mask = LLVMBuildTrunc(p->builder, mask, mask_type, "");
  1819. char const *name = nullptr;
  1820. switch (builtin_id) {
  1821. case BuiltinProc_simd_gather: name = "llvm.masked.gather"; break;
  1822. case BuiltinProc_simd_scatter: name = "llvm.masked.scatter"; break;
  1823. case BuiltinProc_simd_masked_load: name = "llvm.masked.load"; break;
  1824. case BuiltinProc_simd_masked_store: name = "llvm.masked.store"; break;
  1825. case BuiltinProc_simd_masked_expand_load: name = "llvm.masked.expandload"; break;
  1826. case BuiltinProc_simd_masked_compress_store: name = "llvm.masked.compressstore"; break;
  1827. }
  1828. unsigned type_count = 2;
  1829. LLVMTypeRef types[2] = {
  1830. lb_type(p->module, arg1.type),
  1831. lb_type(p->module, arg0.type)
  1832. };
  1833. auto alignment = cast(unsigned long long)type_align_of(base_array_type(arg1.type));
  1834. LLVMValueRef align = LLVMConstInt(LLVMInt32TypeInContext(p->module->ctx), alignment, false);
  1835. unsigned arg_count = 4;
  1836. LLVMValueRef args[4] = {};
  1837. switch (builtin_id) {
  1838. case BuiltinProc_simd_masked_load:
  1839. types[1] = lb_type(p->module, t_rawptr);
  1840. /*fallthrough*/
  1841. case BuiltinProc_simd_gather:
  1842. args[0] = ptr;
  1843. args[1] = align;
  1844. args[2] = mask;
  1845. args[3] = val;
  1846. break;
  1847. case BuiltinProc_simd_masked_store:
  1848. types[1] = lb_type(p->module, t_rawptr);
  1849. /*fallthrough*/
  1850. case BuiltinProc_simd_scatter:
  1851. args[0] = val;
  1852. args[1] = ptr;
  1853. args[2] = align;
  1854. args[3] = mask;
  1855. break;
  1856. case BuiltinProc_simd_masked_expand_load:
  1857. arg_count = 3;
  1858. type_count = 1;
  1859. args[0] = ptr;
  1860. args[1] = mask;
  1861. args[2] = val;
  1862. break;
  1863. case BuiltinProc_simd_masked_compress_store:
  1864. arg_count = 3;
  1865. type_count = 1;
  1866. args[0] = val;
  1867. args[1] = ptr;
  1868. args[2] = mask;
  1869. break;
  1870. }
  1871. res.value = lb_call_intrinsic(p, name, args, arg_count, types, type_count);
  1872. return res;
  1873. }
  1874. }
  1875. GB_PANIC("Unhandled simd intrinsic: '%.*s'", LIT(builtin_procs[builtin_id].name));
  1876. return {};
  1877. }
  1878. gb_internal lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, BuiltinProcId id) {
  1879. ast_node(ce, CallExpr, expr);
  1880. if (BuiltinProc__simd_begin < id && id < BuiltinProc__simd_end) {
  1881. return lb_build_builtin_simd_proc(p, expr, tv, id);
  1882. }
  1883. switch (id) {
  1884. case BuiltinProc_DIRECTIVE: {
  1885. ast_node(bd, BasicDirective, ce->proc);
  1886. String name = bd->name.string;
  1887. if (name == "location") {
  1888. String procedure = p->entity->token.string;
  1889. TokenPos pos = ast_token(ce->proc).pos;
  1890. if (ce->args.count > 0) {
  1891. Ast *ident = unselector_expr(ce->args[0]);
  1892. GB_ASSERT(ident->kind == Ast_Ident);
  1893. Entity *e = entity_of_node(ident);
  1894. GB_ASSERT(e != nullptr);
  1895. if (e->parent_proc_decl != nullptr && e->parent_proc_decl->entity != nullptr) {
  1896. procedure = e->parent_proc_decl->entity.load()->token.string;
  1897. } else {
  1898. procedure = str_lit("");
  1899. }
  1900. pos = e->token.pos;
  1901. }
  1902. return lb_emit_source_code_location_as_global(p, procedure, pos);
  1903. } else if (name == "load_directory") {
  1904. lbModule *m = p->module;
  1905. TEMPORARY_ALLOCATOR_GUARD();
  1906. LoadDirectoryCache *cache = map_must_get(&m->info->load_directory_map, expr);
  1907. isize count = cache->files.count;
  1908. LLVMValueRef *elements = gb_alloc_array(temporary_allocator(), LLVMValueRef, count);
  1909. for_array(i, cache->files) {
  1910. LoadFileCache *file = cache->files[i];
  1911. String file_name = filename_without_directory(file->path);
  1912. LLVMValueRef values[2] = {};
  1913. values[0] = lb_const_string(m, file_name).value;
  1914. values[1] = lb_const_value(m, t_u8_slice, exact_value_string(file->data)).value;
  1915. LLVMValueRef element = llvm_const_named_struct(m, t_load_directory_file, values, gb_count_of(values));
  1916. elements[i] = element;
  1917. }
  1918. LLVMValueRef backing_array = llvm_const_array(m, lb_type(m, t_load_directory_file), elements, count);
  1919. Type *array_type = alloc_type_array(t_load_directory_file, count);
  1920. lbAddr backing_array_addr = lb_add_global_generated_from_procedure(p, array_type, {backing_array, array_type});
  1921. lb_make_global_private_const(backing_array_addr);
  1922. LLVMValueRef backing_array_ptr = backing_array_addr.addr.value;
  1923. backing_array_ptr = LLVMConstPointerCast(backing_array_ptr, lb_type(m, t_load_directory_file_ptr));
  1924. LLVMValueRef const_slice = llvm_const_slice_internal(m, backing_array_ptr, LLVMConstInt(lb_type(m, t_int), count, false));
  1925. lbAddr addr = lb_add_global_generated_from_procedure(p, tv.type, {const_slice, t_load_directory_file_slice});
  1926. lb_make_global_private_const(addr);
  1927. return lb_addr_load(p, addr);
  1928. } else {
  1929. GB_PANIC("UNKNOWN DIRECTIVE: %.*s", LIT(name));
  1930. }
  1931. }
  1932. case BuiltinProc_type_info_of: {
  1933. Ast *arg = ce->args[0];
  1934. TypeAndValue tav = type_and_value_of_expr(arg);
  1935. if (tav.mode == Addressing_Type) {
  1936. Type *t = default_type(type_of_expr(arg));
  1937. return lb_type_info(p, t);
  1938. }
  1939. GB_ASSERT(is_type_typeid(tav.type));
  1940. auto args = array_make<lbValue>(permanent_allocator(), 1);
  1941. args[0] = lb_build_expr(p, arg);
  1942. return lb_emit_runtime_call(p, "__type_info_of", args);
  1943. }
  1944. case BuiltinProc_typeid_of: {
  1945. Ast *arg = ce->args[0];
  1946. TypeAndValue tav = type_and_value_of_expr(arg);
  1947. GB_ASSERT(tav.mode == Addressing_Type);
  1948. Type *t = default_type(type_of_expr(arg));
  1949. return lb_typeid(p->module, t);
  1950. }
  1951. case BuiltinProc_len: {
  1952. lbValue v = lb_build_expr(p, ce->args[0]);
  1953. Type *t = base_type(v.type);
  1954. if (is_type_pointer(t)) {
  1955. v = lb_emit_load(p, v);
  1956. t = type_deref(t);
  1957. }
  1958. if (is_type_cstring(t)) {
  1959. return lb_cstring_len(p, v);
  1960. } else if (is_type_cstring16(t)) {
  1961. return lb_cstring16_len(p, v);
  1962. } else if (is_type_string16(t)) {
  1963. return lb_string_len(p, v);
  1964. } else if (is_type_string(t)) {
  1965. return lb_string_len(p, v);
  1966. } else if (is_type_array(t)) {
  1967. GB_PANIC("Array lengths are constant");
  1968. } else if (is_type_slice(t)) {
  1969. return lb_slice_len(p, v);
  1970. } else if (is_type_dynamic_array(t)) {
  1971. return lb_dynamic_array_len(p, v);
  1972. } else if (is_type_map(t)) {
  1973. return lb_map_len(p, v);
  1974. } else if (is_type_soa_struct(t)) {
  1975. return lb_soa_struct_len(p, v);
  1976. }
  1977. GB_PANIC("Unreachable");
  1978. break;
  1979. }
  1980. case BuiltinProc_cap: {
  1981. lbValue v = lb_build_expr(p, ce->args[0]);
  1982. Type *t = base_type(v.type);
  1983. if (is_type_pointer(t)) {
  1984. v = lb_emit_load(p, v);
  1985. t = type_deref(t);
  1986. }
  1987. if (is_type_string(t)) {
  1988. GB_PANIC("Unreachable");
  1989. } else if (is_type_array(t)) {
  1990. GB_PANIC("Array lengths are constant");
  1991. } else if (is_type_slice(t)) {
  1992. return lb_slice_len(p, v);
  1993. } else if (is_type_dynamic_array(t)) {
  1994. return lb_dynamic_array_cap(p, v);
  1995. } else if (is_type_map(t)) {
  1996. return lb_map_cap(p, v);
  1997. } else if (is_type_soa_struct(t)) {
  1998. return lb_soa_struct_cap(p, v);
  1999. }
  2000. GB_PANIC("Unreachable");
  2001. break;
  2002. }
  2003. case BuiltinProc_swizzle: {
  2004. isize index_count = ce->args.count-1;
  2005. if (is_type_simd_vector(tv.type)) {
  2006. lbValue vec = lb_build_expr(p, ce->args[0]);
  2007. if (index_count == 0) {
  2008. return vec;
  2009. }
  2010. unsigned mask_len = cast(unsigned)index_count;
  2011. LLVMValueRef *mask_elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, index_count);
  2012. for (isize i = 1; i < ce->args.count; i++) {
  2013. TypeAndValue tv = type_and_value_of_expr(ce->args[i]);
  2014. GB_ASSERT(is_type_integer(tv.type));
  2015. GB_ASSERT(tv.value.kind == ExactValue_Integer);
  2016. u32 index = cast(u32)big_int_to_i64(&tv.value.value_integer);
  2017. mask_elems[i-1] = LLVMConstInt(lb_type(p->module, t_u32), index, false);
  2018. }
  2019. LLVMValueRef mask = LLVMConstVector(mask_elems, mask_len);
  2020. LLVMValueRef v1 = vec.value;
  2021. LLVMValueRef v2 = vec.value;
  2022. lbValue res = {};
  2023. res.type = tv.type;
  2024. res.value = LLVMBuildShuffleVector(p->builder, v1, v2, mask, "");
  2025. return res;
  2026. }
  2027. lbAddr addr = lb_build_array_swizzle_addr(p, ce, tv);
  2028. return lb_addr_load(p, addr);
  2029. }
  2030. case BuiltinProc_complex: {
  2031. lbValue real = lb_build_expr(p, ce->args[0]);
  2032. lbValue imag = lb_build_expr(p, ce->args[1]);
  2033. lbAddr dst_addr = lb_add_local_generated(p, tv.type, false);
  2034. lbValue dst = lb_addr_get_ptr(p, dst_addr);
  2035. Type *ft = base_complex_elem_type(tv.type);
  2036. real = lb_emit_conv(p, real, ft);
  2037. imag = lb_emit_conv(p, imag, ft);
  2038. lb_emit_store(p, lb_emit_struct_ep(p, dst, 0), real);
  2039. lb_emit_store(p, lb_emit_struct_ep(p, dst, 1), imag);
  2040. return lb_emit_load(p, dst);
  2041. }
  2042. case BuiltinProc_quaternion: {
  2043. lbValue xyzw[4] = {};
  2044. for (i32 i = 0; i < 4; i++) {
  2045. ast_node(f, FieldValue, ce->args[i]);
  2046. GB_ASSERT(f->field->kind == Ast_Ident);
  2047. String name = f->field->Ident.token.string;
  2048. i32 index = -1;
  2049. // @QuaternionLayout
  2050. if (name == "x" || name == "imag") {
  2051. index = 0;
  2052. } else if (name == "y" || name == "jmag") {
  2053. index = 1;
  2054. } else if (name == "z" || name == "kmag") {
  2055. index = 2;
  2056. } else if (name == "w" || name == "real") {
  2057. index = 3;
  2058. }
  2059. GB_ASSERT(index >= 0);
  2060. xyzw[index] = lb_build_expr(p, f->value);
  2061. }
  2062. lbAddr dst_addr = lb_add_local_generated(p, tv.type, false);
  2063. lbValue dst = lb_addr_get_ptr(p, dst_addr);
  2064. Type *ft = base_complex_elem_type(tv.type);
  2065. xyzw[0] = lb_emit_conv(p, xyzw[0], ft);
  2066. xyzw[1] = lb_emit_conv(p, xyzw[1], ft);
  2067. xyzw[2] = lb_emit_conv(p, xyzw[2], ft);
  2068. xyzw[3] = lb_emit_conv(p, xyzw[3], ft);
  2069. lb_emit_store(p, lb_emit_struct_ep(p, dst, 0), xyzw[0]);
  2070. lb_emit_store(p, lb_emit_struct_ep(p, dst, 1), xyzw[1]);
  2071. lb_emit_store(p, lb_emit_struct_ep(p, dst, 2), xyzw[2]);
  2072. lb_emit_store(p, lb_emit_struct_ep(p, dst, 3), xyzw[3]);
  2073. return lb_emit_load(p, dst);
  2074. }
  2075. case BuiltinProc_real: {
  2076. lbValue val = lb_build_expr(p, ce->args[0]);
  2077. if (is_type_complex(val.type)) {
  2078. lbValue real = lb_emit_struct_ev(p, val, 0);
  2079. return lb_emit_conv(p, real, tv.type);
  2080. } else if (is_type_quaternion(val.type)) {
  2081. // @QuaternionLayout
  2082. lbValue real = lb_emit_struct_ev(p, val, 3);
  2083. return lb_emit_conv(p, real, tv.type);
  2084. }
  2085. GB_PANIC("invalid type for real");
  2086. return {};
  2087. }
  2088. case BuiltinProc_imag: {
  2089. lbValue val = lb_build_expr(p, ce->args[0]);
  2090. if (is_type_complex(val.type)) {
  2091. lbValue imag = lb_emit_struct_ev(p, val, 1);
  2092. return lb_emit_conv(p, imag, tv.type);
  2093. } else if (is_type_quaternion(val.type)) {
  2094. // @QuaternionLayout
  2095. lbValue imag = lb_emit_struct_ev(p, val, 0);
  2096. return lb_emit_conv(p, imag, tv.type);
  2097. }
  2098. GB_PANIC("invalid type for imag");
  2099. return {};
  2100. }
  2101. case BuiltinProc_jmag: {
  2102. lbValue val = lb_build_expr(p, ce->args[0]);
  2103. if (is_type_quaternion(val.type)) {
  2104. // @QuaternionLayout
  2105. lbValue imag = lb_emit_struct_ev(p, val, 1);
  2106. return lb_emit_conv(p, imag, tv.type);
  2107. }
  2108. GB_PANIC("invalid type for jmag");
  2109. return {};
  2110. }
  2111. case BuiltinProc_kmag: {
  2112. lbValue val = lb_build_expr(p, ce->args[0]);
  2113. if (is_type_quaternion(val.type)) {
  2114. // @QuaternionLayout
  2115. lbValue imag = lb_emit_struct_ev(p, val, 2);
  2116. return lb_emit_conv(p, imag, tv.type);
  2117. }
  2118. GB_PANIC("invalid type for kmag");
  2119. return {};
  2120. }
  2121. case BuiltinProc_conj: {
  2122. lbValue val = lb_build_expr(p, ce->args[0]);
  2123. return lb_emit_conjugate(p, val, tv.type);
  2124. }
  2125. case BuiltinProc_expand_values: {
  2126. lbValue val = lb_build_expr(p, ce->args[0]);
  2127. Type *t = base_type(val.type);
  2128. if (!is_type_tuple(tv.type)) {
  2129. if (t->kind == Type_Struct) {
  2130. GB_ASSERT(t->Struct.fields.count == 1);
  2131. return lb_emit_struct_ev(p, val, 0);
  2132. } else if (t->kind == Type_Array) {
  2133. GB_ASSERT(t->Array.count == 1);
  2134. return lb_emit_struct_ev(p, val, 0);
  2135. } else {
  2136. GB_PANIC("Unknown type of expand_values");
  2137. }
  2138. }
  2139. GB_ASSERT(is_type_tuple(tv.type));
  2140. // NOTE(bill): Doesn't need to be zero because it will be initialized in the loops
  2141. lbValue tuple = lb_addr_get_ptr(p, lb_add_local_generated(p, tv.type, false));
  2142. if (t->kind == Type_Struct) {
  2143. for_array(src_index, t->Struct.fields) {
  2144. Entity *field = t->Struct.fields[src_index];
  2145. i32 field_index = field->Variable.field_index;
  2146. lbValue f = lb_emit_struct_ev(p, val, field_index);
  2147. lbValue ep = lb_emit_struct_ep(p, tuple, cast(i32)src_index);
  2148. lb_emit_store(p, ep, f);
  2149. }
  2150. } else if (is_type_array_like(t)) {
  2151. // TODO(bill): Clean-up this code
  2152. lbValue ap = lb_address_from_load_or_generate_local(p, val);
  2153. i32 n = cast(i32)get_array_type_count(t);
  2154. for (i32 i = 0; i < n; i++) {
  2155. lbValue f = lb_emit_load(p, lb_emit_array_epi(p, ap, i));
  2156. lbValue ep = lb_emit_struct_ep(p, tuple, i);
  2157. lb_emit_store(p, ep, f);
  2158. }
  2159. } else {
  2160. GB_PANIC("Unknown type of expand_values");
  2161. }
  2162. return lb_emit_load(p, tuple);
  2163. }
  2164. case BuiltinProc_compress_values: {
  2165. isize value_count = 0;
  2166. for (Ast *arg : ce->args) {
  2167. Type *t = arg->tav.type;
  2168. if (is_type_tuple(t)) {
  2169. value_count += t->Tuple.variables.count;
  2170. } else {
  2171. value_count += 1;
  2172. }
  2173. }
  2174. if (value_count == 1) {
  2175. lbValue x = lb_build_expr(p, ce->args[0]);
  2176. x = lb_emit_conv(p, x, tv.type);
  2177. return x;
  2178. }
  2179. Type *dt = base_type(tv.type);
  2180. lbAddr addr = lb_add_local_generated(p, tv.type, true);
  2181. if (is_type_struct(dt) || is_type_tuple(dt)) {
  2182. i32 index = 0;
  2183. for (Ast *arg : ce->args) {
  2184. lbValue x = lb_build_expr(p, arg);
  2185. if (is_type_tuple(x.type)) {
  2186. for (isize i = 0; i < x.type->Tuple.variables.count; i++) {
  2187. lbValue y = lb_emit_tuple_ev(p, x, cast(i32)i);
  2188. lbValue ptr = lb_emit_struct_ep(p, addr.addr, index++);
  2189. y = lb_emit_conv(p, y, type_deref(ptr.type));
  2190. lb_emit_store(p, ptr, y);
  2191. }
  2192. } else {
  2193. lbValue ptr = lb_emit_struct_ep(p, addr.addr, index++);
  2194. x = lb_emit_conv(p, x, type_deref(ptr.type));
  2195. lb_emit_store(p, ptr, x);
  2196. }
  2197. }
  2198. GB_ASSERT(index == value_count);
  2199. } else if (is_type_array_like(dt)) {
  2200. i32 index = 0;
  2201. for (Ast *arg : ce->args) {
  2202. lbValue x = lb_build_expr(p, arg);
  2203. if (is_type_tuple(x.type)) {
  2204. for (isize i = 0; i < x.type->Tuple.variables.count; i++) {
  2205. lbValue y = lb_emit_tuple_ev(p, x, cast(i32)i);
  2206. lbValue ptr = lb_emit_array_epi(p, addr.addr, index++);
  2207. y = lb_emit_conv(p, y, type_deref(ptr.type));
  2208. lb_emit_store(p, ptr, y);
  2209. }
  2210. } else {
  2211. lbValue ptr = lb_emit_array_epi(p, addr.addr, index++);
  2212. x = lb_emit_conv(p, x, type_deref(ptr.type));
  2213. lb_emit_store(p, ptr, x);
  2214. }
  2215. }
  2216. GB_ASSERT(index == value_count);
  2217. } else {
  2218. GB_PANIC("TODO(bill): compress_values -> %s", type_to_string(tv.type));
  2219. }
  2220. return lb_addr_load(p, addr);
  2221. }
  2222. case BuiltinProc_min: {
  2223. Type *t = type_of_expr(expr);
  2224. if (ce->args.count == 2) {
  2225. return lb_emit_min(p, t, lb_build_expr(p, ce->args[0]), lb_build_expr(p, ce->args[1]));
  2226. } else {
  2227. lbValue x = lb_build_expr(p, ce->args[0]);
  2228. for (isize i = 1; i < ce->args.count; i++) {
  2229. x = lb_emit_min(p, t, x, lb_build_expr(p, ce->args[i]));
  2230. }
  2231. return x;
  2232. }
  2233. }
  2234. case BuiltinProc_max: {
  2235. Type *t = type_of_expr(expr);
  2236. if (ce->args.count == 2) {
  2237. return lb_emit_max(p, t, lb_build_expr(p, ce->args[0]), lb_build_expr(p, ce->args[1]));
  2238. } else {
  2239. lbValue x = lb_build_expr(p, ce->args[0]);
  2240. for (isize i = 1; i < ce->args.count; i++) {
  2241. x = lb_emit_max(p, t, x, lb_build_expr(p, ce->args[i]));
  2242. }
  2243. return x;
  2244. }
  2245. }
  2246. case BuiltinProc_abs: {
  2247. lbValue x = lb_build_expr(p, ce->args[0]);
  2248. Type *t = x.type;
  2249. if (is_type_unsigned(t)) {
  2250. return x;
  2251. }
  2252. if (is_type_quaternion(t)) {
  2253. i64 sz = 8*type_size_of(t);
  2254. auto args = array_make<lbValue>(permanent_allocator(), 1);
  2255. args[0] = x;
  2256. switch (sz) {
  2257. case 64: return lb_emit_runtime_call(p, "abs_quaternion64", args);
  2258. case 128: return lb_emit_runtime_call(p, "abs_quaternion128", args);
  2259. case 256: return lb_emit_runtime_call(p, "abs_quaternion256", args);
  2260. }
  2261. GB_PANIC("Unknown complex type");
  2262. } else if (is_type_complex(t)) {
  2263. i64 sz = 8*type_size_of(t);
  2264. auto args = array_make<lbValue>(permanent_allocator(), 1);
  2265. args[0] = x;
  2266. switch (sz) {
  2267. case 32: return lb_emit_runtime_call(p, "abs_complex32", args);
  2268. case 64: return lb_emit_runtime_call(p, "abs_complex64", args);
  2269. case 128: return lb_emit_runtime_call(p, "abs_complex128", args);
  2270. }
  2271. GB_PANIC("Unknown complex type");
  2272. } else if (is_type_float(t)) {
  2273. bool little = is_type_endian_little(t) || (is_type_endian_platform(t) && build_context.endian_kind == TargetEndian_Little);
  2274. Type *t_unsigned = nullptr;
  2275. lbValue mask = {0};
  2276. switch (type_size_of(t)) {
  2277. case 2:
  2278. t_unsigned = t_u16;
  2279. mask = lb_const_int(p->module, t_unsigned, little ? 0x7FFF : 0xFF7F);
  2280. break;
  2281. case 4:
  2282. t_unsigned = t_u32;
  2283. mask = lb_const_int(p->module, t_unsigned, little ? 0x7FFFFFFF : 0xFFFFFF7F);
  2284. break;
  2285. case 8:
  2286. t_unsigned = t_u64;
  2287. mask = lb_const_int(p->module, t_unsigned, little ? 0x7FFFFFFFFFFFFFFF : 0xFFFFFFFFFFFFFF7F);
  2288. break;
  2289. default:
  2290. GB_PANIC("abs: unhandled float size");
  2291. }
  2292. lbValue as_unsigned = lb_emit_transmute(p, x, t_unsigned);
  2293. lbValue abs = lb_emit_arith(p, Token_And, as_unsigned, mask, t_unsigned);
  2294. return lb_emit_transmute(p, abs, t);
  2295. }
  2296. lbValue zero = lb_const_nil(p->module, t);
  2297. lbValue cond = lb_emit_comp(p, Token_Lt, x, zero);
  2298. lbValue neg = lb_emit_unary_arith(p, Token_Sub, x, t);
  2299. return lb_emit_select(p, cond, neg, x);
  2300. }
  2301. case BuiltinProc_clamp:
  2302. return lb_emit_clamp(p, type_of_expr(expr),
  2303. lb_build_expr(p, ce->args[0]),
  2304. lb_build_expr(p, ce->args[1]),
  2305. lb_build_expr(p, ce->args[2]));
  2306. case BuiltinProc_soa_zip:
  2307. return lb_soa_zip(p, ce, tv);
  2308. case BuiltinProc_soa_unzip:
  2309. return lb_soa_unzip(p, ce, tv);
  2310. case BuiltinProc_transpose:
  2311. {
  2312. lbValue m = lb_build_expr(p, ce->args[0]);
  2313. return lb_emit_matrix_tranpose(p, m, tv.type);
  2314. }
  2315. case BuiltinProc_outer_product:
  2316. {
  2317. lbValue a = lb_build_expr(p, ce->args[0]);
  2318. lbValue b = lb_build_expr(p, ce->args[1]);
  2319. return lb_emit_outer_product(p, a, b, tv.type);
  2320. }
  2321. case BuiltinProc_hadamard_product:
  2322. {
  2323. lbValue a = lb_build_expr(p, ce->args[0]);
  2324. lbValue b = lb_build_expr(p, ce->args[1]);
  2325. if (is_type_array(tv.type)) {
  2326. return lb_emit_arith(p, Token_Mul, a, b, tv.type);
  2327. }
  2328. GB_ASSERT(is_type_matrix(tv.type));
  2329. return lb_emit_arith_matrix(p, Token_Mul, a, b, tv.type, true);
  2330. }
  2331. case BuiltinProc_matrix_flatten:
  2332. {
  2333. lbValue m = lb_build_expr(p, ce->args[0]);
  2334. return lb_emit_matrix_flatten(p, m, tv.type);
  2335. }
  2336. case BuiltinProc_unreachable:
  2337. lb_emit_unreachable(p);
  2338. return {};
  2339. case BuiltinProc_raw_data:
  2340. {
  2341. lbValue x = lb_build_expr(p, ce->args[0]);
  2342. Type *t = base_type(x.type);
  2343. lbValue res = {};
  2344. switch (t->kind) {
  2345. case Type_Slice:
  2346. res = lb_slice_elem(p, x);
  2347. res = lb_emit_conv(p, res, tv.type);
  2348. break;
  2349. case Type_DynamicArray:
  2350. res = lb_dynamic_array_elem(p, x);
  2351. res = lb_emit_conv(p, res, tv.type);
  2352. break;
  2353. case Type_Basic:
  2354. if (t->Basic.kind == Basic_string) {
  2355. res = lb_string_elem(p, x);
  2356. res = lb_emit_conv(p, res, tv.type);
  2357. } else if (t->Basic.kind == Basic_cstring) {
  2358. res = lb_emit_conv(p, x, tv.type);
  2359. } else if (t->Basic.kind == Basic_string16) {
  2360. res = lb_string_elem(p, x);
  2361. res = lb_emit_conv(p, res, tv.type);
  2362. } else if (t->Basic.kind == Basic_cstring16) {
  2363. res = lb_emit_conv(p, x, tv.type);
  2364. }
  2365. break;
  2366. case Type_Pointer:
  2367. case Type_MultiPointer:
  2368. res = lb_emit_conv(p, x, tv.type);
  2369. break;
  2370. }
  2371. GB_ASSERT(res.value != nullptr);
  2372. return res;
  2373. }
  2374. // "Intrinsics"
  2375. case BuiltinProc_alloca:
  2376. {
  2377. lbValue sz = lb_build_expr(p, ce->args[0]);
  2378. i64 al = exact_value_to_i64(type_and_value_of_expr(ce->args[1]).value);
  2379. lbValue res = {};
  2380. res.type = alloc_type_multi_pointer(t_u8);
  2381. res.value = LLVMBuildArrayAlloca(p->builder, lb_type(p->module, t_u8), sz.value, "");
  2382. LLVMSetAlignment(res.value, cast(unsigned)al);
  2383. return res;
  2384. }
  2385. case BuiltinProc_cpu_relax:
  2386. if (build_context.metrics.arch == TargetArch_i386 ||
  2387. build_context.metrics.arch == TargetArch_amd64) {
  2388. LLVMTypeRef func_type = LLVMFunctionType(LLVMVoidTypeInContext(p->module->ctx), nullptr, 0, false);
  2389. LLVMValueRef the_asm = llvm_get_inline_asm(func_type, str_lit("pause"), {}, true);
  2390. GB_ASSERT(the_asm != nullptr);
  2391. LLVMBuildCall2(p->builder, func_type, the_asm, nullptr, 0, "");
  2392. } else if (build_context.metrics.arch == TargetArch_arm64) {
  2393. LLVMTypeRef func_type = LLVMFunctionType(LLVMVoidTypeInContext(p->module->ctx), nullptr, 0, false);
  2394. // NOTE(bill, 2022-03-30): `isb` appears to a better option that `yield`
  2395. // See: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8258604
  2396. LLVMValueRef the_asm = llvm_get_inline_asm(func_type, str_lit("isb"), {}, true);
  2397. GB_ASSERT(the_asm != nullptr);
  2398. LLVMBuildCall2(p->builder, func_type, the_asm, nullptr, 0, "");
  2399. } else {
  2400. // NOTE: default to something to prevent optimization
  2401. LLVMTypeRef func_type = LLVMFunctionType(LLVMVoidTypeInContext(p->module->ctx), nullptr, 0, false);
  2402. LLVMValueRef the_asm = llvm_get_inline_asm(func_type, str_lit(""), {}, true);
  2403. GB_ASSERT(the_asm != nullptr);
  2404. LLVMBuildCall2(p->builder, func_type, the_asm, nullptr, 0, "");
  2405. }
  2406. return {};
  2407. case BuiltinProc_debug_trap:
  2408. case BuiltinProc_trap:
  2409. {
  2410. char const *name = nullptr;
  2411. switch (id) {
  2412. case BuiltinProc_debug_trap: name = "llvm.debugtrap"; break;
  2413. case BuiltinProc_trap: name = "llvm.trap"; break;
  2414. }
  2415. lb_call_intrinsic(p, name, nullptr, 0, nullptr, 0);
  2416. if (id == BuiltinProc_trap) {
  2417. LLVMBuildUnreachable(p->builder);
  2418. }
  2419. return {};
  2420. }
  2421. case BuiltinProc_read_cycle_counter:
  2422. {
  2423. lbValue res = {};
  2424. res.type = tv.type;
  2425. if (build_context.metrics.arch == TargetArch_arm64) {
  2426. LLVMTypeRef func_type = LLVMFunctionType(LLVMInt64TypeInContext(p->module->ctx), nullptr, 0, false);
  2427. bool has_side_effects = false;
  2428. LLVMValueRef the_asm = llvm_get_inline_asm(func_type, str_lit("mrs $0, cntvct_el0"), str_lit("=r"), has_side_effects);
  2429. GB_ASSERT(the_asm != nullptr);
  2430. res.value = LLVMBuildCall2(p->builder, func_type, the_asm, nullptr, 0, "");
  2431. } else {
  2432. char const *name = "llvm.readcyclecounter";
  2433. res.value = lb_call_intrinsic(p, name, nullptr, 0, nullptr, 0);
  2434. }
  2435. return res;
  2436. }
  2437. case BuiltinProc_read_cycle_counter_frequency:
  2438. {
  2439. lbValue res = {};
  2440. res.type = tv.type;
  2441. if (build_context.metrics.arch == TargetArch_arm64) {
  2442. LLVMTypeRef func_type = LLVMFunctionType(LLVMInt64TypeInContext(p->module->ctx), nullptr, 0, false);
  2443. bool has_side_effects = false;
  2444. LLVMValueRef the_asm = llvm_get_inline_asm(func_type, str_lit("mrs $0, cntfrq_el0"), str_lit("=r"), has_side_effects);
  2445. GB_ASSERT(the_asm != nullptr);
  2446. res.value = LLVMBuildCall2(p->builder, func_type, the_asm, nullptr, 0, "");
  2447. }
  2448. return res;
  2449. }
  2450. case BuiltinProc_count_trailing_zeros:
  2451. return lb_emit_count_trailing_zeros(p, lb_build_expr(p, ce->args[0]), tv.type);
  2452. case BuiltinProc_count_leading_zeros:
  2453. return lb_emit_count_leading_zeros(p, lb_build_expr(p, ce->args[0]), tv.type);
  2454. case BuiltinProc_count_ones:
  2455. return lb_emit_count_ones(p, lb_build_expr(p, ce->args[0]), tv.type);
  2456. case BuiltinProc_count_zeros:
  2457. return lb_emit_count_zeros(p, lb_build_expr(p, ce->args[0]), tv.type);
  2458. case BuiltinProc_reverse_bits:
  2459. return lb_emit_reverse_bits(p, lb_build_expr(p, ce->args[0]), tv.type);
  2460. case BuiltinProc_byte_swap:
  2461. {
  2462. lbValue x = lb_build_expr(p, ce->args[0]);
  2463. x = lb_emit_conv(p, x, tv.type);
  2464. return lb_emit_byte_swap(p, x, tv.type);
  2465. }
  2466. case BuiltinProc_overflow_add:
  2467. case BuiltinProc_overflow_sub:
  2468. case BuiltinProc_overflow_mul:
  2469. {
  2470. Type *main_type = tv.type;
  2471. Type *type = main_type;
  2472. if (is_type_tuple(main_type)) {
  2473. type = main_type->Tuple.variables[0]->type;
  2474. }
  2475. lbValue x = lb_build_expr(p, ce->args[0]);
  2476. lbValue y = lb_build_expr(p, ce->args[1]);
  2477. x = lb_emit_conv(p, x, type);
  2478. y = lb_emit_conv(p, y, type);
  2479. char const *name = nullptr;
  2480. if (is_type_unsigned(type)) {
  2481. switch (id) {
  2482. case BuiltinProc_overflow_add: name = "llvm.uadd.with.overflow"; break;
  2483. case BuiltinProc_overflow_sub: name = "llvm.usub.with.overflow"; break;
  2484. case BuiltinProc_overflow_mul: name = "llvm.umul.with.overflow"; break;
  2485. }
  2486. } else {
  2487. switch (id) {
  2488. case BuiltinProc_overflow_add: name = "llvm.sadd.with.overflow"; break;
  2489. case BuiltinProc_overflow_sub: name = "llvm.ssub.with.overflow"; break;
  2490. case BuiltinProc_overflow_mul: name = "llvm.smul.with.overflow"; break;
  2491. }
  2492. }
  2493. LLVMTypeRef types[1] = {lb_type(p->module, type)};
  2494. LLVMValueRef args[2] = { x.value, y.value };
  2495. lbValue res = {};
  2496. res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types));
  2497. if (is_type_tuple(main_type)) {
  2498. Type *res_type = nullptr;
  2499. gbAllocator a = permanent_allocator();
  2500. res_type = alloc_type_tuple();
  2501. slice_init(&res_type->Tuple.variables, a, 2);
  2502. res_type->Tuple.variables[0] = alloc_entity_field(nullptr, blank_token, type, false, 0);
  2503. res_type->Tuple.variables[1] = alloc_entity_field(nullptr, blank_token, t_llvm_bool, false, 1);
  2504. res.type = res_type;
  2505. } else {
  2506. res.value = LLVMBuildExtractValue(p->builder, res.value, 0, "");
  2507. res.type = type;
  2508. }
  2509. return res;
  2510. }
  2511. case BuiltinProc_saturating_add:
  2512. case BuiltinProc_saturating_sub:
  2513. {
  2514. Type *main_type = tv.type;
  2515. Type *type = main_type;
  2516. lbValue x = lb_build_expr(p, ce->args[0]);
  2517. lbValue y = lb_build_expr(p, ce->args[1]);
  2518. x = lb_emit_conv(p, x, type);
  2519. y = lb_emit_conv(p, y, type);
  2520. char const *name = nullptr;
  2521. if (is_type_unsigned(type)) {
  2522. switch (id) {
  2523. case BuiltinProc_saturating_add: name = "llvm.uadd.sat"; break;
  2524. case BuiltinProc_saturating_sub: name = "llvm.usub.sat"; break;
  2525. }
  2526. } else {
  2527. switch (id) {
  2528. case BuiltinProc_saturating_add: name = "llvm.sadd.sat"; break;
  2529. case BuiltinProc_saturating_sub: name = "llvm.ssub.sat"; break;
  2530. }
  2531. }
  2532. LLVMTypeRef types[1] = {lb_type(p->module, type)};
  2533. LLVMValueRef args[2] = { x.value, y.value };
  2534. lbValue res = {};
  2535. res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types));
  2536. res.type = type;
  2537. return res;
  2538. }
  2539. case BuiltinProc_sqrt:
  2540. {
  2541. Type *type = tv.type;
  2542. lbValue x = lb_build_expr(p, ce->args[0]);
  2543. x = lb_emit_conv(p, x, type);
  2544. char const *name = "llvm.sqrt";
  2545. LLVMTypeRef types[1] = {lb_type(p->module, type)};
  2546. LLVMValueRef args[1] = { x.value };
  2547. lbValue res = {};
  2548. res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types));
  2549. res.type = type;
  2550. return res;
  2551. }
  2552. case BuiltinProc_fused_mul_add:
  2553. {
  2554. Type *type = tv.type;
  2555. lbValue x = lb_emit_conv(p, lb_build_expr(p, ce->args[0]), type);
  2556. lbValue y = lb_emit_conv(p, lb_build_expr(p, ce->args[1]), type);
  2557. lbValue z = lb_emit_conv(p, lb_build_expr(p, ce->args[2]), type);
  2558. char const *name = "llvm.fma";
  2559. LLVMTypeRef types[1] = {lb_type(p->module, type)};
  2560. LLVMValueRef args[3] = { x.value, y.value, z.value };
  2561. lbValue res = {};
  2562. res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types));
  2563. res.type = type;
  2564. return res;
  2565. }
  2566. case BuiltinProc_mem_copy:
  2567. {
  2568. lbValue dst = lb_build_expr(p, ce->args[0]);
  2569. lbValue src = lb_build_expr(p, ce->args[1]);
  2570. lbValue len = lb_build_expr(p, ce->args[2]);
  2571. lb_mem_copy_overlapping(p, dst, src, len, false);
  2572. return {};
  2573. }
  2574. case BuiltinProc_mem_copy_non_overlapping:
  2575. {
  2576. lbValue dst = lb_build_expr(p, ce->args[0]);
  2577. lbValue src = lb_build_expr(p, ce->args[1]);
  2578. lbValue len = lb_build_expr(p, ce->args[2]);
  2579. lb_mem_copy_non_overlapping(p, dst, src, len, false);
  2580. return {};
  2581. }
  2582. case BuiltinProc_mem_zero:
  2583. {
  2584. lbValue ptr = lb_build_expr(p, ce->args[0]);
  2585. lbValue len = lb_build_expr(p, ce->args[1]);
  2586. ptr = lb_emit_conv(p, ptr, t_rawptr);
  2587. len = lb_emit_conv(p, len, t_int);
  2588. unsigned alignment = 1;
  2589. lb_mem_zero_ptr_internal(p, ptr.value, len.value, alignment, false);
  2590. return {};
  2591. }
  2592. case BuiltinProc_mem_zero_volatile:
  2593. {
  2594. lbValue ptr = lb_build_expr(p, ce->args[0]);
  2595. lbValue len = lb_build_expr(p, ce->args[1]);
  2596. ptr = lb_emit_conv(p, ptr, t_rawptr);
  2597. len = lb_emit_conv(p, len, t_int);
  2598. unsigned alignment = 1;
  2599. lb_mem_zero_ptr_internal(p, ptr.value, len.value, alignment, true);
  2600. return {};
  2601. }
  2602. case BuiltinProc_ptr_offset:
  2603. {
  2604. lbValue ptr = lb_build_expr(p, ce->args[0]);
  2605. lbValue len = lb_build_expr(p, ce->args[1]);
  2606. len = lb_emit_conv(p, len, t_int);
  2607. return lb_emit_ptr_offset(p, ptr, len);
  2608. }
  2609. case BuiltinProc_ptr_sub:
  2610. {
  2611. Type *elem0 = type_deref(type_of_expr(ce->args[0]), true);
  2612. Type *elem1 = type_deref(type_of_expr(ce->args[1]), true);
  2613. GB_ASSERT(are_types_identical(elem0, elem1));
  2614. Type *elem = elem0;
  2615. lbValue ptr0 = lb_emit_conv(p, lb_build_expr(p, ce->args[0]), t_uintptr);
  2616. lbValue ptr1 = lb_emit_conv(p, lb_build_expr(p, ce->args[1]), t_uintptr);
  2617. ptr0 = lb_emit_conv(p, ptr0, t_int);
  2618. ptr1 = lb_emit_conv(p, ptr1, t_int);
  2619. lbValue diff = lb_emit_arith(p, Token_Sub, ptr0, ptr1, t_int);
  2620. return lb_emit_arith(p, Token_Quo, diff, lb_const_int(p->module, t_int, type_size_of(elem)), t_int);
  2621. }
  2622. // TODO(bill): Which is correct?
  2623. case BuiltinProc_atomic_thread_fence:
  2624. LLVMBuildFence(p->builder, llvm_atomic_ordering_from_odin(ce->args[0]), false, "");
  2625. return {};
  2626. case BuiltinProc_atomic_signal_fence:
  2627. LLVMBuildFence(p->builder, llvm_atomic_ordering_from_odin(ce->args[0]), true, "");
  2628. return {};
  2629. case BuiltinProc_volatile_store:
  2630. case BuiltinProc_non_temporal_store:
  2631. case BuiltinProc_atomic_store:
  2632. case BuiltinProc_atomic_store_explicit: {
  2633. lbValue dst = lb_build_expr(p, ce->args[0]);
  2634. lbValue val = lb_build_expr(p, ce->args[1]);
  2635. val = lb_emit_conv(p, val, type_deref(dst.type));
  2636. LLVMValueRef instr = LLVMBuildStore(p->builder, val.value, dst.value);
  2637. switch (id) {
  2638. case BuiltinProc_non_temporal_store:
  2639. {
  2640. unsigned kind_id = LLVMGetMDKindIDInContext(p->module->ctx, "nontemporal", 11);
  2641. LLVMMetadataRef node = LLVMValueAsMetadata(LLVMConstInt(lb_type(p->module, t_u32), 1, false));
  2642. LLVMSetMetadata(instr, kind_id, LLVMMetadataAsValue(p->module->ctx, node));
  2643. }
  2644. break;
  2645. case BuiltinProc_volatile_store:
  2646. LLVMSetVolatile(instr, true);
  2647. break;
  2648. case BuiltinProc_atomic_store:
  2649. LLVMSetOrdering(instr, LLVMAtomicOrderingSequentiallyConsistent);
  2650. LLVMSetVolatile(instr, true);
  2651. break;
  2652. case BuiltinProc_atomic_store_explicit:
  2653. {
  2654. auto ordering = llvm_atomic_ordering_from_odin(ce->args[2]);
  2655. LLVMSetOrdering(instr, ordering);
  2656. LLVMSetVolatile(instr, true);
  2657. }
  2658. break;
  2659. }
  2660. LLVMSetAlignment(instr, cast(unsigned)type_align_of(type_deref(dst.type)));
  2661. return {};
  2662. }
  2663. case BuiltinProc_volatile_load:
  2664. case BuiltinProc_non_temporal_load:
  2665. case BuiltinProc_atomic_load:
  2666. case BuiltinProc_atomic_load_explicit: {
  2667. lbValue dst = lb_build_expr(p, ce->args[0]);
  2668. LLVMValueRef instr = OdinLLVMBuildLoad(p, lb_type(p->module, type_deref(dst.type)), dst.value);
  2669. switch (id) {
  2670. case BuiltinProc_non_temporal_load:
  2671. {
  2672. unsigned kind_id = LLVMGetMDKindIDInContext(p->module->ctx, "nontemporal", 11);
  2673. LLVMMetadataRef node = LLVMValueAsMetadata(LLVMConstInt(lb_type(p->module, t_u32), 1, false));
  2674. LLVMSetMetadata(instr, kind_id, LLVMMetadataAsValue(p->module->ctx, node));
  2675. }
  2676. break;
  2677. break;
  2678. case BuiltinProc_volatile_load:
  2679. LLVMSetVolatile(instr, true);
  2680. break;
  2681. case BuiltinProc_atomic_load:
  2682. LLVMSetOrdering(instr, LLVMAtomicOrderingSequentiallyConsistent);
  2683. LLVMSetVolatile(instr, true);
  2684. break;
  2685. case BuiltinProc_atomic_load_explicit:
  2686. {
  2687. auto ordering = llvm_atomic_ordering_from_odin(ce->args[1]);
  2688. LLVMSetOrdering(instr, ordering);
  2689. LLVMSetVolatile(instr, true);
  2690. }
  2691. break;
  2692. }
  2693. LLVMSetAlignment(instr, cast(unsigned)type_align_of(type_deref(dst.type)));
  2694. lbValue res = {};
  2695. res.value = instr;
  2696. res.type = type_deref(dst.type);
  2697. return res;
  2698. }
  2699. case BuiltinProc_unaligned_store:
  2700. {
  2701. lbValue dst = lb_build_expr(p, ce->args[0]);
  2702. lbValue src = lb_build_expr(p, ce->args[1]);
  2703. Type *t = type_deref(dst.type);
  2704. if (is_type_simd_vector(t)) {
  2705. LLVMValueRef store = LLVMBuildStore(p->builder, src.value, dst.value);
  2706. LLVMSetAlignment(store, 1);
  2707. } else {
  2708. src = lb_address_from_load_or_generate_local(p, src);
  2709. lb_mem_copy_non_overlapping(p, dst, src, lb_const_int(p->module, t_int, type_size_of(t)), false);
  2710. }
  2711. return {};
  2712. }
  2713. case BuiltinProc_unaligned_load:
  2714. {
  2715. lbValue src = lb_build_expr(p, ce->args[0]);
  2716. Type *t = type_deref(src.type);
  2717. if (is_type_simd_vector(t)) {
  2718. lbValue res = {};
  2719. res.type = t;
  2720. res.value = OdinLLVMBuildLoadAligned(p, lb_type(p->module, t), src.value, 1);
  2721. return res;
  2722. } else {
  2723. lbAddr dst = lb_add_local_generated(p, t, false);
  2724. lb_mem_copy_non_overlapping(p, dst.addr, src, lb_const_int(p->module, t_int, type_size_of(t)), false);
  2725. return lb_addr_load(p, dst);
  2726. }
  2727. }
  2728. case BuiltinProc_atomic_add:
  2729. case BuiltinProc_atomic_sub:
  2730. case BuiltinProc_atomic_and:
  2731. case BuiltinProc_atomic_nand:
  2732. case BuiltinProc_atomic_or:
  2733. case BuiltinProc_atomic_xor:
  2734. case BuiltinProc_atomic_exchange:
  2735. case BuiltinProc_atomic_add_explicit:
  2736. case BuiltinProc_atomic_sub_explicit:
  2737. case BuiltinProc_atomic_and_explicit:
  2738. case BuiltinProc_atomic_nand_explicit:
  2739. case BuiltinProc_atomic_or_explicit:
  2740. case BuiltinProc_atomic_xor_explicit:
  2741. case BuiltinProc_atomic_exchange_explicit: {
  2742. lbValue dst = lb_build_expr(p, ce->args[0]);
  2743. lbValue val = lb_build_expr(p, ce->args[1]);
  2744. val = lb_emit_conv(p, val, type_deref(dst.type));
  2745. LLVMAtomicRMWBinOp op = {};
  2746. LLVMAtomicOrdering ordering = {};
  2747. switch (id) {
  2748. case BuiltinProc_atomic_add: op = LLVMAtomicRMWBinOpAdd; ordering = LLVMAtomicOrderingSequentiallyConsistent; break;
  2749. case BuiltinProc_atomic_sub: op = LLVMAtomicRMWBinOpSub; ordering = LLVMAtomicOrderingSequentiallyConsistent; break;
  2750. case BuiltinProc_atomic_and: op = LLVMAtomicRMWBinOpAnd; ordering = LLVMAtomicOrderingSequentiallyConsistent; break;
  2751. case BuiltinProc_atomic_nand: op = LLVMAtomicRMWBinOpNand; ordering = LLVMAtomicOrderingSequentiallyConsistent; break;
  2752. case BuiltinProc_atomic_or: op = LLVMAtomicRMWBinOpOr; ordering = LLVMAtomicOrderingSequentiallyConsistent; break;
  2753. case BuiltinProc_atomic_xor: op = LLVMAtomicRMWBinOpXor; ordering = LLVMAtomicOrderingSequentiallyConsistent; break;
  2754. case BuiltinProc_atomic_exchange: op = LLVMAtomicRMWBinOpXchg; ordering = LLVMAtomicOrderingSequentiallyConsistent; break;
  2755. case BuiltinProc_atomic_add_explicit: op = LLVMAtomicRMWBinOpAdd; ordering = llvm_atomic_ordering_from_odin(ce->args[2]); break;
  2756. case BuiltinProc_atomic_sub_explicit: op = LLVMAtomicRMWBinOpSub; ordering = llvm_atomic_ordering_from_odin(ce->args[2]); break;
  2757. case BuiltinProc_atomic_and_explicit: op = LLVMAtomicRMWBinOpAnd; ordering = llvm_atomic_ordering_from_odin(ce->args[2]); break;
  2758. case BuiltinProc_atomic_nand_explicit: op = LLVMAtomicRMWBinOpNand; ordering = llvm_atomic_ordering_from_odin(ce->args[2]); break;
  2759. case BuiltinProc_atomic_or_explicit: op = LLVMAtomicRMWBinOpOr; ordering = llvm_atomic_ordering_from_odin(ce->args[2]); break;
  2760. case BuiltinProc_atomic_xor_explicit: op = LLVMAtomicRMWBinOpXor; ordering = llvm_atomic_ordering_from_odin(ce->args[2]); break;
  2761. case BuiltinProc_atomic_exchange_explicit: op = LLVMAtomicRMWBinOpXchg; ordering = llvm_atomic_ordering_from_odin(ce->args[2]); break;
  2762. }
  2763. lbValue res = {};
  2764. res.value = LLVMBuildAtomicRMW(p->builder, op, dst.value, val.value, ordering, false);
  2765. res.type = tv.type;
  2766. LLVMSetVolatile(res.value, true);
  2767. return res;
  2768. }
  2769. case BuiltinProc_atomic_compare_exchange_strong:
  2770. case BuiltinProc_atomic_compare_exchange_weak:
  2771. case BuiltinProc_atomic_compare_exchange_strong_explicit:
  2772. case BuiltinProc_atomic_compare_exchange_weak_explicit: {
  2773. lbValue address = lb_build_expr(p, ce->args[0]);
  2774. Type *elem = type_deref(address.type);
  2775. lbValue old_value = lb_build_expr(p, ce->args[1]);
  2776. lbValue new_value = lb_build_expr(p, ce->args[2]);
  2777. old_value = lb_emit_conv(p, old_value, elem);
  2778. new_value = lb_emit_conv(p, new_value, elem);
  2779. LLVMAtomicOrdering success_ordering = {};
  2780. LLVMAtomicOrdering failure_ordering = {};
  2781. LLVMBool weak = false;
  2782. switch (id) {
  2783. case BuiltinProc_atomic_compare_exchange_strong: success_ordering = LLVMAtomicOrderingSequentiallyConsistent; failure_ordering = LLVMAtomicOrderingSequentiallyConsistent; weak = false; break;
  2784. case BuiltinProc_atomic_compare_exchange_weak: success_ordering = LLVMAtomicOrderingSequentiallyConsistent; failure_ordering = LLVMAtomicOrderingSequentiallyConsistent; weak = true; break;
  2785. case BuiltinProc_atomic_compare_exchange_strong_explicit: success_ordering = llvm_atomic_ordering_from_odin(ce->args[3]); failure_ordering = llvm_atomic_ordering_from_odin(ce->args[4]); weak = false; break;
  2786. case BuiltinProc_atomic_compare_exchange_weak_explicit: success_ordering = llvm_atomic_ordering_from_odin(ce->args[3]); failure_ordering = llvm_atomic_ordering_from_odin(ce->args[4]); weak = true; break;
  2787. }
  2788. LLVMBool single_threaded = false;
  2789. LLVMValueRef value = LLVMBuildAtomicCmpXchg(
  2790. p->builder, address.value,
  2791. old_value.value, new_value.value,
  2792. success_ordering,
  2793. failure_ordering,
  2794. single_threaded
  2795. );
  2796. LLVMSetWeak(value, weak);
  2797. LLVMSetVolatile(value, true);
  2798. if (is_type_tuple(tv.type)) {
  2799. Type *fix_typed = alloc_type_tuple();
  2800. slice_init(&fix_typed->Tuple.variables, permanent_allocator(), 2);
  2801. fix_typed->Tuple.variables[0] = tv.type->Tuple.variables[0];
  2802. fix_typed->Tuple.variables[1] = alloc_entity_field(nullptr, blank_token, t_llvm_bool, false, 1);
  2803. lbValue res = {};
  2804. res.value = value;
  2805. res.type = fix_typed;
  2806. return res;
  2807. } else {
  2808. lbValue res = {};
  2809. res.value = LLVMBuildExtractValue(p->builder, value, 0, "");
  2810. res.type = tv.type;
  2811. return res;
  2812. }
  2813. }
  2814. case BuiltinProc_type_equal_proc:
  2815. return lb_equal_proc_for_type(p->module, ce->args[0]->tav.type);
  2816. case BuiltinProc_type_hasher_proc:
  2817. return lb_hasher_proc_for_type(p->module, ce->args[0]->tav.type);
  2818. case BuiltinProc_type_map_info:
  2819. return lb_gen_map_info_ptr(p->module, ce->args[0]->tav.type);
  2820. case BuiltinProc_type_map_cell_info:
  2821. return lb_gen_map_cell_info_ptr(p->module, ce->args[0]->tav.type);
  2822. case BuiltinProc_fixed_point_mul:
  2823. case BuiltinProc_fixed_point_div:
  2824. case BuiltinProc_fixed_point_mul_sat:
  2825. case BuiltinProc_fixed_point_div_sat:
  2826. {
  2827. Type *platform_type = integer_endian_type_to_platform_type(tv.type);
  2828. lbValue x = lb_emit_conv(p, lb_build_expr(p, ce->args[0]), platform_type);
  2829. lbValue y = lb_emit_conv(p, lb_build_expr(p, ce->args[1]), platform_type);
  2830. lbValue scale = lb_emit_conv(p, lb_build_expr(p, ce->args[2]), t_i32);
  2831. char const *name = nullptr;
  2832. if (is_type_unsigned(tv.type)) {
  2833. switch (id) {
  2834. case BuiltinProc_fixed_point_mul: name = "llvm.umul.fix"; break;
  2835. case BuiltinProc_fixed_point_div: name = "llvm.udiv.fix"; break;
  2836. case BuiltinProc_fixed_point_mul_sat: name = "llvm.umul.fix.sat"; break;
  2837. case BuiltinProc_fixed_point_div_sat: name = "llvm.udiv.fix.sat"; break;
  2838. }
  2839. } else {
  2840. switch (id) {
  2841. case BuiltinProc_fixed_point_mul: name = "llvm.smul.fix"; break;
  2842. case BuiltinProc_fixed_point_div: name = "llvm.sdiv.fix"; break;
  2843. case BuiltinProc_fixed_point_mul_sat: name = "llvm.smul.fix.sat"; break;
  2844. case BuiltinProc_fixed_point_div_sat: name = "llvm.sdiv.fix.sat"; break;
  2845. }
  2846. }
  2847. GB_ASSERT(name != nullptr);
  2848. lbValue res = {};
  2849. res.type = platform_type;
  2850. if (id == BuiltinProc_fixed_point_div ||
  2851. id == BuiltinProc_fixed_point_div_sat) {
  2852. res.value = lb_integer_division_intrinsics(p, x.value, y.value, scale.value, platform_type, name);
  2853. } else {
  2854. LLVMTypeRef types[1] = {lb_type(p->module, platform_type)};
  2855. LLVMValueRef args[3] = {
  2856. x.value,
  2857. y.value,
  2858. scale.value };
  2859. res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types));
  2860. }
  2861. return lb_emit_conv(p, res, tv.type);
  2862. }
  2863. case BuiltinProc_expect:
  2864. {
  2865. Type *t = default_type(tv.type);
  2866. lbValue x = lb_emit_conv(p, lb_build_expr(p, ce->args[0]), t);
  2867. lbValue y = lb_emit_conv(p, lb_build_expr(p, ce->args[1]), t);
  2868. char const *name = "llvm.expect";
  2869. LLVMTypeRef types[1] = {lb_type(p->module, t)};
  2870. lbValue res = {};
  2871. LLVMValueRef args[2] = { x.value, y.value };
  2872. res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types));
  2873. res.type = t;
  2874. return lb_emit_conv(p, res, t);
  2875. }
  2876. case BuiltinProc_prefetch_read_instruction:
  2877. case BuiltinProc_prefetch_read_data:
  2878. case BuiltinProc_prefetch_write_instruction:
  2879. case BuiltinProc_prefetch_write_data:
  2880. {
  2881. lbValue ptr = lb_emit_conv(p, lb_build_expr(p, ce->args[0]), t_rawptr);
  2882. unsigned long long locality = cast(unsigned long long)exact_value_to_i64(ce->args[1]->tav.value);
  2883. unsigned long long rw = 0;
  2884. unsigned long long cache = 0;
  2885. switch (id) {
  2886. case BuiltinProc_prefetch_read_instruction:
  2887. rw = 0;
  2888. cache = 0;
  2889. break;
  2890. case BuiltinProc_prefetch_read_data:
  2891. rw = 0;
  2892. cache = 1;
  2893. break;
  2894. case BuiltinProc_prefetch_write_instruction:
  2895. rw = 1;
  2896. cache = 0;
  2897. break;
  2898. case BuiltinProc_prefetch_write_data:
  2899. rw = 1;
  2900. cache = 1;
  2901. break;
  2902. }
  2903. char const *name = "llvm.prefetch";
  2904. LLVMTypeRef types[1] = {lb_type(p->module, t_rawptr)};
  2905. LLVMTypeRef llvm_i32 = lb_type(p->module, t_i32);
  2906. LLVMValueRef args[4] = {};
  2907. args[0] = ptr.value;
  2908. args[1] = LLVMConstInt(llvm_i32, rw, false);
  2909. args[2] = LLVMConstInt(llvm_i32, locality, false);
  2910. args[3] = LLVMConstInt(llvm_i32, cache, false);
  2911. lbValue res = {};
  2912. res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types));
  2913. res.type = nullptr;
  2914. return res;
  2915. }
  2916. case BuiltinProc___entry_point:
  2917. if (p->module->info->entry_point) {
  2918. lbValue entry_point = lb_find_procedure_value_from_entity(p->module, p->module->info->entry_point);
  2919. GB_ASSERT(entry_point.value != nullptr);
  2920. lb_emit_call(p, entry_point, {});
  2921. }
  2922. return {};
  2923. case BuiltinProc_syscall:
  2924. {
  2925. unsigned arg_count = cast(unsigned)ce->args.count;
  2926. LLVMValueRef *args = gb_alloc_array(permanent_allocator(), LLVMValueRef, arg_count);
  2927. for_array(i, ce->args) {
  2928. lbValue arg = lb_build_expr(p, ce->args[i]);
  2929. arg = lb_emit_conv(p, arg, t_uintptr);
  2930. args[i] = arg.value;
  2931. }
  2932. LLVMTypeRef llvm_uintptr = lb_type(p->module, t_uintptr);
  2933. LLVMTypeRef *llvm_arg_types = gb_alloc_array(permanent_allocator(), LLVMTypeRef, arg_count);
  2934. for (unsigned i = 0; i < arg_count; i++) {
  2935. llvm_arg_types[i] = llvm_uintptr;
  2936. }
  2937. LLVMTypeRef func_type = LLVMFunctionType(llvm_uintptr, llvm_arg_types, arg_count, false);
  2938. LLVMValueRef inline_asm = nullptr;
  2939. switch (build_context.metrics.arch) {
  2940. case TargetArch_riscv64:
  2941. {
  2942. GB_ASSERT(arg_count <= 7);
  2943. char asm_string[] = "ecall";
  2944. gbString constraints = gb_string_make(heap_allocator(), "={a0}");
  2945. for (unsigned i = 0; i < arg_count; i++) {
  2946. constraints = gb_string_appendc(constraints, ",{");
  2947. static char const *regs[] = {
  2948. "a7",
  2949. "a0",
  2950. "a1",
  2951. "a2",
  2952. "a3",
  2953. "a4",
  2954. "a5",
  2955. "a6"
  2956. };
  2957. constraints = gb_string_appendc(constraints, regs[i]);
  2958. constraints = gb_string_appendc(constraints, "}");
  2959. }
  2960. constraints = gb_string_appendc(constraints, ",~{memory}");
  2961. inline_asm = llvm_get_inline_asm(func_type, make_string_c(asm_string), make_string_c(constraints));
  2962. }
  2963. break;
  2964. case TargetArch_amd64:
  2965. {
  2966. GB_ASSERT(arg_count <= 7);
  2967. char asm_string[] = "syscall";
  2968. gbString constraints = gb_string_make(heap_allocator(), "={rax}");
  2969. for (unsigned i = 0; i < arg_count; i++) {
  2970. constraints = gb_string_appendc(constraints, ",{");
  2971. static char const *regs[] = {
  2972. "rax",
  2973. "rdi",
  2974. "rsi",
  2975. "rdx",
  2976. "r10",
  2977. "r8",
  2978. "r9"
  2979. };
  2980. constraints = gb_string_appendc(constraints, regs[i]);
  2981. constraints = gb_string_appendc(constraints, "}");
  2982. }
  2983. // The SYSCALL instruction stores the address of the
  2984. // following instruction into RCX, and RFLAGS in R11.
  2985. //
  2986. // RSP is not saved, but at least on Linux it appears
  2987. // that the kernel system-call handler does the right
  2988. // thing.
  2989. //
  2990. // Some but not all system calls will additionally
  2991. // clobber memory.
  2992. //
  2993. // TODO:
  2994. // * Figure out what Darwin does.
  2995. constraints = gb_string_appendc(constraints, ",~{rcx},~{r11},~{memory}");
  2996. inline_asm = llvm_get_inline_asm(func_type, make_string_c(asm_string), make_string_c(constraints));
  2997. }
  2998. break;
  2999. case TargetArch_i386:
  3000. {
  3001. GB_ASSERT(arg_count <= 7);
  3002. char asm_string[] = "int $$0x80";
  3003. gbString constraints = gb_string_make(heap_allocator(), "={eax}");
  3004. for (unsigned i = 0; i < gb_min(arg_count, 6); i++) {
  3005. constraints = gb_string_appendc(constraints, ",{");
  3006. static char const *regs[] = {
  3007. "eax",
  3008. "ebx",
  3009. "ecx",
  3010. "edx",
  3011. "esi",
  3012. "edi",
  3013. "ebp",
  3014. };
  3015. constraints = gb_string_appendc(constraints, regs[i]);
  3016. constraints = gb_string_appendc(constraints, "}");
  3017. }
  3018. constraints = gb_string_appendc(constraints, ",~{memory}");
  3019. inline_asm = llvm_get_inline_asm(func_type, make_string_c(asm_string), make_string_c(constraints));
  3020. }
  3021. break;
  3022. case TargetArch_arm64:
  3023. {
  3024. GB_ASSERT(arg_count <= 7);
  3025. if(build_context.metrics.os == TargetOs_darwin) {
  3026. char asm_string[] = "svc #0x80";
  3027. gbString constraints = gb_string_make(heap_allocator(), "={x0}");
  3028. for (unsigned i = 0; i < arg_count; i++) {
  3029. constraints = gb_string_appendc(constraints, ",{");
  3030. static char const *regs[] = {
  3031. "x16",
  3032. "x0",
  3033. "x1",
  3034. "x2",
  3035. "x3",
  3036. "x4",
  3037. "x5",
  3038. };
  3039. constraints = gb_string_appendc(constraints, regs[i]);
  3040. constraints = gb_string_appendc(constraints, "}");
  3041. }
  3042. constraints = gb_string_appendc(constraints, ",~{memory}");
  3043. inline_asm = llvm_get_inline_asm(func_type, make_string_c(asm_string), make_string_c(constraints));
  3044. } else {
  3045. char asm_string[] = "svc #0";
  3046. gbString constraints = gb_string_make(heap_allocator(), "={x0}");
  3047. for (unsigned i = 0; i < arg_count; i++) {
  3048. constraints = gb_string_appendc(constraints, ",{");
  3049. static char const *regs[] = {
  3050. "x8",
  3051. "x0",
  3052. "x1",
  3053. "x2",
  3054. "x3",
  3055. "x4",
  3056. "x5",
  3057. };
  3058. constraints = gb_string_appendc(constraints, regs[i]);
  3059. constraints = gb_string_appendc(constraints, "}");
  3060. }
  3061. constraints = gb_string_appendc(constraints, ",~{memory}");
  3062. inline_asm = llvm_get_inline_asm(func_type, make_string_c(asm_string), make_string_c(constraints));
  3063. }
  3064. }
  3065. break;
  3066. case TargetArch_arm32:
  3067. {
  3068. GB_ASSERT(arg_count <= 7);
  3069. char asm_string[] = "svc #0";
  3070. gbString constraints = gb_string_make(heap_allocator(), "={r0}");
  3071. for (unsigned i = 0; i < arg_count; i++) {
  3072. constraints = gb_string_appendc(constraints, ",{");
  3073. static char const *regs[] = {
  3074. "r7",
  3075. "r0",
  3076. "r1",
  3077. "r2",
  3078. "r3",
  3079. "r4",
  3080. "r5",
  3081. "r6",
  3082. };
  3083. constraints = gb_string_appendc(constraints, regs[i]);
  3084. constraints = gb_string_appendc(constraints, "}");
  3085. }
  3086. constraints = gb_string_appendc(constraints, ",~{memory}");
  3087. inline_asm = llvm_get_inline_asm(func_type, make_string_c(asm_string), make_string_c(constraints));
  3088. }
  3089. break;
  3090. default:
  3091. GB_PANIC("Unsupported platform");
  3092. }
  3093. lbValue res = {};
  3094. res.value = LLVMBuildCall2(p->builder, func_type, inline_asm, args, arg_count, "");
  3095. res.type = t_uintptr;
  3096. return res;
  3097. }
  3098. case BuiltinProc_syscall_bsd:
  3099. {
  3100. // This is a BSD-style syscall where errors are indicated by a high
  3101. // Carry Flag and a positive return value, allowing the kernel to
  3102. // return any value that fits into a machine word.
  3103. //
  3104. // This is unlike Linux, where errors are indicated by a negative
  3105. // return value, limiting what can be expressed in one result.
  3106. unsigned arg_count = cast(unsigned)ce->args.count;
  3107. LLVMValueRef *args = gb_alloc_array(permanent_allocator(), LLVMValueRef, arg_count);
  3108. for_array(i, ce->args) {
  3109. lbValue arg = lb_build_expr(p, ce->args[i]);
  3110. arg = lb_emit_conv(p, arg, t_uintptr);
  3111. args[i] = arg.value;
  3112. }
  3113. LLVMTypeRef llvm_uintptr = lb_type(p->module, t_uintptr);
  3114. LLVMTypeRef *llvm_arg_types = gb_alloc_array(permanent_allocator(), LLVMTypeRef, arg_count);
  3115. for (unsigned i = 0; i < arg_count; i++) {
  3116. llvm_arg_types[i] = llvm_uintptr;
  3117. }
  3118. LLVMTypeRef *results = gb_alloc_array(permanent_allocator(), LLVMTypeRef, 2);
  3119. results[0] = lb_type(p->module, t_uintptr);
  3120. results[1] = lb_type(p->module, t_bool);
  3121. LLVMTypeRef llvm_results = LLVMStructTypeInContext(p->module->ctx, results, 2, false);
  3122. LLVMTypeRef func_type = LLVMFunctionType(llvm_results, llvm_arg_types, arg_count, false);
  3123. LLVMValueRef inline_asm = nullptr;
  3124. switch (build_context.metrics.arch) {
  3125. case TargetArch_amd64:
  3126. {
  3127. GB_ASSERT(arg_count <= 7);
  3128. char asm_string[] = "syscall; setnb %cl";
  3129. // Using CL as an output; RCX doesn't need to get clobbered later.
  3130. gbString constraints = gb_string_make(heap_allocator(), "={rax},={cl}");
  3131. for (unsigned i = 0; i < arg_count; i++) {
  3132. constraints = gb_string_appendc(constraints, ",{");
  3133. static char const *regs[] = {
  3134. "rax",
  3135. "rdi",
  3136. "rsi",
  3137. "rdx",
  3138. "r10",
  3139. "r8",
  3140. "r9",
  3141. };
  3142. constraints = gb_string_appendc(constraints, regs[i]);
  3143. constraints = gb_string_appendc(constraints, "}");
  3144. }
  3145. // NOTE(Feoramund): If you're experiencing instability
  3146. // regarding syscalls during optimized builds, it is
  3147. // possible that the ABI has changed for your platform,
  3148. // or I've missed a register clobber.
  3149. //
  3150. // Documentation on this topic is sparse, but I was able to
  3151. // determine what registers were being clobbered by adding
  3152. // dummy values to them, setting a breakpoint after the
  3153. // syscall, and checking the state of the registers afterwards.
  3154. //
  3155. // Be advised that manually stepping through a debugger may
  3156. // cause the kernel to not return via sysret, which will
  3157. // preserve register state that normally would've been
  3158. // otherwise clobbered.
  3159. //
  3160. // It is also possible that some syscalls clobber different registers.
  3161. if (build_context.metrics.os == TargetOs_freebsd) {
  3162. // As a fix for CVE-2019-5595, FreeBSD started
  3163. // clobbering R8, R9, and R10, instead of restoring
  3164. // them.
  3165. //
  3166. // More info here:
  3167. //
  3168. // https://www.freebsd.org/security/advisories/FreeBSD-SA-19:01.syscall.asc
  3169. // https://github.com/freebsd/freebsd-src/blob/098dbd7ff7f3da9dda03802cdb2d8755f816eada/sys/amd64/amd64/exception.S#L605
  3170. // https://stackoverflow.com/q/66878250
  3171. constraints = gb_string_appendc(constraints, ",~{r8},~{r9},~{r10}");
  3172. }
  3173. // Both FreeBSD and NetBSD might clobber RDX.
  3174. //
  3175. // For NetBSD, it was clobbered during a call to sysctl.
  3176. //
  3177. // For FreeBSD, it's listed as "return value 2" in their
  3178. // AMD64 assembly, so there's no guarantee that it will persist.
  3179. constraints = gb_string_appendc(constraints, ",~{rdx},~{r11},~{cc},~{memory}");
  3180. inline_asm = llvm_get_inline_asm(func_type, make_string_c(asm_string), make_string_c(constraints));
  3181. }
  3182. break;
  3183. case TargetArch_arm64:
  3184. {
  3185. GB_ASSERT(arg_count <= 7);
  3186. char const *asm_string;
  3187. char const **regs;
  3188. gbString constraints;
  3189. if (build_context.metrics.os == TargetOs_netbsd) {
  3190. asm_string = "svc #0; cset x17, cc";
  3191. constraints = gb_string_make(heap_allocator(), "={x0},={x17}");
  3192. static char const *_regs[] = {
  3193. "x17",
  3194. "x0",
  3195. "x1",
  3196. "x2",
  3197. "x3",
  3198. "x4",
  3199. "x5",
  3200. };
  3201. regs = _regs;
  3202. } else {
  3203. // FreeBSD (tested), OpenBSD (untested).
  3204. asm_string = "svc #0; cset x8, cc";
  3205. constraints = gb_string_make(heap_allocator(), "={x0},={x8}");
  3206. static char const *_regs[] = {
  3207. "x8",
  3208. "x0",
  3209. "x1",
  3210. "x2",
  3211. "x3",
  3212. "x4",
  3213. "x5",
  3214. };
  3215. regs = _regs;
  3216. // FreeBSD clobbered x1 on a call to sysctl.
  3217. constraints = gb_string_appendc(constraints, ",~{x1}");
  3218. }
  3219. for (unsigned i = 0; i < arg_count; i++) {
  3220. constraints = gb_string_appendc(constraints, ",{");
  3221. constraints = gb_string_appendc(constraints, regs[i]);
  3222. constraints = gb_string_appendc(constraints, "}");
  3223. }
  3224. constraints = gb_string_appendc(constraints, ",~{cc},~{memory}");
  3225. inline_asm = llvm_get_inline_asm(func_type, make_string_c(asm_string), make_string_c(constraints));
  3226. }
  3227. break;
  3228. default:
  3229. GB_PANIC("Unsupported platform");
  3230. }
  3231. lbValue res = {};
  3232. res.value = LLVMBuildCall2(p->builder, func_type, inline_asm, args, arg_count, "");
  3233. res.type = make_optional_ok_type(t_uintptr, true);
  3234. return res;
  3235. }
  3236. case BuiltinProc_objc_send:
  3237. return lb_handle_objc_send(p, expr);
  3238. case BuiltinProc_objc_find_selector: return lb_handle_objc_find_selector(p, expr);
  3239. case BuiltinProc_objc_find_class: return lb_handle_objc_find_class(p, expr);
  3240. case BuiltinProc_objc_register_selector: return lb_handle_objc_register_selector(p, expr);
  3241. case BuiltinProc_objc_register_class: return lb_handle_objc_register_class(p, expr);
  3242. case BuiltinProc_objc_ivar_get: return lb_handle_objc_ivar_get(p, expr);
  3243. case BuiltinProc_objc_block: return lb_handle_objc_block(p, expr);
  3244. case BuiltinProc_constant_utf16_cstring:
  3245. {
  3246. auto const encode_surrogate_pair = [](Rune r, u16 *r1, u16 *r2) {
  3247. if (r < 0x10000 || r > 0x10ffff) {
  3248. *r1 = 0xfffd;
  3249. *r2 = 0xfffd;
  3250. } else {
  3251. r -= 0x10000;
  3252. *r1 = 0xd800 + ((r>>10)&0x3ff);
  3253. *r2 = 0xdc00 + (r&0x3ff);
  3254. }
  3255. };
  3256. lbModule *m = p->module;
  3257. auto tav = type_and_value_of_expr(ce->args[0]);
  3258. GB_ASSERT(tav.value.kind == ExactValue_String);
  3259. String value = tav.value.value_string;
  3260. LLVMTypeRef llvm_u16 = lb_type(m, t_u16);
  3261. isize max_len = value.len*2 + 1;
  3262. LLVMValueRef *buffer = gb_alloc_array(temporary_allocator(), LLVMValueRef, max_len);
  3263. isize n = 0;
  3264. while (value.len > 0) {
  3265. Rune r = 0;
  3266. isize w = gb_utf8_decode(value.text, value.len, &r);
  3267. value.text += w;
  3268. value.len -= w;
  3269. if ((0 <= r && r < 0xd800) || (0xe000 <= r && r < 0x10000)) {
  3270. buffer[n++] = LLVMConstInt(llvm_u16, cast(u16)r, false);
  3271. } else if (0x10000 <= r && r <= 0x10ffff) {
  3272. u16 r1, r2;
  3273. encode_surrogate_pair(r, &r1, &r2);
  3274. buffer[n++] = LLVMConstInt(llvm_u16, r1, false);
  3275. buffer[n++] = LLVMConstInt(llvm_u16, r2, false);
  3276. } else {
  3277. buffer[n++] = LLVMConstInt(llvm_u16, 0xfffd, false);
  3278. }
  3279. }
  3280. buffer[n++] = LLVMConstInt(llvm_u16, 0, false);
  3281. LLVMValueRef array = LLVMConstArray(llvm_u16, buffer, cast(unsigned int)n);
  3282. char *name = nullptr;
  3283. {
  3284. isize max_len = 7+8+1;
  3285. name = gb_alloc_array(permanent_allocator(), char, max_len);
  3286. u32 id = m->global_array_index.fetch_add(1);
  3287. isize len = gb_snprintf(name, max_len, "csbs$%x", id);
  3288. len -= 1;
  3289. }
  3290. LLVMTypeRef type = LLVMTypeOf(array);
  3291. LLVMValueRef global_data = LLVMAddGlobal(m->mod, type, name);
  3292. LLVMSetInitializer(global_data, array);
  3293. LLVMSetUnnamedAddress(global_data, LLVMGlobalUnnamedAddr);
  3294. LLVMSetLinkage(global_data, LLVMInternalLinkage);
  3295. LLVMValueRef indices[] = {
  3296. LLVMConstInt(lb_type(m, t_u32), 0, false),
  3297. LLVMConstInt(lb_type(m, t_u32), 0, false),
  3298. };
  3299. lbValue res = {};
  3300. res.type = tv.type;
  3301. res.value = LLVMBuildInBoundsGEP2(p->builder, type, global_data, indices, gb_count_of(indices), "");
  3302. return res;
  3303. }
  3304. case BuiltinProc_wasm_memory_grow:
  3305. {
  3306. char const *name = "llvm.wasm.memory.grow";
  3307. LLVMTypeRef types[1] = {
  3308. lb_type(p->module, t_i32),
  3309. };
  3310. LLVMValueRef args[2] = {};
  3311. args[0] = lb_emit_conv(p, lb_build_expr(p, ce->args[0]), t_uintptr).value;
  3312. args[1] = lb_emit_conv(p, lb_build_expr(p, ce->args[1]), t_uintptr).value;
  3313. lbValue res = {};
  3314. res.type = t_i32;
  3315. res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types));
  3316. return lb_emit_conv(p, res, tv.type);
  3317. }
  3318. case BuiltinProc_wasm_memory_size:
  3319. {
  3320. char const *name = "llvm.wasm.memory.size";
  3321. LLVMTypeRef types[1] = {
  3322. lb_type(p->module, t_i32),
  3323. };
  3324. LLVMValueRef args[1] = {};
  3325. args[0] = lb_emit_conv(p, lb_build_expr(p, ce->args[0]), t_uintptr).value;
  3326. lbValue res = {};
  3327. res.type = t_i32;
  3328. res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types));
  3329. return lb_emit_conv(p, res, tv.type);
  3330. }
  3331. case BuiltinProc_wasm_memory_atomic_wait32:
  3332. {
  3333. char const *name = "llvm.wasm.memory.atomic.wait32";
  3334. Type *t_u32_ptr = alloc_type_pointer(t_u32);
  3335. LLVMValueRef args[3] = {};
  3336. args[0] = lb_emit_conv(p, lb_build_expr(p, ce->args[0]), t_u32_ptr).value;
  3337. args[1] = lb_emit_conv(p, lb_build_expr(p, ce->args[1]), t_u32).value;
  3338. args[2] = lb_emit_conv(p, lb_build_expr(p, ce->args[2]), t_i64).value;
  3339. lbValue res = {};
  3340. res.type = tv.type;
  3341. res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), nullptr, 0);
  3342. return res;
  3343. }
  3344. case BuiltinProc_wasm_memory_atomic_notify32:
  3345. {
  3346. char const *name = "llvm.wasm.memory.atomic.notify";
  3347. Type *t_u32_ptr = alloc_type_pointer(t_u32);
  3348. LLVMValueRef args[2] = {
  3349. lb_emit_conv(p, lb_build_expr(p, ce->args[0]), t_u32_ptr).value,
  3350. lb_emit_conv(p, lb_build_expr(p, ce->args[1]), t_u32).value
  3351. };
  3352. lbValue res = {};
  3353. res.type = tv.type;
  3354. res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), nullptr, 0);
  3355. return res;
  3356. }
  3357. case BuiltinProc_x86_cpuid:
  3358. {
  3359. Type *param_types[2] = {t_u32, t_u32};
  3360. Type *type = alloc_type_proc_from_types(param_types, gb_count_of(param_types), tv.type, false, ProcCC_None);
  3361. LLVMTypeRef func_type = lb_get_procedure_raw_type(p->module, type);
  3362. LLVMValueRef the_asm = llvm_get_inline_asm(
  3363. func_type,
  3364. str_lit("cpuid"),
  3365. str_lit("={ax},={bx},={cx},={dx},{ax},{cx}"),
  3366. true
  3367. );
  3368. GB_ASSERT(the_asm != nullptr);
  3369. LLVMValueRef args[2] = {};
  3370. args[0] = lb_emit_conv(p, lb_build_expr(p, ce->args[0]), t_u32).value;
  3371. args[1] = lb_emit_conv(p, lb_build_expr(p, ce->args[1]), t_u32).value;
  3372. lbValue res = {};
  3373. res.type = tv.type;
  3374. res.value = LLVMBuildCall2(p->builder, func_type, the_asm, args, gb_count_of(args), "");
  3375. return res;
  3376. }
  3377. case BuiltinProc_x86_xgetbv:
  3378. {
  3379. Type *type = alloc_type_proc_from_types(&t_u32, 1, tv.type, false, ProcCC_None);
  3380. LLVMTypeRef func_type = lb_get_procedure_raw_type(p->module, type);
  3381. LLVMValueRef the_asm = llvm_get_inline_asm(
  3382. func_type,
  3383. str_lit("xgetbv"),
  3384. str_lit("={ax},={dx},{cx}"),
  3385. true
  3386. );
  3387. GB_ASSERT(the_asm != nullptr);
  3388. LLVMValueRef args[1] = {};
  3389. args[0] = lb_emit_conv(p, lb_build_expr(p, ce->args[0]), t_u32).value;
  3390. lbValue res = {};
  3391. res.type = tv.type;
  3392. res.value = LLVMBuildCall2(p->builder, func_type, the_asm, args, gb_count_of(args), "");
  3393. return res;
  3394. }
  3395. case BuiltinProc_valgrind_client_request:
  3396. {
  3397. lbValue args[7] = {};
  3398. for (isize i = 0; i < 7; i++) {
  3399. args[i] = lb_emit_conv(p, lb_build_expr(p, ce->args[i]), t_uintptr);
  3400. }
  3401. if (!build_context.ODIN_VALGRIND_SUPPORT) {
  3402. return args[0];
  3403. }
  3404. lbValue array = lb_generate_local_array(p, t_uintptr, 6, false);
  3405. for (isize i = 0; i < 6; i++) {
  3406. lbValue gep = lb_emit_array_epi(p, array, i);
  3407. lb_emit_store(p, gep, args[i+1]);
  3408. }
  3409. switch (build_context.metrics.arch) {
  3410. case TargetArch_amd64:
  3411. {
  3412. Type *param_types[2] = {};
  3413. param_types[0] = t_uintptr;
  3414. param_types[1] = array.type;
  3415. Type *type = alloc_type_proc_from_types(param_types, gb_count_of(param_types), t_uintptr, false, ProcCC_None);
  3416. LLVMTypeRef func_type = lb_get_procedure_raw_type(p->module, type);
  3417. LLVMValueRef the_asm = llvm_get_inline_asm(
  3418. func_type,
  3419. str_lit("rolq $$3, %rdi; rolq $$13, %rdi\n rolq $$61, %rdi; rolq $$51, %rdi\n xchgq %rbx, %rbx"),
  3420. str_lit("={rdx},{rdx},{rax},~{cc},~{memory}"),
  3421. true
  3422. );
  3423. LLVMValueRef asm_args[2] = {};
  3424. asm_args[0] = args[0].value;
  3425. asm_args[1] = array.value;
  3426. lbValue res = {};
  3427. res.type = t_uintptr;
  3428. res.value = LLVMBuildCall2(p->builder, func_type, the_asm, asm_args, gb_count_of(asm_args), "");
  3429. return res;
  3430. }
  3431. break;
  3432. default:
  3433. GB_PANIC("Unsupported architecture: %.*s", LIT(target_arch_names[build_context.metrics.arch]));
  3434. break;
  3435. }
  3436. }
  3437. }
  3438. GB_PANIC("Unhandled built-in procedure %.*s", LIT(builtin_procs[id].name));
  3439. return {};
  3440. }
  3441. gb_internal lbValue lb_handle_param_value(lbProcedure *p, Type *parameter_type, ParameterValue const &param_value, TypeProc *procedure_type, Ast* call_expression) {
  3442. switch (param_value.kind) {
  3443. case ParameterValue_Constant:
  3444. if (is_type_constant_type(parameter_type)) {
  3445. auto res = lb_const_value(p->module, parameter_type, param_value.value);
  3446. return res;
  3447. } else {
  3448. ExactValue ev = param_value.value;
  3449. lbValue arg = {};
  3450. Type *type = type_of_expr(param_value.original_ast_expr);
  3451. if (type != nullptr) {
  3452. arg = lb_const_value(p->module, type, ev);
  3453. } else {
  3454. arg = lb_const_value(p->module, parameter_type, param_value.value);
  3455. }
  3456. return lb_emit_conv(p, arg, parameter_type);
  3457. }
  3458. case ParameterValue_Nil:
  3459. return lb_const_nil(p->module, parameter_type);
  3460. case ParameterValue_Location:
  3461. {
  3462. String proc_name = {};
  3463. if (p->entity != nullptr) {
  3464. proc_name = p->entity->token.string;
  3465. }
  3466. ast_node(ce, CallExpr, call_expression);
  3467. TokenPos pos = ast_token(ce->proc).pos;
  3468. return lb_emit_source_code_location_as_global(p, proc_name, pos);
  3469. }
  3470. case ParameterValue_Expression:
  3471. {
  3472. Ast *orig = param_value.original_ast_expr;
  3473. if (orig->kind == Ast_BasicDirective) {
  3474. gbString expr = expr_to_string(call_expression, temporary_allocator());
  3475. return lb_const_string(p->module, make_string_c(expr));
  3476. }
  3477. isize param_idx = -1;
  3478. String param_str = {0};
  3479. {
  3480. Ast *call = unparen_expr(orig);
  3481. GB_ASSERT(call->kind == Ast_CallExpr);
  3482. ast_node(ce, CallExpr, call);
  3483. GB_ASSERT(ce->proc->kind == Ast_BasicDirective);
  3484. GB_ASSERT(ce->args.count == 1);
  3485. Ast *target = ce->args[0];
  3486. GB_ASSERT(target->kind == Ast_Ident);
  3487. String target_str = target->Ident.token.string;
  3488. param_idx = lookup_procedure_parameter(procedure_type, target_str);
  3489. param_str = target_str;
  3490. }
  3491. GB_ASSERT(param_idx >= 0);
  3492. Ast *target_expr = nullptr;
  3493. ast_node(ce, CallExpr, call_expression);
  3494. if (ce->split_args->positional.count > param_idx) {
  3495. target_expr = ce->split_args->positional[param_idx];
  3496. }
  3497. for_array(i, ce->split_args->named) {
  3498. Ast *arg = ce->split_args->named[i];
  3499. ast_node(fv, FieldValue, arg);
  3500. GB_ASSERT(fv->field->kind == Ast_Ident);
  3501. String name = fv->field->Ident.token.string;
  3502. if (name == param_str) {
  3503. target_expr = fv->value;
  3504. break;
  3505. }
  3506. }
  3507. gbString expr = expr_to_string(target_expr, temporary_allocator());
  3508. return lb_const_string(p->module, make_string_c(expr));
  3509. }
  3510. case ParameterValue_Value:
  3511. return lb_build_expr(p, param_value.ast_value);
  3512. }
  3513. return lb_const_nil(p->module, parameter_type);
  3514. }
  3515. gb_internal lbValue lb_build_call_expr_internal(lbProcedure *p, Ast *expr);
  3516. gb_internal lbValue lb_build_call_expr(lbProcedure *p, Ast *expr) {
  3517. expr = unparen_expr(expr);
  3518. ast_node(ce, CallExpr, expr);
  3519. lbValue res = lb_build_call_expr_internal(p, expr);
  3520. if (ce->optional_ok_one) {
  3521. GB_ASSERT(is_type_tuple(res.type));
  3522. GB_ASSERT(res.type->Tuple.variables.count == 2);
  3523. return lb_emit_struct_ev(p, res, 0);
  3524. }
  3525. return res;
  3526. }
  3527. gb_internal void lb_add_values_to_array(lbProcedure *p, Array<lbValue> *args, lbValue value) {
  3528. if (is_type_tuple(value.type)) {
  3529. for_array(i, value.type->Tuple.variables) {
  3530. lbValue sub_value = lb_emit_struct_ev(p, value, cast(i32)i);
  3531. array_add(args, sub_value);
  3532. }
  3533. } else {
  3534. array_add(args, value);
  3535. }
  3536. }
  3537. gb_internal lbValue lb_build_call_expr_internal(lbProcedure *p, Ast *expr) {
  3538. lbModule *m = p->module;
  3539. TypeAndValue tv = type_and_value_of_expr(expr);
  3540. ast_node(ce, CallExpr, expr);
  3541. TypeAndValue proc_tv = type_and_value_of_expr(ce->proc);
  3542. AddressingMode proc_mode = proc_tv.mode;
  3543. if (proc_mode == Addressing_Type) {
  3544. GB_ASSERT(ce->args.count == 1);
  3545. lbValue x = lb_build_expr(p, ce->args[0]);
  3546. lbValue y = lb_emit_conv(p, x, tv.type);
  3547. y.type = tv.type;
  3548. return y;
  3549. }
  3550. Ast *proc_expr = unparen_expr(ce->proc);
  3551. if (proc_mode == Addressing_Builtin) {
  3552. Entity *e = entity_of_node(proc_expr);
  3553. BuiltinProcId id = BuiltinProc_Invalid;
  3554. if (e != nullptr) {
  3555. id = cast(BuiltinProcId)e->Builtin.id;
  3556. } else {
  3557. id = BuiltinProc_DIRECTIVE;
  3558. }
  3559. return lb_build_builtin_proc(p, expr, tv, id);
  3560. }
  3561. // NOTE(bill): Regular call
  3562. lbValue value = {};
  3563. Entity *proc_entity = entity_of_node(proc_expr);
  3564. if (proc_entity != nullptr) {
  3565. if (proc_entity->flags & EntityFlag_Disabled) {
  3566. GB_ASSERT(tv.type == nullptr);
  3567. return {};
  3568. }
  3569. }
  3570. if (proc_expr->tav.mode == Addressing_Constant) {
  3571. ExactValue v = proc_expr->tav.value;
  3572. switch (v.kind) {
  3573. case ExactValue_Integer:
  3574. {
  3575. u64 u = big_int_to_u64(&v.value_integer);
  3576. lbValue x = {};
  3577. x.value = LLVMConstInt(lb_type(m, t_uintptr), u, false);
  3578. x.type = t_uintptr;
  3579. x = lb_emit_conv(p, x, t_rawptr);
  3580. value = lb_emit_conv(p, x, proc_expr->tav.type);
  3581. break;
  3582. }
  3583. case ExactValue_Pointer:
  3584. {
  3585. u64 u = cast(u64)v.value_pointer;
  3586. lbValue x = {};
  3587. x.value = LLVMConstInt(lb_type(m, t_uintptr), u, false);
  3588. x.type = t_uintptr;
  3589. x = lb_emit_conv(p, x, t_rawptr);
  3590. value = lb_emit_conv(p, x, proc_expr->tav.type);
  3591. break;
  3592. }
  3593. }
  3594. }
  3595. if (value.value == nullptr) {
  3596. value = lb_build_expr(p, proc_expr);
  3597. }
  3598. GB_ASSERT(value.value != nullptr);
  3599. Type *proc_type_ = base_type(value.type);
  3600. GB_ASSERT(proc_type_->kind == Type_Proc);
  3601. TypeProc *pt = &proc_type_->Proc;
  3602. GB_ASSERT(ce->split_args != nullptr);
  3603. auto args = array_make<lbValue>(permanent_allocator(), 0, pt->param_count);
  3604. bool vari_expand = (ce->ellipsis.pos.line != 0);
  3605. bool is_c_vararg = pt->c_vararg;
  3606. for_array(i, ce->split_args->positional) {
  3607. Entity *e = pt->params->Tuple.variables[i];
  3608. if (e->kind == Entity_TypeName) {
  3609. array_add(&args, lb_const_nil(p->module, e->type));
  3610. continue;
  3611. } else if (e->kind == Entity_Constant) {
  3612. array_add(&args, lb_const_value(p->module, e->type, e->Constant.value));
  3613. continue;
  3614. }
  3615. GB_ASSERT(e->kind == Entity_Variable);
  3616. if (pt->variadic && pt->variadic_index == i) {
  3617. lbValue variadic_args = lb_const_nil(p->module, e->type);
  3618. auto variadic = slice(ce->split_args->positional, pt->variadic_index, ce->split_args->positional.count);
  3619. if (variadic.count != 0) {
  3620. // variadic call argument generation
  3621. Type *slice_type = e->type;
  3622. GB_ASSERT(slice_type->kind == Type_Slice);
  3623. if (is_c_vararg) {
  3624. GB_ASSERT(!vari_expand);
  3625. Type *elem_type = slice_type->Slice.elem;
  3626. for (Ast *var_arg : variadic) {
  3627. lbValue arg = lb_build_expr(p, var_arg);
  3628. if (is_type_any(elem_type)) {
  3629. if (is_type_untyped_nil(arg.type)) {
  3630. arg = lb_const_nil(p->module, t_rawptr);
  3631. }
  3632. array_add(&args, lb_emit_c_vararg(p, arg, arg.type));
  3633. } else {
  3634. array_add(&args, lb_emit_c_vararg(p, arg, elem_type));
  3635. }
  3636. }
  3637. break;
  3638. } else if (vari_expand) {
  3639. GB_ASSERT(variadic.count == 1);
  3640. variadic_args = lb_build_expr(p, variadic[0]);
  3641. variadic_args = lb_emit_conv(p, variadic_args, slice_type);
  3642. } else {
  3643. Type *elem_type = slice_type->Slice.elem;
  3644. auto var_args = array_make<lbValue>(heap_allocator(), 0, variadic.count);
  3645. defer (array_free(&var_args));
  3646. for (Ast *var_arg : variadic) {
  3647. lbValue v = lb_build_expr(p, var_arg);
  3648. lb_add_values_to_array(p, &var_args, v);
  3649. }
  3650. isize slice_len = var_args.count;
  3651. if (slice_len > 0) {
  3652. lbAddr slice = {};
  3653. for (auto const &vr : p->variadic_reuses) {
  3654. if (are_types_identical(vr.slice_type, slice_type)) {
  3655. slice = vr.slice_addr;
  3656. break;
  3657. }
  3658. }
  3659. DeclInfo *d = decl_info_of_entity(p->entity);
  3660. if (d != nullptr && slice.addr.value == nullptr) {
  3661. for (auto const &vr : d->variadic_reuses) {
  3662. if (are_types_identical(vr.slice_type, slice_type)) {
  3663. #if LLVM_VERSION_MAJOR >= 13
  3664. // NOTE(bill): No point wasting even more memory, just reuse this stack variable too
  3665. if (p->variadic_reuses.count > 0) {
  3666. slice = p->variadic_reuses[0].slice_addr;
  3667. } else {
  3668. slice = lb_add_local_generated(p, slice_type, true);
  3669. }
  3670. // NOTE(bill): Change the underlying type to match the specific type
  3671. slice.addr.type = alloc_type_pointer(slice_type);
  3672. #else
  3673. slice = lb_add_local_generated(p, slice_type, true);
  3674. #endif
  3675. array_add(&p->variadic_reuses, lbVariadicReuseSlices{slice_type, slice});
  3676. break;
  3677. }
  3678. }
  3679. }
  3680. lbValue base_array_ptr = p->variadic_reuse_base_array_ptr.addr;
  3681. if (base_array_ptr.value == nullptr) {
  3682. if (d != nullptr) {
  3683. i64 max_bytes = d->variadic_reuse_max_bytes;
  3684. i64 max_align = gb_max(d->variadic_reuse_max_align, 16);
  3685. p->variadic_reuse_base_array_ptr = lb_add_local_generated(p, alloc_type_array(t_u8, max_bytes), true);
  3686. lb_try_update_alignment(p->variadic_reuse_base_array_ptr.addr, cast(unsigned)max_align);
  3687. base_array_ptr = p->variadic_reuse_base_array_ptr.addr;
  3688. } else {
  3689. base_array_ptr = lb_add_local_generated(p, alloc_type_array(elem_type, slice_len), true).addr;
  3690. }
  3691. }
  3692. if (slice.addr.value == nullptr) {
  3693. slice = lb_add_local_generated(p, slice_type, true);
  3694. }
  3695. GB_ASSERT(base_array_ptr.value != nullptr);
  3696. GB_ASSERT(slice.addr.value != nullptr);
  3697. base_array_ptr = lb_emit_conv(p, base_array_ptr, alloc_type_pointer(alloc_type_array(elem_type, slice_len)));
  3698. for (isize i = 0; i < var_args.count; i++) {
  3699. lbValue addr = lb_emit_array_epi(p, base_array_ptr, cast(i32)i);
  3700. lbValue var_arg = var_args[i];
  3701. var_arg = lb_emit_conv(p, var_arg, elem_type);
  3702. lb_emit_store(p, addr, var_arg);
  3703. }
  3704. lbValue base_elem = lb_emit_array_epi(p, base_array_ptr, 0);
  3705. lbValue len = lb_const_int(p->module, t_int, slice_len);
  3706. lb_fill_slice(p, slice, base_elem, len);
  3707. variadic_args = lb_addr_load(p, slice);
  3708. }
  3709. }
  3710. }
  3711. array_add(&args, variadic_args);
  3712. break;
  3713. } else {
  3714. lbValue value = lb_build_expr(p, ce->split_args->positional[i]);
  3715. lb_add_values_to_array(p, &args, value);
  3716. }
  3717. }
  3718. if (!is_c_vararg) {
  3719. array_resize(&args, pt->param_count);
  3720. }
  3721. for (Ast *arg : ce->split_args->named) {
  3722. ast_node(fv, FieldValue, arg);
  3723. GB_ASSERT(fv->field->kind == Ast_Ident);
  3724. String name = fv->field->Ident.token.string;
  3725. gb_unused(name);
  3726. isize param_index = lookup_procedure_parameter(pt, name);
  3727. GB_ASSERT(param_index >= 0);
  3728. Entity *e = pt->params->Tuple.variables[param_index];
  3729. if (e->kind == Entity_TypeName) {
  3730. lbValue value = lb_const_nil(p->module, e->type);
  3731. args[param_index] = value;
  3732. } else if (is_c_vararg && pt->variadic && pt->variadic_index == param_index) {
  3733. GB_ASSERT(param_index == pt->param_count-1);
  3734. Type *slice_type = e->type;
  3735. GB_ASSERT(slice_type->kind == Type_Slice);
  3736. Type *elem_type = slice_type->Slice.elem;
  3737. if (fv->value->kind == Ast_CompoundLit) {
  3738. ast_node(literal, CompoundLit, fv->value);
  3739. for (Ast *var_arg : literal->elems) {
  3740. lbValue arg = lb_build_expr(p, var_arg);
  3741. if (is_type_any(elem_type)) {
  3742. if (is_type_untyped_nil(arg.type)) {
  3743. arg = lb_const_nil(p->module, t_rawptr);
  3744. }
  3745. array_add(&args, lb_emit_c_vararg(p, arg, arg.type));
  3746. } else {
  3747. array_add(&args, lb_emit_c_vararg(p, arg, elem_type));
  3748. }
  3749. }
  3750. } else {
  3751. lbValue value = lb_build_expr(p, fv->value);
  3752. GB_ASSERT(!is_type_tuple(value.type));
  3753. array_add(&args, lb_emit_c_vararg(p, value, value.type));
  3754. }
  3755. } else {
  3756. lbValue value = lb_build_expr(p, fv->value);
  3757. GB_ASSERT(!is_type_tuple(value.type));
  3758. args[param_index] = value;
  3759. }
  3760. }
  3761. if (pt->params != nullptr) {
  3762. isize min_count = pt->params->Tuple.variables.count;
  3763. if (is_c_vararg) {
  3764. min_count -= 1;
  3765. }
  3766. GB_ASSERT(args.count >= min_count);
  3767. for_array(arg_index, pt->params->Tuple.variables) {
  3768. Entity *e = pt->params->Tuple.variables[arg_index];
  3769. if (pt->variadic && arg_index == pt->variadic_index) {
  3770. if (!is_c_vararg && args[arg_index].value == 0) {
  3771. args[arg_index] = lb_const_nil(p->module, e->type);
  3772. }
  3773. continue;
  3774. }
  3775. lbValue arg = args[arg_index];
  3776. if (arg.value == nullptr && arg.type == nullptr) {
  3777. switch (e->kind) {
  3778. case Entity_TypeName:
  3779. args[arg_index] = lb_const_nil(p->module, e->type);
  3780. break;
  3781. case Entity_Variable:
  3782. args[arg_index] = lb_handle_param_value(p, e->type, e->Variable.param_value, pt, expr);
  3783. break;
  3784. case Entity_Constant:
  3785. args[arg_index] = lb_const_value(p->module, e->type, e->Constant.value);
  3786. break;
  3787. default:
  3788. GB_PANIC("Unknown entity kind %.*s\n", LIT(entity_strings[e->kind]));
  3789. }
  3790. } else {
  3791. args[arg_index] = lb_emit_conv(p, arg, e->type);
  3792. }
  3793. }
  3794. }
  3795. isize final_count = is_c_vararg ? args.count : pt->param_count;
  3796. auto call_args = array_slice(args, 0, final_count);
  3797. return lb_emit_call(p, value, call_args, ce->inlining);
  3798. }