check_builtin.cpp 195 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402
  1. typedef bool (BuiltinTypeIsProc)(Type *t);
  2. gb_global BuiltinTypeIsProc *builtin_type_is_procs[BuiltinProc__type_simple_boolean_end - BuiltinProc__type_simple_boolean_begin] = {
  3. nullptr, // BuiltinProc__type_simple_boolean_begin
  4. is_type_boolean,
  5. is_type_integer,
  6. is_type_rune,
  7. is_type_float,
  8. is_type_complex,
  9. is_type_quaternion,
  10. is_type_string,
  11. is_type_typeid,
  12. is_type_any,
  13. is_type_endian_platform,
  14. is_type_endian_little,
  15. is_type_endian_big,
  16. is_type_unsigned,
  17. is_type_numeric,
  18. is_type_ordered,
  19. is_type_ordered_numeric,
  20. is_type_indexable,
  21. is_type_sliceable,
  22. is_type_comparable,
  23. is_type_simple_compare,
  24. is_type_dereferenceable,
  25. is_type_valid_for_keys,
  26. is_type_valid_for_matrix_elems,
  27. is_type_named,
  28. is_type_pointer,
  29. is_type_multi_pointer,
  30. is_type_array,
  31. is_type_enumerated_array,
  32. is_type_slice,
  33. is_type_dynamic_array,
  34. is_type_map,
  35. is_type_struct,
  36. is_type_union,
  37. is_type_enum,
  38. is_type_proc,
  39. is_type_bit_set,
  40. is_type_simd_vector,
  41. is_type_matrix,
  42. is_type_polymorphic_record_specialized,
  43. is_type_polymorphic_record_unspecialized,
  44. type_has_nil,
  45. };
  46. gb_internal void check_or_else_right_type(CheckerContext *c, Ast *expr, String const &name, Type *right_type) {
  47. if (right_type == nullptr) {
  48. return;
  49. }
  50. if (!is_type_boolean(right_type) && !type_has_nil(right_type)) {
  51. gbString str = type_to_string(right_type);
  52. error(expr, "'%.*s' expects an \"optional ok\" like value, or an n-valued expression where the last value is either a boolean or can be compared against 'nil', got %s", LIT(name), str);
  53. gb_string_free(str);
  54. }
  55. }
  56. gb_internal void check_or_else_split_types(CheckerContext *c, Operand *x, String const &name, Type **left_type_, Type **right_type_) {
  57. Type *left_type = nullptr;
  58. Type *right_type = nullptr;
  59. if (x->type->kind == Type_Tuple) {
  60. auto const &vars = x->type->Tuple.variables;
  61. auto lhs = slice(vars, 0, vars.count-1);
  62. auto rhs = vars[vars.count-1];
  63. if (lhs.count == 1) {
  64. left_type = lhs[0]->type;
  65. } else if (lhs.count != 0) {
  66. left_type = alloc_type_tuple();
  67. left_type->Tuple.variables = lhs;
  68. }
  69. right_type = rhs->type;
  70. } else {
  71. check_promote_optional_ok(c, x, &left_type, &right_type);
  72. }
  73. if (left_type_) *left_type_ = left_type;
  74. if (right_type_) *right_type_ = right_type;
  75. check_or_else_right_type(c, x->expr, name, right_type);
  76. }
  77. gb_internal void check_or_else_expr_no_value_error(CheckerContext *c, String const &name, Operand const &x, Type *type_hint) {
  78. ERROR_BLOCK();
  79. gbString t = type_to_string(x.type);
  80. error(x.expr, "'%.*s' does not return a value, value is of type %s", LIT(name), t);
  81. if (is_type_union(type_deref(x.type))) {
  82. Type *bsrc = base_type(type_deref(x.type));
  83. gbString th = nullptr;
  84. if (type_hint != nullptr) {
  85. GB_ASSERT(bsrc->kind == Type_Union);
  86. for (Type *vt : bsrc->Union.variants) {
  87. if (are_types_identical(vt, type_hint)) {
  88. th = type_to_string(type_hint);
  89. break;
  90. }
  91. }
  92. }
  93. gbString expr_str = expr_to_string(x.expr);
  94. if (th != nullptr) {
  95. error_line("\tSuggestion: was a type assertion such as %s.(%s) or %s.? wanted?\n", expr_str, th, expr_str);
  96. } else {
  97. error_line("\tSuggestion: was a type assertion such as %s.(T) or %s.? wanted?\n", expr_str, expr_str);
  98. }
  99. gb_string_free(th);
  100. gb_string_free(expr_str);
  101. }
  102. gb_string_free(t);
  103. }
  104. gb_internal void check_or_return_split_types(CheckerContext *c, Operand *x, String const &name, Type **left_type_, Type **right_type_) {
  105. Type *left_type = nullptr;
  106. Type *right_type = nullptr;
  107. if (x->type->kind == Type_Tuple) {
  108. auto const &vars = x->type->Tuple.variables;
  109. auto lhs = slice(vars, 0, vars.count-1);
  110. auto rhs = vars[vars.count-1];
  111. if (lhs.count == 1) {
  112. left_type = lhs[0]->type;
  113. } else if (lhs.count != 0) {
  114. left_type = alloc_type_tuple();
  115. left_type->Tuple.variables = lhs;
  116. }
  117. right_type = rhs->type;
  118. } else {
  119. check_promote_optional_ok(c, x, &left_type, &right_type);
  120. }
  121. if (left_type_) *left_type_ = left_type;
  122. if (right_type_) *right_type_ = right_type;
  123. check_or_else_right_type(c, x->expr, name, right_type);
  124. }
  125. gb_internal bool does_require_msgSend_stret(Type *return_type) {
  126. if (return_type == nullptr) {
  127. return false;
  128. }
  129. if (build_context.metrics.arch == TargetArch_i386 || build_context.metrics.arch == TargetArch_amd64) {
  130. i64 struct_limit = type_size_of(t_uintptr) << 1;
  131. return type_size_of(return_type) > struct_limit;
  132. }
  133. if (build_context.metrics.arch == TargetArch_arm64) {
  134. return false;
  135. }
  136. // if (build_context.metrics.arch == TargetArch_arm32) {
  137. // i64 struct_limit = type_size_of(t_uintptr);
  138. // // NOTE(bill): This is technically wrong
  139. // return is_type_struct(return_type) && !is_type_raw_union(return_type) && type_size_of(return_type) > struct_limit;
  140. // }
  141. GB_PANIC("unsupported architecture");
  142. return false;
  143. }
  144. gb_internal ObjcMsgKind get_objc_proc_kind(Type *return_type) {
  145. if (return_type == nullptr) {
  146. return ObjcMsg_normal;
  147. }
  148. if (build_context.metrics.arch == TargetArch_i386 || build_context.metrics.arch == TargetArch_amd64) {
  149. if (is_type_float(return_type)) {
  150. return ObjcMsg_fpret;
  151. }
  152. if (build_context.metrics.arch == TargetArch_amd64) {
  153. if (is_type_complex(return_type)) {
  154. // URL: https://github.com/opensource-apple/objc4/blob/cd5e62a5597ea7a31dccef089317abb3a661c154/runtime/message.h#L143-L159
  155. return ObjcMsg_fpret;
  156. }
  157. }
  158. }
  159. if (build_context.metrics.arch != TargetArch_arm64) {
  160. if (does_require_msgSend_stret(return_type)) {
  161. return ObjcMsg_stret;
  162. }
  163. }
  164. return ObjcMsg_normal;
  165. }
  166. gb_internal void add_objc_proc_type(CheckerContext *c, Ast *call, Type *return_type, Slice<Type *> param_types) {
  167. ObjcMsgKind kind = get_objc_proc_kind(return_type);
  168. Scope *scope = create_scope(c->info, nullptr);
  169. // NOTE(bill, 2022-02-08): the backend's ABI handling should handle this correctly, I hope
  170. Type *params = alloc_type_tuple();
  171. {
  172. auto variables = array_make<Entity *>(permanent_allocator(), 0, param_types.count);
  173. for (Type *type : param_types) {
  174. Entity *param = alloc_entity_param(scope, blank_token, type, false, true);
  175. array_add(&variables, param);
  176. }
  177. params->Tuple.variables = slice_from_array(variables);
  178. }
  179. Type *results = alloc_type_tuple();
  180. if (return_type) {
  181. auto variables = array_make<Entity *>(permanent_allocator(), 1);
  182. results->Tuple.variables = slice_from_array(variables);
  183. Entity *param = alloc_entity_param(scope, blank_token, return_type, false, true);
  184. results->Tuple.variables[0] = param;
  185. }
  186. ObjcMsgData data = {};
  187. data.kind = kind;
  188. data.proc_type = alloc_type_proc(scope, params, param_types.count, results, results->Tuple.variables.count, false, ProcCC_CDecl);
  189. mutex_lock(&c->info->objc_types_mutex);
  190. map_set(&c->info->objc_msgSend_types, call, data);
  191. mutex_unlock(&c->info->objc_types_mutex);
  192. try_to_add_package_dependency(c, "runtime", "objc_msgSend");
  193. try_to_add_package_dependency(c, "runtime", "objc_msgSend_fpret");
  194. try_to_add_package_dependency(c, "runtime", "objc_msgSend_fp2ret");
  195. try_to_add_package_dependency(c, "runtime", "objc_msgSend_stret");
  196. }
  197. gb_internal bool is_constant_string(CheckerContext *c, String const &builtin_name, Ast *expr, String *name_) {
  198. Operand op = {};
  199. check_expr(c, &op, expr);
  200. if (op.mode == Addressing_Constant && op.value.kind == ExactValue_String) {
  201. if (name_) *name_ = op.value.value_string;
  202. return true;
  203. }
  204. gbString e = expr_to_string(op.expr);
  205. gbString t = type_to_string(op.type);
  206. error(op.expr, "'%.*s' expected a constant string value, got %s of type %s", LIT(builtin_name), e, t);
  207. gb_string_free(t);
  208. gb_string_free(e);
  209. return false;
  210. }
  211. gb_internal bool check_builtin_objc_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 id, Type *type_hint) {
  212. String const &builtin_name = builtin_procs[id].name;
  213. if (build_context.metrics.os != TargetOs_darwin) {
  214. // allow on doc generation (e.g. Metal stuff)
  215. if (build_context.command_kind != Command_doc && build_context.command_kind != Command_check) {
  216. error(call, "'%.*s' only works on darwin", LIT(builtin_name));
  217. }
  218. }
  219. ast_node(ce, CallExpr, call);
  220. switch (id) {
  221. default:
  222. GB_PANIC("Implement objective built-in procedure: %.*s", LIT(builtin_name));
  223. return false;
  224. case BuiltinProc_objc_send: {
  225. Type *return_type = nullptr;
  226. Operand rt = {};
  227. check_expr_or_type(c, &rt, ce->args[0]);
  228. if (rt.mode == Addressing_Type) {
  229. return_type = rt.type;
  230. } else if (is_operand_nil(rt)) {
  231. return_type = nullptr;
  232. } else {
  233. gbString e = expr_to_string(rt.expr);
  234. error(rt.expr, "'%.*s' expected a type or nil to define the return type of the Objective-C call, got %s", LIT(builtin_name), e);
  235. gb_string_free(e);
  236. return false;
  237. }
  238. operand->type = return_type;
  239. operand->mode = return_type ? Addressing_Value : Addressing_NoValue;
  240. String class_name = {};
  241. String sel_name = {};
  242. Type *sel_type = t_objc_SEL;
  243. Operand self = {};
  244. check_expr_or_type(c, &self, ce->args[1]);
  245. if (self.mode == Addressing_Type) {
  246. if (!is_type_objc_object(self.type)) {
  247. gbString t = type_to_string(self.type);
  248. error(self.expr, "'%.*s' expected a type or value derived from intrinsics.objc_object, got type %s", LIT(builtin_name), t);
  249. gb_string_free(t);
  250. return false;
  251. }
  252. if (!has_type_got_objc_class_attribute(self.type)) {
  253. gbString t = type_to_string(self.type);
  254. error(self.expr, "'%.*s' expected a named type with the attribute @(obj_class=<string>) , got type %s", LIT(builtin_name), t);
  255. gb_string_free(t);
  256. return false;
  257. }
  258. sel_type = t_objc_Class;
  259. } else if (!is_operand_value(self) || !check_is_assignable_to(c, &self, t_objc_id)) {
  260. gbString e = expr_to_string(self.expr);
  261. gbString t = type_to_string(self.type);
  262. error(self.expr, "'%.*s' expected a type or value derived from intrinsics.objc_object, got '%s' of type %s", LIT(builtin_name), e, t);
  263. gb_string_free(t);
  264. gb_string_free(e);
  265. return false;
  266. } else if (!is_type_pointer(self.type)) {
  267. gbString e = expr_to_string(self.expr);
  268. gbString t = type_to_string(self.type);
  269. error(self.expr, "'%.*s' expected a pointer of a value derived from intrinsics.objc_object, got '%s' of type %s", LIT(builtin_name), e, t);
  270. gb_string_free(t);
  271. gb_string_free(e);
  272. return false;
  273. } else {
  274. Type *type = type_deref(self.type);
  275. if (!(type->kind == Type_Named &&
  276. type->Named.type_name != nullptr &&
  277. type->Named.type_name->TypeName.objc_class_name != "")) {
  278. gbString t = type_to_string(type);
  279. error(self.expr, "'%.*s' expected a named type with the attribute @(obj_class=<string>) , got type %s", LIT(builtin_name), t);
  280. gb_string_free(t);
  281. return false;
  282. }
  283. }
  284. if (!is_constant_string(c, builtin_name, ce->args[2], &sel_name)) {
  285. return false;
  286. }
  287. isize const arg_offset = 1;
  288. auto param_types = slice_make<Type *>(permanent_allocator(), ce->args.count-arg_offset);
  289. param_types[0] = t_objc_id;
  290. param_types[1] = sel_type;
  291. for (isize i = 2+arg_offset; i < ce->args.count; i++) {
  292. Operand x = {};
  293. check_expr(c, &x, ce->args[i]);
  294. if (is_type_untyped(x.type)) {
  295. gbString e = expr_to_string(x.expr);
  296. gbString t = type_to_string(x.type);
  297. error(x.expr, "'%.*s' expects typed parameters, got %s of type %s", LIT(builtin_name), e, t);
  298. gb_string_free(t);
  299. gb_string_free(e);
  300. }
  301. param_types[i-arg_offset] = x.type;
  302. }
  303. add_objc_proc_type(c, call, return_type, param_types);
  304. return true;
  305. } break;
  306. case BuiltinProc_objc_find_selector:
  307. case BuiltinProc_objc_find_class:
  308. case BuiltinProc_objc_register_selector:
  309. case BuiltinProc_objc_register_class:
  310. {
  311. String sel_name = {};
  312. if (!is_constant_string(c, builtin_name, ce->args[0], &sel_name)) {
  313. return false;
  314. }
  315. switch (id) {
  316. case BuiltinProc_objc_find_selector:
  317. case BuiltinProc_objc_register_selector:
  318. operand->type = t_objc_SEL;
  319. break;
  320. case BuiltinProc_objc_find_class:
  321. case BuiltinProc_objc_register_class:
  322. operand->type = t_objc_Class;
  323. break;
  324. }
  325. operand->mode = Addressing_Value;
  326. try_to_add_package_dependency(c, "runtime", "objc_lookUpClass");
  327. try_to_add_package_dependency(c, "runtime", "sel_registerName");
  328. try_to_add_package_dependency(c, "runtime", "objc_allocateClassPair");
  329. return true;
  330. } break;
  331. }
  332. }
  333. gb_internal bool check_atomic_memory_order_argument(CheckerContext *c, Ast *expr, String const &builtin_name, OdinAtomicMemoryOrder *memory_order_, char const *extra_message = nullptr) {
  334. Operand x = {};
  335. check_expr_with_type_hint(c, &x, expr, t_atomic_memory_order);
  336. if (x.mode == Addressing_Invalid) {
  337. return false;
  338. }
  339. if (!are_types_identical(x.type, t_atomic_memory_order) || x.mode != Addressing_Constant) {
  340. gbString str = type_to_string(x.type);
  341. if (extra_message) {
  342. error(x.expr, "Expected a constant Atomic_Memory_Order value for the %s of '%.*s', got %s", extra_message, LIT(builtin_name), str);
  343. } else {
  344. error(x.expr, "Expected a constant Atomic_Memory_Order value for '%.*s', got %s", LIT(builtin_name), str);
  345. }
  346. gb_string_free(str);
  347. return false;
  348. }
  349. i64 value = exact_value_to_i64(x.value);
  350. if (value < 0 || value >= OdinAtomicMemoryOrder_COUNT) {
  351. error(x.expr, "Illegal Atomic_Memory_Order value, got %lld", cast(long long)value);
  352. return false;
  353. }
  354. if (memory_order_) {
  355. *memory_order_ = cast(OdinAtomicMemoryOrder)value;
  356. }
  357. return true;
  358. }
  359. gb_internal bool check_builtin_simd_operation(CheckerContext *c, Operand *operand, Ast *call, i32 id, Type *type_hint) {
  360. ast_node(ce, CallExpr, call);
  361. String const &builtin_name = builtin_procs[id].name;
  362. switch (id) {
  363. // Any numeric
  364. case BuiltinProc_simd_add:
  365. case BuiltinProc_simd_sub:
  366. case BuiltinProc_simd_mul:
  367. case BuiltinProc_simd_div:
  368. case BuiltinProc_simd_min:
  369. case BuiltinProc_simd_max:
  370. {
  371. Operand x = {};
  372. Operand y = {};
  373. check_expr(c, &x, ce->args[0]); if (x.mode == Addressing_Invalid) return false;
  374. check_expr_with_type_hint(c, &y, ce->args[1], x.type); if (y.mode == Addressing_Invalid) return false;
  375. convert_to_typed(c, &y, x.type); if (y.mode == Addressing_Invalid) return false;
  376. if (!is_type_simd_vector(x.type)) {
  377. error(x.expr, "'%.*s' expected a simd vector type", LIT(builtin_name));
  378. return false;
  379. }
  380. if (!is_type_simd_vector(y.type)) {
  381. error(y.expr, "'%.*s' expected a simd vector type", LIT(builtin_name));
  382. return false;
  383. }
  384. if (!are_types_identical(x.type, y.type)) {
  385. gbString xs = type_to_string(x.type);
  386. gbString ys = type_to_string(y.type);
  387. error(x.expr, "'%.*s' expected 2 arguments of the same type, got '%s' vs '%s'", LIT(builtin_name), xs, ys);
  388. gb_string_free(ys);
  389. gb_string_free(xs);
  390. return false;
  391. }
  392. Type *elem = base_array_type(x.type);
  393. if (!is_type_integer(elem) && !is_type_float(elem)) {
  394. gbString xs = type_to_string(x.type);
  395. error(x.expr, "'%.*s' expected a #simd type with an integer or floating point element, got '%s'", LIT(builtin_name), xs);
  396. gb_string_free(xs);
  397. return false;
  398. }
  399. if (id == BuiltinProc_simd_div && is_type_integer(elem)) {
  400. gbString xs = type_to_string(x.type);
  401. error(x.expr, "'%.*s' is not supported for integer elements, got '%s'", LIT(builtin_name), xs);
  402. gb_string_free(xs);
  403. // don't return
  404. }
  405. operand->mode = Addressing_Value;
  406. operand->type = x.type;
  407. return true;
  408. }
  409. // Integer only
  410. case BuiltinProc_simd_add_sat:
  411. case BuiltinProc_simd_sub_sat:
  412. case BuiltinProc_simd_bit_and:
  413. case BuiltinProc_simd_bit_or:
  414. case BuiltinProc_simd_bit_xor:
  415. case BuiltinProc_simd_bit_and_not:
  416. {
  417. Operand x = {};
  418. Operand y = {};
  419. check_expr(c, &x, ce->args[0]); if (x.mode == Addressing_Invalid) return false;
  420. check_expr_with_type_hint(c, &y, ce->args[1], x.type); if (y.mode == Addressing_Invalid) return false;
  421. convert_to_typed(c, &y, x.type); if (y.mode == Addressing_Invalid) return false;
  422. if (!is_type_simd_vector(x.type)) {
  423. error(x.expr, "'%.*s' expected a simd vector type", LIT(builtin_name));
  424. return false;
  425. }
  426. if (!is_type_simd_vector(y.type)) {
  427. error(y.expr, "'%.*s' expected a simd vector type", LIT(builtin_name));
  428. return false;
  429. }
  430. if (!are_types_identical(x.type, y.type)) {
  431. gbString xs = type_to_string(x.type);
  432. gbString ys = type_to_string(y.type);
  433. error(x.expr, "'%.*s' expected 2 arguments of the same type, got '%s' vs '%s'", LIT(builtin_name), xs, ys);
  434. gb_string_free(ys);
  435. gb_string_free(xs);
  436. return false;
  437. }
  438. Type *elem = base_array_type(x.type);
  439. switch (id) {
  440. case BuiltinProc_simd_add_sat:
  441. case BuiltinProc_simd_sub_sat:
  442. if (!is_type_integer(elem)) {
  443. gbString xs = type_to_string(x.type);
  444. error(x.expr, "'%.*s' expected a #simd type with an integer element, got '%s'", LIT(builtin_name), xs);
  445. gb_string_free(xs);
  446. return false;
  447. }
  448. break;
  449. default:
  450. if (!is_type_integer(elem) && !is_type_boolean(elem)) {
  451. gbString xs = type_to_string(x.type);
  452. error(x.expr, "'%.*s' expected a #simd type with an integer or boolean element, got '%s'", LIT(builtin_name), xs);
  453. gb_string_free(xs);
  454. return false;
  455. }
  456. break;
  457. }
  458. operand->mode = Addressing_Value;
  459. operand->type = x.type;
  460. return true;
  461. }
  462. case BuiltinProc_simd_shl: // Odin-like
  463. case BuiltinProc_simd_shr: // Odin-like
  464. case BuiltinProc_simd_shl_masked: // C-like
  465. case BuiltinProc_simd_shr_masked: // C-like
  466. {
  467. Operand x = {};
  468. Operand y = {};
  469. check_expr(c, &x, ce->args[0]); if (x.mode == Addressing_Invalid) return false;
  470. check_expr_with_type_hint(c, &y, ce->args[1], x.type); if (y.mode == Addressing_Invalid) return false;
  471. convert_to_typed(c, &y, x.type); if (y.mode == Addressing_Invalid) return false;
  472. if (!is_type_simd_vector(x.type)) {
  473. error(x.expr, "'%.*s' expected a simd vector type", LIT(builtin_name));
  474. return false;
  475. }
  476. if (!is_type_simd_vector(y.type)) {
  477. error(y.expr, "'%.*s' expected a simd vector type", LIT(builtin_name));
  478. return false;
  479. }
  480. GB_ASSERT(x.type->kind == Type_SimdVector);
  481. GB_ASSERT(y.type->kind == Type_SimdVector);
  482. Type *xt = x.type;
  483. Type *yt = y.type;
  484. if (xt->SimdVector.count != yt->SimdVector.count) {
  485. error(x.expr, "'%.*s' mismatched simd vector lengths, got '%lld' vs '%lld'",
  486. LIT(builtin_name),
  487. cast(long long)xt->SimdVector.count,
  488. cast(long long)yt->SimdVector.count);
  489. return false;
  490. }
  491. if (!is_type_integer(base_array_type(x.type))) {
  492. gbString xs = type_to_string(x.type);
  493. error(x.expr, "'%.*s' expected a #simd type with an integer element, got '%s'", LIT(builtin_name), xs);
  494. gb_string_free(xs);
  495. return false;
  496. }
  497. if (!is_type_unsigned(base_array_type(y.type))) {
  498. gbString ys = type_to_string(y.type);
  499. error(y.expr, "'%.*s' expected a #simd type with an unsigned integer element as the shifting operand, got '%s'", LIT(builtin_name), ys);
  500. gb_string_free(ys);
  501. return false;
  502. }
  503. operand->mode = Addressing_Value;
  504. operand->type = x.type;
  505. return true;
  506. }
  507. // Unary
  508. case BuiltinProc_simd_neg:
  509. case BuiltinProc_simd_abs:
  510. {
  511. Operand x = {};
  512. check_expr(c, &x, ce->args[0]);
  513. if (x.mode == Addressing_Invalid) {
  514. return false;
  515. }
  516. if (!is_type_simd_vector(x.type)) {
  517. error(x.expr, "'%.*s' expected a simd vector type", LIT(builtin_name));
  518. return false;
  519. }
  520. Type *elem = base_array_type(x.type);
  521. if (!is_type_integer(elem) && !is_type_float(elem)) {
  522. gbString xs = type_to_string(x.type);
  523. error(x.expr, "'%.*s' expected a #simd type with an integer or floating point element, got '%s'", LIT(builtin_name), xs);
  524. gb_string_free(xs);
  525. return false;
  526. }
  527. operand->mode = Addressing_Value;
  528. operand->type = x.type;
  529. return true;
  530. }
  531. // Return integer masks
  532. case BuiltinProc_simd_lanes_eq:
  533. case BuiltinProc_simd_lanes_ne:
  534. case BuiltinProc_simd_lanes_lt:
  535. case BuiltinProc_simd_lanes_le:
  536. case BuiltinProc_simd_lanes_gt:
  537. case BuiltinProc_simd_lanes_ge:
  538. {
  539. // op(#simd[N]T, #simd[N]T) -> #simd[N]V
  540. // where `V` is an integer, `size_of(T) == size_of(V)`
  541. // `V` will all 0s if false and all 1s if true (e.g. 0x00 and 0xff for false and true, respectively)
  542. Operand x = {};
  543. Operand y = {};
  544. check_expr(c, &x, ce->args[0]); if (x.mode == Addressing_Invalid) return false;
  545. check_expr_with_type_hint(c, &y, ce->args[1], x.type); if (y.mode == Addressing_Invalid) return false;
  546. convert_to_typed(c, &y, x.type); if (y.mode == Addressing_Invalid) return false;
  547. if (!is_type_simd_vector(x.type)) {
  548. error(x.expr, "'%.*s' expected a simd vector type", LIT(builtin_name));
  549. return false;
  550. }
  551. Type *elem = base_array_type(x.type);
  552. switch (id) {
  553. case BuiltinProc_simd_lanes_eq:
  554. case BuiltinProc_simd_lanes_ne:
  555. if (!is_type_integer(elem) && !is_type_float(elem) && !is_type_boolean(elem)) {
  556. gbString xs = type_to_string(x.type);
  557. error(x.expr, "'%.*s' expected a #simd type with an integer, floating point, or boolean element, got '%s'", LIT(builtin_name), xs);
  558. gb_string_free(xs);
  559. return false;
  560. }
  561. break;
  562. default:
  563. if (!is_type_integer(elem) && !is_type_float(elem)) {
  564. gbString xs = type_to_string(x.type);
  565. error(x.expr, "'%.*s' expected a #simd type with an integer or floating point element, got '%s'", LIT(builtin_name), xs);
  566. gb_string_free(xs);
  567. return false;
  568. }
  569. break;
  570. }
  571. Type *vt = base_type(x.type);
  572. GB_ASSERT(vt->kind == Type_SimdVector);
  573. i64 count = vt->SimdVector.count;
  574. i64 sz = type_size_of(elem);
  575. Type *new_elem = nullptr;
  576. switch (sz) {
  577. case 1: new_elem = t_u8; break;
  578. case 2: new_elem = t_u16; break;
  579. case 4: new_elem = t_u32; break;
  580. case 8: new_elem = t_u64; break;
  581. case 16:
  582. error(x.expr, "'%.*s' not supported 128-bit integer backed simd vector types", LIT(builtin_name));
  583. return false;
  584. }
  585. operand->mode = Addressing_Value;
  586. operand->type = alloc_type_simd_vector(count, new_elem);
  587. return true;
  588. }
  589. case BuiltinProc_simd_extract:
  590. {
  591. Operand x = {};
  592. check_expr(c, &x, ce->args[0]); if (x.mode == Addressing_Invalid) return false;
  593. if (!is_type_simd_vector(x.type)) {
  594. error(x.expr, "'%.*s' expected a simd vector type", LIT(builtin_name));
  595. return false;
  596. }
  597. Type *elem = base_array_type(x.type);
  598. i64 max_count = x.type->SimdVector.count;
  599. i64 value = -1;
  600. if (!check_index_value(c, x.type, false, ce->args[1], max_count, &value)) {
  601. return false;
  602. }
  603. if (max_count < 0) {
  604. error(ce->args[1], "'%.*s' expected a constant integer index, got '%lld'", LIT(builtin_name), cast(long long)value);
  605. return false;
  606. }
  607. operand->mode = Addressing_Value;
  608. operand->type = elem;
  609. return true;
  610. }
  611. break;
  612. case BuiltinProc_simd_replace:
  613. {
  614. Operand x = {};
  615. check_expr(c, &x, ce->args[0]); if (x.mode == Addressing_Invalid) return false;
  616. if (!is_type_simd_vector(x.type)) {
  617. error(x.expr, "'%.*s' expected a simd vector type", LIT(builtin_name));
  618. return false;
  619. }
  620. Type *elem = base_array_type(x.type);
  621. i64 max_count = x.type->SimdVector.count;
  622. i64 value = -1;
  623. if (!check_index_value(c, x.type, false, ce->args[1], max_count, &value)) {
  624. return false;
  625. }
  626. if (max_count < 0) {
  627. error(ce->args[1], "'%.*s' expected a constant integer index, got '%lld'", LIT(builtin_name), cast(long long)value);
  628. return false;
  629. }
  630. Operand y = {};
  631. check_expr_with_type_hint(c, &y, ce->args[2], elem); if (y.mode == Addressing_Invalid) return false;
  632. convert_to_typed(c, &y, elem); if (y.mode == Addressing_Invalid) return false;
  633. if (!are_types_identical(y.type, elem)) {
  634. gbString et = type_to_string(elem);
  635. gbString yt = type_to_string(y.type);
  636. error(y.expr, "'%.*s' expected a type of '%s' to insert, got '%s'", LIT(builtin_name), et, yt);
  637. gb_string_free(yt);
  638. gb_string_free(et);
  639. return false;
  640. }
  641. operand->mode = Addressing_Value;
  642. operand->type = x.type;
  643. return true;
  644. }
  645. break;
  646. case BuiltinProc_simd_reduce_add_ordered:
  647. case BuiltinProc_simd_reduce_mul_ordered:
  648. case BuiltinProc_simd_reduce_min:
  649. case BuiltinProc_simd_reduce_max:
  650. {
  651. Operand x = {};
  652. check_expr(c, &x, ce->args[0]); if (x.mode == Addressing_Invalid) return false;
  653. if (!is_type_simd_vector(x.type)) {
  654. error(x.expr, "'%.*s' expected a simd vector type", LIT(builtin_name));
  655. return false;
  656. }
  657. Type *elem = base_array_type(x.type);
  658. if (!is_type_integer(elem) && !is_type_float(elem)) {
  659. gbString xs = type_to_string(x.type);
  660. error(x.expr, "'%.*s' expected a #simd type with an integer or floating point element, got '%s'", LIT(builtin_name), xs);
  661. gb_string_free(xs);
  662. return false;
  663. }
  664. operand->mode = Addressing_Value;
  665. operand->type = base_array_type(x.type);
  666. return true;
  667. }
  668. case BuiltinProc_simd_reduce_and:
  669. case BuiltinProc_simd_reduce_or:
  670. case BuiltinProc_simd_reduce_xor:
  671. {
  672. Operand x = {};
  673. check_expr(c, &x, ce->args[0]); if (x.mode == Addressing_Invalid) return false;
  674. if (!is_type_simd_vector(x.type)) {
  675. error(x.expr, "'%.*s' expected a simd vector type", LIT(builtin_name));
  676. return false;
  677. }
  678. Type *elem = base_array_type(x.type);
  679. if (!is_type_integer(elem) && !is_type_boolean(elem)) {
  680. gbString xs = type_to_string(x.type);
  681. error(x.expr, "'%.*s' expected a #simd type with an integer or boolean element, got '%s'", LIT(builtin_name), xs);
  682. gb_string_free(xs);
  683. return false;
  684. }
  685. operand->mode = Addressing_Value;
  686. operand->type = base_array_type(x.type);
  687. return true;
  688. }
  689. case BuiltinProc_simd_shuffle:
  690. {
  691. Operand x = {};
  692. Operand y = {};
  693. check_expr(c, &x, ce->args[0]); if (x.mode == Addressing_Invalid) return false;
  694. check_expr_with_type_hint(c, &y, ce->args[1], x.type); if (y.mode == Addressing_Invalid) return false;
  695. convert_to_typed(c, &y, x.type); if (y.mode == Addressing_Invalid) return false;
  696. if (!is_type_simd_vector(x.type)) {
  697. error(x.expr, "'%.*s' expected a simd vector type", LIT(builtin_name));
  698. return false;
  699. }
  700. if (!is_type_simd_vector(y.type)) {
  701. error(y.expr, "'%.*s' expected a simd vector type", LIT(builtin_name));
  702. return false;
  703. }
  704. if (!are_types_identical(x.type, y.type)) {
  705. gbString xs = type_to_string(x.type);
  706. gbString ys = type_to_string(y.type);
  707. error(x.expr, "'%.*s' expected 2 arguments of the same type, got '%s' vs '%s'", LIT(builtin_name), xs, ys);
  708. gb_string_free(ys);
  709. gb_string_free(xs);
  710. return false;
  711. }
  712. Type *elem = base_array_type(x.type);
  713. i64 max_count = x.type->SimdVector.count + y.type->SimdVector.count;
  714. i64 arg_count = 0;
  715. for_array(i, ce->args) {
  716. if (i < 2) {
  717. continue;
  718. }
  719. Ast *arg = ce->args[i];
  720. Operand op = {};
  721. check_expr(c, &op, arg);
  722. if (op.mode == Addressing_Invalid) {
  723. return false;
  724. }
  725. Type *arg_type = base_type(op.type);
  726. if (!is_type_integer(arg_type) || op.mode != Addressing_Constant) {
  727. error(op.expr, "Indices to '%.*s' must be constant integers", LIT(builtin_name));
  728. return false;
  729. }
  730. if (big_int_is_neg(&op.value.value_integer)) {
  731. error(op.expr, "Negative '%.*s' index", LIT(builtin_name));
  732. return false;
  733. }
  734. BigInt mc = {};
  735. big_int_from_i64(&mc, max_count);
  736. if (big_int_cmp(&mc, &op.value.value_integer) <= 0) {
  737. error(op.expr, "'%.*s' index exceeds length", LIT(builtin_name));
  738. return false;
  739. }
  740. arg_count++;
  741. }
  742. if (arg_count > max_count) {
  743. error(call, "Too many '%.*s' indices, %td > %td", LIT(builtin_name), arg_count, max_count);
  744. return false;
  745. }
  746. if (!is_power_of_two(arg_count)) {
  747. error(call, "'%.*s' must have a power of two index arguments, got %lld", LIT(builtin_name), cast(long long)arg_count);
  748. return false;
  749. }
  750. operand->mode = Addressing_Value;
  751. operand->type = alloc_type_simd_vector(arg_count, elem);
  752. return true;
  753. }
  754. case BuiltinProc_simd_select:
  755. {
  756. Operand cond = {};
  757. check_expr(c, &cond, ce->args[0]); if (cond.mode == Addressing_Invalid) return false;
  758. if (!is_type_simd_vector(cond.type)) {
  759. error(cond.expr, "'%.*s' expected a simd vector boolean type", LIT(builtin_name));
  760. return false;
  761. }
  762. Type *cond_elem = base_array_type(cond.type);
  763. if (!is_type_boolean(cond_elem) && !is_type_integer(cond_elem)) {
  764. gbString cond_str = type_to_string(cond.type);
  765. error(cond.expr, "'%.*s' expected a simd vector boolean or integer type, got '%s'", LIT(builtin_name), cond_str);
  766. gb_string_free(cond_str);
  767. return false;
  768. }
  769. Operand x = {};
  770. Operand y = {};
  771. check_expr(c, &x, ce->args[1]); if (x.mode == Addressing_Invalid) return false;
  772. check_expr_with_type_hint(c, &y, ce->args[2], x.type); if (y.mode == Addressing_Invalid) return false;
  773. convert_to_typed(c, &y, x.type); if (y.mode == Addressing_Invalid) return false;
  774. if (!is_type_simd_vector(x.type)) {
  775. error(x.expr, "'%.*s' expected a simd vector type", LIT(builtin_name));
  776. return false;
  777. }
  778. if (!is_type_simd_vector(y.type)) {
  779. error(y.expr, "'%.*s' expected a simd vector type", LIT(builtin_name));
  780. return false;
  781. }
  782. if (!are_types_identical(x.type, y.type)) {
  783. gbString xs = type_to_string(x.type);
  784. gbString ys = type_to_string(y.type);
  785. error(x.expr, "'%.*s' expected 2 results of the same type, got '%s' vs '%s'", LIT(builtin_name), xs, ys);
  786. gb_string_free(ys);
  787. gb_string_free(xs);
  788. return false;
  789. }
  790. if (cond.type->SimdVector.count != x.type->SimdVector.count) {
  791. error(x.expr, "'%.*s' expected condition vector to match the length of the result lengths, got '%lld' vs '%lld'",
  792. LIT(builtin_name),
  793. cast(long long)cond.type->SimdVector.count,
  794. cast(long long)x.type->SimdVector.count);
  795. return false;
  796. }
  797. operand->mode = Addressing_Value;
  798. operand->type = x.type;
  799. return true;
  800. }
  801. case BuiltinProc_simd_ceil:
  802. case BuiltinProc_simd_floor:
  803. case BuiltinProc_simd_trunc:
  804. case BuiltinProc_simd_nearest:
  805. {
  806. Operand x = {};
  807. check_expr(c, &x, ce->args[0]); if (x.mode == Addressing_Invalid) return false;
  808. if (!is_type_simd_vector(x.type)) {
  809. error(x.expr, "'%.*s' expected a simd vector boolean type", LIT(builtin_name));
  810. return false;
  811. }
  812. Type *elem = base_array_type(x.type);
  813. if (!is_type_float(elem)) {
  814. gbString x_str = type_to_string(x.type);
  815. error(x.expr, "'%.*s' expected a simd vector floating point type, got '%s'", LIT(builtin_name), x_str);
  816. gb_string_free(x_str);
  817. return false;
  818. }
  819. operand->mode = Addressing_Value;
  820. operand->type = x.type;
  821. return true;
  822. }
  823. case BuiltinProc_simd_lanes_reverse:
  824. {
  825. Operand x = {};
  826. check_expr(c, &x, ce->args[0]); if (x.mode == Addressing_Invalid) return false;
  827. if (!is_type_simd_vector(x.type)) {
  828. error(x.expr, "'%.*s' expected a simd vector type", LIT(builtin_name));
  829. return false;
  830. }
  831. operand->type = x.type;
  832. operand->mode = Addressing_Value;
  833. return true;
  834. }
  835. case BuiltinProc_simd_lanes_rotate_left:
  836. case BuiltinProc_simd_lanes_rotate_right:
  837. {
  838. Operand x = {};
  839. check_expr(c, &x, ce->args[0]); if (x.mode == Addressing_Invalid) return false;
  840. if (!is_type_simd_vector(x.type)) {
  841. error(x.expr, "'%.*s' expected a simd vector type", LIT(builtin_name));
  842. return false;
  843. }
  844. Operand offset = {};
  845. check_expr(c, &offset, ce->args[1]); if (offset.mode == Addressing_Invalid) return false;
  846. convert_to_typed(c, &offset, t_i64);
  847. if (!is_type_integer(offset.type) || offset.mode != Addressing_Constant) {
  848. error(offset.expr, "'%.*s' expected a constant integer offset");
  849. return false;
  850. }
  851. check_assignment(c, &offset, t_i64, builtin_name);
  852. operand->type = x.type;
  853. operand->mode = Addressing_Value;
  854. return true;
  855. }
  856. case BuiltinProc_simd_clamp:
  857. {
  858. Operand x = {};
  859. Operand y = {};
  860. Operand z = {};
  861. check_expr(c, &x, ce->args[0]); if (x.mode == Addressing_Invalid) return false;
  862. check_expr_with_type_hint(c, &y, ce->args[1], x.type); if (y.mode == Addressing_Invalid) return false;
  863. check_expr_with_type_hint(c, &z, ce->args[2], x.type); if (z.mode == Addressing_Invalid) return false;
  864. convert_to_typed(c, &y, x.type); if (y.mode == Addressing_Invalid) return false;
  865. convert_to_typed(c, &z, x.type);
  866. if (!is_type_simd_vector(x.type)) {
  867. error(x.expr, "'%.*s' expected a simd vector type", LIT(builtin_name));
  868. return false;
  869. }
  870. if (!is_type_simd_vector(y.type)) {
  871. error(y.expr, "'%.*s' expected a simd vector type", LIT(builtin_name));
  872. return false;
  873. }
  874. if (!is_type_simd_vector(z.type)) {
  875. error(z.expr, "'%.*s' expected a simd vector type", LIT(builtin_name));
  876. return false;
  877. }
  878. if (!are_types_identical(x.type, y.type)) {
  879. gbString xs = type_to_string(x.type);
  880. gbString ys = type_to_string(y.type);
  881. error(x.expr, "'%.*s' expected 2 arguments of the same type, got '%s' vs '%s'", LIT(builtin_name), xs, ys);
  882. gb_string_free(ys);
  883. gb_string_free(xs);
  884. return false;
  885. }
  886. if (!are_types_identical(x.type, z.type)) {
  887. gbString xs = type_to_string(x.type);
  888. gbString zs = type_to_string(z.type);
  889. error(x.expr, "'%.*s' expected 2 arguments of the same type, got '%s' vs '%s'", LIT(builtin_name), xs, zs);
  890. gb_string_free(zs);
  891. gb_string_free(xs);
  892. return false;
  893. }
  894. Type *elem = base_array_type(x.type);
  895. if (!is_type_integer(elem) && !is_type_float(elem)) {
  896. gbString xs = type_to_string(x.type);
  897. error(x.expr, "'%.*s' expected a #simd type with an integer or floating point element, got '%s'", LIT(builtin_name), xs);
  898. gb_string_free(xs);
  899. return false;
  900. }
  901. operand->mode = Addressing_Value;
  902. operand->type = x.type;
  903. return true;
  904. }
  905. case BuiltinProc_simd_to_bits:
  906. {
  907. Operand x = {};
  908. check_expr(c, &x, ce->args[0]); if (x.mode == Addressing_Invalid) return false;
  909. if (!is_type_simd_vector(x.type)) {
  910. error(x.expr, "'%.*s' expected a simd vector type", LIT(builtin_name));
  911. return false;
  912. }
  913. Type *elem = base_array_type(x.type);
  914. i64 count = get_array_type_count(x.type);
  915. i64 sz = type_size_of(elem);
  916. Type *bit_elem = nullptr;
  917. switch (sz) {
  918. case 1: bit_elem = t_u8; break;
  919. case 2: bit_elem = t_u16; break;
  920. case 4: bit_elem = t_u32; break;
  921. case 8: bit_elem = t_u64; break;
  922. }
  923. GB_ASSERT(bit_elem != nullptr);
  924. operand->type = alloc_type_simd_vector(count, bit_elem);
  925. operand->mode = Addressing_Value;
  926. return true;
  927. }
  928. case BuiltinProc_simd_x86__MM_SHUFFLE:
  929. {
  930. Operand x[4] = {};
  931. for (unsigned i = 0; i < 4; i++) {
  932. check_expr(c, x+i, ce->args[i]); if (x[i].mode == Addressing_Invalid) return false;
  933. }
  934. u32 offsets[4] = {6, 4, 2, 0};
  935. u32 result = 0;
  936. for (unsigned i = 0; i < 4; i++) {
  937. if (!is_type_integer(x[i].type) || x[i].mode != Addressing_Constant) {
  938. gbString xs = type_to_string(x[i].type);
  939. error(x[i].expr, "'%.*s' expected a constant integer", LIT(builtin_name), xs);
  940. gb_string_free(xs);
  941. return false;
  942. }
  943. i64 val = exact_value_to_i64(x[i].value);
  944. if (val < 0 || val > 3) {
  945. error(x[i].expr, "'%.*s' expected a constant integer in the range 0..<4, got %lld", LIT(builtin_name), cast(long long)val);
  946. return false;
  947. }
  948. result |= cast(u32)(val) << offsets[i];
  949. }
  950. operand->type = t_untyped_integer;
  951. operand->mode = Addressing_Constant;
  952. operand->value = exact_value_i64(result);
  953. return true;
  954. }
  955. default:
  956. GB_PANIC("Unhandled simd intrinsic: %.*s", LIT(builtin_name));
  957. }
  958. return false;
  959. }
  960. gb_internal bool cache_load_file_directive(CheckerContext *c, Ast *call, String const &original_string, bool err_on_not_found, LoadFileCache **cache_) {
  961. ast_node(ce, CallExpr, call);
  962. ast_node(bd, BasicDirective, ce->proc);
  963. String builtin_name = bd->name.string;
  964. String path;
  965. if (gb_path_is_absolute((char*)original_string.text)) {
  966. path = original_string;
  967. } else {
  968. String base_dir = dir_from_path(get_file_path_string(call->file_id));
  969. BlockingMutex *ignore_mutex = nullptr;
  970. bool ok = determine_path_from_string(ignore_mutex, call, base_dir, original_string, &path);
  971. if (!ok) {
  972. if (err_on_not_found) {
  973. error(ce->proc, "Failed to `#%.*s` file: %.*s; invalid file or cannot be found", LIT(builtin_name), LIT(original_string));
  974. }
  975. call->state_flags |= StateFlag_DirectiveWasFalse;
  976. return false;
  977. }
  978. }
  979. MUTEX_GUARD(&c->info->load_file_mutex);
  980. gbFileError file_error = gbFileError_None;
  981. String data = {};
  982. LoadFileCache **cache_ptr = string_map_get(&c->info->load_file_cache, path);
  983. LoadFileCache *cache = cache_ptr ? *cache_ptr : nullptr;
  984. if (cache) {
  985. file_error = cache->file_error;
  986. data = cache->data;
  987. }
  988. defer ({
  989. if (cache == nullptr) {
  990. LoadFileCache *new_cache = gb_alloc_item(permanent_allocator(), LoadFileCache);
  991. new_cache->path = path;
  992. new_cache->data = data;
  993. new_cache->file_error = file_error;
  994. string_map_init(&new_cache->hashes, 32);
  995. string_map_set(&c->info->load_file_cache, path, new_cache);
  996. if (cache_) *cache_ = new_cache;
  997. } else {
  998. cache->data = data;
  999. cache->file_error = file_error;
  1000. if (cache_) *cache_ = cache;
  1001. }
  1002. });
  1003. TEMPORARY_ALLOCATOR_GUARD();
  1004. char *c_str = alloc_cstring(temporary_allocator(), path);
  1005. gbFile f = {};
  1006. if (cache == nullptr) {
  1007. file_error = gb_file_open(&f, c_str);
  1008. }
  1009. defer (gb_file_close(&f));
  1010. switch (file_error) {
  1011. default:
  1012. case gbFileError_Invalid:
  1013. if (err_on_not_found) {
  1014. error(ce->proc, "Failed to `#%.*s` file: %s; invalid file or cannot be found", LIT(builtin_name), c_str);
  1015. }
  1016. call->state_flags |= StateFlag_DirectiveWasFalse;
  1017. return false;
  1018. case gbFileError_NotExists:
  1019. if (err_on_not_found) {
  1020. error(ce->proc, "Failed to `#%.*s` file: %s; file cannot be found", LIT(builtin_name), c_str);
  1021. }
  1022. call->state_flags |= StateFlag_DirectiveWasFalse;
  1023. return false;
  1024. case gbFileError_Permission:
  1025. if (err_on_not_found) {
  1026. error(ce->proc, "Failed to `#%.*s` file: %s; file permissions problem", LIT(builtin_name), c_str);
  1027. }
  1028. call->state_flags |= StateFlag_DirectiveWasFalse;
  1029. return false;
  1030. case gbFileError_None:
  1031. // Okay
  1032. break;
  1033. }
  1034. if (cache == nullptr) {
  1035. isize file_size = cast(isize)gb_file_size(&f);
  1036. if (file_size > 0) {
  1037. u8 *ptr = cast(u8 *)gb_alloc(permanent_allocator(), file_size+1);
  1038. gb_file_read_at(&f, ptr, file_size, 0);
  1039. ptr[file_size] = '\0';
  1040. data.text = ptr;
  1041. data.len = file_size;
  1042. }
  1043. }
  1044. return true;
  1045. }
  1046. gb_internal bool is_valid_type_for_load(Type *type) {
  1047. if (type == t_invalid) {
  1048. return false;
  1049. } else if (is_type_string(type)) {
  1050. return true;
  1051. } else if (is_type_slice(type) /*|| is_type_array(type) || is_type_enumerated_array(type)*/) {
  1052. Type *elem = nullptr;
  1053. Type *bt = base_type(type);
  1054. if (bt->kind == Type_Slice) {
  1055. elem = bt->Slice.elem;
  1056. } else if (bt->kind == Type_Array) {
  1057. elem = bt->Array.elem;
  1058. } else if (bt->kind == Type_EnumeratedArray) {
  1059. elem = bt->EnumeratedArray.elem;
  1060. }
  1061. GB_ASSERT(elem != nullptr);
  1062. return is_type_load_safe(elem);
  1063. }
  1064. return false;
  1065. }
  1066. gb_internal bool check_atomic_ptr_argument(Operand *operand, String const &builtin_name, Type *elem) {
  1067. if (!is_type_valid_atomic_type(elem)) {
  1068. error(operand->expr, "Only an integer, floating-point, boolean, or pointer can be used as an atomic for '%.*s'", LIT(builtin_name));
  1069. return false;
  1070. }
  1071. return true;
  1072. }
  1073. gb_internal LoadDirectiveResult check_load_directive(CheckerContext *c, Operand *operand, Ast *call, Type *type_hint, bool err_on_not_found) {
  1074. ast_node(ce, CallExpr, call);
  1075. ast_node(bd, BasicDirective, ce->proc);
  1076. String name = bd->name.string;
  1077. GB_ASSERT(name == "load");
  1078. if (ce->args.count != 1 && ce->args.count != 2) {
  1079. if (ce->args.count == 0) {
  1080. error(ce->close, "'#%.*s' expects 1 or 2 arguments, got 0", LIT(name));
  1081. } else {
  1082. error(ce->args[0], "'#%.*s' expects 1 or 2 arguments, got %td", LIT(name), ce->args.count);
  1083. }
  1084. return LoadDirective_Error;
  1085. }
  1086. Ast *arg = ce->args[0];
  1087. Operand o = {};
  1088. check_expr(c, &o, arg);
  1089. if (o.mode != Addressing_Constant) {
  1090. error(arg, "'#%.*s' expected a constant string argument", LIT(name));
  1091. return LoadDirective_Error;
  1092. }
  1093. if (!is_type_string(o.type)) {
  1094. gbString str = type_to_string(o.type);
  1095. error(arg, "'#%.*s' expected a constant string, got %s", LIT(name), str);
  1096. gb_string_free(str);
  1097. return LoadDirective_Error;
  1098. }
  1099. GB_ASSERT(o.value.kind == ExactValue_String);
  1100. operand->type = t_u8_slice;
  1101. if (ce->args.count == 1) {
  1102. if (type_hint && is_valid_type_for_load(type_hint)) {
  1103. operand->type = type_hint;
  1104. }
  1105. } else if (ce->args.count == 2) {
  1106. Ast *arg_type = ce->args[1];
  1107. Type *type = check_type(c, arg_type);
  1108. if (type != nullptr) {
  1109. if (is_valid_type_for_load(type)) {
  1110. operand->type = type;
  1111. } else {
  1112. gbString type_str = type_to_string(type);
  1113. error(arg_type, "'#%.*s' invalid type, expected a string, or slice of simple types, got %s", LIT(name), type_str);
  1114. gb_string_free(type_str);
  1115. }
  1116. }
  1117. } else {
  1118. GB_PANIC("unreachable");
  1119. }
  1120. operand->mode = Addressing_Constant;
  1121. LoadFileCache *cache = nullptr;
  1122. if (cache_load_file_directive(c, call, o.value.value_string, err_on_not_found, &cache)) {
  1123. operand->value = exact_value_string(cache->data);
  1124. return LoadDirective_Success;
  1125. }
  1126. return LoadDirective_NotFound;
  1127. }
  1128. gb_internal int file_cache_sort_cmp(void const *x, void const *y) {
  1129. LoadFileCache const *a = *(LoadFileCache const **)(x);
  1130. LoadFileCache const *b = *(LoadFileCache const **)(y);
  1131. return string_compare(a->path, b->path);
  1132. }
  1133. gb_internal LoadDirectiveResult check_load_directory_directive(CheckerContext *c, Operand *operand, Ast *call, Type *type_hint, bool err_on_not_found) {
  1134. ast_node(ce, CallExpr, call);
  1135. ast_node(bd, BasicDirective, ce->proc);
  1136. String name = bd->name.string;
  1137. GB_ASSERT(name == "load_directory");
  1138. if (ce->args.count != 1) {
  1139. error(ce->args[0], "'#%.*s' expects 1 argument, got %td", LIT(name), ce->args.count);
  1140. return LoadDirective_Error;
  1141. }
  1142. Ast *arg = ce->args[0];
  1143. Operand o = {};
  1144. check_expr(c, &o, arg);
  1145. if (o.mode != Addressing_Constant) {
  1146. error(arg, "'#%.*s' expected a constant string argument", LIT(name));
  1147. return LoadDirective_Error;
  1148. }
  1149. if (!is_type_string(o.type)) {
  1150. gbString str = type_to_string(o.type);
  1151. error(arg, "'#%.*s' expected a constant string, got %s", LIT(name), str);
  1152. gb_string_free(str);
  1153. return LoadDirective_Error;
  1154. }
  1155. GB_ASSERT(o.value.kind == ExactValue_String);
  1156. init_core_load_directory_file(c->checker);
  1157. operand->type = t_load_directory_file_slice;
  1158. operand->mode = Addressing_Value;
  1159. String original_string = o.value.value_string;
  1160. String path;
  1161. if (gb_path_is_absolute((char*)original_string.text)) {
  1162. path = original_string;
  1163. } else {
  1164. String base_dir = dir_from_path(get_file_path_string(call->file_id));
  1165. BlockingMutex *ignore_mutex = nullptr;
  1166. bool ok = determine_path_from_string(ignore_mutex, call, base_dir, original_string, &path);
  1167. gb_unused(ok);
  1168. }
  1169. MUTEX_GUARD(&c->info->load_directory_mutex);
  1170. gbFileError file_error = gbFileError_None;
  1171. Array<LoadFileCache *> file_caches = {};
  1172. LoadDirectoryCache **cache_ptr = string_map_get(&c->info->load_directory_cache, path);
  1173. LoadDirectoryCache *cache = cache_ptr ? *cache_ptr : nullptr;
  1174. if (cache) {
  1175. file_error = cache->file_error;
  1176. }
  1177. defer ({
  1178. if (cache == nullptr) {
  1179. LoadDirectoryCache *new_cache = gb_alloc_item(permanent_allocator(), LoadDirectoryCache);
  1180. new_cache->path = path;
  1181. new_cache->files = file_caches;
  1182. new_cache->file_error = file_error;
  1183. string_map_set(&c->info->load_directory_cache, path, new_cache);
  1184. map_set(&c->info->load_directory_map, call, new_cache);
  1185. } else {
  1186. cache->file_error = file_error;
  1187. map_set(&c->info->load_directory_map, call, cache);
  1188. }
  1189. });
  1190. LoadDirectiveResult result = LoadDirective_Success;
  1191. if (cache == nullptr) {
  1192. Array<FileInfo> list = {};
  1193. ReadDirectoryError rd_err = read_directory(path, &list);
  1194. defer (array_free(&list));
  1195. if (list.count == 1) {
  1196. GB_ASSERT(path != list[0].fullpath);
  1197. }
  1198. switch (rd_err) {
  1199. case ReadDirectory_InvalidPath:
  1200. error(call, "%.*s error - invalid path: %.*s", LIT(name), LIT(original_string));
  1201. return LoadDirective_NotFound;
  1202. case ReadDirectory_NotExists:
  1203. error(call, "%.*s error - path does not exist: %.*s", LIT(name), LIT(original_string));
  1204. return LoadDirective_NotFound;
  1205. case ReadDirectory_Permission:
  1206. error(call, "%.*s error - unknown error whilst reading path, %.*s", LIT(name), LIT(original_string));
  1207. return LoadDirective_Error;
  1208. case ReadDirectory_NotDir:
  1209. error(call, "%.*s error - expected a directory, got a file: %.*s", LIT(name), LIT(original_string));
  1210. return LoadDirective_Error;
  1211. case ReadDirectory_Empty:
  1212. error(call, "%.*s error - empty directory: %.*s", LIT(name), LIT(original_string));
  1213. return LoadDirective_NotFound;
  1214. case ReadDirectory_Unknown:
  1215. error(call, "%.*s error - unknown error whilst reading path %.*s", LIT(name), LIT(original_string));
  1216. return LoadDirective_Error;
  1217. }
  1218. isize files_to_reserve = list.count+1; // always reserve 1
  1219. file_caches = array_make<LoadFileCache *>(heap_allocator(), 0, files_to_reserve);
  1220. for (FileInfo fi : list) {
  1221. LoadFileCache *cache = nullptr;
  1222. if (cache_load_file_directive(c, call, fi.fullpath, err_on_not_found, &cache)) {
  1223. array_add(&file_caches, cache);
  1224. } else {
  1225. result = LoadDirective_Error;
  1226. }
  1227. }
  1228. array_sort(file_caches, file_cache_sort_cmp);
  1229. }
  1230. return result;
  1231. }
  1232. gb_internal bool check_hash_kind(CheckerContext *c, Ast *call, String const &hash_kind, u8 const *data, isize data_size, u64 *hash_value) {
  1233. ast_node(ce, CallExpr, call);
  1234. ast_node(bd, BasicDirective, ce->proc);
  1235. String name = bd->name.string;
  1236. GB_ASSERT(name == "load_hash" || name == "hash");
  1237. String supported_hashes[] = {
  1238. str_lit("adler32"),
  1239. str_lit("crc32"),
  1240. str_lit("crc64"),
  1241. str_lit("fnv32"),
  1242. str_lit("fnv64"),
  1243. str_lit("fnv32a"),
  1244. str_lit("fnv64a"),
  1245. str_lit("murmur32"),
  1246. str_lit("murmur64"),
  1247. };
  1248. bool hash_found = false;
  1249. for (isize i = 0; i < gb_count_of(supported_hashes); i++) {
  1250. if (supported_hashes[i] == hash_kind) {
  1251. hash_found = true;
  1252. break;
  1253. }
  1254. }
  1255. if (!hash_found) {
  1256. ERROR_BLOCK();
  1257. error(ce->proc, "Invalid hash kind passed to `#%.*s`, got: %.*s", LIT(name), LIT(hash_kind));
  1258. error_line("\tAvailable hash kinds:\n");
  1259. for (isize i = 0; i < gb_count_of(supported_hashes); i++) {
  1260. error_line("\t%.*s\n", LIT(supported_hashes[i]));
  1261. }
  1262. return false;
  1263. }
  1264. if (hash_kind == "adler32") {
  1265. *hash_value = gb_adler32(data, data_size);
  1266. } else if (hash_kind == "crc32") {
  1267. *hash_value = gb_crc32(data, data_size);
  1268. } else if (hash_kind == "crc64") {
  1269. *hash_value = gb_crc64(data, data_size);
  1270. } else if (hash_kind == "fnv32") {
  1271. *hash_value = gb_fnv32(data, data_size);
  1272. } else if (hash_kind == "fnv64") {
  1273. *hash_value = gb_fnv64(data, data_size);
  1274. } else if (hash_kind == "fnv32a") {
  1275. *hash_value = fnv32a(data, data_size);
  1276. } else if (hash_kind == "fnv64a") {
  1277. *hash_value = fnv64a(data, data_size);
  1278. } else if (hash_kind == "murmur32") {
  1279. *hash_value = gb_murmur32(data, data_size);
  1280. } else if (hash_kind == "murmur64") {
  1281. *hash_value = gb_murmur64(data, data_size);
  1282. } else {
  1283. compiler_error("unhandled hash kind: %.*s", LIT(hash_kind));
  1284. }
  1285. return true;
  1286. }
  1287. gb_internal bool check_builtin_procedure_directive(CheckerContext *c, Operand *operand, Ast *call, Type *type_hint) {
  1288. ast_node(ce, CallExpr, call);
  1289. ast_node(bd, BasicDirective, ce->proc);
  1290. String name = bd->name.string;
  1291. if (name == "location") {
  1292. if (ce->args.count > 1) {
  1293. error(ce->args[0], "'#location' expects either 0 or 1 arguments, got %td", ce->args.count);
  1294. }
  1295. if (ce->args.count > 0) {
  1296. Ast *arg = ce->args[0];
  1297. Entity *e = nullptr;
  1298. Operand o = {};
  1299. if (arg->kind == Ast_Ident) {
  1300. e = check_ident(c, &o, arg, nullptr, nullptr, true);
  1301. } else if (arg->kind == Ast_SelectorExpr) {
  1302. e = check_selector(c, &o, arg, nullptr);
  1303. }
  1304. if (e == nullptr) {
  1305. error(ce->args[0], "'#location' expected a valid entity name");
  1306. }
  1307. }
  1308. operand->type = t_source_code_location;
  1309. operand->mode = Addressing_Value;
  1310. } else if (name == "load") {
  1311. return check_load_directive(c, operand, call, type_hint, true) == LoadDirective_Success;
  1312. } else if (name == "load_directory") {
  1313. return check_load_directory_directive(c, operand, call, type_hint, true) == LoadDirective_Success;
  1314. } else if (name == "load_hash") {
  1315. if (ce->args.count != 2) {
  1316. if (ce->args.count == 0) {
  1317. error(ce->close, "'#load_hash' expects 2 argument, got 0");
  1318. } else {
  1319. error(ce->args[0], "'#load_hash' expects 2 argument, got %td", ce->args.count);
  1320. }
  1321. return false;
  1322. }
  1323. Ast *arg0 = ce->args[0];
  1324. Ast *arg1 = ce->args[1];
  1325. Operand o = {};
  1326. check_expr(c, &o, arg0);
  1327. if (o.mode != Addressing_Constant) {
  1328. error(arg0, "'#load_hash' expected a constant string argument");
  1329. return false;
  1330. }
  1331. if (!is_type_string(o.type)) {
  1332. gbString str = type_to_string(o.type);
  1333. error(arg0, "'#load_hash' expected a constant string, got %s", str);
  1334. gb_string_free(str);
  1335. return false;
  1336. }
  1337. Operand o_hash = {};
  1338. check_expr(c, &o_hash, arg1);
  1339. if (o_hash.mode != Addressing_Constant) {
  1340. error(arg1, "'#load_hash' expected a constant string argument");
  1341. return false;
  1342. }
  1343. if (!is_type_string(o_hash.type)) {
  1344. gbString str = type_to_string(o.type);
  1345. error(arg1, "'#load_hash' expected a constant string, got %s", str);
  1346. gb_string_free(str);
  1347. return false;
  1348. }
  1349. gbAllocator a = heap_allocator();
  1350. GB_ASSERT(o.value.kind == ExactValue_String);
  1351. GB_ASSERT(o_hash.value.kind == ExactValue_String);
  1352. String original_string = o.value.value_string;
  1353. String hash_kind = o_hash.value.value_string;
  1354. LoadFileCache *cache = nullptr;
  1355. if (cache_load_file_directive(c, call, original_string, true, &cache)) {
  1356. MUTEX_GUARD(&c->info->load_file_mutex);
  1357. // TODO(bill): make these procedures fast :P
  1358. u64 hash_value = 0;
  1359. u64 *hash_value_ptr = string_map_get(&cache->hashes, hash_kind);
  1360. if (hash_value_ptr) {
  1361. hash_value = *hash_value_ptr;
  1362. } else {
  1363. u8 *data = cache->data.text;
  1364. isize file_size = cache->data.len;
  1365. if (!check_hash_kind(c, call, hash_kind, data, file_size, &hash_value)) {
  1366. return false;
  1367. }
  1368. string_map_set(&cache->hashes, hash_kind, hash_value);
  1369. }
  1370. operand->type = t_untyped_integer;
  1371. operand->mode = Addressing_Constant;
  1372. operand->value = exact_value_u64(hash_value);
  1373. return true;
  1374. }
  1375. return false;
  1376. } else if (name == "hash") {
  1377. if (ce->args.count != 2) {
  1378. if (ce->args.count == 0) {
  1379. error(ce->close, "'#hash' expects 2 argument, got 0");
  1380. } else {
  1381. error(ce->args[0], "'#hash' expects 2 argument, got %td", ce->args.count);
  1382. }
  1383. return false;
  1384. }
  1385. Ast *arg0 = ce->args[0];
  1386. Ast *arg1 = ce->args[1];
  1387. Operand o = {};
  1388. check_expr(c, &o, arg0);
  1389. if (o.mode != Addressing_Constant) {
  1390. error(arg0, "'#hash' expected a constant string argument");
  1391. return false;
  1392. }
  1393. if (!is_type_string(o.type)) {
  1394. gbString str = type_to_string(o.type);
  1395. error(arg0, "'#hash' expected a constant string, got %s", str);
  1396. gb_string_free(str);
  1397. return false;
  1398. }
  1399. Operand o_hash = {};
  1400. check_expr(c, &o_hash, arg1);
  1401. if (o_hash.mode != Addressing_Constant) {
  1402. error(arg1, "'#hash' expected a constant string argument");
  1403. return false;
  1404. }
  1405. if (!is_type_string(o_hash.type)) {
  1406. gbString str = type_to_string(o.type);
  1407. error(arg1, "'#hash' expected a constant string, got %s", str);
  1408. gb_string_free(str);
  1409. return false;
  1410. }
  1411. gbAllocator a = heap_allocator();
  1412. GB_ASSERT(o.value.kind == ExactValue_String);
  1413. GB_ASSERT(o_hash.value.kind == ExactValue_String);
  1414. String original_string = o.value.value_string;
  1415. String hash_kind = o_hash.value.value_string;
  1416. // TODO: Cache hash values based off of string constant and hash kind?
  1417. u64 hash_value = 0;
  1418. if (check_hash_kind(c, call, hash_kind, original_string.text, original_string.len, &hash_value)) {
  1419. operand->type = t_untyped_integer;
  1420. operand->mode = Addressing_Constant;
  1421. operand->value = exact_value_u64(hash_value);
  1422. return true;
  1423. }
  1424. return false;
  1425. } else if (name == "assert") {
  1426. if (ce->args.count != 1 && ce->args.count != 2) {
  1427. error(call, "'#assert' expects either 1 or 2 arguments, got %td", ce->args.count);
  1428. return false;
  1429. }
  1430. if (!is_type_boolean(operand->type) || operand->mode != Addressing_Constant) {
  1431. gbString str = expr_to_string(ce->args[0]);
  1432. error(call, "'%s' is not a constant boolean", str);
  1433. gb_string_free(str);
  1434. return false;
  1435. }
  1436. if (ce->args.count == 2) {
  1437. Ast *arg = unparen_expr(ce->args[1]);
  1438. if (arg == nullptr || arg->kind != Ast_BasicLit || arg->BasicLit.token.kind != Token_String) {
  1439. gbString str = expr_to_string(arg);
  1440. error(call, "'%s' is not a constant string", str);
  1441. gb_string_free(str);
  1442. return false;
  1443. }
  1444. }
  1445. if (!operand->value.value_bool) {
  1446. ERROR_BLOCK();
  1447. gbString arg1 = expr_to_string(ce->args[0]);
  1448. gbString arg2 = {};
  1449. if (ce->args.count == 1) {
  1450. error(call, "Compile time assertion: %s", arg1);
  1451. } else {
  1452. arg2 = expr_to_string(ce->args[1]);
  1453. error(call, "Compile time assertion: %s (%s)", arg1, arg2);
  1454. }
  1455. if (c->proc_name != "") {
  1456. gbString str = type_to_string(c->curr_proc_sig);
  1457. error_line("\tCalled within '%.*s' :: %s\n", LIT(c->proc_name), str);
  1458. gb_string_free(str);
  1459. }
  1460. gb_string_free(arg1);
  1461. if (ce->args.count == 2) {
  1462. gb_string_free(arg2);
  1463. }
  1464. }
  1465. operand->type = t_untyped_bool;
  1466. operand->mode = Addressing_Constant;
  1467. } else if (name == "panic") {
  1468. ERROR_BLOCK();
  1469. if (ce->args.count != 1) {
  1470. error(call, "'#panic' expects 1 argument, got %td", ce->args.count);
  1471. return false;
  1472. }
  1473. if (!is_type_string(operand->type) && operand->mode != Addressing_Constant) {
  1474. gbString str = expr_to_string(ce->args[0]);
  1475. error(call, "'%s' is not a constant string", str);
  1476. gb_string_free(str);
  1477. return false;
  1478. }
  1479. error(call, "Compile time panic: %.*s", LIT(operand->value.value_string));
  1480. if (c->proc_name != "") {
  1481. gbString str = type_to_string(c->curr_proc_sig);
  1482. error_line("\tCalled within '%.*s' :: %s\n", LIT(c->proc_name), str);
  1483. gb_string_free(str);
  1484. }
  1485. operand->type = t_invalid;
  1486. operand->mode = Addressing_NoValue;
  1487. } else if (name == "defined") {
  1488. if (ce->args.count != 1) {
  1489. error(call, "'#defined' expects 1 argument, got %td", ce->args.count);
  1490. return false;
  1491. }
  1492. Ast *arg = unparen_expr(ce->args[0]);
  1493. if (arg == nullptr || (arg->kind != Ast_Ident && arg->kind != Ast_SelectorExpr)) {
  1494. error(call, "'#defined' expects an identifier or selector expression, got %.*s", LIT(ast_strings[arg->kind]));
  1495. return false;
  1496. }
  1497. if (c->curr_proc_decl == nullptr) {
  1498. error(call, "'#defined' is only allowed within a procedure, prefer the replacement '#config(NAME, default_value)'");
  1499. return false;
  1500. }
  1501. bool is_defined = check_identifier_exists(c->scope, arg);
  1502. // gb_unused(is_defined);
  1503. operand->type = t_untyped_bool;
  1504. operand->mode = Addressing_Constant;
  1505. operand->value = exact_value_bool(is_defined);
  1506. } else if (name == "config") {
  1507. if (ce->args.count != 2) {
  1508. error(call, "'#config' expects 2 argument, got %td", ce->args.count);
  1509. return false;
  1510. }
  1511. Ast *arg = unparen_expr(ce->args[0]);
  1512. if (arg == nullptr || arg->kind != Ast_Ident) {
  1513. error(call, "'#config' expects an identifier, got %.*s", LIT(ast_strings[arg->kind]));
  1514. return false;
  1515. }
  1516. Ast *def_arg = unparen_expr(ce->args[1]);
  1517. Operand def = {};
  1518. check_expr(c, &def, def_arg);
  1519. if (def.mode != Addressing_Constant) {
  1520. error(def_arg, "'#config' default value must be a constant");
  1521. return false;
  1522. }
  1523. String name = arg->Ident.token.string;
  1524. operand->type = def.type;
  1525. operand->mode = def.mode;
  1526. operand->value = def.value;
  1527. Entity *found = scope_lookup_current(config_pkg->scope, name);
  1528. if (found != nullptr) {
  1529. if (found->kind != Entity_Constant) {
  1530. error(arg, "'#config' entity '%.*s' found but expected a constant", LIT(name));
  1531. } else {
  1532. operand->type = found->type;
  1533. operand->mode = Addressing_Constant;
  1534. operand->value = found->Constant.value;
  1535. }
  1536. }
  1537. } else {
  1538. error(call, "Unknown directive call: #%.*s", LIT(name));
  1539. }
  1540. return true;
  1541. }
  1542. gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 id, Type *type_hint) {
  1543. ast_node(ce, CallExpr, call);
  1544. if (ce->inlining != ProcInlining_none) {
  1545. error(call, "Inlining operators are not allowed on built-in procedures");
  1546. }
  1547. BuiltinProc *bp = &builtin_procs[id];
  1548. {
  1549. char const *err = nullptr;
  1550. if (ce->args.count < bp->arg_count) {
  1551. err = "Too few";
  1552. } else if (ce->args.count > bp->arg_count && !bp->variadic) {
  1553. err = "Too many";
  1554. }
  1555. if (err != nullptr) {
  1556. gbString expr = expr_to_string(ce->proc);
  1557. error(ce->close, "%s arguments for '%s', expected %td, got %td",
  1558. err, expr,
  1559. bp->arg_count, ce->args.count);
  1560. gb_string_free(expr);
  1561. return false;
  1562. }
  1563. }
  1564. switch (id) {
  1565. case BuiltinProc_size_of:
  1566. case BuiltinProc_align_of:
  1567. case BuiltinProc_offset_of:
  1568. case BuiltinProc_offset_of_by_string:
  1569. case BuiltinProc_type_info_of:
  1570. case BuiltinProc_typeid_of:
  1571. case BuiltinProc_len:
  1572. case BuiltinProc_cap:
  1573. case BuiltinProc_min:
  1574. case BuiltinProc_max:
  1575. case BuiltinProc_type_is_subtype_of:
  1576. case BuiltinProc_objc_send:
  1577. case BuiltinProc_objc_find_selector:
  1578. case BuiltinProc_objc_find_class:
  1579. case BuiltinProc_objc_register_selector:
  1580. case BuiltinProc_objc_register_class:
  1581. case BuiltinProc_atomic_type_is_lock_free:
  1582. case BuiltinProc_has_target_feature:
  1583. // NOTE(bill): The first arg may be a Type, this will be checked case by case
  1584. break;
  1585. case BuiltinProc_atomic_thread_fence:
  1586. case BuiltinProc_atomic_signal_fence:
  1587. // NOTE(bill): first type will require a type hint
  1588. break;
  1589. case BuiltinProc_DIRECTIVE: {
  1590. ast_node(bd, BasicDirective, ce->proc);
  1591. String name = bd->name.string;
  1592. if (name == "defined") {
  1593. break;
  1594. }
  1595. if (name == "config") {
  1596. break;
  1597. }
  1598. /*fallthrough*/
  1599. }
  1600. default:
  1601. if (BuiltinProc__type_begin < id && id < BuiltinProc__type_end) {
  1602. check_expr_or_type(c, operand, ce->args[0]);
  1603. } else if (ce->args.count > 0) {
  1604. check_multi_expr(c, operand, ce->args[0]);
  1605. }
  1606. break;
  1607. }
  1608. String const &builtin_name = builtin_procs[id].name;
  1609. if (ce->args.count > 0) {
  1610. if (ce->args[0]->kind == Ast_FieldValue) {
  1611. switch (id) {
  1612. case BuiltinProc_soa_zip:
  1613. case BuiltinProc_quaternion:
  1614. // okay
  1615. break;
  1616. default:
  1617. error(call, "'field = value' calling is not allowed on built-in procedures");
  1618. return false;
  1619. }
  1620. }
  1621. }
  1622. if (BuiltinProc__simd_begin < id && id < BuiltinProc__simd_end) {
  1623. bool ok = check_builtin_simd_operation(c, operand, call, id, type_hint);
  1624. if (!ok) {
  1625. operand->type = t_invalid;
  1626. }
  1627. operand->mode = Addressing_Value;
  1628. operand->value = {};
  1629. operand->expr = call;
  1630. return ok;
  1631. }
  1632. switch (id) {
  1633. default:
  1634. GB_PANIC("Implement built-in procedure: %.*s", LIT(builtin_name));
  1635. break;
  1636. case BuiltinProc_objc_send:
  1637. case BuiltinProc_objc_find_selector:
  1638. case BuiltinProc_objc_find_class:
  1639. case BuiltinProc_objc_register_selector:
  1640. case BuiltinProc_objc_register_class:
  1641. return check_builtin_objc_procedure(c, operand, call, id, type_hint);
  1642. case BuiltinProc___entry_point:
  1643. operand->mode = Addressing_NoValue;
  1644. operand->type = nullptr;
  1645. mpsc_enqueue(&c->info->intrinsics_entry_point_usage, call);
  1646. break;
  1647. case BuiltinProc_DIRECTIVE:
  1648. return check_builtin_procedure_directive(c, operand, call, type_hint);
  1649. case BuiltinProc_len:
  1650. case BuiltinProc_cap:
  1651. {
  1652. // len :: proc(Type) -> int
  1653. // cap :: proc(Type) -> int
  1654. check_expr_or_type(c, operand, ce->args[0]);
  1655. if (operand->mode == Addressing_Invalid) {
  1656. return false;
  1657. }
  1658. Type *op_type = type_deref(operand->type);
  1659. Type *type = t_int;
  1660. if (type_hint != nullptr) {
  1661. Type *bt = type_hint;
  1662. // bt = base_type(bt);
  1663. if (bt == t_int) {
  1664. type = type_hint;
  1665. } else if (bt == t_uint) {
  1666. type = type_hint;
  1667. }
  1668. }
  1669. AddressingMode mode = Addressing_Invalid;
  1670. ExactValue value = {};
  1671. if (is_type_string(op_type) && id == BuiltinProc_len) {
  1672. if (operand->mode == Addressing_Constant) {
  1673. mode = Addressing_Constant;
  1674. String str = operand->value.value_string;
  1675. value = exact_value_i64(str.len);
  1676. type = t_untyped_integer;
  1677. } else {
  1678. mode = Addressing_Value;
  1679. if (is_type_cstring(op_type)) {
  1680. add_package_dependency(c, "runtime", "cstring_len");
  1681. }
  1682. }
  1683. } else if (is_type_array(op_type)) {
  1684. Type *at = core_type(op_type);
  1685. mode = Addressing_Constant;
  1686. value = exact_value_i64(at->Array.count);
  1687. type = t_untyped_integer;
  1688. } else if (is_type_enumerated_array(op_type) && id == BuiltinProc_len) {
  1689. Type *at = core_type(op_type);
  1690. mode = Addressing_Constant;
  1691. value = exact_value_i64(at->EnumeratedArray.count);
  1692. type = t_untyped_integer;
  1693. } else if (is_type_slice(op_type) && id == BuiltinProc_len) {
  1694. mode = Addressing_Value;
  1695. } else if (is_type_dynamic_array(op_type)) {
  1696. mode = Addressing_Value;
  1697. } else if (is_type_map(op_type)) {
  1698. mode = Addressing_Value;
  1699. } else if (operand->mode == Addressing_Type && is_type_enum(op_type)) {
  1700. Type *bt = base_type(op_type);
  1701. mode = Addressing_Constant;
  1702. type = t_untyped_integer;
  1703. if (id == BuiltinProc_len) {
  1704. value = exact_value_i64(bt->Enum.fields.count);
  1705. } else {
  1706. GB_ASSERT(id == BuiltinProc_cap);
  1707. value = exact_value_sub(*bt->Enum.max_value, *bt->Enum.min_value);
  1708. value = exact_value_increment_one(value);
  1709. }
  1710. } else if (is_type_struct(op_type)) {
  1711. Type *bt = base_type(op_type);
  1712. if (bt->Struct.soa_kind == StructSoa_Fixed) {
  1713. mode = Addressing_Constant;
  1714. value = exact_value_i64(bt->Struct.soa_count);
  1715. type = t_untyped_integer;
  1716. } else if ((bt->Struct.soa_kind == StructSoa_Slice && id == BuiltinProc_len) ||
  1717. bt->Struct.soa_kind == StructSoa_Dynamic) {
  1718. mode = Addressing_Value;
  1719. }
  1720. } else if (is_type_simd_vector(op_type)) {
  1721. Type *bt = base_type(op_type);
  1722. mode = Addressing_Constant;
  1723. value = exact_value_i64(bt->SimdVector.count);
  1724. type = t_untyped_integer;
  1725. }
  1726. if (operand->mode == Addressing_Type && mode != Addressing_Constant) {
  1727. mode = Addressing_Invalid;
  1728. }
  1729. if (mode == Addressing_Invalid) {
  1730. gbString t = type_to_string(operand->type);
  1731. error(call, "'%.*s' is not supported for '%s'", LIT(builtin_name), t);
  1732. return false;
  1733. }
  1734. operand->mode = mode;
  1735. operand->value = value;
  1736. operand->type = type;
  1737. break;
  1738. }
  1739. case BuiltinProc_size_of: {
  1740. // size_of :: proc(Type or expr) -> untyped int
  1741. Operand o = {};
  1742. check_expr_or_type(c, &o, ce->args[0]);
  1743. if (o.mode == Addressing_Invalid) {
  1744. return false;
  1745. }
  1746. Type *t = o.type;
  1747. if (t == nullptr || t == t_invalid) {
  1748. error(ce->args[0], "Invalid argument for 'size_of'");
  1749. return false;
  1750. }
  1751. t = default_type(t);
  1752. operand->mode = Addressing_Constant;
  1753. operand->value = exact_value_i64(type_size_of(t));
  1754. operand->type = t_untyped_integer;
  1755. break;
  1756. }
  1757. case BuiltinProc_align_of: {
  1758. // align_of :: proc(Type or expr) -> untyped int
  1759. Operand o = {};
  1760. check_expr_or_type(c, &o, ce->args[0]);
  1761. if (o.mode == Addressing_Invalid) {
  1762. return false;
  1763. }
  1764. Type *t = o.type;
  1765. if (t == nullptr || t == t_invalid) {
  1766. error(ce->args[0], "Invalid argument for 'align_of'");
  1767. return false;
  1768. }
  1769. t = default_type(t);
  1770. operand->mode = Addressing_Constant;
  1771. operand->value = exact_value_i64(type_align_of(t));
  1772. operand->type = t_untyped_integer;
  1773. break;
  1774. }
  1775. case BuiltinProc_offset_of: {
  1776. // offset_of :: proc(value.field) -> uintptr
  1777. // offset_of :: proc(Type, field) -> uintptr
  1778. Type *type = nullptr;
  1779. Ast *field_arg = nullptr;
  1780. if (ce->args.count == 1) {
  1781. Ast *arg0 = unparen_expr(ce->args[0]);
  1782. if (arg0->kind != Ast_SelectorExpr) {
  1783. gbString x = expr_to_string(arg0);
  1784. error(ce->args[0], "Invalid expression for '%.*s', '%s' is not a selector expression", LIT(builtin_name), x);
  1785. gb_string_free(x);
  1786. return false;
  1787. }
  1788. ast_node(se, SelectorExpr, arg0);
  1789. Operand x = {};
  1790. check_expr(c, &x, se->expr);
  1791. if (x.mode == Addressing_Invalid) {
  1792. return false;
  1793. }
  1794. type = type_deref(x.type);
  1795. Type *bt = base_type(type);
  1796. if (bt == nullptr || bt == t_invalid) {
  1797. error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
  1798. return false;
  1799. }
  1800. field_arg = unparen_expr(se->selector);
  1801. } else if (ce->args.count == 2) {
  1802. type = check_type(c, ce->args[0]);
  1803. Type *bt = base_type(type);
  1804. if (bt == nullptr || bt == t_invalid) {
  1805. error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
  1806. return false;
  1807. }
  1808. field_arg = unparen_expr(ce->args[1]);
  1809. } else {
  1810. error(ce->args[0], "Expected either 1 or 2 arguments to '%.*s', in the format of '%.*s(Type, field)', '%.*s(value.field)'", LIT(builtin_name), LIT(builtin_name), LIT(builtin_name));
  1811. return false;
  1812. }
  1813. GB_ASSERT(type != nullptr);
  1814. String field_name = {};
  1815. if (field_arg == nullptr) {
  1816. error(call, "Expected an identifier for field argument");
  1817. return false;
  1818. }
  1819. if (field_arg->kind == Ast_Ident) {
  1820. field_name = field_arg->Ident.token.string;
  1821. }
  1822. if (field_name.len == 0) {
  1823. error(field_arg, "Expected an identifier for field argument");
  1824. return false;
  1825. }
  1826. if (is_type_array(type)) {
  1827. gbString t = type_to_string(type);
  1828. error(field_arg, "Invalid a struct type for '%.*s', got '%s'", LIT(builtin_name), t);
  1829. gb_string_free(t);
  1830. return false;
  1831. }
  1832. Type *bt = base_type(type);
  1833. if (bt->kind == Type_Struct && bt->Struct.scope != nullptr) {
  1834. if (is_type_polymorphic(bt)) {
  1835. gbString t = type_to_string(type);
  1836. error(field_arg, "Cannot use '%.*s' on an unspecialized polymorphic struct type, got '%s'", LIT(builtin_name), t);
  1837. gb_string_free(t);
  1838. return false;
  1839. } else if (bt->Struct.fields.count == 0 && bt->Struct.node == nullptr) {
  1840. gbString t = type_to_string(type);
  1841. error(field_arg, "Cannot use '%.*s' on incomplete struct declaration, got '%s'", LIT(builtin_name), t);
  1842. gb_string_free(t);
  1843. return false;
  1844. }
  1845. }
  1846. Selection sel = lookup_field(type, field_name, false);
  1847. if (sel.entity == nullptr) {
  1848. ERROR_BLOCK();
  1849. gbString type_str = type_to_string_shorthand(type);
  1850. error(ce->args[0],
  1851. "'%s' has no field named '%.*s'", type_str, LIT(field_name));
  1852. gb_string_free(type_str);
  1853. Type *bt = base_type(type);
  1854. if (bt->kind == Type_Struct) {
  1855. check_did_you_mean_type(field_name, bt->Struct.fields);
  1856. }
  1857. return false;
  1858. }
  1859. if (sel.indirect) {
  1860. gbString type_str = type_to_string_shorthand(type);
  1861. error(ce->args[0],
  1862. "Field '%.*s' is embedded via a pointer in '%s'", LIT(field_name), type_str);
  1863. gb_string_free(type_str);
  1864. return false;
  1865. }
  1866. operand->mode = Addressing_Constant;
  1867. operand->value = exact_value_i64(type_offset_of_from_selection(type, sel));
  1868. operand->type = t_uintptr;
  1869. break;
  1870. }
  1871. case BuiltinProc_offset_of_by_string: {
  1872. // offset_of_by_string :: proc(Type, string) -> uintptr
  1873. Type *type = nullptr;
  1874. Ast *field_arg = nullptr;
  1875. if (ce->args.count == 2) {
  1876. type = check_type(c, ce->args[0]);
  1877. Type *bt = base_type(type);
  1878. if (bt == nullptr || bt == t_invalid) {
  1879. error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
  1880. return false;
  1881. }
  1882. field_arg = unparen_expr(ce->args[1]);
  1883. } else {
  1884. error(ce->args[0], "Expected either 2 arguments to '%.*s', in the format of '%.*s(Type, field)'", LIT(builtin_name), LIT(builtin_name));
  1885. return false;
  1886. }
  1887. GB_ASSERT(type != nullptr);
  1888. String field_name = {};
  1889. if (field_arg == nullptr) {
  1890. error(call, "Expected a constant (not-empty) string for field argument");
  1891. return false;
  1892. }
  1893. Operand x = {};
  1894. check_expr(c, &x, field_arg);
  1895. if (x.mode == Addressing_Constant && x.value.kind == ExactValue_String) {
  1896. field_name = x.value.value_string;
  1897. }
  1898. if (field_name.len == 0) {
  1899. error(field_arg, "Expected a constant (non-empty) string for field argument");
  1900. return false;
  1901. }
  1902. if (is_type_array(type)) {
  1903. gbString t = type_to_string(type);
  1904. error(field_arg, "Invalid a struct type for '%.*s', got '%s'", LIT(builtin_name), t);
  1905. gb_string_free(t);
  1906. return false;
  1907. }
  1908. Selection sel = lookup_field(type, field_name, false);
  1909. if (sel.entity == nullptr) {
  1910. ERROR_BLOCK();
  1911. gbString type_str = type_to_string_shorthand(type);
  1912. error(ce->args[0],
  1913. "'%s' has no field named '%.*s'", type_str, LIT(field_name));
  1914. gb_string_free(type_str);
  1915. Type *bt = base_type(type);
  1916. if (bt->kind == Type_Struct) {
  1917. check_did_you_mean_type(field_name, bt->Struct.fields);
  1918. }
  1919. return false;
  1920. }
  1921. if (sel.indirect) {
  1922. gbString type_str = type_to_string_shorthand(type);
  1923. error(ce->args[0],
  1924. "Field '%.*s' is embedded via a pointer in '%s'", LIT(field_name), type_str);
  1925. gb_string_free(type_str);
  1926. return false;
  1927. }
  1928. operand->mode = Addressing_Constant;
  1929. operand->value = exact_value_i64(type_offset_of_from_selection(type, sel));
  1930. operand->type = t_uintptr;
  1931. break;
  1932. }
  1933. case BuiltinProc_type_of: {
  1934. // type_of :: proc(val: Type) -> type(Type)
  1935. Ast *expr = ce->args[0];
  1936. Operand o = {};
  1937. check_expr_or_type(c, &o, expr);
  1938. // check_assignment(c, operand, nullptr, str_lit("argument of 'type_of'"));
  1939. if (o.mode == Addressing_Invalid || o.mode == Addressing_Builtin) {
  1940. return false;
  1941. }
  1942. if (o.type == nullptr || o.type == t_invalid || is_type_asm_proc(o.type)) {
  1943. error(o.expr, "Invalid argument to 'type_of'");
  1944. return false;
  1945. }
  1946. // NOTE(bill): Prevent type cycles for procedure declarations
  1947. if (c->curr_proc_sig == o.type) {
  1948. gbString s = expr_to_string(o.expr);
  1949. error(o.expr, "Invalid cyclic type usage from 'type_of', got '%s'", s);
  1950. gb_string_free(s);
  1951. return false;
  1952. }
  1953. if (is_type_polymorphic(o.type)) {
  1954. error(o.expr, "'type_of' of polymorphic type cannot be determined");
  1955. return false;
  1956. }
  1957. operand->mode = Addressing_Type;
  1958. operand->type = o.type;
  1959. break;
  1960. }
  1961. case BuiltinProc_type_info_of: {
  1962. // type_info_of :: proc(Type) -> ^Type_Info
  1963. if (c->scope->flags&ScopeFlag_Global) {
  1964. compiler_error("'type_info_of' Cannot be declared within the runtime package due to how the internals of the compiler works");
  1965. }
  1966. if (build_context.no_rtti) {
  1967. error(call, "'%.*s' has been disallowed", LIT(builtin_name));
  1968. return false;
  1969. }
  1970. // NOTE(bill): The type information may not be setup yet
  1971. init_core_type_info(c->checker);
  1972. Ast *expr = ce->args[0];
  1973. Operand o = {};
  1974. check_expr_or_type(c, &o, expr);
  1975. if (o.mode == Addressing_Invalid) {
  1976. return false;
  1977. }
  1978. Type *t = o.type;
  1979. if (t == nullptr || t == t_invalid || is_type_asm_proc(o.type) || is_type_polymorphic(t)) {
  1980. if (is_type_polymorphic(t)) {
  1981. error(ce->args[0], "Invalid argument for '%.*s', unspecialized polymorphic type", LIT(builtin_name));
  1982. } else {
  1983. error(ce->args[0], "Invalid argument for '%.*s'", LIT(builtin_name));
  1984. }
  1985. return false;
  1986. }
  1987. t = default_type(t);
  1988. add_type_info_type(c, t);
  1989. GB_ASSERT(t_type_info_ptr != nullptr);
  1990. add_type_info_type(c, t_type_info_ptr);
  1991. if (is_operand_value(o) && is_type_typeid(t)) {
  1992. add_package_dependency(c, "runtime", "__type_info_of");
  1993. } else if (o.mode != Addressing_Type) {
  1994. error(expr, "Expected a type or typeid for '%.*s'", LIT(builtin_name));
  1995. return false;
  1996. }
  1997. operand->mode = Addressing_Value;
  1998. operand->type = t_type_info_ptr;
  1999. break;
  2000. }
  2001. case BuiltinProc_typeid_of: {
  2002. // typeid_of :: proc(Type) -> typeid
  2003. if (c->scope->flags&ScopeFlag_Global) {
  2004. compiler_error("'typeid_of' Cannot be declared within the runtime package due to how the internals of the compiler works");
  2005. }
  2006. if (build_context.no_rtti) {
  2007. error(call, "'%.*s' has been disallowed", LIT(builtin_name));
  2008. return false;
  2009. }
  2010. // NOTE(bill): The type information may not be setup yet
  2011. init_core_type_info(c->checker);
  2012. Ast *expr = ce->args[0];
  2013. Operand o = {};
  2014. check_expr_or_type(c, &o, expr);
  2015. if (o.mode == Addressing_Invalid) {
  2016. return false;
  2017. }
  2018. Type *t = o.type;
  2019. if (t == nullptr || t == t_invalid || is_type_asm_proc(t) || is_type_polymorphic(t)) {
  2020. error(ce->args[0], "Invalid argument for '%.*s'", LIT(builtin_name));
  2021. return false;
  2022. }
  2023. t = default_type(t);
  2024. add_type_info_type(c, t);
  2025. if (o.mode != Addressing_Type) {
  2026. error(expr, "Expected a type for '%.*s'", LIT(builtin_name));
  2027. return false;
  2028. }
  2029. operand->mode = Addressing_Value;
  2030. operand->type = t_typeid;
  2031. operand->value = exact_value_typeid(t);
  2032. break;
  2033. }
  2034. case BuiltinProc_swizzle: {
  2035. // swizzle :: proc(v: [N]T, ..int) -> [M]T
  2036. Type *original_type = operand->type;
  2037. Type *type = base_type(original_type);
  2038. i64 max_count = 0;
  2039. Type *elem_type = nullptr;
  2040. if (!is_type_array(type) && !is_type_simd_vector(type)) {
  2041. gbString type_str = type_to_string(operand->type);
  2042. error(call,
  2043. "'swizzle' is only allowed on an array or #simd vector, got '%s'",
  2044. type_str);
  2045. gb_string_free(type_str);
  2046. return false;
  2047. }
  2048. if (type->kind == Type_Array) {
  2049. max_count = type->Array.count;
  2050. elem_type = type->Array.elem;
  2051. } else if (type->kind == Type_SimdVector) {
  2052. max_count = type->SimdVector.count;
  2053. elem_type = type->SimdVector.elem;
  2054. }
  2055. i64 arg_count = 0;
  2056. for_array(i, ce->args) {
  2057. if (i == 0) {
  2058. continue;
  2059. }
  2060. Ast *arg = ce->args[i];
  2061. Operand op = {};
  2062. check_expr(c, &op, arg);
  2063. if (op.mode == Addressing_Invalid) {
  2064. return false;
  2065. }
  2066. Type *arg_type = base_type(op.type);
  2067. if (!is_type_integer(arg_type) || op.mode != Addressing_Constant) {
  2068. error(op.expr, "Indices to 'swizzle' must be constant integers");
  2069. return false;
  2070. }
  2071. if (big_int_is_neg(&op.value.value_integer)) {
  2072. error(op.expr, "Negative 'swizzle' index");
  2073. return false;
  2074. }
  2075. BigInt mc = {};
  2076. big_int_from_i64(&mc, max_count);
  2077. if (big_int_cmp(&mc, &op.value.value_integer) <= 0) {
  2078. error(op.expr, "'swizzle' index exceeds length");
  2079. return false;
  2080. }
  2081. arg_count++;
  2082. }
  2083. if (arg_count > max_count) {
  2084. error(call, "Too many 'swizzle' indices, %td > %td", arg_count, max_count);
  2085. return false;
  2086. }
  2087. if (type->kind == Type_Array) {
  2088. if (operand->mode == Addressing_Variable) {
  2089. operand->mode = Addressing_SwizzleVariable;
  2090. } else {
  2091. operand->mode = Addressing_SwizzleValue;
  2092. }
  2093. } else {
  2094. operand->mode = Addressing_Value;
  2095. }
  2096. if (is_type_simd_vector(type) && !is_power_of_two(arg_count)) {
  2097. error(call, "'swizzle' with a #simd vector must have a power of two arguments, got %lld", cast(long long)arg_count);
  2098. return false;
  2099. }
  2100. operand->type = determine_swizzle_array_type(original_type, type_hint, arg_count);
  2101. break;
  2102. }
  2103. case BuiltinProc_complex: {
  2104. // complex :: proc(real, imag: float_type) -> complex_type
  2105. Operand x = *operand;
  2106. Operand y = {};
  2107. // NOTE(bill): Invalid will be the default till fixed
  2108. operand->type = t_invalid;
  2109. operand->mode = Addressing_Invalid;
  2110. check_expr(c, &y, ce->args[1]);
  2111. if (y.mode == Addressing_Invalid) {
  2112. return false;
  2113. }
  2114. convert_to_typed(c, &x, y.type); if (x.mode == Addressing_Invalid) return false;
  2115. convert_to_typed(c, &y, x.type); if (y.mode == Addressing_Invalid) return false;
  2116. if (x.mode == Addressing_Constant &&
  2117. y.mode == Addressing_Constant) {
  2118. x.value = exact_value_to_float(x.value);
  2119. y.value = exact_value_to_float(y.value);
  2120. if (is_type_numeric(x.type) && x.value.kind == ExactValue_Float) {
  2121. x.type = t_untyped_float;
  2122. }
  2123. if (is_type_numeric(y.type) && y.value.kind == ExactValue_Float) {
  2124. y.type = t_untyped_float;
  2125. }
  2126. }
  2127. if (!are_types_identical(x.type, y.type)) {
  2128. gbString tx = type_to_string(x.type);
  2129. gbString ty = type_to_string(y.type);
  2130. error(call, "Mismatched types to 'complex', '%s' vs '%s'", tx, ty);
  2131. gb_string_free(ty);
  2132. gb_string_free(tx);
  2133. return false;
  2134. }
  2135. if (!is_type_float(x.type)) {
  2136. gbString s = type_to_string(x.type);
  2137. error(call, "Arguments have type '%s', expected a floating point", s);
  2138. gb_string_free(s);
  2139. return false;
  2140. }
  2141. if (is_type_endian_specific(x.type)) {
  2142. gbString s = type_to_string(x.type);
  2143. error(call, "Arguments with a specified endian are not allow, expected a normal floating point, got '%s'", s);
  2144. gb_string_free(s);
  2145. return false;
  2146. }
  2147. if (x.mode == Addressing_Constant && y.mode == Addressing_Constant) {
  2148. f64 r = exact_value_to_float(x.value).value_float;
  2149. f64 i = exact_value_to_float(y.value).value_float;
  2150. operand->value = exact_value_complex(r, i);
  2151. operand->mode = Addressing_Constant;
  2152. } else {
  2153. operand->mode = Addressing_Value;
  2154. }
  2155. BasicKind kind = core_type(x.type)->Basic.kind;
  2156. switch (kind) {
  2157. case Basic_f16: operand->type = t_complex32; break;
  2158. case Basic_f32: operand->type = t_complex64; break;
  2159. case Basic_f64: operand->type = t_complex128; break;
  2160. case Basic_UntypedFloat: operand->type = t_untyped_complex; break;
  2161. default: GB_PANIC("Invalid type"); break;
  2162. }
  2163. if (type_hint != nullptr && check_is_castable_to(c, operand, type_hint)) {
  2164. operand->type = type_hint;
  2165. }
  2166. break;
  2167. }
  2168. case BuiltinProc_quaternion: {
  2169. bool first_is_field_value = (ce->args[0]->kind == Ast_FieldValue);
  2170. bool fail = false;
  2171. for (Ast *arg : ce->args) {
  2172. bool mix = false;
  2173. if (first_is_field_value) {
  2174. mix = arg->kind != Ast_FieldValue;
  2175. } else {
  2176. mix = arg->kind == Ast_FieldValue;
  2177. }
  2178. if (mix) {
  2179. error(arg, "Mixture of 'field = value' and value elements in the procedure call '%.*s' is not allowed", LIT(builtin_name));
  2180. fail = true;
  2181. break;
  2182. }
  2183. }
  2184. if (fail) {
  2185. operand->type = t_untyped_quaternion;
  2186. operand->mode = Addressing_Constant;
  2187. operand->value = exact_value_quaternion(0.0, 0.0, 0.0, 0.0);
  2188. break;
  2189. }
  2190. // quaternion :: proc(imag, jmag, kmag, real: float_type) -> complex_type
  2191. Operand xyzw[4] = {};
  2192. u32 first_index = 0;
  2193. // NOTE(bill): Invalid will be the default till fixed
  2194. operand->type = t_invalid;
  2195. operand->mode = Addressing_Invalid;
  2196. if (first_is_field_value) {
  2197. u32 fields_set[4] = {}; // 0 unset, 1 xyzw, 2 real/etc
  2198. auto const check_field = [&fields_set, &builtin_name](CheckerContext *c, Operand *o, Ast *arg, i32 *index) -> bool {
  2199. *index = -1;
  2200. ast_node(field, FieldValue, arg);
  2201. String name = {};
  2202. if (field->field->kind == Ast_Ident) {
  2203. name = field->field->Ident.token.string;
  2204. } else {
  2205. error(field->field, "Expected an identifier for field argument");
  2206. return false;
  2207. }
  2208. u32 style = 0;
  2209. if (name == "x") {
  2210. *index = 0; style = 1;
  2211. } else if (name == "y") {
  2212. *index = 1; style = 1;
  2213. } else if (name == "z") {
  2214. *index = 2; style = 1;
  2215. } else if (name == "w") {
  2216. *index = 3; style = 1;
  2217. } else if (name == "imag") {
  2218. *index = 0; style = 2;
  2219. } else if (name == "jmag") {
  2220. *index = 1; style = 2;
  2221. } else if (name == "kmag") {
  2222. *index = 2; style = 2;
  2223. } else if (name == "real") {
  2224. *index = 3; style = 2;
  2225. } else {
  2226. error(field->field, "Unknown name for '%.*s', expected (w, x, y, z; or real, imag, jmag, kmag), got '%.*s'", LIT(builtin_name), LIT(name));
  2227. return false;
  2228. }
  2229. if (fields_set[*index]) {
  2230. error(field->field, "Previously assigned field: '%.*s'", LIT(name));
  2231. }
  2232. fields_set[*index] = style;
  2233. check_expr(c, o, field->value);
  2234. return o->mode != Addressing_Invalid;
  2235. };
  2236. Operand *refs[4] = {&xyzw[0], &xyzw[1], &xyzw[2], &xyzw[3]};
  2237. for (i32 i = 0; i < 4; i++) {
  2238. i32 index = -1;
  2239. Operand o = {};
  2240. bool ok = check_field(c, &o, ce->args[i], &index);
  2241. if (!ok || index < 0) {
  2242. return false;
  2243. }
  2244. first_index = cast(u32)index;
  2245. *refs[index] = o;
  2246. }
  2247. for (i32 i = 0; i < 4; i++) {
  2248. GB_ASSERT(fields_set[i]);
  2249. }
  2250. for (i32 i = 1; i < 4; i++) {
  2251. if (fields_set[i] != fields_set[i-1]) {
  2252. error(call, "Mixture of xyzw and real/etc is not allowed with '%.*s'", LIT(builtin_name));
  2253. break;
  2254. }
  2255. }
  2256. } else {
  2257. error(call, "'%.*s' requires that all arguments are named (w, x, y, z; or real, imag, jmag, kmag)", LIT(builtin_name));
  2258. for (i32 i = 0; i < 4; i++) {
  2259. check_expr(c, &xyzw[i], ce->args[i]);
  2260. if (xyzw[i].mode == Addressing_Invalid) {
  2261. return false;
  2262. }
  2263. }
  2264. }
  2265. for (u32 i = 0; i < 4; i++ ){
  2266. u32 j = (i + first_index) % 4;
  2267. if (j == first_index) {
  2268. convert_to_typed(c, &xyzw[j], xyzw[(first_index+1)%4].type); if (xyzw[j].mode == Addressing_Invalid) return false;
  2269. } else {
  2270. convert_to_typed(c, &xyzw[j], xyzw[first_index].type); if (xyzw[j].mode == Addressing_Invalid) return false;
  2271. }
  2272. }
  2273. if (xyzw[0].mode == Addressing_Constant &&
  2274. xyzw[1].mode == Addressing_Constant &&
  2275. xyzw[2].mode == Addressing_Constant &&
  2276. xyzw[3].mode == Addressing_Constant) {
  2277. for (i32 i = 0; i < 4; i++) {
  2278. xyzw[i].value = exact_value_to_float(xyzw[i].value);
  2279. }
  2280. for (i32 i = 0; i < 4; i++) {
  2281. if (is_type_numeric(xyzw[i].type) && xyzw[i].value.kind == ExactValue_Float) {
  2282. xyzw[i].type = t_untyped_float;
  2283. }
  2284. }
  2285. }
  2286. if (!(are_types_identical(xyzw[0].type, xyzw[1].type) &&
  2287. are_types_identical(xyzw[0].type, xyzw[2].type) &&
  2288. are_types_identical(xyzw[0].type, xyzw[3].type))) {
  2289. gbString tx = type_to_string(xyzw[0].type);
  2290. gbString ty = type_to_string(xyzw[1].type);
  2291. gbString tz = type_to_string(xyzw[2].type);
  2292. gbString tw = type_to_string(xyzw[3].type);
  2293. error(call, "Mismatched types to 'quaternion', 'x=%s' vs 'y=%s' vs 'z=%s' vs 'w=%s'", tx, ty, tz, tw);
  2294. gb_string_free(tw);
  2295. gb_string_free(tz);
  2296. gb_string_free(ty);
  2297. gb_string_free(tx);
  2298. return false;
  2299. }
  2300. if (!is_type_float(xyzw[0].type)) {
  2301. gbString s = type_to_string(xyzw[0].type);
  2302. error(call, "Arguments have type '%s', expected a floating point", s);
  2303. gb_string_free(s);
  2304. return false;
  2305. }
  2306. if (is_type_endian_specific(xyzw[0].type)) {
  2307. gbString s = type_to_string(xyzw[0].type);
  2308. error(call, "Arguments with a specified endian are not allow, expected a normal floating point, got '%s'", s);
  2309. gb_string_free(s);
  2310. return false;
  2311. }
  2312. operand->mode = Addressing_Value;
  2313. if (xyzw[0].mode == Addressing_Constant &&
  2314. xyzw[1].mode == Addressing_Constant &&
  2315. xyzw[2].mode == Addressing_Constant &&
  2316. xyzw[3].mode == Addressing_Constant) {
  2317. f64 r = exact_value_to_float(xyzw[3].value).value_float;
  2318. f64 i = exact_value_to_float(xyzw[0].value).value_float;
  2319. f64 j = exact_value_to_float(xyzw[1].value).value_float;
  2320. f64 k = exact_value_to_float(xyzw[2].value).value_float;
  2321. operand->value = exact_value_quaternion(r, i, j, k);
  2322. operand->mode = Addressing_Constant;
  2323. }
  2324. BasicKind kind = core_type(xyzw[first_index].type)->Basic.kind;
  2325. switch (kind) {
  2326. case Basic_f16: operand->type = t_quaternion64; break;
  2327. case Basic_f32: operand->type = t_quaternion128; break;
  2328. case Basic_f64: operand->type = t_quaternion256; break;
  2329. case Basic_UntypedFloat: operand->type = t_untyped_quaternion; break;
  2330. default: GB_PANIC("Invalid type"); break;
  2331. }
  2332. if (type_hint != nullptr && check_is_castable_to(c, operand, type_hint)) {
  2333. operand->type = type_hint;
  2334. }
  2335. break;
  2336. }
  2337. case BuiltinProc_real:
  2338. case BuiltinProc_imag: {
  2339. // real :: proc(x: type) -> float_type
  2340. // imag :: proc(x: type) -> float_type
  2341. Operand *x = operand;
  2342. if (is_type_untyped(x->type)) {
  2343. if (x->mode == Addressing_Constant) {
  2344. if (is_type_numeric(x->type)) {
  2345. x->type = t_untyped_complex;
  2346. }
  2347. } else if (is_type_quaternion(x->type)) {
  2348. convert_to_typed(c, x, t_quaternion256);
  2349. if (x->mode == Addressing_Invalid) {
  2350. return false;
  2351. }
  2352. } else{
  2353. convert_to_typed(c, x, t_complex128);
  2354. if (x->mode == Addressing_Invalid) {
  2355. return false;
  2356. }
  2357. }
  2358. }
  2359. if (!is_type_complex(x->type) && !is_type_quaternion(x->type)) {
  2360. gbString s = type_to_string(x->type);
  2361. error(call, "Argument has type '%s', expected a complex or quaternion type", s);
  2362. gb_string_free(s);
  2363. return false;
  2364. }
  2365. if (x->mode == Addressing_Constant) {
  2366. switch (id) {
  2367. case BuiltinProc_real: x->value = exact_value_real(x->value); break;
  2368. case BuiltinProc_imag: x->value = exact_value_imag(x->value); break;
  2369. }
  2370. } else {
  2371. x->mode = Addressing_Value;
  2372. }
  2373. BasicKind kind = core_type(x->type)->Basic.kind;
  2374. switch (kind) {
  2375. case Basic_complex32: x->type = t_f16; break;
  2376. case Basic_complex64: x->type = t_f32; break;
  2377. case Basic_complex128: x->type = t_f64; break;
  2378. case Basic_quaternion64: x->type = t_f16; break;
  2379. case Basic_quaternion128: x->type = t_f32; break;
  2380. case Basic_quaternion256: x->type = t_f64; break;
  2381. case Basic_UntypedComplex: x->type = t_untyped_float; break;
  2382. case Basic_UntypedQuaternion: x->type = t_untyped_float; break;
  2383. default: GB_PANIC("Invalid type"); break;
  2384. }
  2385. if (type_hint != nullptr && check_is_castable_to(c, operand, type_hint)) {
  2386. operand->type = type_hint;
  2387. }
  2388. break;
  2389. }
  2390. case BuiltinProc_jmag:
  2391. case BuiltinProc_kmag: {
  2392. // jmag :: proc(x: type) -> float_type
  2393. // kmag :: proc(x: type) -> float_type
  2394. Operand *x = operand;
  2395. if (is_type_untyped(x->type)) {
  2396. if (x->mode == Addressing_Constant) {
  2397. if (is_type_numeric(x->type)) {
  2398. x->type = t_untyped_complex;
  2399. }
  2400. } else{
  2401. convert_to_typed(c, x, t_quaternion256);
  2402. if (x->mode == Addressing_Invalid) {
  2403. return false;
  2404. }
  2405. }
  2406. }
  2407. if (!is_type_quaternion(x->type)) {
  2408. gbString s = type_to_string(x->type);
  2409. error(call, "Argument has type '%s', expected a quaternion type", s);
  2410. gb_string_free(s);
  2411. return false;
  2412. }
  2413. if (x->mode == Addressing_Constant) {
  2414. switch (id) {
  2415. case BuiltinProc_jmag: x->value = exact_value_jmag(x->value); break;
  2416. case BuiltinProc_kmag: x->value = exact_value_kmag(x->value); break;
  2417. }
  2418. } else {
  2419. x->mode = Addressing_Value;
  2420. }
  2421. BasicKind kind = core_type(x->type)->Basic.kind;
  2422. switch (kind) {
  2423. case Basic_quaternion64: x->type = t_f16; break;
  2424. case Basic_quaternion128: x->type = t_f32; break;
  2425. case Basic_quaternion256: x->type = t_f64; break;
  2426. case Basic_UntypedComplex: x->type = t_untyped_float; break;
  2427. case Basic_UntypedQuaternion: x->type = t_untyped_float; break;
  2428. default: GB_PANIC("Invalid type"); break;
  2429. }
  2430. if (type_hint != nullptr && check_is_castable_to(c, operand, type_hint)) {
  2431. operand->type = type_hint;
  2432. }
  2433. break;
  2434. }
  2435. case BuiltinProc_conj: {
  2436. // conj :: proc(x: type) -> type
  2437. Operand *x = operand;
  2438. Type *t = x->type;
  2439. Type *elem = core_array_type(t);
  2440. if (is_type_complex(t)) {
  2441. if (x->mode == Addressing_Constant) {
  2442. ExactValue v = exact_value_to_complex(x->value);
  2443. f64 r = v.value_complex->real;
  2444. f64 i = -v.value_complex->imag;
  2445. x->value = exact_value_complex(r, i);
  2446. x->mode = Addressing_Constant;
  2447. } else {
  2448. x->mode = Addressing_Value;
  2449. }
  2450. } else if (is_type_quaternion(t)) {
  2451. if (x->mode == Addressing_Constant) {
  2452. ExactValue v = exact_value_to_quaternion(x->value);
  2453. f64 r = +v.value_quaternion->real;
  2454. f64 i = -v.value_quaternion->imag;
  2455. f64 j = -v.value_quaternion->jmag;
  2456. f64 k = -v.value_quaternion->kmag;
  2457. x->value = exact_value_quaternion(r, i, j, k);
  2458. x->mode = Addressing_Constant;
  2459. } else {
  2460. x->mode = Addressing_Value;
  2461. }
  2462. } else if (is_type_array_like(t) && (is_type_complex(elem) || is_type_quaternion(elem))) {
  2463. x->mode = Addressing_Value;
  2464. } else if (is_type_matrix(t) && (is_type_complex(elem) || is_type_quaternion(elem))) {
  2465. x->mode = Addressing_Value;
  2466. }else {
  2467. gbString s = type_to_string(x->type);
  2468. error(call, "Expected a complex or quaternion, got '%s'", s);
  2469. gb_string_free(s);
  2470. return false;
  2471. }
  2472. break;
  2473. }
  2474. case BuiltinProc_expand_values: {
  2475. Type *type = base_type(operand->type);
  2476. if (!is_type_struct(type) && !is_type_array(type)) {
  2477. gbString type_str = type_to_string(operand->type);
  2478. error(call, "Expected a struct or array type, got '%s'", type_str);
  2479. gb_string_free(type_str);
  2480. return false;
  2481. }
  2482. gbAllocator a = permanent_allocator();
  2483. Type *tuple = alloc_type_tuple();
  2484. if (is_type_struct(type)) {
  2485. isize variable_count = type->Struct.fields.count;
  2486. slice_init(&tuple->Tuple.variables, a, variable_count);
  2487. // NOTE(bill): don't copy the entities, this should be good enough
  2488. gb_memmove_array(tuple->Tuple.variables.data, type->Struct.fields.data, variable_count);
  2489. } else if (is_type_array(type)) {
  2490. isize variable_count = cast(isize)type->Array.count;
  2491. slice_init(&tuple->Tuple.variables, a, variable_count);
  2492. for (isize i = 0; i < variable_count; i++) {
  2493. tuple->Tuple.variables[i] = alloc_entity_array_elem(nullptr, blank_token, type->Array.elem, cast(i32)i);
  2494. }
  2495. }
  2496. operand->type = tuple;
  2497. operand->mode = Addressing_Value;
  2498. if (tuple->Tuple.variables.count == 1) {
  2499. operand->type = tuple->Tuple.variables[0]->type;
  2500. }
  2501. break;
  2502. }
  2503. case BuiltinProc_min: {
  2504. // min :: proc($T: typeid) -> ordered
  2505. // min :: proc(a: ..ordered) -> ordered
  2506. check_multi_expr_or_type(c, operand, ce->args[0]);
  2507. Type *original_type = operand->type;
  2508. Type *type = base_type(operand->type);
  2509. if (operand->mode == Addressing_Type && is_type_enumerated_array(type)) {
  2510. // Okay
  2511. } else if (!is_type_ordered(type) || !(is_type_numeric(type) || is_type_string(type))) {
  2512. gbString type_str = type_to_string(original_type);
  2513. error(call, "Expected a ordered numeric type to 'min', got '%s'", type_str);
  2514. gb_string_free(type_str);
  2515. return false;
  2516. }
  2517. if (operand->mode == Addressing_Type) {
  2518. if (ce->args.count != 1) {
  2519. error(call, "If 'min' gets a type, only 1 arguments is allowed, got %td", ce->args.count);
  2520. return false;
  2521. }
  2522. if (is_type_boolean(type)) {
  2523. operand->mode = Addressing_Constant;
  2524. operand->type = original_type;
  2525. operand->value = exact_value_bool(false);
  2526. return true;
  2527. } else if (is_type_integer(type)) {
  2528. operand->mode = Addressing_Constant;
  2529. operand->type = original_type;
  2530. if (is_type_unsigned(type)) {
  2531. operand->value = exact_value_u64(0);
  2532. return true;
  2533. } else {
  2534. i64 sz = 8*type_size_of(type);
  2535. ExactValue a = exact_value_i64(1);
  2536. ExactValue b = exact_value_i64(sz-1);
  2537. ExactValue v = exact_binary_operator_value(Token_Shl, a, b);
  2538. v = exact_unary_operator_value(Token_Sub, v, cast(i32)sz, false);
  2539. operand->value = v;
  2540. return true;
  2541. }
  2542. } else if (is_type_float(type)) {
  2543. operand->mode = Addressing_Constant;
  2544. operand->type = original_type;
  2545. switch (type_size_of(type)) {
  2546. case 2:
  2547. operand->value = exact_value_float(-65504.0f);
  2548. break;
  2549. case 4:
  2550. operand->value = exact_value_float(-3.402823466e+38f);
  2551. break;
  2552. case 8:
  2553. operand->value = exact_value_float(-1.7976931348623158e+308);
  2554. break;
  2555. default:
  2556. GB_PANIC("Unhandled float type");
  2557. break;
  2558. }
  2559. return true;
  2560. } else if (is_type_enum(type)) {
  2561. operand->mode = Addressing_Constant;
  2562. operand->type = original_type;
  2563. operand->value = *type->Enum.min_value;
  2564. return true;
  2565. } else if (is_type_enumerated_array(type)) {
  2566. Type *bt = base_type(type);
  2567. GB_ASSERT(bt->kind == Type_EnumeratedArray);
  2568. operand->mode = Addressing_Constant;
  2569. operand->type = bt->EnumeratedArray.index;
  2570. operand->value = *bt->EnumeratedArray.min_value;
  2571. return true;
  2572. }
  2573. gbString type_str = type_to_string(original_type);
  2574. error(call, "Invalid type for 'min', got %s", type_str);
  2575. gb_string_free(type_str);
  2576. return false;
  2577. }
  2578. bool all_constant = operand->mode == Addressing_Constant;
  2579. auto operands = array_make<Operand>(heap_allocator(), 0, ce->args.count);
  2580. defer (array_free(&operands));
  2581. array_add(&operands, *operand);
  2582. for (isize i = 1; i < ce->args.count; i++) {
  2583. Ast *other_arg = ce->args[i];
  2584. Operand b = {};
  2585. check_expr(c, &b, other_arg);
  2586. if (b.mode == Addressing_Invalid) {
  2587. return false;
  2588. }
  2589. if (!is_type_ordered(b.type) || !(is_type_numeric(b.type) || is_type_string(b.type))) {
  2590. gbString type_str = type_to_string(b.type);
  2591. error(call,
  2592. "Expected a ordered numeric type to 'min', got '%s'",
  2593. type_str);
  2594. gb_string_free(type_str);
  2595. return false;
  2596. }
  2597. array_add(&operands, b);
  2598. if (all_constant) {
  2599. all_constant = b.mode == Addressing_Constant;
  2600. }
  2601. }
  2602. if (all_constant) {
  2603. ExactValue value = operands[0].value;
  2604. Type *type = operands[0].type;
  2605. for (isize i = 1; i < operands.count; i++) {
  2606. Operand y = operands[i];
  2607. if (compare_exact_values(Token_Lt, value, y.value)) {
  2608. // okay
  2609. } else {
  2610. value = y.value;
  2611. type = y.type;
  2612. }
  2613. }
  2614. operand->value = value;
  2615. operand->type = type;
  2616. } else {
  2617. operand->mode = Addressing_Value;
  2618. operand->type = original_type;
  2619. for_array(i, operands) {
  2620. Operand *a = &operands[i];
  2621. for_array(j, operands) {
  2622. if (i == j) {
  2623. continue;
  2624. }
  2625. Operand *b = &operands[j];
  2626. convert_to_typed(c, a, b->type);
  2627. if (a->mode == Addressing_Invalid) {
  2628. return false;
  2629. }
  2630. convert_to_typed(c, b, a->type);
  2631. if (b->mode == Addressing_Invalid) {
  2632. return false;
  2633. }
  2634. }
  2635. }
  2636. for (isize i = 0; i < operands.count-1; i++) {
  2637. Operand *a = &operands[i];
  2638. Operand *b = &operands[i+1];
  2639. if (!are_types_identical(a->type, b->type)) {
  2640. gbString type_a = type_to_string(a->type);
  2641. gbString type_b = type_to_string(b->type);
  2642. error(a->expr,
  2643. "Mismatched types to 'min', '%s' vs '%s'",
  2644. type_a, type_b);
  2645. gb_string_free(type_b);
  2646. gb_string_free(type_a);
  2647. return false;
  2648. }
  2649. }
  2650. operand->type = operands[0].type;
  2651. }
  2652. break;
  2653. }
  2654. case BuiltinProc_max: {
  2655. // max :: proc($T: typeid) -> ordered
  2656. // max :: proc(a: ..ordered) -> ordered
  2657. check_multi_expr_or_type(c, operand, ce->args[0]);
  2658. Type *original_type = operand->type;
  2659. Type *type = base_type(operand->type);
  2660. if (operand->mode == Addressing_Type && is_type_enumerated_array(type)) {
  2661. // Okay
  2662. } else if (!is_type_ordered(type) || !(is_type_numeric(type) || is_type_string(type))) {
  2663. gbString type_str = type_to_string(original_type);
  2664. error(call, "Expected a ordered numeric type to 'max', got '%s'", type_str);
  2665. gb_string_free(type_str);
  2666. return false;
  2667. }
  2668. if (operand->mode == Addressing_Type) {
  2669. if (ce->args.count != 1) {
  2670. error(call, "If 'max' gets a type, only 1 arguments is allowed, got %td", ce->args.count);
  2671. return false;
  2672. }
  2673. if (is_type_boolean(type)) {
  2674. operand->mode = Addressing_Constant;
  2675. operand->type = original_type;
  2676. operand->value = exact_value_bool(true);
  2677. return true;
  2678. } else if (is_type_integer(type)) {
  2679. operand->mode = Addressing_Constant;
  2680. operand->type = original_type;
  2681. if (is_type_unsigned(type)) {
  2682. i64 sz = 8*type_size_of(type);
  2683. ExactValue a = exact_value_i64(1);
  2684. ExactValue b = exact_value_i64(sz);
  2685. ExactValue v = exact_binary_operator_value(Token_Shl, a, b);
  2686. v = exact_binary_operator_value(Token_Sub, v, a);
  2687. operand->value = v;
  2688. return true;
  2689. } else {
  2690. i64 sz = 8*type_size_of(type);
  2691. ExactValue a = exact_value_i64(1);
  2692. ExactValue b = exact_value_i64(sz-1);
  2693. ExactValue v = exact_binary_operator_value(Token_Shl, a, b);
  2694. v = exact_binary_operator_value(Token_Sub, v, a);
  2695. operand->value = v;
  2696. return true;
  2697. }
  2698. } else if (is_type_float(type)) {
  2699. operand->mode = Addressing_Constant;
  2700. operand->type = original_type;
  2701. switch (type_size_of(type)) {
  2702. case 2:
  2703. operand->value = exact_value_float(65504.0f);
  2704. break;
  2705. case 4:
  2706. operand->value = exact_value_float(3.402823466e+38f);
  2707. break;
  2708. case 8:
  2709. operand->value = exact_value_float(1.7976931348623158e+308);
  2710. break;
  2711. default:
  2712. GB_PANIC("Unhandled float type");
  2713. break;
  2714. }
  2715. return true;
  2716. } else if (is_type_enum(type)) {
  2717. operand->mode = Addressing_Constant;
  2718. operand->type = original_type;
  2719. operand->value = *type->Enum.max_value;
  2720. return true;
  2721. } else if (is_type_enumerated_array(type)) {
  2722. Type *bt = base_type(type);
  2723. GB_ASSERT(bt->kind == Type_EnumeratedArray);
  2724. operand->mode = Addressing_Constant;
  2725. operand->type = bt->EnumeratedArray.index;
  2726. operand->value = *bt->EnumeratedArray.max_value;
  2727. return true;
  2728. }
  2729. gbString type_str = type_to_string(original_type);
  2730. error(call, "Invalid type for 'max', got %s", type_str);
  2731. gb_string_free(type_str);
  2732. return false;
  2733. }
  2734. bool all_constant = operand->mode == Addressing_Constant;
  2735. auto operands = array_make<Operand>(heap_allocator(), 0, ce->args.count);
  2736. defer (array_free(&operands));
  2737. array_add(&operands, *operand);
  2738. for (isize i = 1; i < ce->args.count; i++) {
  2739. Ast *arg = ce->args[i];
  2740. Operand b = {};
  2741. check_expr(c, &b, arg);
  2742. if (b.mode == Addressing_Invalid) {
  2743. return false;
  2744. }
  2745. if (!is_type_ordered(b.type) || !(is_type_numeric(b.type) || is_type_string(b.type))) {
  2746. gbString type_str = type_to_string(b.type);
  2747. error(arg,
  2748. "Expected a ordered numeric type to 'max', got '%s'",
  2749. type_str);
  2750. gb_string_free(type_str);
  2751. return false;
  2752. }
  2753. array_add(&operands, b);
  2754. if (all_constant) {
  2755. all_constant = b.mode == Addressing_Constant;
  2756. }
  2757. }
  2758. if (all_constant) {
  2759. ExactValue value = operands[0].value;
  2760. Type *type = operands[0].type;
  2761. for (isize i = 1; i < operands.count; i++) {
  2762. Operand y = operands[i];
  2763. if (compare_exact_values(Token_Gt, value, y.value)) {
  2764. // okay
  2765. } else {
  2766. type = y.type;
  2767. value = y.value;
  2768. }
  2769. }
  2770. operand->value = value;
  2771. operand->type = type;
  2772. } else {
  2773. operand->mode = Addressing_Value;
  2774. operand->type = original_type;
  2775. for_array(i, operands) {
  2776. Operand *a = &operands[i];
  2777. for_array(j, operands) {
  2778. if (i == j) {
  2779. continue;
  2780. }
  2781. Operand *b = &operands[j];
  2782. convert_to_typed(c, a, b->type);
  2783. if (a->mode == Addressing_Invalid) {
  2784. return false;
  2785. }
  2786. convert_to_typed(c, b, a->type);
  2787. if (b->mode == Addressing_Invalid) {
  2788. return false;
  2789. }
  2790. }
  2791. }
  2792. for (isize i = 0; i < operands.count-1; i++) {
  2793. Operand *a = &operands[i];
  2794. Operand *b = &operands[i+1];
  2795. if (!are_types_identical(a->type, b->type)) {
  2796. gbString type_a = type_to_string(a->type);
  2797. gbString type_b = type_to_string(b->type);
  2798. error(a->expr,
  2799. "Mismatched types to 'max', '%s' vs '%s'",
  2800. type_a, type_b);
  2801. gb_string_free(type_b);
  2802. gb_string_free(type_a);
  2803. return false;
  2804. }
  2805. }
  2806. operand->type = operands[0].type;
  2807. }
  2808. break;
  2809. }
  2810. case BuiltinProc_abs: {
  2811. // abs :: proc(n: numeric) -> numeric
  2812. if (!(is_type_numeric(operand->type) && !is_type_array(operand->type))) {
  2813. gbString type_str = type_to_string(operand->type);
  2814. error(call, "Expected a numeric type to 'abs', got '%s'", type_str);
  2815. gb_string_free(type_str);
  2816. return false;
  2817. }
  2818. if (operand->mode == Addressing_Constant) {
  2819. switch (operand->value.kind) {
  2820. case ExactValue_Integer:
  2821. mp_abs(&operand->value.value_integer, &operand->value.value_integer);
  2822. break;
  2823. case ExactValue_Float:
  2824. operand->value.value_float = gb_abs(operand->value.value_float);
  2825. break;
  2826. case ExactValue_Complex: {
  2827. f64 r = operand->value.value_complex->real;
  2828. f64 i = operand->value.value_complex->imag;
  2829. operand->value = exact_value_float(gb_sqrt(r*r + i*i));
  2830. break;
  2831. }
  2832. case ExactValue_Quaternion: {
  2833. f64 r = operand->value.value_quaternion->real;
  2834. f64 i = operand->value.value_quaternion->imag;
  2835. f64 j = operand->value.value_quaternion->jmag;
  2836. f64 k = operand->value.value_quaternion->kmag;
  2837. operand->value = exact_value_float(gb_sqrt(r*r + i*i + j*j + k*k));
  2838. break;
  2839. }
  2840. default:
  2841. GB_PANIC("Invalid numeric constant");
  2842. break;
  2843. }
  2844. } else {
  2845. operand->mode = Addressing_Value;
  2846. {
  2847. Type *bt = base_type(operand->type);
  2848. if (are_types_identical(bt, t_complex64)) add_package_dependency(c, "runtime", "abs_complex64");
  2849. if (are_types_identical(bt, t_complex128)) add_package_dependency(c, "runtime", "abs_complex128");
  2850. if (are_types_identical(bt, t_quaternion128)) add_package_dependency(c, "runtime", "abs_quaternion128");
  2851. if (are_types_identical(bt, t_quaternion256)) add_package_dependency(c, "runtime", "abs_quaternion256");
  2852. }
  2853. }
  2854. if (is_type_complex_or_quaternion(operand->type)) {
  2855. operand->type = base_complex_elem_type(operand->type);
  2856. }
  2857. GB_ASSERT(!is_type_complex_or_quaternion(operand->type));
  2858. break;
  2859. }
  2860. case BuiltinProc_clamp: {
  2861. // clamp :: proc(a, min, max: ordered) -> ordered
  2862. Type *type = operand->type;
  2863. if (!is_type_ordered(type) || !(is_type_numeric(type) || is_type_string(type))) {
  2864. gbString type_str = type_to_string(operand->type);
  2865. error(call, "Expected a ordered numeric or string type to 'clamp', got '%s'", type_str);
  2866. gb_string_free(type_str);
  2867. return false;
  2868. }
  2869. Ast *min_arg = ce->args[1];
  2870. Ast *max_arg = ce->args[2];
  2871. Operand x = *operand;
  2872. Operand y = {};
  2873. Operand z = {};
  2874. check_expr(c, &y, min_arg);
  2875. if (y.mode == Addressing_Invalid) {
  2876. return false;
  2877. }
  2878. if (!is_type_ordered(y.type) || !(is_type_numeric(y.type) || is_type_string(y.type))) {
  2879. gbString type_str = type_to_string(y.type);
  2880. error(call, "Expected a ordered numeric or string type to 'clamp', got '%s'", type_str);
  2881. gb_string_free(type_str);
  2882. return false;
  2883. }
  2884. check_expr(c, &z, max_arg);
  2885. if (z.mode == Addressing_Invalid) {
  2886. return false;
  2887. }
  2888. if (!is_type_ordered(z.type) || !(is_type_numeric(z.type) || is_type_string(z.type))) {
  2889. gbString type_str = type_to_string(z.type);
  2890. error(call, "Expected a ordered numeric or string type to 'clamp', got '%s'", type_str);
  2891. gb_string_free(type_str);
  2892. return false;
  2893. }
  2894. if (x.mode == Addressing_Constant &&
  2895. y.mode == Addressing_Constant &&
  2896. z.mode == Addressing_Constant) {
  2897. ExactValue a = x.value;
  2898. ExactValue b = y.value;
  2899. ExactValue c = z.value;
  2900. operand->mode = Addressing_Constant;
  2901. if (compare_exact_values(Token_Lt, a, b)) {
  2902. operand->value = b;
  2903. operand->type = y.type;
  2904. } else if (compare_exact_values(Token_Gt, a, c)) {
  2905. operand->value = c;
  2906. operand->type = z.type;
  2907. } else {
  2908. operand->value = a;
  2909. operand->type = x.type;
  2910. }
  2911. } else {
  2912. operand->mode = Addressing_Value;
  2913. operand->type = type;
  2914. Operand *ops[3] = {&x, &y, &z};
  2915. for (isize i = 0; i < 3; i++) {
  2916. Operand *a = ops[i];
  2917. for (isize j = 0; j < 3; j++) {
  2918. if (i == j) continue;
  2919. Operand *b = ops[j];
  2920. convert_to_typed(c, a, b->type);
  2921. if (a->mode == Addressing_Invalid) return false;
  2922. }
  2923. }
  2924. if (!are_types_identical(x.type, y.type) || !are_types_identical(x.type, z.type)) {
  2925. gbString type_x = type_to_string(x.type);
  2926. gbString type_y = type_to_string(y.type);
  2927. gbString type_z = type_to_string(z.type);
  2928. error(call,
  2929. "Mismatched types to 'clamp', '%s', '%s', '%s'",
  2930. type_x, type_y, type_z);
  2931. gb_string_free(type_z);
  2932. gb_string_free(type_y);
  2933. gb_string_free(type_x);
  2934. return false;
  2935. }
  2936. operand->type = ops[0]->type;
  2937. }
  2938. break;
  2939. }
  2940. case BuiltinProc_soa_zip: {
  2941. TEMPORARY_ALLOCATOR_GUARD();
  2942. auto types = array_make<Type *>(temporary_allocator(), 0, ce->args.count);
  2943. auto names = array_make<String>(temporary_allocator(), 0, ce->args.count);
  2944. bool first_is_field_value = (ce->args[0]->kind == Ast_FieldValue);
  2945. bool fail = false;
  2946. for (Ast *arg : ce->args) {
  2947. bool mix = false;
  2948. if (first_is_field_value) {
  2949. mix = arg->kind != Ast_FieldValue;
  2950. } else {
  2951. mix = arg->kind == Ast_FieldValue;
  2952. }
  2953. if (mix) {
  2954. error(arg, "Mixture of 'field = value' and value elements in the procedure call '%.*s' is not allowed", LIT(builtin_name));
  2955. fail = true;
  2956. break;
  2957. }
  2958. }
  2959. StringSet name_set = {};
  2960. string_set_init(&name_set, 2*ce->args.count);
  2961. for (Ast *arg : ce->args) {
  2962. String name = {};
  2963. if (arg->kind == Ast_FieldValue) {
  2964. Ast *ename = arg->FieldValue.field;
  2965. if (!fail && ename->kind != Ast_Ident) {
  2966. error(ename, "Expected an identifier for field argument");
  2967. } else if (ename->kind == Ast_Ident) {
  2968. name = ename->Ident.token.string;
  2969. }
  2970. arg = arg->FieldValue.value;
  2971. }
  2972. Operand op = {};
  2973. check_expr(c, &op, arg);
  2974. if (op.mode == Addressing_Invalid) {
  2975. return false;
  2976. }
  2977. Type *arg_type = base_type(op.type);
  2978. if (!is_type_slice(arg_type)) {
  2979. gbString s = type_to_string(op.type);
  2980. error(op.expr, "Indices to 'soa_zip' must be slices, got %s", s);
  2981. gb_string_free(s);
  2982. return false;
  2983. }
  2984. GB_ASSERT(arg_type->kind == Type_Slice);
  2985. if (name == "_") {
  2986. error(op.expr, "Field argument name '%.*s' is not allowed", LIT(name));
  2987. name = {};
  2988. }
  2989. if (name.len == 0) {
  2990. gbString field_name = gb_string_make(permanent_allocator(), "_");
  2991. field_name = gb_string_append_fmt(field_name, "%td", types.count);
  2992. name = make_string_c(field_name);
  2993. }
  2994. if (string_set_update(&name_set, name)) {
  2995. error(op.expr, "Field argument name '%.*s' already exists", LIT(name));
  2996. } else {
  2997. array_add(&types, arg_type->Slice.elem);
  2998. array_add(&names, name);
  2999. }
  3000. }
  3001. Ast *dummy_node_struct = alloc_ast_node(nullptr, Ast_Invalid);
  3002. Ast *dummy_node_soa = alloc_ast_node(nullptr, Ast_Invalid);
  3003. Scope *s = create_scope(c->info, builtin_pkg->scope);
  3004. auto fields = array_make<Entity *>(permanent_allocator(), 0, types.count);
  3005. for_array(i, types) {
  3006. Type *type = types[i];
  3007. String name = names[i];
  3008. GB_ASSERT(name != "");
  3009. Entity *e = alloc_entity_field(s, make_token_ident(name), type, false, cast(i32)i, EntityState_Resolved);
  3010. array_add(&fields, e);
  3011. scope_insert(s, e);
  3012. }
  3013. Type *elem = nullptr;
  3014. if (type_hint != nullptr && is_type_struct(type_hint)) {
  3015. Type *soa_type = base_type(type_hint);
  3016. if (soa_type->Struct.soa_kind != StructSoa_Slice) {
  3017. goto soa_zip_end;
  3018. }
  3019. Type *soa_elem_type = soa_type->Struct.soa_elem;
  3020. Type *et = base_type(soa_elem_type);
  3021. if (et->kind != Type_Struct) {
  3022. goto soa_zip_end;
  3023. }
  3024. if (et->Struct.fields.count != fields.count) {
  3025. goto soa_zip_end;
  3026. }
  3027. if (!fail && first_is_field_value) {
  3028. for_array(i, names) {
  3029. Selection sel = lookup_field(et, names[i], false);
  3030. if (sel.entity == nullptr) {
  3031. goto soa_zip_end;
  3032. }
  3033. if (sel.index.count != 1) {
  3034. goto soa_zip_end;
  3035. }
  3036. if (!are_types_identical(sel.entity->type, types[i])) {
  3037. goto soa_zip_end;
  3038. }
  3039. }
  3040. } else {
  3041. for_array(i, et->Struct.fields) {
  3042. if (!are_types_identical(et->Struct.fields[i]->type, types[i])) {
  3043. goto soa_zip_end;
  3044. }
  3045. }
  3046. }
  3047. elem = soa_elem_type;
  3048. }
  3049. soa_zip_end:;
  3050. if (elem == nullptr) {
  3051. elem = alloc_type_struct();
  3052. elem->Struct.scope = s;
  3053. elem->Struct.fields = slice_from_array(fields);
  3054. elem->Struct.tags = gb_alloc_array(permanent_allocator(), String, fields.count);
  3055. elem->Struct.node = dummy_node_struct;
  3056. type_set_offsets(elem);
  3057. wait_signal_set(&elem->Struct.fields_wait_signal);
  3058. }
  3059. Type *soa_type = make_soa_struct_slice(c, dummy_node_soa, nullptr, elem);
  3060. type_set_offsets(soa_type);
  3061. operand->type = soa_type;
  3062. operand->mode = Addressing_Value;
  3063. break;
  3064. }
  3065. case BuiltinProc_soa_unzip: {
  3066. Operand x = {};
  3067. check_expr(c, &x, ce->args[0]);
  3068. if (x.mode == Addressing_Invalid) {
  3069. return false;
  3070. }
  3071. if (!is_operand_value(x)) {
  3072. error(call, "'%.*s' expects an #soa slice", LIT(builtin_name));
  3073. return false;
  3074. }
  3075. Type *t = base_type(x.type);
  3076. if (!is_type_soa_struct(t) || t->Struct.soa_kind != StructSoa_Slice) {
  3077. gbString s = type_to_string(x.type);
  3078. error(call, "'%.*s' expects an #soa slice, got %s", LIT(builtin_name), s);
  3079. gb_string_free(s);
  3080. return false;
  3081. }
  3082. auto types = slice_make<Type *>(permanent_allocator(), t->Struct.fields.count-1);
  3083. for_array(i, types) {
  3084. Entity *f = t->Struct.fields[i];
  3085. GB_ASSERT(f->type->kind == Type_MultiPointer);
  3086. types[i] = alloc_type_slice(f->type->MultiPointer.elem);
  3087. }
  3088. operand->type = alloc_type_tuple_from_field_types(types.data, types.count, false, false);
  3089. operand->mode = Addressing_Value;
  3090. break;
  3091. }
  3092. case BuiltinProc_transpose: {
  3093. Operand x = {};
  3094. check_expr(c, &x, ce->args[0]);
  3095. if (x.mode == Addressing_Invalid) {
  3096. return false;
  3097. }
  3098. if (!is_operand_value(x)) {
  3099. error(call, "'%.*s' expects a matrix or array", LIT(builtin_name));
  3100. return false;
  3101. }
  3102. Type *t = base_type(x.type);
  3103. if (!is_type_matrix(t) && !is_type_array(t)) {
  3104. gbString s = type_to_string(x.type);
  3105. error(call, "'%.*s' expects a matrix or array, got %s", LIT(builtin_name), s);
  3106. gb_string_free(s);
  3107. return false;
  3108. }
  3109. operand->mode = Addressing_Value;
  3110. if (t->kind == Type_Array) {
  3111. i32 rank = type_math_rank(t);
  3112. // Do nothing
  3113. operand->type = x.type;
  3114. if (rank > 2) {
  3115. gbString s = type_to_string(x.type);
  3116. error(call, "'%.*s' expects a matrix or array with a rank of 2, got %s of rank %d", LIT(builtin_name), s, rank);
  3117. gb_string_free(s);
  3118. return false;
  3119. } else if (rank == 2) {
  3120. Type *inner = base_type(t->Array.elem);
  3121. GB_ASSERT(inner->kind == Type_Array);
  3122. Type *elem = inner->Array.elem;
  3123. Type *array_inner = alloc_type_array(elem, t->Array.count);
  3124. Type *array_outer = alloc_type_array(array_inner, inner->Array.count);
  3125. operand->type = array_outer;
  3126. i64 elements = t->Array.count*inner->Array.count;
  3127. i64 size = type_size_of(operand->type);
  3128. if (!is_type_valid_for_matrix_elems(elem)) {
  3129. gbString s = type_to_string(x.type);
  3130. error(call, "'%.*s' expects a matrix or array with a base element type of an integer, float, or complex number, got %s", LIT(builtin_name), s);
  3131. gb_string_free(s);
  3132. } else if (elements > MATRIX_ELEMENT_COUNT_MAX) {
  3133. gbString s = type_to_string(x.type);
  3134. error(call, "'%.*s' expects a matrix or array with a maximum of %d elements, got %s with %lld elements", LIT(builtin_name), MATRIX_ELEMENT_COUNT_MAX, s, elements);
  3135. gb_string_free(s);
  3136. } else if (elements > MATRIX_ELEMENT_COUNT_MAX) {
  3137. gbString s = type_to_string(x.type);
  3138. error(call, "'%.*s' expects a matrix or array with non-zero elements, got %s", LIT(builtin_name), MATRIX_ELEMENT_COUNT_MAX, s);
  3139. gb_string_free(s);
  3140. } else if (size > MATRIX_ELEMENT_MAX_SIZE) {
  3141. gbString s = type_to_string(x.type);
  3142. error(call, "Too large of a type for '%.*s', got %s of size %lld, maximum size %d", LIT(builtin_name), s, cast(long long)size, MATRIX_ELEMENT_MAX_SIZE);
  3143. gb_string_free(s);
  3144. }
  3145. }
  3146. } else {
  3147. GB_ASSERT(t->kind == Type_Matrix);
  3148. operand->type = alloc_type_matrix(t->Matrix.elem, t->Matrix.column_count, t->Matrix.row_count, nullptr, nullptr, t->Matrix.is_row_major);
  3149. }
  3150. operand->type = check_matrix_type_hint(operand->type, type_hint);
  3151. break;
  3152. }
  3153. case BuiltinProc_outer_product: {
  3154. Operand x = {};
  3155. Operand y = {};
  3156. check_expr(c, &x, ce->args[0]);
  3157. if (x.mode == Addressing_Invalid) {
  3158. return false;
  3159. }
  3160. check_expr(c, &y, ce->args[1]);
  3161. if (y.mode == Addressing_Invalid) {
  3162. return false;
  3163. }
  3164. if (!is_operand_value(x) || !is_operand_value(y)) {
  3165. error(call, "'%.*s' expects only arrays", LIT(builtin_name));
  3166. return false;
  3167. }
  3168. if (!is_type_array(x.type) && !is_type_array(y.type)) {
  3169. gbString s1 = type_to_string(x.type);
  3170. gbString s2 = type_to_string(y.type);
  3171. error(call, "'%.*s' expects only arrays, got %s and %s", LIT(builtin_name), s1, s2);
  3172. gb_string_free(s2);
  3173. gb_string_free(s1);
  3174. return false;
  3175. }
  3176. Type *xt = base_type(x.type);
  3177. Type *yt = base_type(y.type);
  3178. GB_ASSERT(xt->kind == Type_Array);
  3179. GB_ASSERT(yt->kind == Type_Array);
  3180. if (!are_types_identical(xt->Array.elem, yt->Array.elem)) {
  3181. gbString s1 = type_to_string(xt->Array.elem);
  3182. gbString s2 = type_to_string(yt->Array.elem);
  3183. error(call, "'%.*s' mismatched element types, got %s vs %s", LIT(builtin_name), s1, s2);
  3184. gb_string_free(s2);
  3185. gb_string_free(s1);
  3186. return false;
  3187. }
  3188. Type *elem = xt->Array.elem;
  3189. if (!is_type_valid_for_matrix_elems(elem)) {
  3190. gbString s = type_to_string(elem);
  3191. error(call, "Matrix elements types are limited to integers, floats, and complex, got %s", s);
  3192. gb_string_free(s);
  3193. }
  3194. if (xt->Array.count == 0 || yt->Array.count == 0) {
  3195. gbString s1 = type_to_string(x.type);
  3196. gbString s2 = type_to_string(y.type);
  3197. error(call, "'%.*s' expects only arrays of non-zero length, got %s and %s", LIT(builtin_name), s1, s2);
  3198. gb_string_free(s2);
  3199. gb_string_free(s1);
  3200. return false;
  3201. }
  3202. i64 max_count = xt->Array.count*yt->Array.count;
  3203. if (max_count > MATRIX_ELEMENT_COUNT_MAX) {
  3204. error(call, "Product of the array lengths exceed the maximum matrix element count, got %d, expected a maximum of %d", cast(int)max_count, MATRIX_ELEMENT_COUNT_MAX);
  3205. return false;
  3206. }
  3207. operand->mode = Addressing_Value;
  3208. operand->type = alloc_type_matrix(elem, xt->Array.count, yt->Array.count, nullptr, nullptr, false);
  3209. operand->type = check_matrix_type_hint(operand->type, type_hint);
  3210. break;
  3211. }
  3212. case BuiltinProc_hadamard_product: {
  3213. Operand x = {};
  3214. Operand y = {};
  3215. check_expr(c, &x, ce->args[0]);
  3216. if (x.mode == Addressing_Invalid) {
  3217. return false;
  3218. }
  3219. check_expr(c, &y, ce->args[1]);
  3220. if (y.mode == Addressing_Invalid) {
  3221. return false;
  3222. }
  3223. if (!is_operand_value(x) || !is_operand_value(y)) {
  3224. error(call, "'%.*s' expects a matrix or array types", LIT(builtin_name));
  3225. return false;
  3226. }
  3227. if (!is_type_matrix(x.type) && !is_type_array(y.type)) {
  3228. gbString s1 = type_to_string(x.type);
  3229. gbString s2 = type_to_string(y.type);
  3230. error(call, "'%.*s' expects matrix or array values, got %s and %s", LIT(builtin_name), s1, s2);
  3231. gb_string_free(s2);
  3232. gb_string_free(s1);
  3233. return false;
  3234. }
  3235. if (!are_types_identical(x.type, y.type)) {
  3236. gbString s1 = type_to_string(x.type);
  3237. gbString s2 = type_to_string(y.type);
  3238. error(call, "'%.*s' values of the same type, got %s and %s", LIT(builtin_name), s1, s2);
  3239. gb_string_free(s2);
  3240. gb_string_free(s1);
  3241. return false;
  3242. }
  3243. Type *elem = core_array_type(x.type);
  3244. if (!is_type_valid_for_matrix_elems(elem)) {
  3245. gbString s = type_to_string(elem);
  3246. error(call, "'%.*s' expects elements to be types are limited to integers, floats, and complex, got %s", LIT(builtin_name), s);
  3247. gb_string_free(s);
  3248. }
  3249. operand->mode = Addressing_Value;
  3250. operand->type = x.type;
  3251. operand->type = check_matrix_type_hint(operand->type, type_hint);
  3252. break;
  3253. }
  3254. case BuiltinProc_matrix_flatten: {
  3255. Operand x = {};
  3256. check_expr(c, &x, ce->args[0]);
  3257. if (x.mode == Addressing_Invalid) {
  3258. return false;
  3259. }
  3260. if (!is_operand_value(x)) {
  3261. error(call, "'%.*s' expects a matrix or array", LIT(builtin_name));
  3262. return false;
  3263. }
  3264. Type *t = base_type(x.type);
  3265. if (!is_type_matrix(t) && !is_type_array(t)) {
  3266. gbString s = type_to_string(x.type);
  3267. error(call, "'%.*s' expects a matrix or array, got %s", LIT(builtin_name), s);
  3268. gb_string_free(s);
  3269. return false;
  3270. }
  3271. operand->mode = Addressing_Value;
  3272. if (is_type_array(t)) {
  3273. // Do nothing
  3274. operand->type = x.type;
  3275. } else {
  3276. GB_ASSERT(t->kind == Type_Matrix);
  3277. operand->type = alloc_type_array(t->Matrix.elem, t->Matrix.row_count*t->Matrix.column_count);
  3278. }
  3279. operand->type = check_matrix_type_hint(operand->type, type_hint);
  3280. break;
  3281. }
  3282. case BuiltinProc_is_package_imported: {
  3283. bool value = false;
  3284. if (!is_type_string(operand->type) && (operand->mode != Addressing_Constant)) {
  3285. error(ce->args[0], "Expected a constant string for '%.*s'", LIT(builtin_name));
  3286. } else if (operand->value.kind == ExactValue_String) {
  3287. String pkg_name = operand->value.value_string;
  3288. for (auto const &entry : c->info->packages) {
  3289. AstPackage *pkg = entry.value;
  3290. if (pkg->name == pkg_name) {
  3291. value = true;
  3292. break;
  3293. }
  3294. }
  3295. }
  3296. operand->mode = Addressing_Constant;
  3297. operand->type = t_untyped_bool;
  3298. operand->value = exact_value_bool(value);
  3299. break;
  3300. }
  3301. case BuiltinProc_has_target_feature: {
  3302. String features = str_lit("");
  3303. check_expr_or_type(c, operand, ce->args[0]);
  3304. if (is_type_string(operand->type) && operand->mode == Addressing_Constant) {
  3305. GB_ASSERT(operand->value.kind == ExactValue_String);
  3306. features = operand->value.value_string;
  3307. } else {
  3308. Type *pt = base_type(operand->type);
  3309. if (pt->kind == Type_Proc) {
  3310. if (pt->Proc.require_target_feature.len != 0) {
  3311. GB_ASSERT(pt->Proc.enable_target_feature.len == 0);
  3312. features = pt->Proc.require_target_feature;
  3313. } else if (pt->Proc.enable_target_feature.len != 0) {
  3314. features = pt->Proc.enable_target_feature;
  3315. } else {
  3316. error(ce->args[0], "Expected the procedure type given to '%.*s' to have @(require_target_feature=\"...\") or @(enable_target_feature=\"...\")", LIT(builtin_name));
  3317. }
  3318. } else {
  3319. error(ce->args[0], "Expected a constant string or procedure type for '%.*s'", LIT(builtin_name));
  3320. }
  3321. }
  3322. String invalid;
  3323. if (!check_target_feature_is_valid_globally(features, &invalid)) {
  3324. error(ce->args[0], "Target feature '%.*s' is not a valid target feature", LIT(invalid));
  3325. }
  3326. operand->value = exact_value_bool(check_target_feature_is_enabled(features, nullptr));
  3327. operand->mode = Addressing_Constant;
  3328. operand->type = t_untyped_bool;
  3329. break;
  3330. }
  3331. case BuiltinProc_soa_struct: {
  3332. Operand x = {};
  3333. Operand y = {};
  3334. x = *operand;
  3335. if (!is_type_integer(x.type) || x.mode != Addressing_Constant) {
  3336. error(call, "Expected a constant integer for 'intrinsics.soa_struct'");
  3337. operand->mode = Addressing_Type;
  3338. operand->type = t_invalid;
  3339. return false;
  3340. }
  3341. if (big_int_is_neg(&x.value.value_integer)) {
  3342. error(call, "Negative array element length");
  3343. operand->mode = Addressing_Type;
  3344. operand->type = t_invalid;
  3345. return false;
  3346. }
  3347. i64 count = big_int_to_i64(&x.value.value_integer);
  3348. check_expr_or_type(c, &y, ce->args[1]);
  3349. if (y.mode != Addressing_Type) {
  3350. error(call, "Expected a type 'intrinsics.soa_struct'");
  3351. operand->mode = Addressing_Type;
  3352. operand->type = t_invalid;
  3353. return false;
  3354. }
  3355. Type *elem = y.type;
  3356. Type *bt_elem = base_type(elem);
  3357. if (!is_type_struct(elem) && !is_type_raw_union(elem) && !(is_type_array(elem) && bt_elem->Array.count <= 4)) {
  3358. gbString str = type_to_string(elem);
  3359. error(call, "Invalid type for 'intrinsics.soa_struct', expected a struct or array of length 4 or below, got '%s'", str);
  3360. gb_string_free(str);
  3361. operand->mode = Addressing_Type;
  3362. operand->type = t_invalid;
  3363. return false;
  3364. }
  3365. operand->mode = Addressing_Type;
  3366. Type *soa_struct = nullptr;
  3367. Scope *scope = nullptr;
  3368. if (is_type_array(elem)) {
  3369. Type *old_array = base_type(elem);
  3370. soa_struct = alloc_type_struct();
  3371. soa_struct->Struct.fields = slice_make<Entity *>(heap_allocator(), cast(isize)old_array->Array.count);
  3372. soa_struct->Struct.tags = gb_alloc_array(permanent_allocator(), String, cast(isize)old_array->Array.count);
  3373. soa_struct->Struct.node = operand->expr;
  3374. soa_struct->Struct.soa_kind = StructSoa_Fixed;
  3375. soa_struct->Struct.soa_elem = elem;
  3376. soa_struct->Struct.soa_count = cast(i32)count;
  3377. scope = create_scope(c->info, c->scope);
  3378. soa_struct->Struct.scope = scope;
  3379. String params_xyzw[4] = {
  3380. str_lit("x"),
  3381. str_lit("y"),
  3382. str_lit("z"),
  3383. str_lit("w")
  3384. };
  3385. for (isize i = 0; i < cast(isize)old_array->Array.count; i++) {
  3386. Type *array_type = alloc_type_array(old_array->Array.elem, count);
  3387. Token token = {};
  3388. token.string = params_xyzw[i];
  3389. Entity *new_field = alloc_entity_field(scope, token, array_type, false, cast(i32)i);
  3390. soa_struct->Struct.fields[i] = new_field;
  3391. add_entity(c, scope, nullptr, new_field);
  3392. add_entity_use(c, nullptr, new_field);
  3393. }
  3394. } else {
  3395. GB_ASSERT(is_type_struct(elem));
  3396. Type *old_struct = base_type(elem);
  3397. soa_struct = alloc_type_struct();
  3398. soa_struct->Struct.fields = slice_make<Entity *>(heap_allocator(), old_struct->Struct.fields.count);
  3399. soa_struct->Struct.tags = gb_alloc_array(permanent_allocator(), String, old_struct->Struct.fields.count);
  3400. soa_struct->Struct.node = operand->expr;
  3401. soa_struct->Struct.soa_kind = StructSoa_Fixed;
  3402. soa_struct->Struct.soa_elem = elem;
  3403. if (count > I32_MAX) {
  3404. count = I32_MAX;
  3405. error(call, "Array count too large for an #soa struct, got %lld", cast(long long)count);
  3406. }
  3407. soa_struct->Struct.soa_count = cast(i32)count;
  3408. scope = create_scope(c->info, old_struct->Struct.scope->parent);
  3409. soa_struct->Struct.scope = scope;
  3410. for_array(i, old_struct->Struct.fields) {
  3411. Entity *old_field = old_struct->Struct.fields[i];
  3412. if (old_field->kind == Entity_Variable) {
  3413. Type *array_type = alloc_type_array(old_field->type, count);
  3414. Entity *new_field = alloc_entity_field(scope, old_field->token, array_type, false, old_field->Variable.field_index);
  3415. soa_struct->Struct.fields[i] = new_field;
  3416. add_entity(c, scope, nullptr, new_field);
  3417. } else {
  3418. soa_struct->Struct.fields[i] = old_field;
  3419. }
  3420. soa_struct->Struct.tags[i] = old_struct->Struct.tags[i];
  3421. }
  3422. }
  3423. wait_signal_set(&soa_struct->Struct.fields_wait_signal);
  3424. Token token = {};
  3425. token.string = str_lit("Base_Type");
  3426. Entity *base_type_entity = alloc_entity_type_name(scope, token, elem, EntityState_Resolved);
  3427. add_entity(c, scope, nullptr, base_type_entity);
  3428. add_type_info_type(c, soa_struct);
  3429. operand->type = soa_struct;
  3430. break;
  3431. }
  3432. case BuiltinProc_alloca:
  3433. {
  3434. Operand sz = {};
  3435. Operand al = {};
  3436. check_expr(c, &sz, ce->args[0]);
  3437. if (sz.mode == Addressing_Invalid) {
  3438. return false;
  3439. }
  3440. check_expr(c, &al, ce->args[1]);
  3441. if (al.mode == Addressing_Invalid) {
  3442. return false;
  3443. }
  3444. convert_to_typed(c, &sz, t_int); if (sz.mode == Addressing_Invalid) return false;
  3445. convert_to_typed(c, &al, t_int); if (al.mode == Addressing_Invalid) return false;
  3446. if (!is_type_integer(sz.type) || !is_type_integer(al.type)) {
  3447. error(operand->expr, "Both parameters to '%.*s' must integers", LIT(builtin_name));
  3448. return false;
  3449. }
  3450. if (sz.mode == Addressing_Constant) {
  3451. i64 i_sz = exact_value_to_i64(sz.value);
  3452. if (i_sz < 0) {
  3453. error(sz.expr, "Size parameter to '%.*s' must be non-negative, got %lld", LIT(builtin_name), cast(long long)i_sz);
  3454. return false;
  3455. }
  3456. }
  3457. if (al.mode == Addressing_Constant) {
  3458. i64 i_al = exact_value_to_i64(al.value);
  3459. if (i_al < 0) {
  3460. error(al.expr, "Alignment parameter to '%.*s' must be non-negative, got %lld", LIT(builtin_name), cast(long long)i_al);
  3461. return false;
  3462. }
  3463. if (i_al > 1<<29) {
  3464. error(al.expr, "Alignment parameter to '%.*s' must not exceed '1<<29', got %lld", LIT(builtin_name), cast(long long)i_al);
  3465. return false;
  3466. }
  3467. if (!gb_is_power_of_two(cast(isize)i_al) && i_al != 0) {
  3468. error(al.expr, "Alignment parameter to '%.*s' must be a power of 2 or 0, got %lld", LIT(builtin_name), cast(long long)i_al);
  3469. return false;
  3470. }
  3471. } else {
  3472. error(al.expr, "Alignment parameter to '%.*s' must be constant", LIT(builtin_name));
  3473. }
  3474. operand->type = alloc_type_multi_pointer(t_u8);
  3475. operand->mode = Addressing_Value;
  3476. break;
  3477. }
  3478. case BuiltinProc_cpu_relax:
  3479. operand->mode = Addressing_NoValue;
  3480. break;
  3481. case BuiltinProc_unreachable:
  3482. case BuiltinProc_trap:
  3483. case BuiltinProc_debug_trap:
  3484. operand->mode = Addressing_NoValue;
  3485. break;
  3486. case BuiltinProc_raw_data:
  3487. {
  3488. Operand x = {};
  3489. check_expr(c, &x, ce->args[0]);
  3490. if (x.mode == Addressing_Invalid) {
  3491. return false;
  3492. }
  3493. if (!is_operand_value(x)) {
  3494. gbString s = expr_to_string(x.expr);
  3495. error(call, "'%.*s' expects a string, slice, dynamic array, or pointer to array type, got %s", LIT(builtin_name), s);
  3496. gb_string_free(s);
  3497. return false;
  3498. }
  3499. Type *t = base_type(x.type);
  3500. operand->mode = Addressing_Value;
  3501. operand->type = nullptr;
  3502. switch (t->kind) {
  3503. case Type_Slice:
  3504. operand->type = alloc_type_multi_pointer(t->MultiPointer.elem);
  3505. break;
  3506. case Type_DynamicArray:
  3507. operand->type = alloc_type_multi_pointer(t->DynamicArray.elem);
  3508. break;
  3509. case Type_Basic:
  3510. if (t->Basic.kind == Basic_string) {
  3511. operand->type = alloc_type_multi_pointer(t_u8);
  3512. }
  3513. break;
  3514. case Type_Pointer:
  3515. case Type_MultiPointer:
  3516. {
  3517. Type *base = base_type(type_deref(t, true));
  3518. switch (base->kind) {
  3519. case Type_Array:
  3520. case Type_EnumeratedArray:
  3521. case Type_SimdVector:
  3522. operand->type = alloc_type_multi_pointer(base_array_type(base));
  3523. break;
  3524. case Type_Matrix:
  3525. operand->type = alloc_type_multi_pointer(base->Matrix.elem);
  3526. break;
  3527. }
  3528. }
  3529. break;
  3530. }
  3531. if (operand->type == nullptr) {
  3532. gbString s = type_to_string(x.type);
  3533. error(call, "'%.*s' expects a string, slice, dynamic array, or pointer to array type, got %s", LIT(builtin_name), s);
  3534. gb_string_free(s);
  3535. return false;
  3536. }
  3537. }
  3538. break;
  3539. case BuiltinProc_read_cycle_counter:
  3540. operand->mode = Addressing_Value;
  3541. operand->type = t_i64;
  3542. break;
  3543. case BuiltinProc_count_ones:
  3544. case BuiltinProc_count_zeros:
  3545. case BuiltinProc_count_trailing_zeros:
  3546. case BuiltinProc_count_leading_zeros:
  3547. case BuiltinProc_reverse_bits:
  3548. {
  3549. Operand x = {};
  3550. check_expr(c, &x, ce->args[0]);
  3551. if (x.mode == Addressing_Invalid) {
  3552. return false;
  3553. }
  3554. if (is_type_simd_vector(x.type)) {
  3555. Type *elem = base_array_type(x.type);
  3556. if (!is_type_integer_like(elem)) {
  3557. gbString xts = type_to_string(x.type);
  3558. error(x.expr, "#simd values passed to '%.*s' must have an element of an integer-like type (integer, boolean, enum, bit_set), got %s", LIT(builtin_name), xts);
  3559. gb_string_free(xts);
  3560. }
  3561. } else if (!is_type_integer_like(x.type)) {
  3562. gbString xts = type_to_string(x.type);
  3563. error(x.expr, "Values passed to '%.*s' must be an integer-like type (integer, boolean, enum, bit_set), got %s", LIT(builtin_name), xts);
  3564. gb_string_free(xts);
  3565. } else if (x.type == t_llvm_bool) {
  3566. gbString xts = type_to_string(x.type);
  3567. error(x.expr, "Invalid type passed to '%.*s', got %s", LIT(builtin_name), xts);
  3568. gb_string_free(xts);
  3569. }
  3570. Type *type = default_type(x.type);
  3571. operand->mode = Addressing_Value;
  3572. operand->type = type;
  3573. if (id == BuiltinProc_reverse_bits) {
  3574. // make runtime only for the time being
  3575. } else if (x.mode == Addressing_Constant && x.value.kind == ExactValue_Integer) {
  3576. convert_to_typed(c, &x, type);
  3577. if (x.mode == Addressing_Invalid) {
  3578. return false;
  3579. }
  3580. ExactValue res = {};
  3581. i64 sz = type_size_of(x.type);
  3582. u64 bit_size = sz*8;
  3583. u64 rop64[4] = {}; // 2 u64 is the maximum we will ever need, so doubling it will ne fine
  3584. u8 *rop = cast(u8 *)rop64;
  3585. size_t max_count = 0;
  3586. size_t written = 0;
  3587. size_t size = 1;
  3588. size_t nails = 0;
  3589. mp_endian endian = MP_LITTLE_ENDIAN;
  3590. max_count = mp_pack_count(&x.value.value_integer, nails, size);
  3591. GB_ASSERT(sz >= cast(i64)max_count);
  3592. mp_err err = mp_pack(rop, max_count, &written, MP_LSB_FIRST, size, endian, nails, &x.value.value_integer);
  3593. GB_ASSERT(err == MP_OKAY);
  3594. if (id != BuiltinProc_reverse_bits) {
  3595. u64 v = 0;
  3596. switch (id) {
  3597. case BuiltinProc_count_ones:
  3598. case BuiltinProc_count_zeros:
  3599. switch (sz) {
  3600. case 1: v = bit_set_count(cast(u32)rop[0]); break;
  3601. case 2: v = bit_set_count(cast(u32)*(u16 *)rop); break;
  3602. case 4: v = bit_set_count(*(u32 *)rop); break;
  3603. case 8: v = bit_set_count(rop64[0]); break;
  3604. case 16:
  3605. v += bit_set_count(rop64[0]);
  3606. v += bit_set_count(rop64[1]);
  3607. break;
  3608. default: GB_PANIC("Unhandled sized");
  3609. }
  3610. if (id == BuiltinProc_count_zeros) {
  3611. // flip the result
  3612. v = bit_size - v;
  3613. }
  3614. break;
  3615. case BuiltinProc_count_trailing_zeros:
  3616. for (u64 i = 0; i < bit_size; i++) {
  3617. u8 b = cast(u8)(i & 7);
  3618. u8 j = cast(u8)(i >> 3);
  3619. if (rop[j] & (1 << b)) {
  3620. break;
  3621. }
  3622. v += 1;
  3623. }
  3624. break;
  3625. case BuiltinProc_count_leading_zeros:
  3626. for (u64 i = bit_size-1; i < bit_size; i--) {
  3627. u8 b = cast(u8)(i & 7);
  3628. u8 j = cast(u8)(i >> 3);
  3629. if (rop[j] & (1 << b)) {
  3630. break;
  3631. }
  3632. v += 1;
  3633. }
  3634. break;
  3635. }
  3636. res = exact_value_u64(v);
  3637. }
  3638. if (res.kind != ExactValue_Invalid) {
  3639. operand->mode = Addressing_Constant;
  3640. operand->value = res;
  3641. }
  3642. }
  3643. }
  3644. break;
  3645. case BuiltinProc_byte_swap:
  3646. {
  3647. Operand x = {};
  3648. check_expr(c, &x, ce->args[0]);
  3649. if (x.mode == Addressing_Invalid) {
  3650. return false;
  3651. }
  3652. if (!is_type_integer_like(x.type) && !is_type_float(x.type)) {
  3653. gbString xts = type_to_string(x.type);
  3654. error(x.expr, "Values passed to '%.*s' must be an integer-like type (integer, boolean, enum, bit_set) or float, got %s", LIT(builtin_name), xts);
  3655. gb_string_free(xts);
  3656. } else if (x.type == t_llvm_bool) {
  3657. gbString xts = type_to_string(x.type);
  3658. error(x.expr, "Invalid type passed to '%.*s', got %s", LIT(builtin_name), xts);
  3659. gb_string_free(xts);
  3660. }
  3661. i64 sz = type_size_of(x.type);
  3662. if (sz < 2) {
  3663. gbString xts = type_to_string(x.type);
  3664. error(x.expr, "Type passed to '%.*s' must be at least 2 bytes, got %s with size of %lld", LIT(builtin_name), xts, sz);
  3665. gb_string_free(xts);
  3666. }
  3667. operand->mode = Addressing_Value;
  3668. operand->type = default_type(x.type);
  3669. }
  3670. break;
  3671. case BuiltinProc_overflow_add:
  3672. case BuiltinProc_overflow_sub:
  3673. case BuiltinProc_overflow_mul:
  3674. {
  3675. Operand x = {};
  3676. Operand y = {};
  3677. check_expr(c, &x, ce->args[0]);
  3678. check_expr(c, &y, ce->args[1]);
  3679. if (x.mode == Addressing_Invalid) {
  3680. return false;
  3681. }
  3682. if (y.mode == Addressing_Invalid) {
  3683. return false;
  3684. }
  3685. convert_to_typed(c, &y, x.type); if (y.mode == Addressing_Invalid) return false;
  3686. convert_to_typed(c, &x, y.type);
  3687. if (is_type_untyped(x.type)) {
  3688. gbString xts = type_to_string(x.type);
  3689. error(x.expr, "Expected a typed integer for '%.*s', got %s", LIT(builtin_name), xts);
  3690. gb_string_free(xts);
  3691. return false;
  3692. }
  3693. if (!is_type_integer(x.type)) {
  3694. gbString xts = type_to_string(x.type);
  3695. error(x.expr, "Expected an integer for '%.*s', got %s", LIT(builtin_name), xts);
  3696. gb_string_free(xts);
  3697. return false;
  3698. }
  3699. Type *ct = core_type(x.type);
  3700. if (is_type_different_to_arch_endianness(ct)) {
  3701. GB_ASSERT(ct->kind == Type_Basic);
  3702. if (ct->Basic.flags & (BasicFlag_EndianLittle|BasicFlag_EndianBig)) {
  3703. gbString xts = type_to_string(x.type);
  3704. error(x.expr, "Expected an integer which does not specify the explicit endianness for '%.*s', got %s", LIT(builtin_name), xts);
  3705. gb_string_free(xts);
  3706. return false;
  3707. }
  3708. }
  3709. operand->mode = Addressing_Value;
  3710. operand->type = make_optional_ok_type(default_type(x.type));
  3711. }
  3712. break;
  3713. case BuiltinProc_sqrt:
  3714. {
  3715. Operand x = {};
  3716. check_expr(c, &x, ce->args[0]);
  3717. if (x.mode == Addressing_Invalid) {
  3718. return false;
  3719. }
  3720. Type *elem = core_array_type(x.type);
  3721. if (!is_type_float(x.type) && !(is_type_simd_vector(x.type) && is_type_float(elem))) {
  3722. gbString xts = type_to_string(x.type);
  3723. error(x.expr, "Expected a floating point or #simd vector value for '%.*s', got %s", LIT(builtin_name), xts);
  3724. gb_string_free(xts);
  3725. return false;
  3726. } else if (is_type_different_to_arch_endianness(elem)) {
  3727. GB_ASSERT(elem->kind == Type_Basic);
  3728. if (elem->Basic.flags & (BasicFlag_EndianLittle|BasicFlag_EndianBig)) {
  3729. gbString xts = type_to_string(x.type);
  3730. error(x.expr, "Expected a float which does not specify the explicit endianness for '%.*s', got %s", LIT(builtin_name), xts);
  3731. gb_string_free(xts);
  3732. return false;
  3733. }
  3734. }
  3735. if (is_type_float(x.type) && x.mode == Addressing_Constant) {
  3736. f64 v = exact_value_to_f64(x.value);
  3737. operand->mode = Addressing_Constant;
  3738. operand->type = x.type;
  3739. operand->value = exact_value_float(gb_sqrt(v));
  3740. break;
  3741. }
  3742. operand->mode = Addressing_Value;
  3743. operand->type = default_type(x.type);
  3744. }
  3745. break;
  3746. case BuiltinProc_fused_mul_add:
  3747. {
  3748. Operand x = {};
  3749. Operand y = {};
  3750. Operand z = {};
  3751. check_expr(c, &x, ce->args[0]); if (x.mode == Addressing_Invalid) return false;
  3752. check_expr(c, &y, ce->args[1]); if (y.mode == Addressing_Invalid) return false;
  3753. check_expr(c, &z, ce->args[2]); if (z.mode == Addressing_Invalid) return false;
  3754. convert_to_typed(c, &y, x.type); if (y.mode == Addressing_Invalid) return false;
  3755. convert_to_typed(c, &x, y.type); if (x.mode == Addressing_Invalid) return false;
  3756. convert_to_typed(c, &z, x.type); if (z.mode == Addressing_Invalid) return false;
  3757. convert_to_typed(c, &x, z.type); if (x.mode == Addressing_Invalid) return false;
  3758. if (is_type_untyped(x.type)) {
  3759. gbString xts = type_to_string(x.type);
  3760. error(x.expr, "Expected a typed floating point value or #simd vector for '%.*s', got %s", LIT(builtin_name), xts);
  3761. gb_string_free(xts);
  3762. return false;
  3763. }
  3764. Type *elem = core_array_type(x.type);
  3765. if (!is_type_float(x.type) && !(is_type_simd_vector(x.type) && is_type_float(elem))) {
  3766. gbString xts = type_to_string(x.type);
  3767. error(x.expr, "Expected a floating point or #simd vector value for '%.*s', got %s", LIT(builtin_name), xts);
  3768. gb_string_free(xts);
  3769. return false;
  3770. }
  3771. if (is_type_different_to_arch_endianness(elem)) {
  3772. GB_ASSERT(elem->kind == Type_Basic);
  3773. if (elem->Basic.flags & (BasicFlag_EndianLittle|BasicFlag_EndianBig)) {
  3774. gbString xts = type_to_string(x.type);
  3775. error(x.expr, "Expected a float which does not specify the explicit endianness for '%.*s', got %s", LIT(builtin_name), xts);
  3776. gb_string_free(xts);
  3777. return false;
  3778. }
  3779. }
  3780. if (!are_types_identical(x.type, y.type) || !are_types_identical(y.type, z.type)) {
  3781. gbString xts = type_to_string(x.type);
  3782. gbString yts = type_to_string(y.type);
  3783. gbString zts = type_to_string(z.type);
  3784. error(x.expr, "Mismatched types for '%.*s', got %s vs %s vs %s", LIT(builtin_name), xts, yts, zts);
  3785. gb_string_free(zts);
  3786. gb_string_free(yts);
  3787. gb_string_free(xts);
  3788. return false;
  3789. }
  3790. operand->mode = Addressing_Value;
  3791. operand->type = default_type(x.type);
  3792. }
  3793. break;
  3794. case BuiltinProc_mem_copy:
  3795. case BuiltinProc_mem_copy_non_overlapping:
  3796. {
  3797. operand->mode = Addressing_NoValue;
  3798. operand->type = t_invalid;
  3799. Operand dst = {};
  3800. Operand src = {};
  3801. Operand len = {};
  3802. check_expr(c, &dst, ce->args[0]);
  3803. check_expr(c, &src, ce->args[1]);
  3804. check_expr(c, &len, ce->args[2]);
  3805. if (dst.mode == Addressing_Invalid) {
  3806. return false;
  3807. }
  3808. if (src.mode == Addressing_Invalid) {
  3809. return false;
  3810. }
  3811. if (len.mode == Addressing_Invalid) {
  3812. return false;
  3813. }
  3814. if (!is_type_pointer(dst.type) && !is_type_multi_pointer(dst.type)) {
  3815. gbString str = type_to_string(dst.type);
  3816. error(dst.expr, "Expected a pointer value for '%.*s', got %s", LIT(builtin_name), str);
  3817. gb_string_free(str);
  3818. return false;
  3819. }
  3820. if (!is_type_pointer(src.type) && !is_type_multi_pointer(src.type)) {
  3821. gbString str = type_to_string(src.type);
  3822. error(src.expr, "Expected a pointer value for '%.*s', got %s", LIT(builtin_name), str);
  3823. gb_string_free(str);
  3824. return false;
  3825. }
  3826. if (!is_type_integer(len.type)) {
  3827. gbString str = type_to_string(len.type);
  3828. error(len.expr, "Expected an integer value for the number of bytes for '%.*s', got %s", LIT(builtin_name), str);
  3829. gb_string_free(str);
  3830. return false;
  3831. }
  3832. if (len.mode == Addressing_Constant) {
  3833. i64 n = exact_value_to_i64(len.value);
  3834. if (n < 0) {
  3835. gbString str = expr_to_string(len.expr);
  3836. error(len.expr, "Expected a non-negative integer value for the number of bytes for '%.*s', got %s", LIT(builtin_name), str);
  3837. gb_string_free(str);
  3838. }
  3839. }
  3840. }
  3841. break;
  3842. case BuiltinProc_mem_zero:
  3843. case BuiltinProc_mem_zero_volatile:
  3844. {
  3845. operand->mode = Addressing_NoValue;
  3846. operand->type = t_invalid;
  3847. Operand ptr = {};
  3848. Operand len = {};
  3849. check_expr(c, &ptr, ce->args[0]);
  3850. check_expr(c, &len, ce->args[1]);
  3851. if (ptr.mode == Addressing_Invalid) {
  3852. return false;
  3853. }
  3854. if (len.mode == Addressing_Invalid) {
  3855. return false;
  3856. }
  3857. if (!is_type_pointer(ptr.type) && !is_type_multi_pointer(ptr.type)) {
  3858. gbString str = type_to_string(ptr.type);
  3859. error(ptr.expr, "Expected a pointer value for '%.*s', got %s", LIT(builtin_name), str);
  3860. gb_string_free(str);
  3861. return false;
  3862. }
  3863. if (!is_type_integer(len.type)) {
  3864. gbString str = type_to_string(len.type);
  3865. error(len.expr, "Expected an integer value for the number of bytes for '%.*s', got %s", LIT(builtin_name), str);
  3866. gb_string_free(str);
  3867. return false;
  3868. }
  3869. if (len.mode == Addressing_Constant) {
  3870. i64 n = exact_value_to_i64(len.value);
  3871. if (n < 0) {
  3872. gbString str = expr_to_string(len.expr);
  3873. error(len.expr, "Expected a non-negative integer value for the number of bytes for '%.*s', got %s", LIT(builtin_name), str);
  3874. gb_string_free(str);
  3875. }
  3876. }
  3877. }
  3878. break;
  3879. case BuiltinProc_ptr_offset:
  3880. {
  3881. Operand ptr = {};
  3882. Operand offset = {};
  3883. check_expr(c, &ptr, ce->args[0]);
  3884. check_expr(c, &offset, ce->args[1]);
  3885. if (ptr.mode == Addressing_Invalid) {
  3886. operand->mode = Addressing_Invalid;
  3887. operand->type = t_invalid;
  3888. return false;
  3889. }
  3890. if (offset.mode == Addressing_Invalid) {
  3891. operand->mode = Addressing_Invalid;
  3892. operand->type = t_invalid;
  3893. return false;
  3894. }
  3895. operand->mode = Addressing_Value;
  3896. operand->type = ptr.type;
  3897. if (!is_type_pointer(ptr.type) && !is_type_multi_pointer(ptr.type)) {
  3898. gbString str = type_to_string(ptr.type);
  3899. error(ptr.expr, "Expected a pointer value for '%.*s', got %s", LIT(builtin_name), str);
  3900. gb_string_free(str);
  3901. return false;
  3902. }
  3903. if (are_types_identical(core_type(ptr.type), t_rawptr)) {
  3904. gbString str = type_to_string(ptr.type);
  3905. error(ptr.expr, "Expected a dereferenceable pointer value for '%.*s', got %s", LIT(builtin_name), str);
  3906. gb_string_free(str);
  3907. return false;
  3908. }
  3909. if (!is_type_integer(offset.type)) {
  3910. gbString str = type_to_string(offset.type);
  3911. error(offset.expr, "Expected an integer value for the offset parameter for '%.*s', got %s", LIT(builtin_name), str);
  3912. gb_string_free(str);
  3913. return false;
  3914. }
  3915. }
  3916. break;
  3917. case BuiltinProc_ptr_sub:
  3918. {
  3919. operand->mode = Addressing_NoValue;
  3920. operand->type = t_invalid;
  3921. Operand ptr0 = {};
  3922. Operand ptr1 = {};
  3923. check_expr(c, &ptr0, ce->args[0]);
  3924. check_expr(c, &ptr1, ce->args[1]);
  3925. if (ptr0.mode == Addressing_Invalid) {
  3926. operand->mode = Addressing_Invalid;
  3927. operand->type = t_invalid;
  3928. return false;
  3929. }
  3930. if (ptr1.mode == Addressing_Invalid) {
  3931. operand->mode = Addressing_Invalid;
  3932. operand->type = t_invalid;
  3933. return false;
  3934. }
  3935. operand->mode = Addressing_Value;
  3936. operand->type = t_int;
  3937. if (!is_type_pointer(ptr0.type) && !is_type_multi_pointer(ptr0.type)) {
  3938. gbString str = type_to_string(ptr0.type);
  3939. error(ptr0.expr, "Expected a pointer value for '%.*s', got %s", LIT(builtin_name), str);
  3940. gb_string_free(str);
  3941. return false;
  3942. }
  3943. if (are_types_identical(core_type(ptr0.type), t_rawptr)) {
  3944. gbString str = type_to_string(ptr0.type);
  3945. error(ptr0.expr, "Expected a dereferenceable pointer value for '%.*s', got %s", LIT(builtin_name), str);
  3946. gb_string_free(str);
  3947. return false;
  3948. }
  3949. if (!is_type_pointer(ptr1.type) && !is_type_multi_pointer(ptr1.type)) {
  3950. gbString str = type_to_string(ptr1.type);
  3951. error(ptr1.expr, "Expected a pointer value for '%.*s', got %s", LIT(builtin_name), str);
  3952. gb_string_free(str);
  3953. return false;
  3954. }
  3955. if (are_types_identical(core_type(ptr1.type), t_rawptr)) {
  3956. gbString str = type_to_string(ptr1.type);
  3957. error(ptr1.expr, "Expected a dereferenceable pointer value for '%.*s', got %s", LIT(builtin_name), str);
  3958. gb_string_free(str);
  3959. return false;
  3960. }
  3961. if (!are_types_identical(ptr0.type, ptr1.type)) {
  3962. gbString xts = type_to_string(ptr0.type);
  3963. gbString yts = type_to_string(ptr1.type);
  3964. error(ptr0.expr, "Mismatched types for '%.*s', %s vs %s", LIT(builtin_name), xts, yts);
  3965. gb_string_free(yts);
  3966. gb_string_free(xts);
  3967. return false;
  3968. }
  3969. Type *elem = type_deref(ptr0.type);
  3970. if (type_size_of(elem) == 0) {
  3971. gbString str = type_to_string(ptr0.type);
  3972. error(ptr0.expr, "Expected a pointer to a non-zero sized element for '%.*s', got %s", LIT(builtin_name), str);
  3973. gb_string_free(str);
  3974. return false;
  3975. }
  3976. }
  3977. break;
  3978. case BuiltinProc_atomic_type_is_lock_free:
  3979. {
  3980. Ast *expr = ce->args[0];
  3981. Operand o = {};
  3982. check_expr_or_type(c, &o, expr);
  3983. if (o.mode == Addressing_Invalid || o.mode == Addressing_Builtin) {
  3984. return false;
  3985. }
  3986. if (o.type == nullptr || o.type == t_invalid || is_type_asm_proc(o.type)) {
  3987. error(o.expr, "Invalid argument to '%.*s'", LIT(builtin_name));
  3988. return false;
  3989. }
  3990. if (is_type_polymorphic(o.type)) {
  3991. error(o.expr, "'%.*s' of polymorphic type cannot be determined", LIT(builtin_name));
  3992. return false;
  3993. }
  3994. if (is_type_untyped(o.type)) {
  3995. error(o.expr, "'%.*s' of untyped type is not allowed", LIT(builtin_name));
  3996. return false;
  3997. }
  3998. Type *t = o.type;
  3999. bool is_lock_free = is_type_lock_free(t);
  4000. operand->mode = Addressing_Constant;
  4001. operand->type = t_untyped_bool;
  4002. operand->value = exact_value_bool(is_lock_free);
  4003. break;
  4004. }
  4005. case BuiltinProc_atomic_thread_fence:
  4006. case BuiltinProc_atomic_signal_fence:
  4007. {
  4008. OdinAtomicMemoryOrder memory_order = {};
  4009. if (!check_atomic_memory_order_argument(c, ce->args[0], builtin_name, &memory_order)) {
  4010. return false;
  4011. }
  4012. switch (memory_order) {
  4013. case OdinAtomicMemoryOrder_acquire:
  4014. case OdinAtomicMemoryOrder_release:
  4015. case OdinAtomicMemoryOrder_acq_rel:
  4016. case OdinAtomicMemoryOrder_seq_cst:
  4017. break;
  4018. default:
  4019. error(ce->args[0], "Illegal memory ordering for '%.*s', got .%s", LIT(builtin_name), OdinAtomicMemoryOrder_strings[memory_order]);
  4020. break;
  4021. }
  4022. operand->mode = Addressing_NoValue;
  4023. }
  4024. break;
  4025. case BuiltinProc_volatile_store:
  4026. case BuiltinProc_unaligned_store:
  4027. case BuiltinProc_non_temporal_store:
  4028. case BuiltinProc_atomic_store:
  4029. {
  4030. Type *elem = nullptr;
  4031. if (!is_type_normal_pointer(operand->type, &elem)) {
  4032. error(operand->expr, "Expected a pointer for '%.*s'", LIT(builtin_name));
  4033. return false;
  4034. }
  4035. if (id == BuiltinProc_atomic_store && !check_atomic_ptr_argument(operand, builtin_name, elem)) {
  4036. return false;
  4037. }
  4038. Operand x = {};
  4039. check_expr_with_type_hint(c, &x, ce->args[1], elem);
  4040. check_assignment(c, &x, elem, builtin_name);
  4041. operand->type = nullptr;
  4042. operand->mode = Addressing_NoValue;
  4043. break;
  4044. }
  4045. case BuiltinProc_atomic_store_explicit:
  4046. {
  4047. Type *elem = nullptr;
  4048. if (!is_type_normal_pointer(operand->type, &elem)) {
  4049. error(operand->expr, "Expected a pointer for '%.*s'", LIT(builtin_name));
  4050. return false;
  4051. }
  4052. if (!check_atomic_ptr_argument(operand, builtin_name, elem)) {
  4053. return false;
  4054. }
  4055. Operand x = {};
  4056. check_expr_with_type_hint(c, &x, ce->args[1], elem);
  4057. check_assignment(c, &x, elem, builtin_name);
  4058. OdinAtomicMemoryOrder memory_order = {};
  4059. if (!check_atomic_memory_order_argument(c, ce->args[2], builtin_name, &memory_order)) {
  4060. return false;
  4061. }
  4062. switch (memory_order) {
  4063. case OdinAtomicMemoryOrder_consume:
  4064. case OdinAtomicMemoryOrder_acquire:
  4065. case OdinAtomicMemoryOrder_acq_rel:
  4066. error(ce->args[2], "Illegal memory order .%s for '%.*s'", OdinAtomicMemoryOrder_strings[memory_order], LIT(builtin_name));
  4067. break;
  4068. }
  4069. operand->type = nullptr;
  4070. operand->mode = Addressing_NoValue;
  4071. break;
  4072. }
  4073. case BuiltinProc_volatile_load:
  4074. case BuiltinProc_unaligned_load:
  4075. case BuiltinProc_non_temporal_load:
  4076. case BuiltinProc_atomic_load:
  4077. {
  4078. Type *elem = nullptr;
  4079. if (!is_type_normal_pointer(operand->type, &elem)) {
  4080. error(operand->expr, "Expected a pointer for '%.*s'", LIT(builtin_name));
  4081. return false;
  4082. }
  4083. if (id == BuiltinProc_atomic_load && !check_atomic_ptr_argument(operand, builtin_name, elem)) {
  4084. return false;
  4085. }
  4086. operand->type = elem;
  4087. operand->mode = Addressing_Value;
  4088. break;
  4089. }
  4090. case BuiltinProc_atomic_load_explicit:
  4091. {
  4092. Type *elem = nullptr;
  4093. if (!is_type_normal_pointer(operand->type, &elem)) {
  4094. error(operand->expr, "Expected a pointer for '%.*s'", LIT(builtin_name));
  4095. return false;
  4096. }
  4097. if (!check_atomic_ptr_argument(operand, builtin_name, elem)) {
  4098. return false;
  4099. }
  4100. OdinAtomicMemoryOrder memory_order = {};
  4101. if (!check_atomic_memory_order_argument(c, ce->args[1], builtin_name, &memory_order)) {
  4102. return false;
  4103. }
  4104. switch (memory_order) {
  4105. case OdinAtomicMemoryOrder_release:
  4106. case OdinAtomicMemoryOrder_acq_rel:
  4107. error(ce->args[1], "Illegal memory order .%s for '%.*s'", OdinAtomicMemoryOrder_strings[memory_order], LIT(builtin_name));
  4108. break;
  4109. }
  4110. operand->type = elem;
  4111. operand->mode = Addressing_Value;
  4112. break;
  4113. }
  4114. case BuiltinProc_atomic_add:
  4115. case BuiltinProc_atomic_sub:
  4116. case BuiltinProc_atomic_and:
  4117. case BuiltinProc_atomic_nand:
  4118. case BuiltinProc_atomic_or:
  4119. case BuiltinProc_atomic_xor:
  4120. case BuiltinProc_atomic_exchange:
  4121. {
  4122. Type *elem = nullptr;
  4123. if (!is_type_normal_pointer(operand->type, &elem)) {
  4124. error(operand->expr, "Expected a pointer for '%.*s'", LIT(builtin_name));
  4125. return false;
  4126. }
  4127. if (!check_atomic_ptr_argument(operand, builtin_name, elem)) {
  4128. return false;
  4129. }
  4130. Operand x = {};
  4131. check_expr_with_type_hint(c, &x, ce->args[1], elem);
  4132. check_assignment(c, &x, elem, builtin_name);
  4133. Type *t = type_deref(operand->type);
  4134. switch (id) {
  4135. case BuiltinProc_atomic_add:
  4136. case BuiltinProc_atomic_sub:
  4137. if (!is_type_numeric(t)) {
  4138. gbString str = type_to_string(t);
  4139. error(operand->expr, "Expected a numeric type for '%.*s', got %s", LIT(builtin_name), str);
  4140. gb_string_free(str);
  4141. } else if (is_type_different_to_arch_endianness(t)) {
  4142. gbString str = type_to_string(t);
  4143. error(operand->expr, "Expected a numeric type of the same platform endianness for '%.*s', got %s", LIT(builtin_name), str);
  4144. gb_string_free(str);
  4145. }
  4146. }
  4147. operand->type = elem;
  4148. operand->mode = Addressing_Value;
  4149. break;
  4150. }
  4151. case BuiltinProc_atomic_add_explicit:
  4152. case BuiltinProc_atomic_sub_explicit:
  4153. case BuiltinProc_atomic_and_explicit:
  4154. case BuiltinProc_atomic_nand_explicit:
  4155. case BuiltinProc_atomic_or_explicit:
  4156. case BuiltinProc_atomic_xor_explicit:
  4157. case BuiltinProc_atomic_exchange_explicit:
  4158. {
  4159. Type *elem = nullptr;
  4160. if (!is_type_normal_pointer(operand->type, &elem)) {
  4161. error(operand->expr, "Expected a pointer for '%.*s'", LIT(builtin_name));
  4162. return false;
  4163. }
  4164. if (!check_atomic_ptr_argument(operand, builtin_name, elem)) {
  4165. return false;
  4166. }
  4167. Operand x = {};
  4168. check_expr_with_type_hint(c, &x, ce->args[1], elem);
  4169. check_assignment(c, &x, elem, builtin_name);
  4170. if (!check_atomic_memory_order_argument(c, ce->args[2], builtin_name, nullptr)) {
  4171. return false;
  4172. }
  4173. Type *t = type_deref(operand->type);
  4174. switch (id) {
  4175. case BuiltinProc_atomic_add_explicit:
  4176. case BuiltinProc_atomic_sub_explicit:
  4177. if (!is_type_numeric(t)) {
  4178. gbString str = type_to_string(t);
  4179. error(operand->expr, "Expected a numeric type for '%.*s', got %s", LIT(builtin_name), str);
  4180. gb_string_free(str);
  4181. } else if (is_type_different_to_arch_endianness(t)) {
  4182. gbString str = type_to_string(t);
  4183. error(operand->expr, "Expected a numeric type of the same platform endianness for '%.*s', got %s", LIT(builtin_name), str);
  4184. gb_string_free(str);
  4185. }
  4186. break;
  4187. }
  4188. operand->type = elem;
  4189. operand->mode = Addressing_Value;
  4190. break;
  4191. }
  4192. case BuiltinProc_atomic_compare_exchange_strong:
  4193. case BuiltinProc_atomic_compare_exchange_weak:
  4194. {
  4195. Type *elem = nullptr;
  4196. if (!is_type_normal_pointer(operand->type, &elem)) {
  4197. error(operand->expr, "Expected a pointer for '%.*s'", LIT(builtin_name));
  4198. return false;
  4199. }
  4200. if (!check_atomic_ptr_argument(operand, builtin_name, elem)) {
  4201. return false;
  4202. }
  4203. Operand x = {};
  4204. Operand y = {};
  4205. check_expr_with_type_hint(c, &x, ce->args[1], elem);
  4206. check_expr_with_type_hint(c, &y, ce->args[2], elem);
  4207. check_assignment(c, &x, elem, builtin_name);
  4208. check_assignment(c, &y, elem, builtin_name);
  4209. Type *t = type_deref(operand->type);
  4210. if (!is_type_comparable(t)) {
  4211. gbString str = type_to_string(t);
  4212. error(operand->expr, "Expected a comparable type for '%.*s', got %s", LIT(builtin_name), str);
  4213. gb_string_free(str);
  4214. }
  4215. operand->mode = Addressing_OptionalOk;
  4216. operand->type = elem;
  4217. break;
  4218. }
  4219. case BuiltinProc_atomic_compare_exchange_strong_explicit:
  4220. case BuiltinProc_atomic_compare_exchange_weak_explicit:
  4221. {
  4222. Type *elem = nullptr;
  4223. if (!is_type_normal_pointer(operand->type, &elem)) {
  4224. error(operand->expr, "Expected a pointer for '%.*s'", LIT(builtin_name));
  4225. return false;
  4226. }
  4227. if (!check_atomic_ptr_argument(operand, builtin_name, elem)) {
  4228. return false;
  4229. }
  4230. Operand x = {};
  4231. Operand y = {};
  4232. check_expr_with_type_hint(c, &x, ce->args[1], elem);
  4233. check_expr_with_type_hint(c, &y, ce->args[2], elem);
  4234. check_assignment(c, &x, elem, builtin_name);
  4235. check_assignment(c, &y, elem, builtin_name);
  4236. OdinAtomicMemoryOrder success_memory_order = {};
  4237. OdinAtomicMemoryOrder failure_memory_order = {};
  4238. if (!check_atomic_memory_order_argument(c, ce->args[3], builtin_name, &success_memory_order, "success ordering")) {
  4239. return false;
  4240. }
  4241. if (!check_atomic_memory_order_argument(c, ce->args[4], builtin_name, &failure_memory_order, "failure ordering")) {
  4242. return false;
  4243. }
  4244. Type *t = type_deref(operand->type);
  4245. if (!is_type_comparable(t)) {
  4246. gbString str = type_to_string(t);
  4247. error(operand->expr, "Expected a comparable type for '%.*s', got %s", LIT(builtin_name), str);
  4248. gb_string_free(str);
  4249. }
  4250. bool invalid_combination = false;
  4251. switch (success_memory_order) {
  4252. case OdinAtomicMemoryOrder_relaxed:
  4253. case OdinAtomicMemoryOrder_release:
  4254. if (failure_memory_order != OdinAtomicMemoryOrder_relaxed) {
  4255. invalid_combination = true;
  4256. }
  4257. break;
  4258. case OdinAtomicMemoryOrder_consume:
  4259. switch (failure_memory_order) {
  4260. case OdinAtomicMemoryOrder_relaxed:
  4261. case OdinAtomicMemoryOrder_consume:
  4262. break;
  4263. default:
  4264. invalid_combination = true;
  4265. break;
  4266. }
  4267. break;
  4268. case OdinAtomicMemoryOrder_acquire:
  4269. case OdinAtomicMemoryOrder_acq_rel:
  4270. switch (failure_memory_order) {
  4271. case OdinAtomicMemoryOrder_relaxed:
  4272. case OdinAtomicMemoryOrder_consume:
  4273. case OdinAtomicMemoryOrder_acquire:
  4274. break;
  4275. default:
  4276. invalid_combination = true;
  4277. break;
  4278. }
  4279. break;
  4280. case OdinAtomicMemoryOrder_seq_cst:
  4281. switch (failure_memory_order) {
  4282. case OdinAtomicMemoryOrder_relaxed:
  4283. case OdinAtomicMemoryOrder_consume:
  4284. case OdinAtomicMemoryOrder_acquire:
  4285. case OdinAtomicMemoryOrder_seq_cst:
  4286. break;
  4287. default:
  4288. invalid_combination = true;
  4289. break;
  4290. }
  4291. break;
  4292. default:
  4293. invalid_combination = true;
  4294. break;
  4295. }
  4296. if (invalid_combination) {
  4297. error(ce->args[3], "Illegal memory order pairing for '%.*s', success = .%s, failure = .%s",
  4298. LIT(builtin_name),
  4299. OdinAtomicMemoryOrder_strings[success_memory_order],
  4300. OdinAtomicMemoryOrder_strings[failure_memory_order]
  4301. );
  4302. }
  4303. operand->mode = Addressing_OptionalOk;
  4304. operand->type = elem;
  4305. break;
  4306. }
  4307. case BuiltinProc_fixed_point_mul:
  4308. case BuiltinProc_fixed_point_div:
  4309. case BuiltinProc_fixed_point_mul_sat:
  4310. case BuiltinProc_fixed_point_div_sat:
  4311. {
  4312. Operand x = {};
  4313. Operand y = {};
  4314. Operand z = {};
  4315. check_expr(c, &x, ce->args[0]);
  4316. if (x.mode == Addressing_Invalid) {
  4317. return false;
  4318. }
  4319. check_expr(c, &y, ce->args[1]);
  4320. if (y.mode == Addressing_Invalid) {
  4321. return false;
  4322. }
  4323. convert_to_typed(c, &x, y.type);
  4324. if (x.mode == Addressing_Invalid) {
  4325. return false;
  4326. }
  4327. convert_to_typed(c, &y, x.type); if (y.mode == Addressing_Invalid) return false;
  4328. if (x.mode == Addressing_Invalid) {
  4329. return false;
  4330. }
  4331. if (!are_types_identical(x.type, y.type)) {
  4332. gbString xts = type_to_string(x.type);
  4333. gbString yts = type_to_string(y.type);
  4334. error(x.expr, "Mismatched types for '%.*s', %s vs %s", LIT(builtin_name), xts, yts);
  4335. gb_string_free(yts);
  4336. gb_string_free(xts);
  4337. return false;
  4338. }
  4339. if (!is_type_integer(x.type) || is_type_untyped(x.type)) {
  4340. gbString xts = type_to_string(x.type);
  4341. error(x.expr, "Expected an integer type for '%.*s', got %s", LIT(builtin_name), xts);
  4342. gb_string_free(xts);
  4343. return false;
  4344. }
  4345. check_expr(c, &z, ce->args[2]);
  4346. if (z.mode == Addressing_Invalid) {
  4347. return false;
  4348. }
  4349. if (z.mode != Addressing_Constant || !is_type_integer(z.type)) {
  4350. error(z.expr, "Expected a constant integer for the scale in '%.*s'", LIT(builtin_name));
  4351. return false;
  4352. }
  4353. i64 n = exact_value_to_i64(z.value);
  4354. if (n <= 0) {
  4355. error(z.expr, "Scale parameter in '%.*s' must be positive, got %lld", LIT(builtin_name), n);
  4356. return false;
  4357. }
  4358. i64 sz = 8*type_size_of(x.type);
  4359. if (n > sz) {
  4360. error(z.expr, "Scale parameter in '%.*s' is larger than the base integer bit width, got %lld, expected a maximum of %lld", LIT(builtin_name), n, sz);
  4361. return false;
  4362. }
  4363. operand->type = x.type;
  4364. operand->mode = Addressing_Value;
  4365. }
  4366. break;
  4367. case BuiltinProc_expect:
  4368. {
  4369. Operand x = {};
  4370. Operand y = {};
  4371. check_expr(c, &x, ce->args[0]);
  4372. check_expr(c, &y, ce->args[1]);
  4373. if (x.mode == Addressing_Invalid) {
  4374. return false;
  4375. }
  4376. if (y.mode == Addressing_Invalid) {
  4377. return false;
  4378. }
  4379. convert_to_typed(c, &y, x.type); if (y.mode == Addressing_Invalid) return false;
  4380. convert_to_typed(c, &x, y.type);
  4381. if (!are_types_identical(x.type, y.type)) {
  4382. gbString xts = type_to_string(x.type);
  4383. gbString yts = type_to_string(y.type);
  4384. error(x.expr, "Mismatched types for '%.*s', %s vs %s", LIT(builtin_name), xts, yts);
  4385. gb_string_free(yts);
  4386. gb_string_free(xts);
  4387. *operand = x; // minimize error propagation
  4388. return true;
  4389. }
  4390. if (!is_type_integer_like(x.type)) {
  4391. gbString xts = type_to_string(x.type);
  4392. error(x.expr, "Values passed to '%.*s' must be an integer-like type (integer, boolean, enum, bit_set), got %s", LIT(builtin_name), xts);
  4393. gb_string_free(xts);
  4394. *operand = x;
  4395. return true;
  4396. }
  4397. if (y.mode != Addressing_Constant) {
  4398. error(y.expr, "Second argument to '%.*s' must be constant as it is the expected value", LIT(builtin_name));
  4399. }
  4400. if (x.mode == Addressing_Constant) {
  4401. // NOTE(bill): just completely ignore this intrinsic entirely
  4402. *operand = x;
  4403. return true;
  4404. }
  4405. operand->mode = Addressing_Value;
  4406. operand->type = x.type;
  4407. }
  4408. break;
  4409. case BuiltinProc_prefetch_read_instruction:
  4410. case BuiltinProc_prefetch_read_data:
  4411. case BuiltinProc_prefetch_write_instruction:
  4412. case BuiltinProc_prefetch_write_data:
  4413. {
  4414. operand->mode = Addressing_NoValue;
  4415. operand->type = nullptr;
  4416. Operand x = {};
  4417. Operand y = {};
  4418. check_expr(c, &x, ce->args[0]);
  4419. check_expr(c, &y, ce->args[1]);
  4420. if (x.mode == Addressing_Invalid) {
  4421. return false;
  4422. }
  4423. if (y.mode == Addressing_Invalid) {
  4424. return false;
  4425. }
  4426. check_assignment(c, &x, t_rawptr, builtin_name);
  4427. if (x.mode == Addressing_Invalid) {
  4428. return false;
  4429. }
  4430. if (y.mode != Addressing_Constant && is_type_integer(y.type)) {
  4431. error(y.expr, "Second argument to '%.*s' representing the locality must be an integer in the range 0..=3", LIT(builtin_name));
  4432. return false;
  4433. }
  4434. i64 locality = exact_value_to_i64(y.value);
  4435. if (!(0 <= locality && locality <= 3)) {
  4436. error(y.expr, "Second argument to '%.*s' representing the locality must be an integer in the range 0..=3", LIT(builtin_name));
  4437. return false;
  4438. }
  4439. }
  4440. break;
  4441. case BuiltinProc_syscall:
  4442. {
  4443. convert_to_typed(c, operand, t_uintptr);
  4444. if (!is_type_uintptr(operand->type)) {
  4445. gbString t = type_to_string(operand->type);
  4446. error(operand->expr, "Argument 0 must be of type 'uintptr', got %s", t);
  4447. gb_string_free(t);
  4448. }
  4449. for (isize i = 1; i < ce->args.count; i++) {
  4450. Operand x = {};
  4451. check_expr(c, &x, ce->args[i]);
  4452. if (x.mode != Addressing_Invalid) {
  4453. convert_to_typed(c, &x, t_uintptr);
  4454. }
  4455. convert_to_typed(c, &x, t_uintptr);
  4456. if (!is_type_uintptr(x.type)) {
  4457. gbString t = type_to_string(x.type);
  4458. error(x.expr, "Argument %td must be of type 'uintptr', got %s", i, t);
  4459. gb_string_free(t);
  4460. }
  4461. }
  4462. isize max_arg_count = 32;
  4463. switch (build_context.metrics.os) {
  4464. case TargetOs_windows:
  4465. case TargetOs_freestanding:
  4466. error(call, "'%.*s' is not supported on this platform (%.*s)", LIT(builtin_name), LIT(target_os_names[build_context.metrics.os]));
  4467. break;
  4468. case TargetOs_darwin:
  4469. case TargetOs_linux:
  4470. case TargetOs_essence:
  4471. case TargetOs_freebsd:
  4472. case TargetOs_openbsd:
  4473. case TargetOs_haiku:
  4474. switch (build_context.metrics.arch) {
  4475. case TargetArch_i386:
  4476. case TargetArch_amd64:
  4477. case TargetArch_arm64:
  4478. max_arg_count = 7;
  4479. break;
  4480. }
  4481. break;
  4482. }
  4483. if (ce->args.count > max_arg_count) {
  4484. error(ast_end_token(call), "'%.*s' has a maximum of %td arguments on this platform (%.*s), got %td", LIT(builtin_name), max_arg_count, LIT(target_os_names[build_context.metrics.os]), ce->args.count);
  4485. }
  4486. operand->mode = Addressing_Value;
  4487. operand->type = t_uintptr;
  4488. return true;
  4489. }
  4490. break;
  4491. case BuiltinProc_type_base_type:
  4492. if (operand->mode != Addressing_Type) {
  4493. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  4494. } else {
  4495. operand->type = base_type(operand->type);
  4496. }
  4497. operand->mode = Addressing_Type;
  4498. break;
  4499. case BuiltinProc_type_core_type:
  4500. if (operand->mode != Addressing_Type) {
  4501. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  4502. } else {
  4503. operand->type = core_type(operand->type);
  4504. }
  4505. operand->mode = Addressing_Type;
  4506. break;
  4507. case BuiltinProc_type_elem_type:
  4508. if (operand->mode != Addressing_Type) {
  4509. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  4510. } else {
  4511. Type *bt = base_type(operand->type);
  4512. switch (bt->kind) {
  4513. case Type_Basic:
  4514. switch (bt->Basic.kind) {
  4515. case Basic_complex32: operand->type = t_f16; break;
  4516. case Basic_complex64: operand->type = t_f32; break;
  4517. case Basic_complex128: operand->type = t_f64; break;
  4518. case Basic_quaternion64: operand->type = t_f16; break;
  4519. case Basic_quaternion128: operand->type = t_f32; break;
  4520. case Basic_quaternion256: operand->type = t_f64; break;
  4521. }
  4522. break;
  4523. case Type_Pointer: operand->type = bt->Pointer.elem; break;
  4524. case Type_Array: operand->type = bt->Array.elem; break;
  4525. case Type_EnumeratedArray: operand->type = bt->EnumeratedArray.elem; break;
  4526. case Type_Slice: operand->type = bt->Slice.elem; break;
  4527. case Type_DynamicArray: operand->type = bt->DynamicArray.elem; break;
  4528. }
  4529. }
  4530. operand->mode = Addressing_Type;
  4531. break;
  4532. case BuiltinProc_type_convert_variants_to_pointers:
  4533. if (operand->mode != Addressing_Type) {
  4534. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  4535. } else {
  4536. Type *bt = base_type(operand->type);
  4537. if (is_type_polymorphic(bt)) {
  4538. // IGNORE polymorphic types
  4539. return true;
  4540. } else if (bt->kind != Type_Union) {
  4541. gbString t = type_to_string(operand->type);
  4542. error(operand->expr, "Expected a union type for '%.*s', got %s", LIT(builtin_name), t);
  4543. gb_string_free(t);
  4544. operand->mode = Addressing_Invalid;
  4545. operand->type = t_invalid;
  4546. return false;
  4547. } else if (bt->Union.is_polymorphic) {
  4548. gbString t = type_to_string(operand->type);
  4549. error(operand->expr, "Expected a non-polymorphic union type for '%.*s', got %s", LIT(builtin_name), t);
  4550. gb_string_free(t);
  4551. operand->mode = Addressing_Invalid;
  4552. operand->type = t_invalid;
  4553. return false;
  4554. }
  4555. Type *new_type = alloc_type_union();
  4556. auto variants = slice_make<Type *>(permanent_allocator(), bt->Union.variants.count);
  4557. for_array(i, bt->Union.variants) {
  4558. variants[i] = alloc_type_pointer(bt->Union.variants[i]);
  4559. }
  4560. new_type->Union.variants = variants;
  4561. // NOTE(bill): Is this even correct?
  4562. new_type->Union.node = operand->expr;
  4563. new_type->Union.scope = bt->Union.scope;
  4564. operand->type = new_type;
  4565. }
  4566. operand->mode = Addressing_Type;
  4567. break;
  4568. case BuiltinProc_type_merge:
  4569. {
  4570. operand->mode = Addressing_Type;
  4571. operand->type = t_invalid;
  4572. Operand x = {};
  4573. Operand y = {};
  4574. check_expr_or_type(c, &x, ce->args[0]);
  4575. check_expr_or_type(c, &y, ce->args[1]);
  4576. if (x.mode != Addressing_Type) {
  4577. error(x.expr, "Expected a type for '%.*s'", LIT(builtin_name));
  4578. return false;
  4579. }
  4580. if (y.mode != Addressing_Type) {
  4581. error(y.expr, "Expected a type for '%.*s'", LIT(builtin_name));
  4582. return false;
  4583. }
  4584. if (is_type_polymorphic(x.type)) {
  4585. gbString t = type_to_string(x.type);
  4586. error(x.expr, "Expected a non-polymorphic type for '%.*s', got %s", LIT(builtin_name), t);
  4587. gb_string_free(t);
  4588. return false;
  4589. }
  4590. if (is_type_polymorphic(y.type)) {
  4591. gbString t = type_to_string(y.type);
  4592. error(y.expr, "Expected a non-polymorphic type for '%.*s', got %s", LIT(builtin_name), t);
  4593. gb_string_free(t);
  4594. return false;
  4595. }
  4596. if (!is_type_union(x.type)) {
  4597. gbString t = type_to_string(x.type);
  4598. error(x.expr, "Expected a union type for '%.*s', got %s", LIT(builtin_name), t);
  4599. gb_string_free(t);
  4600. return false;
  4601. }
  4602. if (!is_type_union(y.type)) {
  4603. gbString t = type_to_string(y.type);
  4604. error(x.expr, "Expected a union type for '%.*s', got %s", LIT(builtin_name), t);
  4605. gb_string_free(t);
  4606. return false;
  4607. }
  4608. Type *ux = base_type(x.type);
  4609. Type *uy = base_type(y.type);
  4610. GB_ASSERT(ux->kind == Type_Union);
  4611. GB_ASSERT(uy->kind == Type_Union);
  4612. i64 custom_align = gb_max(ux->Union.custom_align, uy->Union.custom_align);
  4613. if (ux->Union.kind != uy->Union.kind) {
  4614. error(x.expr, "Union kinds must match, got %s vs %s", union_type_kind_strings[ux->Union.kind], union_type_kind_strings[uy->Union.kind]);
  4615. }
  4616. Type *merged_union = alloc_type_union();
  4617. merged_union->Union.node = call;
  4618. merged_union->Union.scope = create_scope(c->info, c->scope);
  4619. merged_union->Union.kind = ux->Union.kind;
  4620. merged_union->Union.custom_align = custom_align;
  4621. auto variants = array_make<Type *>(permanent_allocator(), 0, ux->Union.variants.count+uy->Union.variants.count);
  4622. for (Type *t : ux->Union.variants) {
  4623. array_add(&variants, t);
  4624. }
  4625. for (Type *t : uy->Union.variants) {
  4626. bool ok = true;
  4627. for (Type *other_t : ux->Union.variants) {
  4628. if (are_types_identical(other_t, t)) {
  4629. ok = false;
  4630. break;
  4631. }
  4632. }
  4633. if (ok) {
  4634. array_add(&variants, t);
  4635. }
  4636. }
  4637. merged_union->Union.variants = slice_from_array(variants);
  4638. operand->mode = Addressing_Type;
  4639. operand->type = merged_union;
  4640. }
  4641. break;
  4642. case BuiltinProc_type_is_boolean:
  4643. case BuiltinProc_type_is_integer:
  4644. case BuiltinProc_type_is_rune:
  4645. case BuiltinProc_type_is_float:
  4646. case BuiltinProc_type_is_complex:
  4647. case BuiltinProc_type_is_quaternion:
  4648. case BuiltinProc_type_is_string:
  4649. case BuiltinProc_type_is_typeid:
  4650. case BuiltinProc_type_is_any:
  4651. case BuiltinProc_type_is_endian_platform:
  4652. case BuiltinProc_type_is_endian_little:
  4653. case BuiltinProc_type_is_endian_big:
  4654. case BuiltinProc_type_is_unsigned:
  4655. case BuiltinProc_type_is_numeric:
  4656. case BuiltinProc_type_is_ordered:
  4657. case BuiltinProc_type_is_ordered_numeric:
  4658. case BuiltinProc_type_is_indexable:
  4659. case BuiltinProc_type_is_sliceable:
  4660. case BuiltinProc_type_is_comparable:
  4661. case BuiltinProc_type_is_simple_compare:
  4662. case BuiltinProc_type_is_dereferenceable:
  4663. case BuiltinProc_type_is_valid_map_key:
  4664. case BuiltinProc_type_is_valid_matrix_elements:
  4665. case BuiltinProc_type_is_named:
  4666. case BuiltinProc_type_is_pointer:
  4667. case BuiltinProc_type_is_multi_pointer:
  4668. case BuiltinProc_type_is_array:
  4669. case BuiltinProc_type_is_enumerated_array:
  4670. case BuiltinProc_type_is_slice:
  4671. case BuiltinProc_type_is_dynamic_array:
  4672. case BuiltinProc_type_is_map:
  4673. case BuiltinProc_type_is_struct:
  4674. case BuiltinProc_type_is_union:
  4675. case BuiltinProc_type_is_enum:
  4676. case BuiltinProc_type_is_proc:
  4677. case BuiltinProc_type_is_bit_set:
  4678. case BuiltinProc_type_is_simd_vector:
  4679. case BuiltinProc_type_is_matrix:
  4680. case BuiltinProc_type_is_specialized_polymorphic_record:
  4681. case BuiltinProc_type_is_unspecialized_polymorphic_record:
  4682. case BuiltinProc_type_has_nil:
  4683. GB_ASSERT(BuiltinProc__type_simple_boolean_begin < id && id < BuiltinProc__type_simple_boolean_end);
  4684. operand->value = exact_value_bool(false);
  4685. if (operand->mode != Addressing_Type) {
  4686. gbString str = expr_to_string(ce->args[0]);
  4687. error(operand->expr, "Expected a type for '%.*s', got '%s'", LIT(builtin_name), str);
  4688. gb_string_free(str);
  4689. } else {
  4690. i32 i = id - cast(i32)BuiltinProc__type_simple_boolean_begin;
  4691. auto procedure = builtin_type_is_procs[i];
  4692. GB_ASSERT_MSG(procedure != nullptr, "%.*s", LIT(builtin_name));
  4693. bool ok = procedure(operand->type);
  4694. operand->value = exact_value_bool(ok);
  4695. }
  4696. operand->mode = Addressing_Constant;
  4697. operand->type = t_untyped_bool;
  4698. break;
  4699. case BuiltinProc_type_is_matrix_row_major:
  4700. case BuiltinProc_type_is_matrix_column_major:
  4701. {
  4702. Operand op = {};
  4703. Type *bt = check_type(c, ce->args[0]);
  4704. Type *type = base_type(bt);
  4705. if (type == nullptr || type == t_invalid) {
  4706. error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
  4707. return false;
  4708. }
  4709. if (type->kind != Type_Matrix) {
  4710. gbString s = type_to_string(bt);
  4711. error(ce->args[0], "Expected a matrix type for '%.*s', got '%s'", LIT(builtin_name), s);
  4712. gb_string_free(s);
  4713. return false;
  4714. }
  4715. if (id == BuiltinProc_type_is_matrix_row_major) {
  4716. operand->value = exact_value_bool(bt->Matrix.is_row_major == true);
  4717. } else {
  4718. operand->value = exact_value_bool(bt->Matrix.is_row_major == false);
  4719. }
  4720. operand->mode = Addressing_Constant;
  4721. operand->type = t_untyped_bool;
  4722. break;
  4723. }
  4724. case BuiltinProc_type_has_field:
  4725. {
  4726. Operand op = {};
  4727. Type *bt = check_type(c, ce->args[0]);
  4728. Type *type = base_type(bt);
  4729. if (type == nullptr || type == t_invalid) {
  4730. error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
  4731. return false;
  4732. }
  4733. Operand x = {};
  4734. check_expr(c, &x, ce->args[1]);
  4735. if (!is_type_string(x.type) || x.mode != Addressing_Constant || x.value.kind != ExactValue_String) {
  4736. error(ce->args[1], "Expected a const string for field argument");
  4737. return false;
  4738. }
  4739. String field_name = x.value.value_string;
  4740. Selection sel = lookup_field(type, field_name, false);
  4741. operand->mode = Addressing_Constant;
  4742. operand->value = exact_value_bool(sel.index.count != 0);
  4743. operand->type = t_untyped_bool;
  4744. break;
  4745. }
  4746. break;
  4747. case BuiltinProc_type_field_type:
  4748. {
  4749. Operand op = {};
  4750. Type *bt = check_type(c, ce->args[0]);
  4751. Type *type = base_type(bt);
  4752. if (type == nullptr || type == t_invalid) {
  4753. error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
  4754. return false;
  4755. }
  4756. Operand x = {};
  4757. check_expr(c, &x, ce->args[1]);
  4758. if (!is_type_string(x.type) || x.mode != Addressing_Constant || x.value.kind != ExactValue_String) {
  4759. error(ce->args[1], "Expected a const string for field argument");
  4760. return false;
  4761. }
  4762. String field_name = x.value.value_string;
  4763. Selection sel = lookup_field(type, field_name, false);
  4764. if (sel.index.count == 0) {
  4765. gbString t = type_to_string(type);
  4766. error(ce->args[1], "'%.*s' is not a field of type %s", LIT(field_name), t);
  4767. gb_string_free(t);
  4768. return false;
  4769. }
  4770. operand->mode = Addressing_Type;
  4771. operand->type = sel.entity->type;
  4772. break;
  4773. }
  4774. break;
  4775. case BuiltinProc_type_is_specialization_of:
  4776. {
  4777. if (operand->mode != Addressing_Type) {
  4778. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  4779. operand->mode = Addressing_Invalid;
  4780. operand->type = t_invalid;
  4781. return false;
  4782. }
  4783. Type *t = operand->type;
  4784. Type *s = nullptr;
  4785. bool prev_ips = c->in_polymorphic_specialization;
  4786. c->in_polymorphic_specialization = true;
  4787. s = check_type(c, ce->args[1]);
  4788. c->in_polymorphic_specialization = prev_ips;
  4789. if (s == t_invalid) {
  4790. error(ce->args[1], "Invalid specialization type for '%.*s'", LIT(builtin_name));
  4791. operand->mode = Addressing_Invalid;
  4792. operand->type = t_invalid;
  4793. return false;
  4794. }
  4795. operand->mode = Addressing_Constant;
  4796. operand->type = t_untyped_bool;
  4797. operand->value = exact_value_bool(check_type_specialization_to(c, s, t, false, false));
  4798. }
  4799. break;
  4800. case BuiltinProc_type_is_variant_of:
  4801. {
  4802. if (operand->mode != Addressing_Type) {
  4803. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  4804. operand->mode = Addressing_Invalid;
  4805. operand->type = t_invalid;
  4806. return false;
  4807. }
  4808. Type *u = operand->type;
  4809. if (!is_type_union(u)) {
  4810. error(operand->expr, "Expected a union type for '%.*s'", LIT(builtin_name));
  4811. operand->mode = Addressing_Invalid;
  4812. operand->type = t_invalid;
  4813. return false;
  4814. }
  4815. Type *v = check_type(c, ce->args[1]);
  4816. u = base_type(u);
  4817. GB_ASSERT(u->kind == Type_Union);
  4818. bool is_variant = false;
  4819. for (Type *vt : u->Union.variants) {
  4820. if (are_types_identical(v, vt)) {
  4821. is_variant = true;
  4822. break;
  4823. }
  4824. }
  4825. operand->mode = Addressing_Constant;
  4826. operand->type = t_untyped_bool;
  4827. operand->value = exact_value_bool(is_variant);
  4828. }
  4829. break;
  4830. case BuiltinProc_type_union_tag_type:
  4831. {
  4832. if (operand->mode != Addressing_Type) {
  4833. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  4834. operand->mode = Addressing_Invalid;
  4835. operand->type = t_invalid;
  4836. return false;
  4837. }
  4838. Type *u = operand->type;
  4839. if (!is_type_union(u)) {
  4840. error(operand->expr, "Expected a union type for '%.*s'", LIT(builtin_name));
  4841. operand->mode = Addressing_Invalid;
  4842. operand->type = t_invalid;
  4843. return false;
  4844. }
  4845. u = base_type(u);
  4846. GB_ASSERT(u->kind == Type_Union);
  4847. operand->mode = Addressing_Type;
  4848. operand->type = union_tag_type(u);
  4849. }
  4850. break;
  4851. case BuiltinProc_type_union_tag_offset:
  4852. {
  4853. if (operand->mode != Addressing_Type) {
  4854. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  4855. operand->mode = Addressing_Invalid;
  4856. operand->type = t_invalid;
  4857. return false;
  4858. }
  4859. Type *u = operand->type;
  4860. if (!is_type_union(u)) {
  4861. error(operand->expr, "Expected a union type for '%.*s'", LIT(builtin_name));
  4862. operand->mode = Addressing_Invalid;
  4863. operand->type = t_invalid;
  4864. return false;
  4865. }
  4866. u = base_type(u);
  4867. GB_ASSERT(u->kind == Type_Union);
  4868. // NOTE(jakubtomsu): forces calculation of variant_block_size
  4869. type_size_of(u);
  4870. i64 tag_offset = u->Union.variant_block_size;
  4871. GB_ASSERT(tag_offset > 0);
  4872. operand->mode = Addressing_Constant;
  4873. operand->type = t_untyped_integer;
  4874. operand->value = exact_value_i64(tag_offset);
  4875. }
  4876. break;
  4877. case BuiltinProc_type_union_base_tag_value:
  4878. {
  4879. if (operand->mode != Addressing_Type) {
  4880. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  4881. operand->mode = Addressing_Invalid;
  4882. operand->type = t_invalid;
  4883. return false;
  4884. }
  4885. Type *u = operand->type;
  4886. if (!is_type_union(u)) {
  4887. error(operand->expr, "Expected a union type for '%.*s'", LIT(builtin_name));
  4888. operand->mode = Addressing_Invalid;
  4889. operand->type = t_invalid;
  4890. return false;
  4891. }
  4892. u = base_type(u);
  4893. GB_ASSERT(u->kind == Type_Union);
  4894. operand->mode = Addressing_Constant;
  4895. operand->type = t_untyped_integer;
  4896. operand->value = exact_value_i64(u->Union.kind == UnionType_no_nil ? 0 : 1);
  4897. } break;
  4898. case BuiltinProc_type_bit_set_elem_type:
  4899. {
  4900. if (operand->mode != Addressing_Type) {
  4901. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  4902. operand->mode = Addressing_Invalid;
  4903. operand->type = t_invalid;
  4904. return false;
  4905. }
  4906. Type *bs = operand->type;
  4907. if (!is_type_bit_set(bs)) {
  4908. error(operand->expr, "Expected a bit_set type for '%.*s'", LIT(builtin_name));
  4909. operand->mode = Addressing_Invalid;
  4910. operand->type = t_invalid;
  4911. return false;
  4912. }
  4913. bs = base_type(bs);
  4914. GB_ASSERT(bs->kind == Type_BitSet);
  4915. operand->mode = Addressing_Type;
  4916. operand->type = bs->BitSet.elem;
  4917. } break;
  4918. case BuiltinProc_type_bit_set_underlying_type:
  4919. {
  4920. if (operand->mode != Addressing_Type) {
  4921. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  4922. operand->mode = Addressing_Invalid;
  4923. operand->type = t_invalid;
  4924. return false;
  4925. }
  4926. Type *bs = operand->type;
  4927. if (!is_type_bit_set(bs)) {
  4928. error(operand->expr, "Expected a bit_set type for '%.*s'", LIT(builtin_name));
  4929. operand->mode = Addressing_Invalid;
  4930. operand->type = t_invalid;
  4931. return false;
  4932. }
  4933. bs = base_type(bs);
  4934. GB_ASSERT(bs->kind == Type_BitSet);
  4935. operand->mode = Addressing_Type;
  4936. operand->type = bit_set_to_int(bs);
  4937. } break;
  4938. case BuiltinProc_type_union_variant_count:
  4939. {
  4940. if (operand->mode != Addressing_Type) {
  4941. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  4942. operand->mode = Addressing_Invalid;
  4943. operand->type = t_invalid;
  4944. return false;
  4945. }
  4946. Type *u = operand->type;
  4947. if (!is_type_union(u)) {
  4948. error(operand->expr, "Expected a union type for '%.*s'", LIT(builtin_name));
  4949. operand->mode = Addressing_Invalid;
  4950. operand->type = t_invalid;
  4951. return false;
  4952. }
  4953. u = base_type(u);
  4954. GB_ASSERT(u->kind == Type_Union);
  4955. operand->mode = Addressing_Constant;
  4956. operand->type = t_untyped_integer;
  4957. operand->value = exact_value_i64(u->Union.variants.count);
  4958. } break;
  4959. case BuiltinProc_type_variant_type_of:
  4960. {
  4961. if (operand->mode != Addressing_Type) {
  4962. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  4963. operand->mode = Addressing_Invalid;
  4964. operand->type = t_invalid;
  4965. return false;
  4966. }
  4967. Type *u = operand->type;
  4968. if (!is_type_union(u)) {
  4969. error(operand->expr, "Expected a union type for '%.*s'", LIT(builtin_name));
  4970. operand->mode = Addressing_Invalid;
  4971. operand->type = t_invalid;
  4972. return false;
  4973. }
  4974. u = base_type(u);
  4975. GB_ASSERT(u->kind == Type_Union);
  4976. Operand x = {};
  4977. check_expr_or_type(c, &x, ce->args[1]);
  4978. if (!is_type_integer(x.type) || x.mode != Addressing_Constant) {
  4979. error(call, "Expected a constant integer for '%.*s", LIT(builtin_name));
  4980. operand->mode = Addressing_Type;
  4981. operand->type = t_invalid;
  4982. return false;
  4983. }
  4984. i64 index = big_int_to_i64(&x.value.value_integer);
  4985. if (index < 0 || index >= u->Union.variants.count) {
  4986. error(call, "Variant tag out of bounds index for '%.*s", LIT(builtin_name));
  4987. operand->mode = Addressing_Type;
  4988. operand->type = t_invalid;
  4989. return false;
  4990. }
  4991. operand->mode = Addressing_Type;
  4992. operand->type = u->Union.variants[index];
  4993. }
  4994. break;
  4995. case BuiltinProc_type_variant_index_of:
  4996. {
  4997. if (operand->mode != Addressing_Type) {
  4998. error(operand->expr, "Expected a type for '%.*s'", LIT(builtin_name));
  4999. operand->mode = Addressing_Invalid;
  5000. operand->type = t_invalid;
  5001. return false;
  5002. }
  5003. Type *u = operand->type;
  5004. if (!is_type_union(u)) {
  5005. error(operand->expr, "Expected a union type for '%.*s'", LIT(builtin_name));
  5006. operand->mode = Addressing_Invalid;
  5007. operand->type = t_invalid;
  5008. return false;
  5009. }
  5010. Type *v = check_type(c, ce->args[1]);
  5011. u = base_type(u);
  5012. GB_ASSERT(u->kind == Type_Union);
  5013. i64 index = -1;
  5014. for_array(i, u->Union.variants) {
  5015. Type *vt = u->Union.variants[i];
  5016. if (union_variant_index_types_equal(v, vt)) {
  5017. index = i64(i);
  5018. break;
  5019. }
  5020. }
  5021. if (index < 0) {
  5022. error(operand->expr, "Expected a variant type for '%.*s'", LIT(builtin_name));
  5023. operand->mode = Addressing_Invalid;
  5024. operand->type = t_invalid;
  5025. return false;
  5026. }
  5027. operand->mode = Addressing_Constant;
  5028. operand->type = t_untyped_integer;
  5029. operand->value = exact_value_i64(index);
  5030. }
  5031. break;
  5032. case BuiltinProc_type_struct_field_count:
  5033. operand->value = exact_value_i64(0);
  5034. if (operand->mode != Addressing_Type) {
  5035. error(operand->expr, "Expected a struct type for '%.*s'", LIT(builtin_name));
  5036. } else if (!is_type_struct(operand->type)) {
  5037. error(operand->expr, "Expected a struct type for '%.*s'", LIT(builtin_name));
  5038. } else {
  5039. Type *bt = base_type(operand->type);
  5040. operand->value = exact_value_i64(bt->Struct.fields.count);
  5041. }
  5042. operand->mode = Addressing_Constant;
  5043. operand->type = t_untyped_integer;
  5044. break;
  5045. case BuiltinProc_type_proc_parameter_count:
  5046. operand->value = exact_value_i64(0);
  5047. if (operand->mode != Addressing_Type) {
  5048. error(operand->expr, "Expected a procedure type for '%.*s'", LIT(builtin_name));
  5049. } else if (!is_type_proc(operand->type)) {
  5050. error(operand->expr, "Expected a procedure type for '%.*s'", LIT(builtin_name));
  5051. } else {
  5052. Type *bt = base_type(operand->type);
  5053. operand->value = exact_value_i64(bt->Proc.param_count);
  5054. }
  5055. operand->mode = Addressing_Constant;
  5056. operand->type = t_untyped_integer;
  5057. break;
  5058. case BuiltinProc_type_proc_return_count:
  5059. operand->value = exact_value_i64(0);
  5060. if (operand->mode != Addressing_Type) {
  5061. error(operand->expr, "Expected a procedure type for '%.*s'", LIT(builtin_name));
  5062. } else if (!is_type_proc(operand->type)) {
  5063. error(operand->expr, "Expected a procedure type for '%.*s'", LIT(builtin_name));
  5064. } else {
  5065. Type *bt = base_type(operand->type);
  5066. operand->value = exact_value_i64(bt->Proc.result_count);
  5067. }
  5068. operand->mode = Addressing_Constant;
  5069. operand->type = t_untyped_integer;
  5070. break;
  5071. case BuiltinProc_type_proc_parameter_type:
  5072. if (operand->mode != Addressing_Type || !is_type_proc(operand->type)) {
  5073. error(operand->expr, "Expected a procedure type for '%.*s'", LIT(builtin_name));
  5074. return false;
  5075. } else {
  5076. if (is_type_polymorphic(operand->type)) {
  5077. error(operand->expr, "Expected a non-polymorphic procedure type for '%.*s'", LIT(builtin_name));
  5078. return false;
  5079. }
  5080. Operand op = {};
  5081. check_expr(c, &op, ce->args[1]);
  5082. if (op.mode != Addressing_Constant || !is_type_integer(op.type)) {
  5083. error(op.expr, "Expected a constant integer for the index of procedure parameter value");
  5084. return false;
  5085. }
  5086. i64 index = exact_value_to_i64(op.value);
  5087. if (index < 0) {
  5088. error(op.expr, "Expected a non-negative integer for the index of procedure parameter value, got %lld", cast(long long)index);
  5089. return false;
  5090. }
  5091. Entity *param = nullptr;
  5092. i64 count = 0;
  5093. Type *bt = base_type(operand->type);
  5094. if (bt->kind == Type_Proc) {
  5095. count = bt->Proc.param_count;
  5096. if (index < count) {
  5097. param = bt->Proc.params->Tuple.variables[cast(isize)index];
  5098. }
  5099. }
  5100. if (index >= count) {
  5101. error(op.expr, "Index of procedure parameter value out of bounds, expected 0..<%lld, got %lld", cast(long long)count, cast(long long)index);
  5102. return false;
  5103. }
  5104. GB_ASSERT(param != nullptr);
  5105. switch (param->kind) {
  5106. case Entity_Constant:
  5107. operand->mode = Addressing_Constant;
  5108. operand->type = param->type;
  5109. operand->value = param->Constant.value;
  5110. break;
  5111. case Entity_TypeName:
  5112. case Entity_Variable:
  5113. operand->mode = Addressing_Type;
  5114. operand->type = param->type;
  5115. break;
  5116. default:
  5117. GB_PANIC("Unhandled procedure entity type %d", param->kind);
  5118. break;
  5119. }
  5120. }
  5121. break;
  5122. case BuiltinProc_type_proc_return_type:
  5123. if (operand->mode != Addressing_Type || !is_type_proc(operand->type)) {
  5124. error(operand->expr, "Expected a procedure type for '%.*s'", LIT(builtin_name));
  5125. return false;
  5126. } else {
  5127. if (is_type_polymorphic(operand->type)) {
  5128. error(operand->expr, "Expected a non-polymorphic procedure type for '%.*s'", LIT(builtin_name));
  5129. return false;
  5130. }
  5131. Operand op = {};
  5132. check_expr(c, &op, ce->args[1]);
  5133. if (op.mode != Addressing_Constant || !is_type_integer(op.type)) {
  5134. error(op.expr, "Expected a constant integer for the index of procedure parameter value");
  5135. return false;
  5136. }
  5137. i64 index = exact_value_to_i64(op.value);
  5138. if (index < 0) {
  5139. error(op.expr, "Expected a non-negative integer for the index of procedure parameter value, got %lld", cast(long long)index);
  5140. return false;
  5141. }
  5142. Entity *param = nullptr;
  5143. i64 count = 0;
  5144. Type *bt = base_type(operand->type);
  5145. if (bt->kind == Type_Proc) {
  5146. count = bt->Proc.result_count;
  5147. if (index < count) {
  5148. param = bt->Proc.results->Tuple.variables[cast(isize)index];
  5149. }
  5150. }
  5151. if (index >= count) {
  5152. error(op.expr, "Index of procedure parameter value out of bounds, expected 0..<%lld, got %lld", cast(long long)count, cast(long long)index);
  5153. return false;
  5154. }
  5155. GB_ASSERT(param != nullptr);
  5156. switch (param->kind) {
  5157. case Entity_Constant:
  5158. operand->mode = Addressing_Constant;
  5159. operand->type = param->type;
  5160. operand->value = param->Constant.value;
  5161. break;
  5162. case Entity_TypeName:
  5163. case Entity_Variable:
  5164. operand->mode = Addressing_Type;
  5165. operand->type = param->type;
  5166. break;
  5167. default:
  5168. GB_PANIC("Unhandled procedure entity type %d", param->kind);
  5169. break;
  5170. }
  5171. }
  5172. break;
  5173. case BuiltinProc_type_polymorphic_record_parameter_count:
  5174. operand->value = exact_value_i64(0);
  5175. if (operand->mode != Addressing_Type) {
  5176. error(operand->expr, "Expected a record type for '%.*s'", LIT(builtin_name));
  5177. } else {
  5178. Type *bt = base_type(operand->type);
  5179. if (bt->kind == Type_Struct) {
  5180. if (bt->Struct.polymorphic_params != nullptr) {
  5181. operand->value = exact_value_i64(bt->Struct.polymorphic_params->Tuple.variables.count);
  5182. }
  5183. } else if (bt->kind == Type_Union) {
  5184. if (bt->Union.polymorphic_params != nullptr) {
  5185. operand->value = exact_value_i64(bt->Union.polymorphic_params->Tuple.variables.count);
  5186. }
  5187. } else {
  5188. error(operand->expr, "Expected a record type for '%.*s'", LIT(builtin_name));
  5189. }
  5190. }
  5191. operand->mode = Addressing_Constant;
  5192. operand->type = t_untyped_integer;
  5193. break;
  5194. case BuiltinProc_type_polymorphic_record_parameter_value:
  5195. if (operand->mode != Addressing_Type) {
  5196. error(operand->expr, "Expected a record type for '%.*s'", LIT(builtin_name));
  5197. return false;
  5198. } else if (!is_type_polymorphic_record_specialized(operand->type)) {
  5199. error(operand->expr, "Expected a specialized polymorphic record type for '%.*s'", LIT(builtin_name));
  5200. return false;
  5201. } else {
  5202. Operand op = {};
  5203. check_expr(c, &op, ce->args[1]);
  5204. if (op.mode != Addressing_Constant || !is_type_integer(op.type)) {
  5205. error(op.expr, "Expected a constant integer for the index of record parameter value");
  5206. return false;
  5207. }
  5208. i64 index = exact_value_to_i64(op.value);
  5209. if (index < 0) {
  5210. error(op.expr, "Expected a non-negative integer for the index of record parameter value, got %lld", cast(long long)index);
  5211. return false;
  5212. }
  5213. Entity *param = nullptr;
  5214. i64 count = 0;
  5215. Type *bt = base_type(operand->type);
  5216. if (bt->kind == Type_Struct) {
  5217. if (bt->Struct.polymorphic_params != nullptr) {
  5218. count = bt->Struct.polymorphic_params->Tuple.variables.count;
  5219. if (index < count) {
  5220. param = bt->Struct.polymorphic_params->Tuple.variables[cast(isize)index];
  5221. }
  5222. }
  5223. } else if (bt->kind == Type_Union) {
  5224. if (bt->Union.polymorphic_params != nullptr) {
  5225. count = bt->Union.polymorphic_params->Tuple.variables.count;
  5226. if (index < count) {
  5227. param = bt->Union.polymorphic_params->Tuple.variables[cast(isize)index];
  5228. }
  5229. }
  5230. } else {
  5231. error(operand->expr, "Expected a specialized polymorphic record type for '%.*s'", LIT(builtin_name));
  5232. return false;
  5233. }
  5234. if (index >= count) {
  5235. error(op.expr, "Index of record parameter value out of bounds, expected 0..<%lld, got %lld", cast(long long)count, cast(long long)index);
  5236. return false;
  5237. }
  5238. GB_ASSERT(param != nullptr);
  5239. switch (param->kind) {
  5240. case Entity_Constant:
  5241. operand->mode = Addressing_Constant;
  5242. operand->type = param->type;
  5243. operand->value = param->Constant.value;
  5244. break;
  5245. case Entity_TypeName:
  5246. operand->mode = Addressing_Type;
  5247. operand->type = param->type;
  5248. break;
  5249. default:
  5250. GB_PANIC("Unhandled polymorphic record type");
  5251. break;
  5252. }
  5253. }
  5254. break;
  5255. case BuiltinProc_type_is_subtype_of:
  5256. {
  5257. Operand op_src = {};
  5258. Operand op_dst = {};
  5259. check_expr_or_type(c, &op_src, ce->args[0]);
  5260. if (op_src.mode != Addressing_Type) {
  5261. gbString e = expr_to_string(op_src.expr);
  5262. error(op_src.expr, "'%.*s' expects a type, got %s", LIT(builtin_name), e);
  5263. gb_string_free(e);
  5264. return false;
  5265. }
  5266. check_expr_or_type(c, &op_dst, ce->args[1]);
  5267. if (op_dst.mode != Addressing_Type) {
  5268. gbString e = expr_to_string(op_dst.expr);
  5269. error(op_dst.expr, "'%.*s' expects a type, got %s", LIT(builtin_name), e);
  5270. gb_string_free(e);
  5271. return false;
  5272. }
  5273. operand->value = exact_value_bool(is_type_subtype_of_and_allow_polymorphic(op_src.type, op_dst.type));
  5274. operand->mode = Addressing_Constant;
  5275. operand->type = t_untyped_bool;
  5276. } break;
  5277. case BuiltinProc_type_field_index_of:
  5278. {
  5279. Operand op = {};
  5280. Type *bt = check_type(c, ce->args[0]);
  5281. Type *type = base_type(bt);
  5282. if (type == nullptr || type == t_invalid) {
  5283. error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
  5284. return false;
  5285. }
  5286. Operand x = {};
  5287. check_expr(c, &x, ce->args[1]);
  5288. if (!is_type_string(x.type) || x.mode != Addressing_Constant || x.value.kind != ExactValue_String) {
  5289. error(ce->args[1], "Expected a const string for field argument");
  5290. return false;
  5291. }
  5292. String field_name = x.value.value_string;
  5293. Selection sel = lookup_field(type, field_name, false);
  5294. if (sel.entity == nullptr) {
  5295. ERROR_BLOCK();
  5296. gbString type_str = type_to_string(bt);
  5297. error(ce->args[0],
  5298. "'%s' has no field named '%.*s'", type_str, LIT(field_name));
  5299. gb_string_free(type_str);
  5300. if (bt->kind == Type_Struct) {
  5301. check_did_you_mean_type(field_name, bt->Struct.fields);
  5302. }
  5303. return false;
  5304. }
  5305. if (sel.indirect) {
  5306. gbString type_str = type_to_string(bt);
  5307. error(ce->args[0],
  5308. "Field '%.*s' is embedded via a pointer in '%s'", LIT(field_name), type_str);
  5309. gb_string_free(type_str);
  5310. return false;
  5311. }
  5312. operand->mode = Addressing_Constant;
  5313. operand->value = exact_value_u64(sel.index[0]);
  5314. operand->type = t_uintptr;
  5315. break;
  5316. }
  5317. break;
  5318. case BuiltinProc_type_bit_set_backing_type:
  5319. {
  5320. Operand op = {};
  5321. Type *type = check_type(c, ce->args[0]);
  5322. Type *bt = base_type(type);
  5323. if (bt == nullptr || bt == t_invalid) {
  5324. error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
  5325. return false;
  5326. }
  5327. if (bt->kind != Type_BitSet) {
  5328. gbString s = type_to_string(type);
  5329. error(ce->args[0], "Expected a bit_set type for '%.*s', got %s", LIT(builtin_name), s);
  5330. return false;
  5331. }
  5332. operand->mode = Addressing_Type;
  5333. operand->type = bit_set_to_int(bt);
  5334. break;
  5335. }
  5336. case BuiltinProc_type_equal_proc:
  5337. {
  5338. Operand op = {};
  5339. Type *bt = check_type(c, ce->args[0]);
  5340. Type *type = base_type(bt);
  5341. if (type == nullptr || type == t_invalid) {
  5342. error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
  5343. return false;
  5344. }
  5345. if (!is_type_comparable(type)) {
  5346. gbString t = type_to_string(type);
  5347. error(ce->args[0], "Expected a comparable type for '%.*s', got %s", LIT(builtin_name), t);
  5348. gb_string_free(t);
  5349. return false;
  5350. }
  5351. operand->mode = Addressing_Value;
  5352. operand->type = t_equal_proc;
  5353. break;
  5354. }
  5355. case BuiltinProc_type_hasher_proc:
  5356. {
  5357. Operand op = {};
  5358. Type *bt = check_type(c, ce->args[0]);
  5359. Type *type = base_type(bt);
  5360. if (type == nullptr || type == t_invalid) {
  5361. error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
  5362. return false;
  5363. }
  5364. if (!is_type_valid_for_keys(type)) {
  5365. gbString t = type_to_string(type);
  5366. error(ce->args[0], "Expected a valid type for map keys for '%.*s', got %s", LIT(builtin_name), t);
  5367. gb_string_free(t);
  5368. return false;
  5369. }
  5370. add_map_key_type_dependencies(c, type);
  5371. operand->mode = Addressing_Value;
  5372. operand->type = t_hasher_proc;
  5373. break;
  5374. }
  5375. case BuiltinProc_type_map_info:
  5376. {
  5377. Operand op = {};
  5378. Type *bt = check_type(c, ce->args[0]);
  5379. Type *type = base_type(bt);
  5380. if (type == nullptr || type == t_invalid) {
  5381. error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
  5382. return false;
  5383. }
  5384. if (!is_type_map(type)) {
  5385. gbString t = type_to_string(type);
  5386. error(ce->args[0], "Expected a map type for '%.*s', got %s", LIT(builtin_name), t);
  5387. gb_string_free(t);
  5388. return false;
  5389. }
  5390. add_map_key_type_dependencies(c, type);
  5391. operand->mode = Addressing_Value;
  5392. operand->type = t_map_info_ptr;
  5393. break;
  5394. }
  5395. case BuiltinProc_type_map_cell_info:
  5396. {
  5397. Operand op = {};
  5398. Type *bt = check_type(c, ce->args[0]);
  5399. Type *type = base_type(bt);
  5400. if (type == nullptr || type == t_invalid) {
  5401. error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
  5402. return false;
  5403. }
  5404. operand->mode = Addressing_Value;
  5405. operand->type = t_map_cell_info_ptr;
  5406. break;
  5407. }
  5408. case BuiltinProc_constant_utf16_cstring:
  5409. {
  5410. String value = {};
  5411. if (!is_constant_string(c, builtin_name, ce->args[0], &value)) {
  5412. return false;
  5413. }
  5414. operand->mode = Addressing_Value;
  5415. operand->type = alloc_type_multi_pointer(t_u16);
  5416. operand->value = {};
  5417. break;
  5418. }
  5419. case BuiltinProc_wasm_memory_grow:
  5420. {
  5421. if (!is_arch_wasm()) {
  5422. error(call, "'%.*s' is only allowed on wasm targets", LIT(builtin_name));
  5423. return false;
  5424. }
  5425. Operand index = {};
  5426. Operand delta = {};
  5427. check_expr(c, &index, ce->args[0]); if (index.mode == Addressing_Invalid) return false;
  5428. check_expr(c, &delta, ce->args[1]); if (delta.mode == Addressing_Invalid) return false;
  5429. convert_to_typed(c, &index, t_uintptr); if (index.mode == Addressing_Invalid) return false;
  5430. convert_to_typed(c, &delta, t_uintptr); if (delta.mode == Addressing_Invalid) return false;
  5431. if (!is_operand_value(index) || !check_is_assignable_to(c, &index, t_uintptr)) {
  5432. gbString e = expr_to_string(index.expr);
  5433. gbString t = type_to_string(index.type);
  5434. error(index.expr, "'%.*s' expected a uintptr for the memory index, got '%s' of type %s", LIT(builtin_name), e, t);
  5435. gb_string_free(t);
  5436. gb_string_free(e);
  5437. return false;
  5438. }
  5439. if (!is_operand_value(delta) || !check_is_assignable_to(c, &delta, t_uintptr)) {
  5440. gbString e = expr_to_string(delta.expr);
  5441. gbString t = type_to_string(delta.type);
  5442. error(delta.expr, "'%.*s' expected a uintptr for the memory delta, got '%s' of type %s", LIT(builtin_name), e, t);
  5443. gb_string_free(t);
  5444. gb_string_free(e);
  5445. return false;
  5446. }
  5447. operand->mode = Addressing_Value;
  5448. operand->type = t_int;
  5449. operand->value = {};
  5450. break;
  5451. }
  5452. break;
  5453. case BuiltinProc_wasm_memory_size:
  5454. {
  5455. if (!is_arch_wasm()) {
  5456. error(call, "'%.*s' is only allowed on wasm targets", LIT(builtin_name));
  5457. return false;
  5458. }
  5459. Operand index = {};
  5460. check_expr(c, &index, ce->args[0]); if (index.mode == Addressing_Invalid) return false;
  5461. convert_to_typed(c, &index, t_uintptr); if (index.mode == Addressing_Invalid) return false;
  5462. if (!is_operand_value(index) || !check_is_assignable_to(c, &index, t_uintptr)) {
  5463. gbString e = expr_to_string(index.expr);
  5464. gbString t = type_to_string(index.type);
  5465. error(index.expr, "'%.*s' expected a uintptr for the memory index, got '%s' of type %s", LIT(builtin_name), e, t);
  5466. gb_string_free(t);
  5467. gb_string_free(e);
  5468. return false;
  5469. }
  5470. operand->mode = Addressing_Value;
  5471. operand->type = t_int;
  5472. operand->value = {};
  5473. break;
  5474. }
  5475. break;
  5476. case BuiltinProc_wasm_memory_atomic_wait32:
  5477. {
  5478. if (!is_arch_wasm()) {
  5479. error(call, "'%.*s' is only allowed on wasm targets", LIT(builtin_name));
  5480. return false;
  5481. }
  5482. if (!check_target_feature_is_enabled(str_lit("atomics"), nullptr)) {
  5483. error(call, "'%.*s' requires target feature 'atomics' to be enabled, enable it with -target-features:\"atomics\" or choose a different -microarch", LIT(builtin_name));
  5484. return false;
  5485. }
  5486. Operand ptr = {};
  5487. Operand expected = {};
  5488. Operand timeout = {};
  5489. check_expr(c, &ptr, ce->args[0]); if (ptr.mode == Addressing_Invalid) return false;
  5490. check_expr(c, &expected, ce->args[1]); if (expected.mode == Addressing_Invalid) return false;
  5491. check_expr(c, &timeout, ce->args[2]); if (timeout.mode == Addressing_Invalid) return false;
  5492. Type *t_u32_ptr = alloc_type_pointer(t_u32);
  5493. convert_to_typed(c, &ptr, t_u32_ptr); if (ptr.mode == Addressing_Invalid) return false;
  5494. convert_to_typed(c, &expected, t_u32); if (expected.mode == Addressing_Invalid) return false;
  5495. convert_to_typed(c, &timeout, t_i64); if (timeout.mode == Addressing_Invalid) return false;
  5496. if (!is_operand_value(ptr) || !check_is_assignable_to(c, &ptr, t_u32_ptr)) {
  5497. gbString e = expr_to_string(ptr.expr);
  5498. gbString t = type_to_string(ptr.type);
  5499. error(ptr.expr, "'%.*s' expected ^u32 for the memory pointer, got '%s' of type %s", LIT(builtin_name), e, t);
  5500. gb_string_free(t);
  5501. gb_string_free(e);
  5502. return false;
  5503. }
  5504. if (!is_operand_value(expected) || !check_is_assignable_to(c, &expected, t_u32)) {
  5505. gbString e = expr_to_string(expected.expr);
  5506. gbString t = type_to_string(expected.type);
  5507. error(expected.expr, "'%.*s' expected u32 for the 'expected' value, got '%s' of type %s", LIT(builtin_name), e, t);
  5508. gb_string_free(t);
  5509. gb_string_free(e);
  5510. return false;
  5511. }
  5512. if (!is_operand_value(timeout) || !check_is_assignable_to(c, &timeout, t_i64)) {
  5513. gbString e = expr_to_string(timeout.expr);
  5514. gbString t = type_to_string(timeout.type);
  5515. error(timeout.expr, "'%.*s' expected i64 for the timeout, got '%s' of type %s", LIT(builtin_name), e, t);
  5516. gb_string_free(t);
  5517. gb_string_free(e);
  5518. return false;
  5519. }
  5520. operand->mode = Addressing_Value;
  5521. operand->type = t_u32;
  5522. operand->value = {};
  5523. break;
  5524. }
  5525. break;
  5526. case BuiltinProc_wasm_memory_atomic_notify32:
  5527. {
  5528. if (!is_arch_wasm()) {
  5529. error(call, "'%.*s' is only allowed on wasm targets", LIT(builtin_name));
  5530. return false;
  5531. }
  5532. if (!check_target_feature_is_enabled(str_lit("atomics"), nullptr)) {
  5533. error(call, "'%.*s' requires target feature 'atomics' to be enabled, enable it with -target-features:\"atomics\" or choose a different -microarch", LIT(builtin_name));
  5534. return false;
  5535. }
  5536. Operand ptr = {};
  5537. Operand waiters = {};
  5538. check_expr(c, &ptr, ce->args[0]); if (ptr.mode == Addressing_Invalid) return false;
  5539. check_expr(c, &waiters, ce->args[1]); if (waiters.mode == Addressing_Invalid) return false;
  5540. Type *t_u32_ptr = alloc_type_pointer(t_u32);
  5541. convert_to_typed(c, &ptr, t_u32_ptr); if (ptr.mode == Addressing_Invalid) return false;
  5542. convert_to_typed(c, &waiters, t_u32); if (waiters.mode == Addressing_Invalid) return false;
  5543. if (!is_operand_value(ptr) || !check_is_assignable_to(c, &ptr, t_u32_ptr)) {
  5544. gbString e = expr_to_string(ptr.expr);
  5545. gbString t = type_to_string(ptr.type);
  5546. error(ptr.expr, "'%.*s' expected ^u32 for the memory pointer, got '%s' of type %s", LIT(builtin_name), e, t);
  5547. gb_string_free(t);
  5548. gb_string_free(e);
  5549. return false;
  5550. }
  5551. if (!is_operand_value(waiters) || !check_is_assignable_to(c, &waiters, t_u32)) {
  5552. gbString e = expr_to_string(waiters.expr);
  5553. gbString t = type_to_string(waiters.type);
  5554. error(waiters.expr, "'%.*s' expected u32 for the 'waiters' value, got '%s' of type %s", LIT(builtin_name), e, t);
  5555. gb_string_free(t);
  5556. gb_string_free(e);
  5557. return false;
  5558. }
  5559. operand->mode = Addressing_Value;
  5560. operand->type = t_u32;
  5561. operand->value = {};
  5562. break;
  5563. }
  5564. break;
  5565. case BuiltinProc_x86_cpuid:
  5566. {
  5567. if (!is_arch_x86()) {
  5568. error(call, "'%.*s' is only allowed on x86 targets (i386, amd64)", LIT(builtin_name));
  5569. return false;
  5570. }
  5571. Operand ax = {};
  5572. Operand cx = {};
  5573. check_expr_with_type_hint(c, &ax, ce->args[0], t_u32); if (ax.mode == Addressing_Invalid) return false;
  5574. check_expr_with_type_hint(c, &cx, ce->args[1], t_u32); if (cx.mode == Addressing_Invalid) return false;
  5575. convert_to_typed(c, &ax, t_u32); if (ax.mode == Addressing_Invalid) return false;
  5576. convert_to_typed(c, &cx, t_u32); if (cx.mode == Addressing_Invalid) return false;
  5577. if (!are_types_identical(ax.type, t_u32)) {
  5578. gbString str = type_to_string(ax.type);
  5579. error(ax.expr, "'%.*s' expected a u32, got %s", LIT(builtin_name), str);
  5580. gb_string_free(str);
  5581. return false;
  5582. }
  5583. if (!are_types_identical(cx.type, t_u32)) {
  5584. gbString str = type_to_string(cx.type);
  5585. error(cx.expr, "'%.*s' expected a u32, got %s", LIT(builtin_name), str);
  5586. gb_string_free(str);
  5587. return false;
  5588. }
  5589. Type *types[4] = {t_u32, t_u32, t_u32, t_u32}; // eax ebc ecx edx
  5590. operand->type = alloc_type_tuple_from_field_types(types, gb_count_of(types), false, false);
  5591. operand->mode = Addressing_Value;
  5592. operand->value = {};
  5593. return true;
  5594. }
  5595. break;
  5596. case BuiltinProc_x86_xgetbv:
  5597. {
  5598. if (!is_arch_x86()) {
  5599. error(call, "'%.*s' is only allowed on x86 targets (i386, amd64)", LIT(builtin_name));
  5600. return false;
  5601. }
  5602. Operand cx = {};
  5603. check_expr_with_type_hint(c, &cx, ce->args[0], t_u32); if (cx.mode == Addressing_Invalid) return false;
  5604. convert_to_typed(c, &cx, t_u32); if (cx.mode == Addressing_Invalid) return false;
  5605. if (!are_types_identical(cx.type, t_u32)) {
  5606. gbString str = type_to_string(cx.type);
  5607. error(cx.expr, "'%.*s' expected a u32, got %s", LIT(builtin_name), str);
  5608. gb_string_free(str);
  5609. return false;
  5610. }
  5611. Type *types[2] = {t_u32, t_u32};
  5612. operand->type = alloc_type_tuple_from_field_types(types, gb_count_of(types), false, false);
  5613. operand->mode = Addressing_Value;
  5614. operand->value = {};
  5615. return true;
  5616. }
  5617. break;
  5618. case BuiltinProc_valgrind_client_request:
  5619. {
  5620. // NOTE(bill): Check it but make it a no-op for non x86 (i386, amd64) targets
  5621. enum {ARG_COUNT = 7};
  5622. GB_ASSERT(builtin_procs[BuiltinProc_valgrind_client_request].arg_count == ARG_COUNT);
  5623. Operand operands[ARG_COUNT] = {};
  5624. for (isize i = 0; i < ARG_COUNT; i++) {
  5625. Operand *op = &operands[i];
  5626. check_expr_with_type_hint(c, op, ce->args[i], t_uintptr);
  5627. if (op->mode == Addressing_Invalid) {
  5628. return false;
  5629. }
  5630. convert_to_typed(c, op, t_uintptr);
  5631. if (op->mode == Addressing_Invalid) {
  5632. return false;
  5633. }
  5634. if (!are_types_identical(op->type, t_uintptr)) {
  5635. gbString str = type_to_string(op->type);
  5636. error(op->expr, "'%.*s' expected a uintptr, got %s", LIT(builtin_name), str);
  5637. gb_string_free(str);
  5638. return false;
  5639. }
  5640. }
  5641. operand->type = t_uintptr;
  5642. operand->mode = Addressing_Value;
  5643. operand->value = {};
  5644. return true;
  5645. }
  5646. }
  5647. return true;
  5648. }