check_builtin.cpp 174 KB

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