check_builtin.cpp 209 KB

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