tccgen.c 216 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370
  1. /*
  2. * TCC - Tiny C Compiler
  3. *
  4. * Copyright (c) 2001-2004 Fabrice Bellard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include "tcc.h"
  21. /********************************************************/
  22. /* global variables */
  23. /* loc : local variable index
  24. ind : output code index
  25. rsym: return symbol
  26. anon_sym: anonymous symbol index
  27. */
  28. ST_DATA int rsym, anon_sym, ind, loc;
  29. ST_DATA Sym *sym_free_first;
  30. ST_DATA void **sym_pools;
  31. ST_DATA int nb_sym_pools;
  32. ST_DATA Sym *global_stack;
  33. ST_DATA Sym *local_stack;
  34. ST_DATA Sym *define_stack;
  35. ST_DATA Sym *global_label_stack;
  36. ST_DATA Sym *local_label_stack;
  37. static int local_scope;
  38. static int in_sizeof;
  39. static int section_sym;
  40. ST_DATA int vlas_in_scope; /* number of VLAs that are currently in scope */
  41. ST_DATA int vla_sp_root_loc; /* vla_sp_loc for SP before any VLAs were pushed */
  42. ST_DATA int vla_sp_loc; /* Pointer to variable holding location to store stack pointer on the stack when modifying stack pointer */
  43. ST_DATA SValue __vstack[1+VSTACK_SIZE], *vtop, *pvtop;
  44. ST_DATA int const_wanted; /* true if constant wanted */
  45. ST_DATA int nocode_wanted; /* no code generation wanted */
  46. #define NODATA_WANTED (nocode_wanted > 0) /* no static data output wanted either */
  47. #define STATIC_DATA_WANTED (nocode_wanted & 0xC0000000) /* only static data output */
  48. ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */
  49. ST_DATA CType func_vt; /* current function return type (used by return instruction) */
  50. ST_DATA int func_var; /* true if current function is variadic (used by return instruction) */
  51. ST_DATA int func_vc;
  52. ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */
  53. ST_DATA const char *funcname;
  54. ST_DATA int g_debug;
  55. ST_DATA CType char_pointer_type, func_old_type, int_type, size_type, ptrdiff_type;
  56. ST_DATA struct switch_t {
  57. struct case_t {
  58. int64_t v1, v2;
  59. int sym;
  60. } **p; int n; /* list of case ranges */
  61. int def_sym; /* default symbol */
  62. } *cur_switch; /* current switch */
  63. /* ------------------------------------------------------------------------- */
  64. static void gen_cast(CType *type);
  65. static void gen_cast_s(int t);
  66. static inline CType *pointed_type(CType *type);
  67. static int is_compatible_types(CType *type1, CType *type2);
  68. static int parse_btype(CType *type, AttributeDef *ad);
  69. static CType *type_decl(CType *type, AttributeDef *ad, int *v, int td);
  70. static void parse_expr_type(CType *type);
  71. static void init_putv(CType *type, Section *sec, unsigned long c);
  72. static void decl_initializer(CType *type, Section *sec, unsigned long c, int first, int size_only);
  73. static void block(int *bsym, int *csym, int is_expr);
  74. static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, int has_init, int v, int scope);
  75. static void decl(int l);
  76. static int decl0(int l, int is_for_loop_init, Sym *);
  77. static void expr_eq(void);
  78. static void vla_runtime_type_size(CType *type, int *a);
  79. static void vla_sp_restore(void);
  80. static void vla_sp_restore_root(void);
  81. static int is_compatible_unqualified_types(CType *type1, CType *type2);
  82. static inline int64_t expr_const64(void);
  83. static void vpush64(int ty, unsigned long long v);
  84. static void vpush(CType *type);
  85. static int gvtst(int inv, int t);
  86. static void gen_inline_functions(TCCState *s);
  87. static void skip_or_save_block(TokenString **str);
  88. static void gv_dup(void);
  89. ST_INLN int is_float(int t)
  90. {
  91. int bt;
  92. bt = t & VT_BTYPE;
  93. return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT || bt == VT_QFLOAT;
  94. }
  95. /* we use our own 'finite' function to avoid potential problems with
  96. non standard math libs */
  97. /* XXX: endianness dependent */
  98. ST_FUNC int ieee_finite(double d)
  99. {
  100. int p[4];
  101. memcpy(p, &d, sizeof(double));
  102. return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
  103. }
  104. /* compiling intel long double natively */
  105. #if (defined __i386__ || defined __x86_64__) \
  106. && (defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64)
  107. # define TCC_IS_NATIVE_387
  108. #endif
  109. ST_FUNC void test_lvalue(void)
  110. {
  111. if (!(vtop->r & VT_LVAL))
  112. expect("lvalue");
  113. }
  114. ST_FUNC void check_vstack(void)
  115. {
  116. if (pvtop != vtop)
  117. tcc_error("internal compiler error: vstack leak (%d)", vtop - pvtop);
  118. }
  119. /* ------------------------------------------------------------------------- */
  120. /* vstack debugging aid */
  121. #if 0
  122. void pv (const char *lbl, int a, int b)
  123. {
  124. int i;
  125. for (i = a; i < a + b; ++i) {
  126. SValue *p = &vtop[-i];
  127. printf("%s vtop[-%d] : type.t:%04x r:%04x r2:%04x c.i:%d\n",
  128. lbl, i, p->type.t, p->r, p->r2, (int)p->c.i);
  129. }
  130. }
  131. #endif
  132. /* ------------------------------------------------------------------------- */
  133. /* start of translation unit info */
  134. ST_FUNC void tcc_debug_start(TCCState *s1)
  135. {
  136. if (s1->do_debug) {
  137. char buf[512];
  138. /* file info: full path + filename */
  139. section_sym = put_elf_sym(symtab_section, 0, 0,
  140. ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0,
  141. text_section->sh_num, NULL);
  142. getcwd(buf, sizeof(buf));
  143. #ifdef _WIN32
  144. normalize_slashes(buf);
  145. #endif
  146. pstrcat(buf, sizeof(buf), "/");
  147. put_stabs_r(buf, N_SO, 0, 0,
  148. text_section->data_offset, text_section, section_sym);
  149. put_stabs_r(file->filename, N_SO, 0, 0,
  150. text_section->data_offset, text_section, section_sym);
  151. last_ind = 0;
  152. last_line_num = 0;
  153. }
  154. /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
  155. symbols can be safely used */
  156. put_elf_sym(symtab_section, 0, 0,
  157. ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0,
  158. SHN_ABS, file->filename);
  159. }
  160. /* put end of translation unit info */
  161. ST_FUNC void tcc_debug_end(TCCState *s1)
  162. {
  163. if (!s1->do_debug)
  164. return;
  165. put_stabs_r(NULL, N_SO, 0, 0,
  166. text_section->data_offset, text_section, section_sym);
  167. }
  168. /* generate line number info */
  169. ST_FUNC void tcc_debug_line(TCCState *s1)
  170. {
  171. if (!s1->do_debug)
  172. return;
  173. if ((last_line_num != file->line_num || last_ind != ind)) {
  174. put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
  175. last_ind = ind;
  176. last_line_num = file->line_num;
  177. }
  178. }
  179. /* put function symbol */
  180. ST_FUNC void tcc_debug_funcstart(TCCState *s1, Sym *sym)
  181. {
  182. char buf[512];
  183. if (!s1->do_debug)
  184. return;
  185. /* stabs info */
  186. /* XXX: we put here a dummy type */
  187. snprintf(buf, sizeof(buf), "%s:%c1",
  188. funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
  189. put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
  190. cur_text_section, sym->c);
  191. /* //gr gdb wants a line at the function */
  192. put_stabn(N_SLINE, 0, file->line_num, 0);
  193. last_ind = 0;
  194. last_line_num = 0;
  195. }
  196. /* put function size */
  197. ST_FUNC void tcc_debug_funcend(TCCState *s1, int size)
  198. {
  199. if (!s1->do_debug)
  200. return;
  201. put_stabn(N_FUN, 0, 0, size);
  202. }
  203. /* ------------------------------------------------------------------------- */
  204. ST_FUNC int tccgen_compile(TCCState *s1)
  205. {
  206. cur_text_section = NULL;
  207. funcname = "";
  208. anon_sym = SYM_FIRST_ANOM;
  209. section_sym = 0;
  210. const_wanted = 0;
  211. nocode_wanted = 0x80000000;
  212. /* define some often used types */
  213. int_type.t = VT_INT;
  214. char_pointer_type.t = VT_BYTE;
  215. mk_pointer(&char_pointer_type);
  216. #if PTR_SIZE == 4
  217. size_type.t = VT_INT | VT_UNSIGNED;
  218. ptrdiff_type.t = VT_INT;
  219. #elif LONG_SIZE == 4
  220. size_type.t = VT_LLONG | VT_UNSIGNED;
  221. ptrdiff_type.t = VT_LLONG;
  222. #else
  223. size_type.t = VT_LONG | VT_LLONG | VT_UNSIGNED;
  224. ptrdiff_type.t = VT_LONG | VT_LLONG;
  225. #endif
  226. func_old_type.t = VT_FUNC;
  227. func_old_type.ref = sym_push(SYM_FIELD, &int_type, 0, 0);
  228. func_old_type.ref->f.func_call = FUNC_CDECL;
  229. func_old_type.ref->f.func_type = FUNC_OLD;
  230. tcc_debug_start(s1);
  231. #ifdef TCC_TARGET_ARM
  232. arm_init(s1);
  233. #endif
  234. #ifdef INC_DEBUG
  235. printf("%s: **** new file\n", file->filename);
  236. #endif
  237. parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM | PARSE_FLAG_TOK_STR;
  238. next();
  239. decl(VT_CONST);
  240. gen_inline_functions(s1);
  241. check_vstack();
  242. /* end of translation unit info */
  243. tcc_debug_end(s1);
  244. return 0;
  245. }
  246. /* ------------------------------------------------------------------------- */
  247. /* apply storage attributes to Elf symbol */
  248. static void update_storage(Sym *sym)
  249. {
  250. ElfW(Sym) *esym;
  251. if (0 == sym->c)
  252. return;
  253. esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
  254. if (sym->a.visibility)
  255. esym->st_other = (esym->st_other & ~ELFW(ST_VISIBILITY)(-1))
  256. | sym->a.visibility;
  257. if (sym->a.weak)
  258. esym->st_info = ELFW(ST_INFO)(STB_WEAK, ELFW(ST_TYPE)(esym->st_info));
  259. #ifdef TCC_TARGET_PE
  260. if (sym->a.dllimport)
  261. esym->st_other |= ST_PE_IMPORT;
  262. if (sym->a.dllexport)
  263. esym->st_other |= ST_PE_EXPORT;
  264. #endif
  265. #if 0
  266. printf("storage %s: vis=%d weak=%d exp=%d imp=%d\n",
  267. get_tok_str(sym->v, NULL),
  268. sym->a.visibility,
  269. sym->a.weak,
  270. sym->a.dllexport,
  271. sym->a.dllimport
  272. );
  273. #endif
  274. }
  275. /* ------------------------------------------------------------------------- */
  276. /* update sym->c so that it points to an external symbol in section
  277. 'section' with value 'value' */
  278. ST_FUNC void put_extern_sym2(Sym *sym, Section *section,
  279. addr_t value, unsigned long size,
  280. int can_add_underscore)
  281. {
  282. int sym_type, sym_bind, sh_num, info, other, t;
  283. ElfW(Sym) *esym;
  284. const char *name;
  285. char buf1[256];
  286. #ifdef CONFIG_TCC_BCHECK
  287. char buf[32];
  288. #endif
  289. if (section == NULL)
  290. sh_num = SHN_UNDEF;
  291. else if (section == SECTION_ABS)
  292. sh_num = SHN_ABS;
  293. else
  294. sh_num = section->sh_num;
  295. if (!sym->c) {
  296. name = get_tok_str(sym->v, NULL);
  297. #ifdef CONFIG_TCC_BCHECK
  298. if (tcc_state->do_bounds_check) {
  299. /* XXX: avoid doing that for statics ? */
  300. /* if bound checking is activated, we change some function
  301. names by adding the "__bound" prefix */
  302. switch(sym->v) {
  303. #ifdef TCC_TARGET_PE
  304. /* XXX: we rely only on malloc hooks */
  305. case TOK_malloc:
  306. case TOK_free:
  307. case TOK_realloc:
  308. case TOK_memalign:
  309. case TOK_calloc:
  310. #endif
  311. case TOK_memcpy:
  312. case TOK_memmove:
  313. case TOK_memset:
  314. case TOK_strlen:
  315. case TOK_strcpy:
  316. case TOK_alloca:
  317. strcpy(buf, "__bound_");
  318. strcat(buf, name);
  319. name = buf;
  320. break;
  321. }
  322. }
  323. #endif
  324. t = sym->type.t;
  325. if ((t & VT_BTYPE) == VT_FUNC) {
  326. sym_type = STT_FUNC;
  327. } else if ((t & VT_BTYPE) == VT_VOID) {
  328. sym_type = STT_NOTYPE;
  329. } else {
  330. sym_type = STT_OBJECT;
  331. }
  332. if (t & VT_STATIC)
  333. sym_bind = STB_LOCAL;
  334. else
  335. sym_bind = STB_GLOBAL;
  336. other = 0;
  337. #ifdef TCC_TARGET_PE
  338. if (sym_type == STT_FUNC && sym->type.ref) {
  339. Sym *ref = sym->type.ref;
  340. if (ref->f.func_call == FUNC_STDCALL && can_add_underscore) {
  341. sprintf(buf1, "_%s@%d", name, ref->f.func_args * PTR_SIZE);
  342. name = buf1;
  343. other |= ST_PE_STDCALL;
  344. can_add_underscore = 0;
  345. }
  346. }
  347. #endif
  348. if (tcc_state->leading_underscore && can_add_underscore) {
  349. buf1[0] = '_';
  350. pstrcpy(buf1 + 1, sizeof(buf1) - 1, name);
  351. name = buf1;
  352. }
  353. if (sym->asm_label)
  354. name = get_tok_str(sym->asm_label, NULL);
  355. info = ELFW(ST_INFO)(sym_bind, sym_type);
  356. sym->c = set_elf_sym(symtab_section, value, size, info, other, sh_num, name);
  357. } else {
  358. esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
  359. esym->st_value = value;
  360. esym->st_size = size;
  361. esym->st_shndx = sh_num;
  362. }
  363. update_storage(sym);
  364. }
  365. ST_FUNC void put_extern_sym(Sym *sym, Section *section,
  366. addr_t value, unsigned long size)
  367. {
  368. put_extern_sym2(sym, section, value, size, 1);
  369. }
  370. /* add a new relocation entry to symbol 'sym' in section 's' */
  371. ST_FUNC void greloca(Section *s, Sym *sym, unsigned long offset, int type,
  372. addr_t addend)
  373. {
  374. int c = 0;
  375. if (nocode_wanted && s == cur_text_section)
  376. return;
  377. if (sym) {
  378. if (0 == sym->c)
  379. put_extern_sym(sym, NULL, 0, 0);
  380. c = sym->c;
  381. }
  382. /* now we can add ELF relocation info */
  383. put_elf_reloca(symtab_section, s, offset, type, c, addend);
  384. }
  385. #if PTR_SIZE == 4
  386. ST_FUNC void greloc(Section *s, Sym *sym, unsigned long offset, int type)
  387. {
  388. greloca(s, sym, offset, type, 0);
  389. }
  390. #endif
  391. /* ------------------------------------------------------------------------- */
  392. /* symbol allocator */
  393. static Sym *__sym_malloc(void)
  394. {
  395. Sym *sym_pool, *sym, *last_sym;
  396. int i;
  397. sym_pool = tcc_malloc(SYM_POOL_NB * sizeof(Sym));
  398. dynarray_add(&sym_pools, &nb_sym_pools, sym_pool);
  399. last_sym = sym_free_first;
  400. sym = sym_pool;
  401. for(i = 0; i < SYM_POOL_NB; i++) {
  402. sym->next = last_sym;
  403. last_sym = sym;
  404. sym++;
  405. }
  406. sym_free_first = last_sym;
  407. return last_sym;
  408. }
  409. static inline Sym *sym_malloc(void)
  410. {
  411. Sym *sym;
  412. #ifndef SYM_DEBUG
  413. sym = sym_free_first;
  414. if (!sym)
  415. sym = __sym_malloc();
  416. sym_free_first = sym->next;
  417. return sym;
  418. #else
  419. sym = tcc_malloc(sizeof(Sym));
  420. return sym;
  421. #endif
  422. }
  423. ST_INLN void sym_free(Sym *sym)
  424. {
  425. #ifndef SYM_DEBUG
  426. sym->next = sym_free_first;
  427. sym_free_first = sym;
  428. #else
  429. tcc_free(sym);
  430. #endif
  431. }
  432. /* push, without hashing */
  433. ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, int c)
  434. {
  435. Sym *s;
  436. s = sym_malloc();
  437. memset(s, 0, sizeof *s);
  438. s->v = v;
  439. s->type.t = t;
  440. s->c = c;
  441. /* add in stack */
  442. s->prev = *ps;
  443. *ps = s;
  444. return s;
  445. }
  446. /* find a symbol and return its associated structure. 's' is the top
  447. of the symbol stack */
  448. ST_FUNC Sym *sym_find2(Sym *s, int v)
  449. {
  450. while (s) {
  451. if (s->v == v)
  452. return s;
  453. else if (s->v == -1)
  454. return NULL;
  455. s = s->prev;
  456. }
  457. return NULL;
  458. }
  459. /* structure lookup */
  460. ST_INLN Sym *struct_find(int v)
  461. {
  462. v -= TOK_IDENT;
  463. if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
  464. return NULL;
  465. return table_ident[v]->sym_struct;
  466. }
  467. /* find an identifier */
  468. ST_INLN Sym *sym_find(int v)
  469. {
  470. v -= TOK_IDENT;
  471. if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
  472. return NULL;
  473. return table_ident[v]->sym_identifier;
  474. }
  475. /* push a given symbol on the symbol stack */
  476. ST_FUNC Sym *sym_push(int v, CType *type, int r, int c)
  477. {
  478. Sym *s, **ps;
  479. TokenSym *ts;
  480. if (local_stack)
  481. ps = &local_stack;
  482. else
  483. ps = &global_stack;
  484. s = sym_push2(ps, v, type->t, c);
  485. s->type.ref = type->ref;
  486. s->r = r;
  487. /* don't record fields or anonymous symbols */
  488. /* XXX: simplify */
  489. if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
  490. /* record symbol in token array */
  491. ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
  492. if (v & SYM_STRUCT)
  493. ps = &ts->sym_struct;
  494. else
  495. ps = &ts->sym_identifier;
  496. s->prev_tok = *ps;
  497. *ps = s;
  498. s->sym_scope = local_scope;
  499. if (s->prev_tok && s->prev_tok->sym_scope == s->sym_scope)
  500. tcc_error("redeclaration of '%s'",
  501. get_tok_str(v & ~SYM_STRUCT, NULL));
  502. }
  503. return s;
  504. }
  505. /* push a global identifier */
  506. ST_FUNC Sym *global_identifier_push(int v, int t, int c)
  507. {
  508. Sym *s, **ps;
  509. s = sym_push2(&global_stack, v, t, c);
  510. /* don't record anonymous symbol */
  511. if (v < SYM_FIRST_ANOM) {
  512. ps = &table_ident[v - TOK_IDENT]->sym_identifier;
  513. /* modify the top most local identifier, so that
  514. sym_identifier will point to 's' when popped */
  515. while (*ps != NULL)
  516. ps = &(*ps)->prev_tok;
  517. s->prev_tok = NULL;
  518. *ps = s;
  519. }
  520. return s;
  521. }
  522. /* pop symbols until top reaches 'b'. If KEEP is non-zero don't really
  523. pop them yet from the list, but do remove them from the token array. */
  524. ST_FUNC void sym_pop(Sym **ptop, Sym *b, int keep)
  525. {
  526. Sym *s, *ss, **ps;
  527. TokenSym *ts;
  528. int v;
  529. s = *ptop;
  530. while(s != b) {
  531. ss = s->prev;
  532. v = s->v;
  533. /* remove symbol in token array */
  534. /* XXX: simplify */
  535. if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
  536. ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
  537. if (v & SYM_STRUCT)
  538. ps = &ts->sym_struct;
  539. else
  540. ps = &ts->sym_identifier;
  541. *ps = s->prev_tok;
  542. }
  543. if (!keep)
  544. sym_free(s);
  545. s = ss;
  546. }
  547. if (!keep)
  548. *ptop = b;
  549. }
  550. /* ------------------------------------------------------------------------- */
  551. static void vsetc(CType *type, int r, CValue *vc)
  552. {
  553. int v;
  554. if (vtop >= vstack + (VSTACK_SIZE - 1))
  555. tcc_error("memory full (vstack)");
  556. /* cannot let cpu flags if other instruction are generated. Also
  557. avoid leaving VT_JMP anywhere except on the top of the stack
  558. because it would complicate the code generator.
  559. Don't do this when nocode_wanted. vtop might come from
  560. !nocode_wanted regions (see 88_codeopt.c) and transforming
  561. it to a register without actually generating code is wrong
  562. as their value might still be used for real. All values
  563. we push under nocode_wanted will eventually be popped
  564. again, so that the VT_CMP/VT_JMP value will be in vtop
  565. when code is unsuppressed again.
  566. Same logic below in vswap(); */
  567. if (vtop >= vstack && !nocode_wanted) {
  568. v = vtop->r & VT_VALMASK;
  569. if (v == VT_CMP || (v & ~1) == VT_JMP)
  570. gv(RC_INT);
  571. }
  572. vtop++;
  573. vtop->type = *type;
  574. vtop->r = r;
  575. vtop->r2 = VT_CONST;
  576. vtop->c = *vc;
  577. vtop->sym = NULL;
  578. }
  579. ST_FUNC void vswap(void)
  580. {
  581. SValue tmp;
  582. /* cannot vswap cpu flags. See comment at vsetc() above */
  583. if (vtop >= vstack && !nocode_wanted) {
  584. int v = vtop->r & VT_VALMASK;
  585. if (v == VT_CMP || (v & ~1) == VT_JMP)
  586. gv(RC_INT);
  587. }
  588. tmp = vtop[0];
  589. vtop[0] = vtop[-1];
  590. vtop[-1] = tmp;
  591. }
  592. /* pop stack value */
  593. ST_FUNC void vpop(void)
  594. {
  595. int v;
  596. v = vtop->r & VT_VALMASK;
  597. #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
  598. /* for x86, we need to pop the FP stack */
  599. if (v == TREG_ST0) {
  600. o(0xd8dd); /* fstp %st(0) */
  601. } else
  602. #endif
  603. if (v == VT_JMP || v == VT_JMPI) {
  604. /* need to put correct jump if && or || without test */
  605. gsym(vtop->c.i);
  606. }
  607. vtop--;
  608. }
  609. /* push constant of type "type" with useless value */
  610. ST_FUNC void vpush(CType *type)
  611. {
  612. vset(type, VT_CONST, 0);
  613. }
  614. /* push integer constant */
  615. ST_FUNC void vpushi(int v)
  616. {
  617. CValue cval;
  618. cval.i = v;
  619. vsetc(&int_type, VT_CONST, &cval);
  620. }
  621. /* push a pointer sized constant */
  622. static void vpushs(addr_t v)
  623. {
  624. CValue cval;
  625. cval.i = v;
  626. vsetc(&size_type, VT_CONST, &cval);
  627. }
  628. /* push arbitrary 64bit constant */
  629. ST_FUNC void vpush64(int ty, unsigned long long v)
  630. {
  631. CValue cval;
  632. CType ctype;
  633. ctype.t = ty;
  634. ctype.ref = NULL;
  635. cval.i = v;
  636. vsetc(&ctype, VT_CONST, &cval);
  637. }
  638. /* push long long constant */
  639. static inline void vpushll(long long v)
  640. {
  641. vpush64(VT_LLONG, v);
  642. }
  643. ST_FUNC void vset(CType *type, int r, int v)
  644. {
  645. CValue cval;
  646. cval.i = v;
  647. vsetc(type, r, &cval);
  648. }
  649. static void vseti(int r, int v)
  650. {
  651. CType type;
  652. type.t = VT_INT;
  653. type.ref = NULL;
  654. vset(&type, r, v);
  655. }
  656. ST_FUNC void vpushv(SValue *v)
  657. {
  658. if (vtop >= vstack + (VSTACK_SIZE - 1))
  659. tcc_error("memory full (vstack)");
  660. vtop++;
  661. *vtop = *v;
  662. }
  663. static void vdup(void)
  664. {
  665. vpushv(vtop);
  666. }
  667. /* rotate n first stack elements to the bottom
  668. I1 ... In -> I2 ... In I1 [top is right]
  669. */
  670. ST_FUNC void vrotb(int n)
  671. {
  672. int i;
  673. SValue tmp;
  674. tmp = vtop[-n + 1];
  675. for(i=-n+1;i!=0;i++)
  676. vtop[i] = vtop[i+1];
  677. vtop[0] = tmp;
  678. }
  679. /* rotate the n elements before entry e towards the top
  680. I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
  681. */
  682. ST_FUNC void vrote(SValue *e, int n)
  683. {
  684. int i;
  685. SValue tmp;
  686. tmp = *e;
  687. for(i = 0;i < n - 1; i++)
  688. e[-i] = e[-i - 1];
  689. e[-n + 1] = tmp;
  690. }
  691. /* rotate n first stack elements to the top
  692. I1 ... In -> In I1 ... I(n-1) [top is right]
  693. */
  694. ST_FUNC void vrott(int n)
  695. {
  696. vrote(vtop, n);
  697. }
  698. /* push a symbol value of TYPE */
  699. static inline void vpushsym(CType *type, Sym *sym)
  700. {
  701. CValue cval;
  702. cval.i = 0;
  703. vsetc(type, VT_CONST | VT_SYM, &cval);
  704. vtop->sym = sym;
  705. }
  706. /* Return a static symbol pointing to a section */
  707. ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
  708. {
  709. int v;
  710. Sym *sym;
  711. v = anon_sym++;
  712. sym = global_identifier_push(v, type->t | VT_STATIC, 0);
  713. sym->type.ref = type->ref;
  714. sym->r = VT_CONST | VT_SYM;
  715. put_extern_sym(sym, sec, offset, size);
  716. return sym;
  717. }
  718. /* push a reference to a section offset by adding a dummy symbol */
  719. static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
  720. {
  721. vpushsym(type, get_sym_ref(type, sec, offset, size));
  722. }
  723. /* define a new external reference to a symbol 'v' of type 'u' */
  724. ST_FUNC Sym *external_global_sym(int v, CType *type, int r)
  725. {
  726. Sym *s;
  727. s = sym_find(v);
  728. if (!s) {
  729. /* push forward reference */
  730. s = global_identifier_push(v, type->t | VT_EXTERN, 0);
  731. s->type.ref = type->ref;
  732. s->r = r | VT_CONST | VT_SYM;
  733. }
  734. return s;
  735. }
  736. /* Merge some storage attributes. */
  737. static void patch_storage(Sym *sym, AttributeDef *ad, CType *type)
  738. {
  739. if (type && !is_compatible_types(&sym->type, type))
  740. tcc_error("incompatible types for redefinition of '%s'",
  741. get_tok_str(sym->v, NULL));
  742. #ifdef TCC_TARGET_PE
  743. if (sym->a.dllimport != ad->a.dllimport)
  744. tcc_error("incompatible dll linkage for redefinition of '%s'",
  745. get_tok_str(sym->v, NULL));
  746. #endif
  747. sym->a.dllexport |= ad->a.dllexport;
  748. sym->a.weak |= ad->a.weak;
  749. if (ad->a.visibility) {
  750. int vis = sym->a.visibility;
  751. int vis2 = ad->a.visibility;
  752. if (vis == STV_DEFAULT)
  753. vis = vis2;
  754. else if (vis2 != STV_DEFAULT)
  755. vis = (vis < vis2) ? vis : vis2;
  756. sym->a.visibility = vis;
  757. }
  758. if (ad->a.aligned)
  759. sym->a.aligned = ad->a.aligned;
  760. if (ad->asm_label)
  761. sym->asm_label = ad->asm_label;
  762. update_storage(sym);
  763. }
  764. /* define a new external reference to a symbol 'v' */
  765. static Sym *external_sym(int v, CType *type, int r, AttributeDef *ad)
  766. {
  767. Sym *s;
  768. s = sym_find(v);
  769. if (!s) {
  770. /* push forward reference */
  771. s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
  772. s->type.t |= VT_EXTERN;
  773. s->a = ad->a;
  774. s->sym_scope = 0;
  775. } else {
  776. if (s->type.ref == func_old_type.ref) {
  777. s->type.ref = type->ref;
  778. s->r = r | VT_CONST | VT_SYM;
  779. s->type.t |= VT_EXTERN;
  780. }
  781. patch_storage(s, ad, type);
  782. }
  783. return s;
  784. }
  785. /* push a reference to global symbol v */
  786. ST_FUNC void vpush_global_sym(CType *type, int v)
  787. {
  788. vpushsym(type, external_global_sym(v, type, 0));
  789. }
  790. /* save registers up to (vtop - n) stack entry */
  791. ST_FUNC void save_regs(int n)
  792. {
  793. SValue *p, *p1;
  794. for(p = vstack, p1 = vtop - n; p <= p1; p++)
  795. save_reg(p->r);
  796. }
  797. /* save r to the memory stack, and mark it as being free */
  798. ST_FUNC void save_reg(int r)
  799. {
  800. save_reg_upstack(r, 0);
  801. }
  802. /* save r to the memory stack, and mark it as being free,
  803. if seen up to (vtop - n) stack entry */
  804. ST_FUNC void save_reg_upstack(int r, int n)
  805. {
  806. int l, saved, size, align;
  807. SValue *p, *p1, sv;
  808. CType *type;
  809. if ((r &= VT_VALMASK) >= VT_CONST)
  810. return;
  811. if (nocode_wanted)
  812. return;
  813. /* modify all stack values */
  814. saved = 0;
  815. l = 0;
  816. for(p = vstack, p1 = vtop - n; p <= p1; p++) {
  817. if ((p->r & VT_VALMASK) == r ||
  818. ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
  819. /* must save value on stack if not already done */
  820. if (!saved) {
  821. /* NOTE: must reload 'r' because r might be equal to r2 */
  822. r = p->r & VT_VALMASK;
  823. /* store register in the stack */
  824. type = &p->type;
  825. if ((p->r & VT_LVAL) ||
  826. (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
  827. #if PTR_SIZE == 8
  828. type = &char_pointer_type;
  829. #else
  830. type = &int_type;
  831. #endif
  832. size = type_size(type, &align);
  833. loc = (loc - size) & -align;
  834. sv.type.t = type->t;
  835. sv.r = VT_LOCAL | VT_LVAL;
  836. sv.c.i = loc;
  837. store(r, &sv);
  838. #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
  839. /* x86 specific: need to pop fp register ST0 if saved */
  840. if (r == TREG_ST0) {
  841. o(0xd8dd); /* fstp %st(0) */
  842. }
  843. #endif
  844. #if PTR_SIZE == 4
  845. /* special long long case */
  846. if ((type->t & VT_BTYPE) == VT_LLONG) {
  847. sv.c.i += 4;
  848. store(p->r2, &sv);
  849. }
  850. #endif
  851. l = loc;
  852. saved = 1;
  853. }
  854. /* mark that stack entry as being saved on the stack */
  855. if (p->r & VT_LVAL) {
  856. /* also clear the bounded flag because the
  857. relocation address of the function was stored in
  858. p->c.i */
  859. p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
  860. } else {
  861. p->r = lvalue_type(p->type.t) | VT_LOCAL;
  862. }
  863. p->r2 = VT_CONST;
  864. p->c.i = l;
  865. }
  866. }
  867. }
  868. #ifdef TCC_TARGET_ARM
  869. /* find a register of class 'rc2' with at most one reference on stack.
  870. * If none, call get_reg(rc) */
  871. ST_FUNC int get_reg_ex(int rc, int rc2)
  872. {
  873. int r;
  874. SValue *p;
  875. for(r=0;r<NB_REGS;r++) {
  876. if (reg_classes[r] & rc2) {
  877. int n;
  878. n=0;
  879. for(p = vstack; p <= vtop; p++) {
  880. if ((p->r & VT_VALMASK) == r ||
  881. (p->r2 & VT_VALMASK) == r)
  882. n++;
  883. }
  884. if (n <= 1)
  885. return r;
  886. }
  887. }
  888. return get_reg(rc);
  889. }
  890. #endif
  891. /* find a free register of class 'rc'. If none, save one register */
  892. ST_FUNC int get_reg(int rc)
  893. {
  894. int r;
  895. SValue *p;
  896. /* find a free register */
  897. for(r=0;r<NB_REGS;r++) {
  898. if (reg_classes[r] & rc) {
  899. if (nocode_wanted)
  900. return r;
  901. for(p=vstack;p<=vtop;p++) {
  902. if ((p->r & VT_VALMASK) == r ||
  903. (p->r2 & VT_VALMASK) == r)
  904. goto notfound;
  905. }
  906. return r;
  907. }
  908. notfound: ;
  909. }
  910. /* no register left : free the first one on the stack (VERY
  911. IMPORTANT to start from the bottom to ensure that we don't
  912. spill registers used in gen_opi()) */
  913. for(p=vstack;p<=vtop;p++) {
  914. /* look at second register (if long long) */
  915. r = p->r2 & VT_VALMASK;
  916. if (r < VT_CONST && (reg_classes[r] & rc))
  917. goto save_found;
  918. r = p->r & VT_VALMASK;
  919. if (r < VT_CONST && (reg_classes[r] & rc)) {
  920. save_found:
  921. save_reg(r);
  922. return r;
  923. }
  924. }
  925. /* Should never comes here */
  926. return -1;
  927. }
  928. /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
  929. if needed */
  930. static void move_reg(int r, int s, int t)
  931. {
  932. SValue sv;
  933. if (r != s) {
  934. save_reg(r);
  935. sv.type.t = t;
  936. sv.type.ref = NULL;
  937. sv.r = s;
  938. sv.c.i = 0;
  939. load(r, &sv);
  940. }
  941. }
  942. /* get address of vtop (vtop MUST BE an lvalue) */
  943. ST_FUNC void gaddrof(void)
  944. {
  945. vtop->r &= ~VT_LVAL;
  946. /* tricky: if saved lvalue, then we can go back to lvalue */
  947. if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
  948. vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
  949. }
  950. #ifdef CONFIG_TCC_BCHECK
  951. /* generate lvalue bound code */
  952. static void gbound(void)
  953. {
  954. int lval_type;
  955. CType type1;
  956. vtop->r &= ~VT_MUSTBOUND;
  957. /* if lvalue, then use checking code before dereferencing */
  958. if (vtop->r & VT_LVAL) {
  959. /* if not VT_BOUNDED value, then make one */
  960. if (!(vtop->r & VT_BOUNDED)) {
  961. lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
  962. /* must save type because we must set it to int to get pointer */
  963. type1 = vtop->type;
  964. vtop->type.t = VT_PTR;
  965. gaddrof();
  966. vpushi(0);
  967. gen_bounded_ptr_add();
  968. vtop->r |= lval_type;
  969. vtop->type = type1;
  970. }
  971. /* then check for dereferencing */
  972. gen_bounded_ptr_deref();
  973. }
  974. }
  975. #endif
  976. static void incr_bf_adr(int o)
  977. {
  978. vtop->type = char_pointer_type;
  979. gaddrof();
  980. vpushi(o);
  981. gen_op('+');
  982. vtop->type.t = (vtop->type.t & ~(VT_BTYPE|VT_DEFSIGN))
  983. | (VT_BYTE|VT_UNSIGNED);
  984. vtop->r = (vtop->r & ~VT_LVAL_TYPE)
  985. | (VT_LVAL_BYTE|VT_LVAL_UNSIGNED|VT_LVAL);
  986. }
  987. /* single-byte load mode for packed or otherwise unaligned bitfields */
  988. static void load_packed_bf(CType *type, int bit_pos, int bit_size)
  989. {
  990. int n, o, bits;
  991. save_reg_upstack(vtop->r, 1);
  992. vpush64(type->t & VT_BTYPE, 0); // B X
  993. bits = 0, o = bit_pos >> 3, bit_pos &= 7;
  994. do {
  995. vswap(); // X B
  996. incr_bf_adr(o);
  997. vdup(); // X B B
  998. n = 8 - bit_pos;
  999. if (n > bit_size)
  1000. n = bit_size;
  1001. if (bit_pos)
  1002. vpushi(bit_pos), gen_op(TOK_SHR), bit_pos = 0; // X B Y
  1003. if (n < 8)
  1004. vpushi((1 << n) - 1), gen_op('&');
  1005. gen_cast(type);
  1006. if (bits)
  1007. vpushi(bits), gen_op(TOK_SHL);
  1008. vrotb(3); // B Y X
  1009. gen_op('|'); // B X
  1010. bits += n, bit_size -= n, o = 1;
  1011. } while (bit_size);
  1012. vswap(), vpop();
  1013. if (!(type->t & VT_UNSIGNED)) {
  1014. n = ((type->t & VT_BTYPE) == VT_LLONG ? 64 : 32) - bits;
  1015. vpushi(n), gen_op(TOK_SHL);
  1016. vpushi(n), gen_op(TOK_SAR);
  1017. }
  1018. }
  1019. /* single-byte store mode for packed or otherwise unaligned bitfields */
  1020. static void store_packed_bf(int bit_pos, int bit_size)
  1021. {
  1022. int bits, n, o, m, c;
  1023. c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
  1024. vswap(); // X B
  1025. save_reg_upstack(vtop->r, 1);
  1026. bits = 0, o = bit_pos >> 3, bit_pos &= 7;
  1027. do {
  1028. incr_bf_adr(o); // X B
  1029. vswap(); //B X
  1030. c ? vdup() : gv_dup(); // B V X
  1031. vrott(3); // X B V
  1032. if (bits)
  1033. vpushi(bits), gen_op(TOK_SHR);
  1034. if (bit_pos)
  1035. vpushi(bit_pos), gen_op(TOK_SHL);
  1036. n = 8 - bit_pos;
  1037. if (n > bit_size)
  1038. n = bit_size;
  1039. if (n < 8) {
  1040. m = ((1 << n) - 1) << bit_pos;
  1041. vpushi(m), gen_op('&'); // X B V1
  1042. vpushv(vtop-1); // X B V1 B
  1043. vpushi(m & 0x80 ? ~m & 0x7f : ~m);
  1044. gen_op('&'); // X B V1 B1
  1045. gen_op('|'); // X B V2
  1046. }
  1047. vdup(), vtop[-1] = vtop[-2]; // X B B V2
  1048. vstore(), vpop(); // X B
  1049. bits += n, bit_size -= n, bit_pos = 0, o = 1;
  1050. } while (bit_size);
  1051. vpop(), vpop();
  1052. }
  1053. static int adjust_bf(SValue *sv, int bit_pos, int bit_size)
  1054. {
  1055. int t;
  1056. if (0 == sv->type.ref)
  1057. return 0;
  1058. t = sv->type.ref->auxtype;
  1059. if (t != -1 && t != VT_STRUCT) {
  1060. sv->type.t = (sv->type.t & ~VT_BTYPE) | t;
  1061. sv->r = (sv->r & ~VT_LVAL_TYPE) | lvalue_type(sv->type.t);
  1062. }
  1063. return t;
  1064. }
  1065. /* store vtop a register belonging to class 'rc'. lvalues are
  1066. converted to values. Cannot be used if cannot be converted to
  1067. register value (such as structures). */
  1068. ST_FUNC int gv(int rc)
  1069. {
  1070. int r, bit_pos, bit_size, size, align, rc2;
  1071. /* NOTE: get_reg can modify vstack[] */
  1072. if (vtop->type.t & VT_BITFIELD) {
  1073. CType type;
  1074. bit_pos = BIT_POS(vtop->type.t);
  1075. bit_size = BIT_SIZE(vtop->type.t);
  1076. /* remove bit field info to avoid loops */
  1077. vtop->type.t &= ~VT_STRUCT_MASK;
  1078. type.ref = NULL;
  1079. type.t = vtop->type.t & VT_UNSIGNED;
  1080. if ((vtop->type.t & VT_BTYPE) == VT_BOOL)
  1081. type.t |= VT_UNSIGNED;
  1082. r = adjust_bf(vtop, bit_pos, bit_size);
  1083. if ((vtop->type.t & VT_BTYPE) == VT_LLONG)
  1084. type.t |= VT_LLONG;
  1085. else
  1086. type.t |= VT_INT;
  1087. if (r == VT_STRUCT) {
  1088. load_packed_bf(&type, bit_pos, bit_size);
  1089. } else {
  1090. int bits = (type.t & VT_BTYPE) == VT_LLONG ? 64 : 32;
  1091. /* cast to int to propagate signedness in following ops */
  1092. gen_cast(&type);
  1093. /* generate shifts */
  1094. vpushi(bits - (bit_pos + bit_size));
  1095. gen_op(TOK_SHL);
  1096. vpushi(bits - bit_size);
  1097. /* NOTE: transformed to SHR if unsigned */
  1098. gen_op(TOK_SAR);
  1099. }
  1100. r = gv(rc);
  1101. } else {
  1102. if (is_float(vtop->type.t) &&
  1103. (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
  1104. unsigned long offset;
  1105. /* CPUs usually cannot use float constants, so we store them
  1106. generically in data segment */
  1107. size = type_size(&vtop->type, &align);
  1108. if (NODATA_WANTED)
  1109. size = 0, align = 1;
  1110. offset = section_add(data_section, size, align);
  1111. vpush_ref(&vtop->type, data_section, offset, size);
  1112. vswap();
  1113. init_putv(&vtop->type, data_section, offset);
  1114. vtop->r |= VT_LVAL;
  1115. }
  1116. #ifdef CONFIG_TCC_BCHECK
  1117. if (vtop->r & VT_MUSTBOUND)
  1118. gbound();
  1119. #endif
  1120. r = vtop->r & VT_VALMASK;
  1121. rc2 = (rc & RC_FLOAT) ? RC_FLOAT : RC_INT;
  1122. #ifndef TCC_TARGET_ARM64
  1123. if (rc == RC_IRET)
  1124. rc2 = RC_LRET;
  1125. #ifdef TCC_TARGET_X86_64
  1126. else if (rc == RC_FRET)
  1127. rc2 = RC_QRET;
  1128. #endif
  1129. #endif
  1130. /* need to reload if:
  1131. - constant
  1132. - lvalue (need to dereference pointer)
  1133. - already a register, but not in the right class */
  1134. if (r >= VT_CONST
  1135. || (vtop->r & VT_LVAL)
  1136. || !(reg_classes[r] & rc)
  1137. #if PTR_SIZE == 8
  1138. || ((vtop->type.t & VT_BTYPE) == VT_QLONG && !(reg_classes[vtop->r2] & rc2))
  1139. || ((vtop->type.t & VT_BTYPE) == VT_QFLOAT && !(reg_classes[vtop->r2] & rc2))
  1140. #else
  1141. || ((vtop->type.t & VT_BTYPE) == VT_LLONG && !(reg_classes[vtop->r2] & rc2))
  1142. #endif
  1143. )
  1144. {
  1145. r = get_reg(rc);
  1146. #if PTR_SIZE == 8
  1147. if (((vtop->type.t & VT_BTYPE) == VT_QLONG) || ((vtop->type.t & VT_BTYPE) == VT_QFLOAT)) {
  1148. int addr_type = VT_LLONG, load_size = 8, load_type = ((vtop->type.t & VT_BTYPE) == VT_QLONG) ? VT_LLONG : VT_DOUBLE;
  1149. #else
  1150. if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
  1151. int addr_type = VT_INT, load_size = 4, load_type = VT_INT;
  1152. unsigned long long ll;
  1153. #endif
  1154. int r2, original_type;
  1155. original_type = vtop->type.t;
  1156. /* two register type load : expand to two words
  1157. temporarily */
  1158. #if PTR_SIZE == 4
  1159. if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
  1160. /* load constant */
  1161. ll = vtop->c.i;
  1162. vtop->c.i = ll; /* first word */
  1163. load(r, vtop);
  1164. vtop->r = r; /* save register value */
  1165. vpushi(ll >> 32); /* second word */
  1166. } else
  1167. #endif
  1168. if (vtop->r & VT_LVAL) {
  1169. /* We do not want to modifier the long long
  1170. pointer here, so the safest (and less
  1171. efficient) is to save all the other registers
  1172. in the stack. XXX: totally inefficient. */
  1173. #if 0
  1174. save_regs(1);
  1175. #else
  1176. /* lvalue_save: save only if used further down the stack */
  1177. save_reg_upstack(vtop->r, 1);
  1178. #endif
  1179. /* load from memory */
  1180. vtop->type.t = load_type;
  1181. load(r, vtop);
  1182. vdup();
  1183. vtop[-1].r = r; /* save register value */
  1184. /* increment pointer to get second word */
  1185. vtop->type.t = addr_type;
  1186. gaddrof();
  1187. vpushi(load_size);
  1188. gen_op('+');
  1189. vtop->r |= VT_LVAL;
  1190. vtop->type.t = load_type;
  1191. } else {
  1192. /* move registers */
  1193. load(r, vtop);
  1194. vdup();
  1195. vtop[-1].r = r; /* save register value */
  1196. vtop->r = vtop[-1].r2;
  1197. }
  1198. /* Allocate second register. Here we rely on the fact that
  1199. get_reg() tries first to free r2 of an SValue. */
  1200. r2 = get_reg(rc2);
  1201. load(r2, vtop);
  1202. vpop();
  1203. /* write second register */
  1204. vtop->r2 = r2;
  1205. vtop->type.t = original_type;
  1206. } else if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
  1207. int t1, t;
  1208. /* lvalue of scalar type : need to use lvalue type
  1209. because of possible cast */
  1210. t = vtop->type.t;
  1211. t1 = t;
  1212. /* compute memory access type */
  1213. if (vtop->r & VT_LVAL_BYTE)
  1214. t = VT_BYTE;
  1215. else if (vtop->r & VT_LVAL_SHORT)
  1216. t = VT_SHORT;
  1217. if (vtop->r & VT_LVAL_UNSIGNED)
  1218. t |= VT_UNSIGNED;
  1219. vtop->type.t = t;
  1220. load(r, vtop);
  1221. /* restore wanted type */
  1222. vtop->type.t = t1;
  1223. } else {
  1224. /* one register type load */
  1225. load(r, vtop);
  1226. }
  1227. }
  1228. vtop->r = r;
  1229. #ifdef TCC_TARGET_C67
  1230. /* uses register pairs for doubles */
  1231. if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
  1232. vtop->r2 = r+1;
  1233. #endif
  1234. }
  1235. return r;
  1236. }
  1237. /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
  1238. ST_FUNC void gv2(int rc1, int rc2)
  1239. {
  1240. int v;
  1241. /* generate more generic register first. But VT_JMP or VT_CMP
  1242. values must be generated first in all cases to avoid possible
  1243. reload errors */
  1244. v = vtop[0].r & VT_VALMASK;
  1245. if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
  1246. vswap();
  1247. gv(rc1);
  1248. vswap();
  1249. gv(rc2);
  1250. /* test if reload is needed for first register */
  1251. if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
  1252. vswap();
  1253. gv(rc1);
  1254. vswap();
  1255. }
  1256. } else {
  1257. gv(rc2);
  1258. vswap();
  1259. gv(rc1);
  1260. vswap();
  1261. /* test if reload is needed for first register */
  1262. if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
  1263. gv(rc2);
  1264. }
  1265. }
  1266. }
  1267. #ifndef TCC_TARGET_ARM64
  1268. /* wrapper around RC_FRET to return a register by type */
  1269. static int rc_fret(int t)
  1270. {
  1271. #ifdef TCC_TARGET_X86_64
  1272. if (t == VT_LDOUBLE) {
  1273. return RC_ST0;
  1274. }
  1275. #endif
  1276. return RC_FRET;
  1277. }
  1278. #endif
  1279. /* wrapper around REG_FRET to return a register by type */
  1280. static int reg_fret(int t)
  1281. {
  1282. #ifdef TCC_TARGET_X86_64
  1283. if (t == VT_LDOUBLE) {
  1284. return TREG_ST0;
  1285. }
  1286. #endif
  1287. return REG_FRET;
  1288. }
  1289. #if PTR_SIZE == 4
  1290. /* expand 64bit on stack in two ints */
  1291. static void lexpand(void)
  1292. {
  1293. int u, v;
  1294. u = vtop->type.t & (VT_DEFSIGN | VT_UNSIGNED);
  1295. v = vtop->r & (VT_VALMASK | VT_LVAL);
  1296. if (v == VT_CONST) {
  1297. vdup();
  1298. vtop[0].c.i >>= 32;
  1299. } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
  1300. vdup();
  1301. vtop[0].c.i += 4;
  1302. } else {
  1303. gv(RC_INT);
  1304. vdup();
  1305. vtop[0].r = vtop[-1].r2;
  1306. vtop[0].r2 = vtop[-1].r2 = VT_CONST;
  1307. }
  1308. vtop[0].type.t = vtop[-1].type.t = VT_INT | u;
  1309. }
  1310. #endif
  1311. #ifdef TCC_TARGET_ARM
  1312. /* expand long long on stack */
  1313. ST_FUNC void lexpand_nr(void)
  1314. {
  1315. int u,v;
  1316. u = vtop->type.t & (VT_DEFSIGN | VT_UNSIGNED);
  1317. vdup();
  1318. vtop->r2 = VT_CONST;
  1319. vtop->type.t = VT_INT | u;
  1320. v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
  1321. if (v == VT_CONST) {
  1322. vtop[-1].c.i = vtop->c.i;
  1323. vtop->c.i = vtop->c.i >> 32;
  1324. vtop->r = VT_CONST;
  1325. } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
  1326. vtop->c.i += 4;
  1327. vtop->r = vtop[-1].r;
  1328. } else if (v > VT_CONST) {
  1329. vtop--;
  1330. lexpand();
  1331. } else
  1332. vtop->r = vtop[-1].r2;
  1333. vtop[-1].r2 = VT_CONST;
  1334. vtop[-1].type.t = VT_INT | u;
  1335. }
  1336. #endif
  1337. #if PTR_SIZE == 4
  1338. /* build a long long from two ints */
  1339. static void lbuild(int t)
  1340. {
  1341. gv2(RC_INT, RC_INT);
  1342. vtop[-1].r2 = vtop[0].r;
  1343. vtop[-1].type.t = t;
  1344. vpop();
  1345. }
  1346. #endif
  1347. /* convert stack entry to register and duplicate its value in another
  1348. register */
  1349. static void gv_dup(void)
  1350. {
  1351. int rc, t, r, r1;
  1352. SValue sv;
  1353. t = vtop->type.t;
  1354. #if PTR_SIZE == 4
  1355. if ((t & VT_BTYPE) == VT_LLONG) {
  1356. if (t & VT_BITFIELD) {
  1357. gv(RC_INT);
  1358. t = vtop->type.t;
  1359. }
  1360. lexpand();
  1361. gv_dup();
  1362. vswap();
  1363. vrotb(3);
  1364. gv_dup();
  1365. vrotb(4);
  1366. /* stack: H L L1 H1 */
  1367. lbuild(t);
  1368. vrotb(3);
  1369. vrotb(3);
  1370. vswap();
  1371. lbuild(t);
  1372. vswap();
  1373. } else
  1374. #endif
  1375. {
  1376. /* duplicate value */
  1377. rc = RC_INT;
  1378. sv.type.t = VT_INT;
  1379. if (is_float(t)) {
  1380. rc = RC_FLOAT;
  1381. #ifdef TCC_TARGET_X86_64
  1382. if ((t & VT_BTYPE) == VT_LDOUBLE) {
  1383. rc = RC_ST0;
  1384. }
  1385. #endif
  1386. sv.type.t = t;
  1387. }
  1388. r = gv(rc);
  1389. r1 = get_reg(rc);
  1390. sv.r = r;
  1391. sv.c.i = 0;
  1392. load(r1, &sv); /* move r to r1 */
  1393. vdup();
  1394. /* duplicates value */
  1395. if (r != r1)
  1396. vtop->r = r1;
  1397. }
  1398. }
  1399. /* Generate value test
  1400. *
  1401. * Generate a test for any value (jump, comparison and integers) */
  1402. ST_FUNC int gvtst(int inv, int t)
  1403. {
  1404. int v = vtop->r & VT_VALMASK;
  1405. if (v != VT_CMP && v != VT_JMP && v != VT_JMPI) {
  1406. vpushi(0);
  1407. gen_op(TOK_NE);
  1408. }
  1409. if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
  1410. /* constant jmp optimization */
  1411. if ((vtop->c.i != 0) != inv)
  1412. t = gjmp(t);
  1413. vtop--;
  1414. return t;
  1415. }
  1416. return gtst(inv, t);
  1417. }
  1418. #if PTR_SIZE == 4
  1419. /* generate CPU independent (unsigned) long long operations */
  1420. static void gen_opl(int op)
  1421. {
  1422. int t, a, b, op1, c, i;
  1423. int func;
  1424. unsigned short reg_iret = REG_IRET;
  1425. unsigned short reg_lret = REG_LRET;
  1426. SValue tmp;
  1427. switch(op) {
  1428. case '/':
  1429. case TOK_PDIV:
  1430. func = TOK___divdi3;
  1431. goto gen_func;
  1432. case TOK_UDIV:
  1433. func = TOK___udivdi3;
  1434. goto gen_func;
  1435. case '%':
  1436. func = TOK___moddi3;
  1437. goto gen_mod_func;
  1438. case TOK_UMOD:
  1439. func = TOK___umoddi3;
  1440. gen_mod_func:
  1441. #ifdef TCC_ARM_EABI
  1442. reg_iret = TREG_R2;
  1443. reg_lret = TREG_R3;
  1444. #endif
  1445. gen_func:
  1446. /* call generic long long function */
  1447. vpush_global_sym(&func_old_type, func);
  1448. vrott(3);
  1449. gfunc_call(2);
  1450. vpushi(0);
  1451. vtop->r = reg_iret;
  1452. vtop->r2 = reg_lret;
  1453. break;
  1454. case '^':
  1455. case '&':
  1456. case '|':
  1457. case '*':
  1458. case '+':
  1459. case '-':
  1460. //pv("gen_opl A",0,2);
  1461. t = vtop->type.t;
  1462. vswap();
  1463. lexpand();
  1464. vrotb(3);
  1465. lexpand();
  1466. /* stack: L1 H1 L2 H2 */
  1467. tmp = vtop[0];
  1468. vtop[0] = vtop[-3];
  1469. vtop[-3] = tmp;
  1470. tmp = vtop[-2];
  1471. vtop[-2] = vtop[-3];
  1472. vtop[-3] = tmp;
  1473. vswap();
  1474. /* stack: H1 H2 L1 L2 */
  1475. //pv("gen_opl B",0,4);
  1476. if (op == '*') {
  1477. vpushv(vtop - 1);
  1478. vpushv(vtop - 1);
  1479. gen_op(TOK_UMULL);
  1480. lexpand();
  1481. /* stack: H1 H2 L1 L2 ML MH */
  1482. for(i=0;i<4;i++)
  1483. vrotb(6);
  1484. /* stack: ML MH H1 H2 L1 L2 */
  1485. tmp = vtop[0];
  1486. vtop[0] = vtop[-2];
  1487. vtop[-2] = tmp;
  1488. /* stack: ML MH H1 L2 H2 L1 */
  1489. gen_op('*');
  1490. vrotb(3);
  1491. vrotb(3);
  1492. gen_op('*');
  1493. /* stack: ML MH M1 M2 */
  1494. gen_op('+');
  1495. gen_op('+');
  1496. } else if (op == '+' || op == '-') {
  1497. /* XXX: add non carry method too (for MIPS or alpha) */
  1498. if (op == '+')
  1499. op1 = TOK_ADDC1;
  1500. else
  1501. op1 = TOK_SUBC1;
  1502. gen_op(op1);
  1503. /* stack: H1 H2 (L1 op L2) */
  1504. vrotb(3);
  1505. vrotb(3);
  1506. gen_op(op1 + 1); /* TOK_xxxC2 */
  1507. } else {
  1508. gen_op(op);
  1509. /* stack: H1 H2 (L1 op L2) */
  1510. vrotb(3);
  1511. vrotb(3);
  1512. /* stack: (L1 op L2) H1 H2 */
  1513. gen_op(op);
  1514. /* stack: (L1 op L2) (H1 op H2) */
  1515. }
  1516. /* stack: L H */
  1517. lbuild(t);
  1518. break;
  1519. case TOK_SAR:
  1520. case TOK_SHR:
  1521. case TOK_SHL:
  1522. if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
  1523. t = vtop[-1].type.t;
  1524. vswap();
  1525. lexpand();
  1526. vrotb(3);
  1527. /* stack: L H shift */
  1528. c = (int)vtop->c.i;
  1529. /* constant: simpler */
  1530. /* NOTE: all comments are for SHL. the other cases are
  1531. done by swapping words */
  1532. vpop();
  1533. if (op != TOK_SHL)
  1534. vswap();
  1535. if (c >= 32) {
  1536. /* stack: L H */
  1537. vpop();
  1538. if (c > 32) {
  1539. vpushi(c - 32);
  1540. gen_op(op);
  1541. }
  1542. if (op != TOK_SAR) {
  1543. vpushi(0);
  1544. } else {
  1545. gv_dup();
  1546. vpushi(31);
  1547. gen_op(TOK_SAR);
  1548. }
  1549. vswap();
  1550. } else {
  1551. vswap();
  1552. gv_dup();
  1553. /* stack: H L L */
  1554. vpushi(c);
  1555. gen_op(op);
  1556. vswap();
  1557. vpushi(32 - c);
  1558. if (op == TOK_SHL)
  1559. gen_op(TOK_SHR);
  1560. else
  1561. gen_op(TOK_SHL);
  1562. vrotb(3);
  1563. /* stack: L L H */
  1564. vpushi(c);
  1565. if (op == TOK_SHL)
  1566. gen_op(TOK_SHL);
  1567. else
  1568. gen_op(TOK_SHR);
  1569. gen_op('|');
  1570. }
  1571. if (op != TOK_SHL)
  1572. vswap();
  1573. lbuild(t);
  1574. } else {
  1575. /* XXX: should provide a faster fallback on x86 ? */
  1576. switch(op) {
  1577. case TOK_SAR:
  1578. func = TOK___ashrdi3;
  1579. goto gen_func;
  1580. case TOK_SHR:
  1581. func = TOK___lshrdi3;
  1582. goto gen_func;
  1583. case TOK_SHL:
  1584. func = TOK___ashldi3;
  1585. goto gen_func;
  1586. }
  1587. }
  1588. break;
  1589. default:
  1590. /* compare operations */
  1591. t = vtop->type.t;
  1592. vswap();
  1593. lexpand();
  1594. vrotb(3);
  1595. lexpand();
  1596. /* stack: L1 H1 L2 H2 */
  1597. tmp = vtop[-1];
  1598. vtop[-1] = vtop[-2];
  1599. vtop[-2] = tmp;
  1600. /* stack: L1 L2 H1 H2 */
  1601. /* compare high */
  1602. op1 = op;
  1603. /* when values are equal, we need to compare low words. since
  1604. the jump is inverted, we invert the test too. */
  1605. if (op1 == TOK_LT)
  1606. op1 = TOK_LE;
  1607. else if (op1 == TOK_GT)
  1608. op1 = TOK_GE;
  1609. else if (op1 == TOK_ULT)
  1610. op1 = TOK_ULE;
  1611. else if (op1 == TOK_UGT)
  1612. op1 = TOK_UGE;
  1613. a = 0;
  1614. b = 0;
  1615. gen_op(op1);
  1616. if (op == TOK_NE) {
  1617. b = gvtst(0, 0);
  1618. } else {
  1619. a = gvtst(1, 0);
  1620. if (op != TOK_EQ) {
  1621. /* generate non equal test */
  1622. vpushi(TOK_NE);
  1623. vtop->r = VT_CMP;
  1624. b = gvtst(0, 0);
  1625. }
  1626. }
  1627. /* compare low. Always unsigned */
  1628. op1 = op;
  1629. if (op1 == TOK_LT)
  1630. op1 = TOK_ULT;
  1631. else if (op1 == TOK_LE)
  1632. op1 = TOK_ULE;
  1633. else if (op1 == TOK_GT)
  1634. op1 = TOK_UGT;
  1635. else if (op1 == TOK_GE)
  1636. op1 = TOK_UGE;
  1637. gen_op(op1);
  1638. a = gvtst(1, a);
  1639. gsym(b);
  1640. vseti(VT_JMPI, a);
  1641. break;
  1642. }
  1643. }
  1644. #endif
  1645. static uint64_t gen_opic_sdiv(uint64_t a, uint64_t b)
  1646. {
  1647. uint64_t x = (a >> 63 ? -a : a) / (b >> 63 ? -b : b);
  1648. return (a ^ b) >> 63 ? -x : x;
  1649. }
  1650. static int gen_opic_lt(uint64_t a, uint64_t b)
  1651. {
  1652. return (a ^ (uint64_t)1 << 63) < (b ^ (uint64_t)1 << 63);
  1653. }
  1654. /* handle integer constant optimizations and various machine
  1655. independent opt */
  1656. static void gen_opic(int op)
  1657. {
  1658. SValue *v1 = vtop - 1;
  1659. SValue *v2 = vtop;
  1660. int t1 = v1->type.t & VT_BTYPE;
  1661. int t2 = v2->type.t & VT_BTYPE;
  1662. int c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
  1663. int c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
  1664. uint64_t l1 = c1 ? v1->c.i : 0;
  1665. uint64_t l2 = c2 ? v2->c.i : 0;
  1666. int shm = (t1 == VT_LLONG) ? 63 : 31;
  1667. if (t1 != VT_LLONG && (PTR_SIZE != 8 || t1 != VT_PTR))
  1668. l1 = ((uint32_t)l1 |
  1669. (v1->type.t & VT_UNSIGNED ? 0 : -(l1 & 0x80000000)));
  1670. if (t2 != VT_LLONG && (PTR_SIZE != 8 || t2 != VT_PTR))
  1671. l2 = ((uint32_t)l2 |
  1672. (v2->type.t & VT_UNSIGNED ? 0 : -(l2 & 0x80000000)));
  1673. if (c1 && c2) {
  1674. switch(op) {
  1675. case '+': l1 += l2; break;
  1676. case '-': l1 -= l2; break;
  1677. case '&': l1 &= l2; break;
  1678. case '^': l1 ^= l2; break;
  1679. case '|': l1 |= l2; break;
  1680. case '*': l1 *= l2; break;
  1681. case TOK_PDIV:
  1682. case '/':
  1683. case '%':
  1684. case TOK_UDIV:
  1685. case TOK_UMOD:
  1686. /* if division by zero, generate explicit division */
  1687. if (l2 == 0) {
  1688. if (const_wanted)
  1689. tcc_error("division by zero in constant");
  1690. goto general_case;
  1691. }
  1692. switch(op) {
  1693. default: l1 = gen_opic_sdiv(l1, l2); break;
  1694. case '%': l1 = l1 - l2 * gen_opic_sdiv(l1, l2); break;
  1695. case TOK_UDIV: l1 = l1 / l2; break;
  1696. case TOK_UMOD: l1 = l1 % l2; break;
  1697. }
  1698. break;
  1699. case TOK_SHL: l1 <<= (l2 & shm); break;
  1700. case TOK_SHR: l1 >>= (l2 & shm); break;
  1701. case TOK_SAR:
  1702. l1 = (l1 >> 63) ? ~(~l1 >> (l2 & shm)) : l1 >> (l2 & shm);
  1703. break;
  1704. /* tests */
  1705. case TOK_ULT: l1 = l1 < l2; break;
  1706. case TOK_UGE: l1 = l1 >= l2; break;
  1707. case TOK_EQ: l1 = l1 == l2; break;
  1708. case TOK_NE: l1 = l1 != l2; break;
  1709. case TOK_ULE: l1 = l1 <= l2; break;
  1710. case TOK_UGT: l1 = l1 > l2; break;
  1711. case TOK_LT: l1 = gen_opic_lt(l1, l2); break;
  1712. case TOK_GE: l1 = !gen_opic_lt(l1, l2); break;
  1713. case TOK_LE: l1 = !gen_opic_lt(l2, l1); break;
  1714. case TOK_GT: l1 = gen_opic_lt(l2, l1); break;
  1715. /* logical */
  1716. case TOK_LAND: l1 = l1 && l2; break;
  1717. case TOK_LOR: l1 = l1 || l2; break;
  1718. default:
  1719. goto general_case;
  1720. }
  1721. if (t1 != VT_LLONG && (PTR_SIZE != 8 || t1 != VT_PTR))
  1722. l1 = ((uint32_t)l1 |
  1723. (v1->type.t & VT_UNSIGNED ? 0 : -(l1 & 0x80000000)));
  1724. v1->c.i = l1;
  1725. vtop--;
  1726. } else {
  1727. /* if commutative ops, put c2 as constant */
  1728. if (c1 && (op == '+' || op == '&' || op == '^' ||
  1729. op == '|' || op == '*')) {
  1730. vswap();
  1731. c2 = c1; //c = c1, c1 = c2, c2 = c;
  1732. l2 = l1; //l = l1, l1 = l2, l2 = l;
  1733. }
  1734. if (!const_wanted &&
  1735. c1 && ((l1 == 0 &&
  1736. (op == TOK_SHL || op == TOK_SHR || op == TOK_SAR)) ||
  1737. (l1 == -1 && op == TOK_SAR))) {
  1738. /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
  1739. vtop--;
  1740. } else if (!const_wanted &&
  1741. c2 && ((l2 == 0 && (op == '&' || op == '*')) ||
  1742. (op == '|' &&
  1743. (l2 == -1 || (l2 == 0xFFFFFFFF && t2 != VT_LLONG))) ||
  1744. (l2 == 1 && (op == '%' || op == TOK_UMOD)))) {
  1745. /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
  1746. if (l2 == 1)
  1747. vtop->c.i = 0;
  1748. vswap();
  1749. vtop--;
  1750. } else if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
  1751. op == TOK_PDIV) &&
  1752. l2 == 1) ||
  1753. ((op == '+' || op == '-' || op == '|' || op == '^' ||
  1754. op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
  1755. l2 == 0) ||
  1756. (op == '&' &&
  1757. (l2 == -1 || (l2 == 0xFFFFFFFF && t2 != VT_LLONG))))) {
  1758. /* filter out NOP operations like x*1, x-0, x&-1... */
  1759. vtop--;
  1760. } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
  1761. /* try to use shifts instead of muls or divs */
  1762. if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
  1763. int n = -1;
  1764. while (l2) {
  1765. l2 >>= 1;
  1766. n++;
  1767. }
  1768. vtop->c.i = n;
  1769. if (op == '*')
  1770. op = TOK_SHL;
  1771. else if (op == TOK_PDIV)
  1772. op = TOK_SAR;
  1773. else
  1774. op = TOK_SHR;
  1775. }
  1776. goto general_case;
  1777. } else if (c2 && (op == '+' || op == '-') &&
  1778. (((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM))
  1779. || (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
  1780. /* symbol + constant case */
  1781. if (op == '-')
  1782. l2 = -l2;
  1783. l2 += vtop[-1].c.i;
  1784. /* The backends can't always deal with addends to symbols
  1785. larger than +-1<<31. Don't construct such. */
  1786. if ((int)l2 != l2)
  1787. goto general_case;
  1788. vtop--;
  1789. vtop->c.i = l2;
  1790. } else {
  1791. general_case:
  1792. /* call low level op generator */
  1793. if (t1 == VT_LLONG || t2 == VT_LLONG ||
  1794. (PTR_SIZE == 8 && (t1 == VT_PTR || t2 == VT_PTR)))
  1795. gen_opl(op);
  1796. else
  1797. gen_opi(op);
  1798. }
  1799. }
  1800. }
  1801. /* generate a floating point operation with constant propagation */
  1802. static void gen_opif(int op)
  1803. {
  1804. int c1, c2;
  1805. SValue *v1, *v2;
  1806. #if defined _MSC_VER && defined _AMD64_
  1807. /* avoid bad optimization with f1 -= f2 for f1:-0.0, f2:0.0 */
  1808. volatile
  1809. #endif
  1810. long double f1, f2;
  1811. v1 = vtop - 1;
  1812. v2 = vtop;
  1813. /* currently, we cannot do computations with forward symbols */
  1814. c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
  1815. c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
  1816. if (c1 && c2) {
  1817. if (v1->type.t == VT_FLOAT) {
  1818. f1 = v1->c.f;
  1819. f2 = v2->c.f;
  1820. } else if (v1->type.t == VT_DOUBLE) {
  1821. f1 = v1->c.d;
  1822. f2 = v2->c.d;
  1823. } else {
  1824. f1 = v1->c.ld;
  1825. f2 = v2->c.ld;
  1826. }
  1827. /* NOTE: we only do constant propagation if finite number (not
  1828. NaN or infinity) (ANSI spec) */
  1829. if (!ieee_finite(f1) || !ieee_finite(f2))
  1830. goto general_case;
  1831. switch(op) {
  1832. case '+': f1 += f2; break;
  1833. case '-': f1 -= f2; break;
  1834. case '*': f1 *= f2; break;
  1835. case '/':
  1836. if (f2 == 0.0) {
  1837. if (const_wanted)
  1838. tcc_error("division by zero in constant");
  1839. goto general_case;
  1840. }
  1841. f1 /= f2;
  1842. break;
  1843. /* XXX: also handles tests ? */
  1844. default:
  1845. goto general_case;
  1846. }
  1847. /* XXX: overflow test ? */
  1848. if (v1->type.t == VT_FLOAT) {
  1849. v1->c.f = f1;
  1850. } else if (v1->type.t == VT_DOUBLE) {
  1851. v1->c.d = f1;
  1852. } else {
  1853. v1->c.ld = f1;
  1854. }
  1855. vtop--;
  1856. } else {
  1857. general_case:
  1858. gen_opf(op);
  1859. }
  1860. }
  1861. static int pointed_size(CType *type)
  1862. {
  1863. int align;
  1864. return type_size(pointed_type(type), &align);
  1865. }
  1866. static void vla_runtime_pointed_size(CType *type)
  1867. {
  1868. int align;
  1869. vla_runtime_type_size(pointed_type(type), &align);
  1870. }
  1871. static inline int is_null_pointer(SValue *p)
  1872. {
  1873. if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
  1874. return 0;
  1875. return ((p->type.t & VT_BTYPE) == VT_INT && (uint32_t)p->c.i == 0) ||
  1876. ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.i == 0) ||
  1877. ((p->type.t & VT_BTYPE) == VT_PTR &&
  1878. (PTR_SIZE == 4 ? (uint32_t)p->c.i == 0 : p->c.i == 0));
  1879. }
  1880. static inline int is_integer_btype(int bt)
  1881. {
  1882. return (bt == VT_BYTE || bt == VT_SHORT ||
  1883. bt == VT_INT || bt == VT_LLONG);
  1884. }
  1885. /* check types for comparison or subtraction of pointers */
  1886. static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
  1887. {
  1888. CType *type1, *type2, tmp_type1, tmp_type2;
  1889. int bt1, bt2;
  1890. /* null pointers are accepted for all comparisons as gcc */
  1891. if (is_null_pointer(p1) || is_null_pointer(p2))
  1892. return;
  1893. type1 = &p1->type;
  1894. type2 = &p2->type;
  1895. bt1 = type1->t & VT_BTYPE;
  1896. bt2 = type2->t & VT_BTYPE;
  1897. /* accept comparison between pointer and integer with a warning */
  1898. if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
  1899. if (op != TOK_LOR && op != TOK_LAND )
  1900. tcc_warning("comparison between pointer and integer");
  1901. return;
  1902. }
  1903. /* both must be pointers or implicit function pointers */
  1904. if (bt1 == VT_PTR) {
  1905. type1 = pointed_type(type1);
  1906. } else if (bt1 != VT_FUNC)
  1907. goto invalid_operands;
  1908. if (bt2 == VT_PTR) {
  1909. type2 = pointed_type(type2);
  1910. } else if (bt2 != VT_FUNC) {
  1911. invalid_operands:
  1912. tcc_error("invalid operands to binary %s", get_tok_str(op, NULL));
  1913. }
  1914. if ((type1->t & VT_BTYPE) == VT_VOID ||
  1915. (type2->t & VT_BTYPE) == VT_VOID)
  1916. return;
  1917. tmp_type1 = *type1;
  1918. tmp_type2 = *type2;
  1919. tmp_type1.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
  1920. tmp_type2.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
  1921. if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
  1922. /* gcc-like error if '-' is used */
  1923. if (op == '-')
  1924. goto invalid_operands;
  1925. else
  1926. tcc_warning("comparison of distinct pointer types lacks a cast");
  1927. }
  1928. }
  1929. /* generic gen_op: handles types problems */
  1930. ST_FUNC void gen_op(int op)
  1931. {
  1932. int u, t1, t2, bt1, bt2, t;
  1933. CType type1;
  1934. redo:
  1935. t1 = vtop[-1].type.t;
  1936. t2 = vtop[0].type.t;
  1937. bt1 = t1 & VT_BTYPE;
  1938. bt2 = t2 & VT_BTYPE;
  1939. if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
  1940. tcc_error("operation on a struct");
  1941. } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
  1942. if (bt2 == VT_FUNC) {
  1943. mk_pointer(&vtop->type);
  1944. gaddrof();
  1945. }
  1946. if (bt1 == VT_FUNC) {
  1947. vswap();
  1948. mk_pointer(&vtop->type);
  1949. gaddrof();
  1950. vswap();
  1951. }
  1952. goto redo;
  1953. } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
  1954. /* at least one operand is a pointer */
  1955. /* relational op: must be both pointers */
  1956. if (op >= TOK_ULT && op <= TOK_LOR) {
  1957. check_comparison_pointer_types(vtop - 1, vtop, op);
  1958. /* pointers are handled are unsigned */
  1959. #if PTR_SIZE == 8
  1960. t = VT_LLONG | VT_UNSIGNED;
  1961. #else
  1962. t = VT_INT | VT_UNSIGNED;
  1963. #endif
  1964. goto std_op;
  1965. }
  1966. /* if both pointers, then it must be the '-' op */
  1967. if (bt1 == VT_PTR && bt2 == VT_PTR) {
  1968. if (op != '-')
  1969. tcc_error("cannot use pointers here");
  1970. check_comparison_pointer_types(vtop - 1, vtop, op);
  1971. /* XXX: check that types are compatible */
  1972. if (vtop[-1].type.t & VT_VLA) {
  1973. vla_runtime_pointed_size(&vtop[-1].type);
  1974. } else {
  1975. vpushi(pointed_size(&vtop[-1].type));
  1976. }
  1977. vrott(3);
  1978. gen_opic(op);
  1979. vtop->type.t = ptrdiff_type.t;
  1980. vswap();
  1981. gen_op(TOK_PDIV);
  1982. } else {
  1983. /* exactly one pointer : must be '+' or '-'. */
  1984. if (op != '-' && op != '+')
  1985. tcc_error("cannot use pointers here");
  1986. /* Put pointer as first operand */
  1987. if (bt2 == VT_PTR) {
  1988. vswap();
  1989. t = t1, t1 = t2, t2 = t;
  1990. }
  1991. #if PTR_SIZE == 4
  1992. if ((vtop[0].type.t & VT_BTYPE) == VT_LLONG)
  1993. /* XXX: truncate here because gen_opl can't handle ptr + long long */
  1994. gen_cast_s(VT_INT);
  1995. #endif
  1996. type1 = vtop[-1].type;
  1997. type1.t &= ~VT_ARRAY;
  1998. if (vtop[-1].type.t & VT_VLA)
  1999. vla_runtime_pointed_size(&vtop[-1].type);
  2000. else {
  2001. u = pointed_size(&vtop[-1].type);
  2002. if (u < 0)
  2003. tcc_error("unknown array element size");
  2004. #if PTR_SIZE == 8
  2005. vpushll(u);
  2006. #else
  2007. /* XXX: cast to int ? (long long case) */
  2008. vpushi(u);
  2009. #endif
  2010. }
  2011. gen_op('*');
  2012. #if 0
  2013. /* #ifdef CONFIG_TCC_BCHECK
  2014. The main reason to removing this code:
  2015. #include <stdio.h>
  2016. int main ()
  2017. {
  2018. int v[10];
  2019. int i = 10;
  2020. int j = 9;
  2021. fprintf(stderr, "v+i-j = %p\n", v+i-j);
  2022. fprintf(stderr, "v+(i-j) = %p\n", v+(i-j));
  2023. }
  2024. When this code is on. then the output looks like
  2025. v+i-j = 0xfffffffe
  2026. v+(i-j) = 0xbff84000
  2027. */
  2028. /* if evaluating constant expression, no code should be
  2029. generated, so no bound check */
  2030. if (tcc_state->do_bounds_check && !const_wanted) {
  2031. /* if bounded pointers, we generate a special code to
  2032. test bounds */
  2033. if (op == '-') {
  2034. vpushi(0);
  2035. vswap();
  2036. gen_op('-');
  2037. }
  2038. gen_bounded_ptr_add();
  2039. } else
  2040. #endif
  2041. {
  2042. gen_opic(op);
  2043. }
  2044. /* put again type if gen_opic() swaped operands */
  2045. vtop->type = type1;
  2046. }
  2047. } else if (is_float(bt1) || is_float(bt2)) {
  2048. /* compute bigger type and do implicit casts */
  2049. if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
  2050. t = VT_LDOUBLE;
  2051. } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
  2052. t = VT_DOUBLE;
  2053. } else {
  2054. t = VT_FLOAT;
  2055. }
  2056. /* floats can only be used for a few operations */
  2057. if (op != '+' && op != '-' && op != '*' && op != '/' &&
  2058. (op < TOK_ULT || op > TOK_GT))
  2059. tcc_error("invalid operands for binary operation");
  2060. goto std_op;
  2061. } else if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL) {
  2062. t = bt1 == VT_LLONG ? VT_LLONG : VT_INT;
  2063. if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (t | VT_UNSIGNED))
  2064. t |= VT_UNSIGNED;
  2065. t |= (VT_LONG & t1);
  2066. goto std_op;
  2067. } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
  2068. /* cast to biggest op */
  2069. t = VT_LLONG | VT_LONG;
  2070. if (bt1 == VT_LLONG)
  2071. t &= t1;
  2072. if (bt2 == VT_LLONG)
  2073. t &= t2;
  2074. /* convert to unsigned if it does not fit in a long long */
  2075. if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_LLONG | VT_UNSIGNED) ||
  2076. (t2 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_LLONG | VT_UNSIGNED))
  2077. t |= VT_UNSIGNED;
  2078. goto std_op;
  2079. } else {
  2080. /* integer operations */
  2081. t = VT_INT | (VT_LONG & (t1 | t2));
  2082. /* convert to unsigned if it does not fit in an integer */
  2083. if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_INT | VT_UNSIGNED) ||
  2084. (t2 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_INT | VT_UNSIGNED))
  2085. t |= VT_UNSIGNED;
  2086. std_op:
  2087. /* XXX: currently, some unsigned operations are explicit, so
  2088. we modify them here */
  2089. if (t & VT_UNSIGNED) {
  2090. if (op == TOK_SAR)
  2091. op = TOK_SHR;
  2092. else if (op == '/')
  2093. op = TOK_UDIV;
  2094. else if (op == '%')
  2095. op = TOK_UMOD;
  2096. else if (op == TOK_LT)
  2097. op = TOK_ULT;
  2098. else if (op == TOK_GT)
  2099. op = TOK_UGT;
  2100. else if (op == TOK_LE)
  2101. op = TOK_ULE;
  2102. else if (op == TOK_GE)
  2103. op = TOK_UGE;
  2104. }
  2105. vswap();
  2106. type1.t = t;
  2107. type1.ref = NULL;
  2108. gen_cast(&type1);
  2109. vswap();
  2110. /* special case for shifts and long long: we keep the shift as
  2111. an integer */
  2112. if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
  2113. type1.t = VT_INT;
  2114. gen_cast(&type1);
  2115. if (is_float(t))
  2116. gen_opif(op);
  2117. else
  2118. gen_opic(op);
  2119. if (op >= TOK_ULT && op <= TOK_GT) {
  2120. /* relational op: the result is an int */
  2121. vtop->type.t = VT_INT;
  2122. } else {
  2123. vtop->type.t = t;
  2124. }
  2125. }
  2126. // Make sure that we have converted to an rvalue:
  2127. if (vtop->r & VT_LVAL)
  2128. gv(is_float(vtop->type.t & VT_BTYPE) ? RC_FLOAT : RC_INT);
  2129. }
  2130. #ifndef TCC_TARGET_ARM
  2131. /* generic itof for unsigned long long case */
  2132. static void gen_cvt_itof1(int t)
  2133. {
  2134. #ifdef TCC_TARGET_ARM64
  2135. gen_cvt_itof(t);
  2136. #else
  2137. if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
  2138. (VT_LLONG | VT_UNSIGNED)) {
  2139. if (t == VT_FLOAT)
  2140. vpush_global_sym(&func_old_type, TOK___floatundisf);
  2141. #if LDOUBLE_SIZE != 8
  2142. else if (t == VT_LDOUBLE)
  2143. vpush_global_sym(&func_old_type, TOK___floatundixf);
  2144. #endif
  2145. else
  2146. vpush_global_sym(&func_old_type, TOK___floatundidf);
  2147. vrott(2);
  2148. gfunc_call(1);
  2149. vpushi(0);
  2150. vtop->r = reg_fret(t);
  2151. } else {
  2152. gen_cvt_itof(t);
  2153. }
  2154. #endif
  2155. }
  2156. #endif
  2157. /* generic ftoi for unsigned long long case */
  2158. static void gen_cvt_ftoi1(int t)
  2159. {
  2160. #ifdef TCC_TARGET_ARM64
  2161. gen_cvt_ftoi(t);
  2162. #else
  2163. int st;
  2164. if (t == (VT_LLONG | VT_UNSIGNED)) {
  2165. /* not handled natively */
  2166. st = vtop->type.t & VT_BTYPE;
  2167. if (st == VT_FLOAT)
  2168. vpush_global_sym(&func_old_type, TOK___fixunssfdi);
  2169. #if LDOUBLE_SIZE != 8
  2170. else if (st == VT_LDOUBLE)
  2171. vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
  2172. #endif
  2173. else
  2174. vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
  2175. vrott(2);
  2176. gfunc_call(1);
  2177. vpushi(0);
  2178. vtop->r = REG_IRET;
  2179. vtop->r2 = REG_LRET;
  2180. } else {
  2181. gen_cvt_ftoi(t);
  2182. }
  2183. #endif
  2184. }
  2185. /* force char or short cast */
  2186. static void force_charshort_cast(int t)
  2187. {
  2188. int bits, dbt;
  2189. /* cannot cast static initializers */
  2190. if (STATIC_DATA_WANTED)
  2191. return;
  2192. dbt = t & VT_BTYPE;
  2193. /* XXX: add optimization if lvalue : just change type and offset */
  2194. if (dbt == VT_BYTE)
  2195. bits = 8;
  2196. else
  2197. bits = 16;
  2198. if (t & VT_UNSIGNED) {
  2199. vpushi((1 << bits) - 1);
  2200. gen_op('&');
  2201. } else {
  2202. if ((vtop->type.t & VT_BTYPE) == VT_LLONG)
  2203. bits = 64 - bits;
  2204. else
  2205. bits = 32 - bits;
  2206. vpushi(bits);
  2207. gen_op(TOK_SHL);
  2208. /* result must be signed or the SAR is converted to an SHL
  2209. This was not the case when "t" was a signed short
  2210. and the last value on the stack was an unsigned int */
  2211. vtop->type.t &= ~VT_UNSIGNED;
  2212. vpushi(bits);
  2213. gen_op(TOK_SAR);
  2214. }
  2215. }
  2216. /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
  2217. static void gen_cast_s(int t)
  2218. {
  2219. CType type;
  2220. type.t = t;
  2221. type.ref = NULL;
  2222. gen_cast(&type);
  2223. }
  2224. static void gen_cast(CType *type)
  2225. {
  2226. int sbt, dbt, sf, df, c, p;
  2227. /* special delayed cast for char/short */
  2228. /* XXX: in some cases (multiple cascaded casts), it may still
  2229. be incorrect */
  2230. if (vtop->r & VT_MUSTCAST) {
  2231. vtop->r &= ~VT_MUSTCAST;
  2232. force_charshort_cast(vtop->type.t);
  2233. }
  2234. /* bitfields first get cast to ints */
  2235. if (vtop->type.t & VT_BITFIELD) {
  2236. gv(RC_INT);
  2237. }
  2238. dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
  2239. sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
  2240. if (sbt != dbt) {
  2241. sf = is_float(sbt);
  2242. df = is_float(dbt);
  2243. c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
  2244. p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
  2245. #if !defined TCC_IS_NATIVE && !defined TCC_IS_NATIVE_387
  2246. c &= dbt != VT_LDOUBLE;
  2247. #endif
  2248. if (c) {
  2249. /* constant case: we can do it now */
  2250. /* XXX: in ISOC, cannot do it if error in convert */
  2251. if (sbt == VT_FLOAT)
  2252. vtop->c.ld = vtop->c.f;
  2253. else if (sbt == VT_DOUBLE)
  2254. vtop->c.ld = vtop->c.d;
  2255. if (df) {
  2256. if ((sbt & VT_BTYPE) == VT_LLONG) {
  2257. if ((sbt & VT_UNSIGNED) || !(vtop->c.i >> 63))
  2258. vtop->c.ld = vtop->c.i;
  2259. else
  2260. vtop->c.ld = -(long double)-vtop->c.i;
  2261. } else if(!sf) {
  2262. if ((sbt & VT_UNSIGNED) || !(vtop->c.i >> 31))
  2263. vtop->c.ld = (uint32_t)vtop->c.i;
  2264. else
  2265. vtop->c.ld = -(long double)-(uint32_t)vtop->c.i;
  2266. }
  2267. if (dbt == VT_FLOAT)
  2268. vtop->c.f = (float)vtop->c.ld;
  2269. else if (dbt == VT_DOUBLE)
  2270. vtop->c.d = (double)vtop->c.ld;
  2271. } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
  2272. vtop->c.i = vtop->c.ld;
  2273. } else if (sf && dbt == VT_BOOL) {
  2274. vtop->c.i = (vtop->c.ld != 0);
  2275. } else {
  2276. if(sf)
  2277. vtop->c.i = vtop->c.ld;
  2278. else if (sbt == (VT_LLONG|VT_UNSIGNED))
  2279. ;
  2280. else if (sbt & VT_UNSIGNED)
  2281. vtop->c.i = (uint32_t)vtop->c.i;
  2282. #if PTR_SIZE == 8
  2283. else if (sbt == VT_PTR)
  2284. ;
  2285. #endif
  2286. else if (sbt != VT_LLONG)
  2287. vtop->c.i = ((uint32_t)vtop->c.i |
  2288. -(vtop->c.i & 0x80000000));
  2289. if (dbt == (VT_LLONG|VT_UNSIGNED))
  2290. ;
  2291. else if (dbt == VT_BOOL)
  2292. vtop->c.i = (vtop->c.i != 0);
  2293. #if PTR_SIZE == 8
  2294. else if (dbt == VT_PTR)
  2295. ;
  2296. #endif
  2297. else if (dbt != VT_LLONG) {
  2298. uint32_t m = ((dbt & VT_BTYPE) == VT_BYTE ? 0xff :
  2299. (dbt & VT_BTYPE) == VT_SHORT ? 0xffff :
  2300. 0xffffffff);
  2301. vtop->c.i &= m;
  2302. if (!(dbt & VT_UNSIGNED))
  2303. vtop->c.i |= -(vtop->c.i & ((m >> 1) + 1));
  2304. }
  2305. }
  2306. } else if (p && dbt == VT_BOOL) {
  2307. vtop->r = VT_CONST;
  2308. vtop->c.i = 1;
  2309. } else {
  2310. /* non constant case: generate code */
  2311. if (sf && df) {
  2312. /* convert from fp to fp */
  2313. gen_cvt_ftof(dbt);
  2314. } else if (df) {
  2315. /* convert int to fp */
  2316. gen_cvt_itof1(dbt);
  2317. } else if (sf) {
  2318. /* convert fp to int */
  2319. if (dbt == VT_BOOL) {
  2320. vpushi(0);
  2321. gen_op(TOK_NE);
  2322. } else {
  2323. /* we handle char/short/etc... with generic code */
  2324. if (dbt != (VT_INT | VT_UNSIGNED) &&
  2325. dbt != (VT_LLONG | VT_UNSIGNED) &&
  2326. dbt != VT_LLONG)
  2327. dbt = VT_INT;
  2328. gen_cvt_ftoi1(dbt);
  2329. if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
  2330. /* additional cast for char/short... */
  2331. vtop->type.t = dbt;
  2332. gen_cast(type);
  2333. }
  2334. }
  2335. #if PTR_SIZE == 4
  2336. } else if ((dbt & VT_BTYPE) == VT_LLONG) {
  2337. if ((sbt & VT_BTYPE) != VT_LLONG) {
  2338. /* scalar to long long */
  2339. /* machine independent conversion */
  2340. gv(RC_INT);
  2341. /* generate high word */
  2342. if (sbt == (VT_INT | VT_UNSIGNED)) {
  2343. vpushi(0);
  2344. gv(RC_INT);
  2345. } else {
  2346. if (sbt == VT_PTR) {
  2347. /* cast from pointer to int before we apply
  2348. shift operation, which pointers don't support*/
  2349. gen_cast_s(VT_INT);
  2350. }
  2351. gv_dup();
  2352. vpushi(31);
  2353. gen_op(TOK_SAR);
  2354. }
  2355. /* patch second register */
  2356. vtop[-1].r2 = vtop->r;
  2357. vpop();
  2358. }
  2359. #else
  2360. } else if ((dbt & VT_BTYPE) == VT_LLONG ||
  2361. (dbt & VT_BTYPE) == VT_PTR ||
  2362. (dbt & VT_BTYPE) == VT_FUNC) {
  2363. if ((sbt & VT_BTYPE) != VT_LLONG &&
  2364. (sbt & VT_BTYPE) != VT_PTR &&
  2365. (sbt & VT_BTYPE) != VT_FUNC) {
  2366. /* need to convert from 32bit to 64bit */
  2367. gv(RC_INT);
  2368. if (sbt != (VT_INT | VT_UNSIGNED)) {
  2369. #if defined(TCC_TARGET_ARM64)
  2370. gen_cvt_sxtw();
  2371. #elif defined(TCC_TARGET_X86_64)
  2372. int r = gv(RC_INT);
  2373. /* x86_64 specific: movslq */
  2374. o(0x6348);
  2375. o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
  2376. #else
  2377. #error
  2378. #endif
  2379. }
  2380. }
  2381. #endif
  2382. } else if (dbt == VT_BOOL) {
  2383. /* scalar to bool */
  2384. vpushi(0);
  2385. gen_op(TOK_NE);
  2386. } else if ((dbt & VT_BTYPE) == VT_BYTE ||
  2387. (dbt & VT_BTYPE) == VT_SHORT) {
  2388. if (sbt == VT_PTR) {
  2389. vtop->type.t = VT_INT;
  2390. tcc_warning("nonportable conversion from pointer to char/short");
  2391. }
  2392. force_charshort_cast(dbt);
  2393. #if PTR_SIZE == 4
  2394. } else if ((dbt & VT_BTYPE) == VT_INT) {
  2395. /* scalar to int */
  2396. if ((sbt & VT_BTYPE) == VT_LLONG) {
  2397. /* from long long: just take low order word */
  2398. lexpand();
  2399. vpop();
  2400. }
  2401. /* if lvalue and single word type, nothing to do because
  2402. the lvalue already contains the real type size (see
  2403. VT_LVAL_xxx constants) */
  2404. #endif
  2405. }
  2406. }
  2407. } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
  2408. /* if we are casting between pointer types,
  2409. we must update the VT_LVAL_xxx size */
  2410. vtop->r = (vtop->r & ~VT_LVAL_TYPE)
  2411. | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
  2412. }
  2413. vtop->type = *type;
  2414. }
  2415. /* return type size as known at compile time. Put alignment at 'a' */
  2416. ST_FUNC int type_size(CType *type, int *a)
  2417. {
  2418. Sym *s;
  2419. int bt;
  2420. bt = type->t & VT_BTYPE;
  2421. if (bt == VT_STRUCT) {
  2422. /* struct/union */
  2423. s = type->ref;
  2424. *a = s->r;
  2425. return s->c;
  2426. } else if (bt == VT_PTR) {
  2427. if (type->t & VT_ARRAY) {
  2428. int ts;
  2429. s = type->ref;
  2430. ts = type_size(&s->type, a);
  2431. if (ts < 0 && s->c < 0)
  2432. ts = -ts;
  2433. return ts * s->c;
  2434. } else {
  2435. *a = PTR_SIZE;
  2436. return PTR_SIZE;
  2437. }
  2438. } else if (IS_ENUM(type->t) && type->ref->c == -1) {
  2439. return -1; /* incomplete enum */
  2440. } else if (bt == VT_LDOUBLE) {
  2441. *a = LDOUBLE_ALIGN;
  2442. return LDOUBLE_SIZE;
  2443. } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
  2444. #ifdef TCC_TARGET_I386
  2445. #ifdef TCC_TARGET_PE
  2446. *a = 8;
  2447. #else
  2448. *a = 4;
  2449. #endif
  2450. #elif defined(TCC_TARGET_ARM)
  2451. #ifdef TCC_ARM_EABI
  2452. *a = 8;
  2453. #else
  2454. *a = 4;
  2455. #endif
  2456. #else
  2457. *a = 8;
  2458. #endif
  2459. return 8;
  2460. } else if (bt == VT_INT || bt == VT_FLOAT) {
  2461. *a = 4;
  2462. return 4;
  2463. } else if (bt == VT_SHORT) {
  2464. *a = 2;
  2465. return 2;
  2466. } else if (bt == VT_QLONG || bt == VT_QFLOAT) {
  2467. *a = 8;
  2468. return 16;
  2469. } else {
  2470. /* char, void, function, _Bool */
  2471. *a = 1;
  2472. return 1;
  2473. }
  2474. }
  2475. /* push type size as known at runtime time on top of value stack. Put
  2476. alignment at 'a' */
  2477. ST_FUNC void vla_runtime_type_size(CType *type, int *a)
  2478. {
  2479. if (type->t & VT_VLA) {
  2480. type_size(&type->ref->type, a);
  2481. vset(&int_type, VT_LOCAL|VT_LVAL, type->ref->c);
  2482. } else {
  2483. vpushi(type_size(type, a));
  2484. }
  2485. }
  2486. static void vla_sp_restore(void) {
  2487. if (vlas_in_scope) {
  2488. gen_vla_sp_restore(vla_sp_loc);
  2489. }
  2490. }
  2491. static void vla_sp_restore_root(void) {
  2492. if (vlas_in_scope) {
  2493. gen_vla_sp_restore(vla_sp_root_loc);
  2494. }
  2495. }
  2496. /* return the pointed type of t */
  2497. static inline CType *pointed_type(CType *type)
  2498. {
  2499. return &type->ref->type;
  2500. }
  2501. /* modify type so that its it is a pointer to type. */
  2502. ST_FUNC void mk_pointer(CType *type)
  2503. {
  2504. Sym *s;
  2505. s = sym_push(SYM_FIELD, type, 0, -1);
  2506. type->t = VT_PTR | (type->t & VT_STORAGE);
  2507. type->ref = s;
  2508. }
  2509. /* compare function types. OLD functions match any new functions */
  2510. static int is_compatible_func(CType *type1, CType *type2)
  2511. {
  2512. Sym *s1, *s2;
  2513. s1 = type1->ref;
  2514. s2 = type2->ref;
  2515. if (!is_compatible_types(&s1->type, &s2->type))
  2516. return 0;
  2517. /* check func_call */
  2518. if (s1->f.func_call != s2->f.func_call)
  2519. return 0;
  2520. /* XXX: not complete */
  2521. if (s1->f.func_type == FUNC_OLD || s2->f.func_type == FUNC_OLD)
  2522. return 1;
  2523. if (s1->f.func_type != s2->f.func_type)
  2524. return 0;
  2525. while (s1 != NULL) {
  2526. if (s2 == NULL)
  2527. return 0;
  2528. if (!is_compatible_unqualified_types(&s1->type, &s2->type))
  2529. return 0;
  2530. s1 = s1->next;
  2531. s2 = s2->next;
  2532. }
  2533. if (s2)
  2534. return 0;
  2535. return 1;
  2536. }
  2537. /* return true if type1 and type2 are the same. If unqualified is
  2538. true, qualifiers on the types are ignored.
  2539. - enums are not checked as gcc __builtin_types_compatible_p ()
  2540. */
  2541. static int compare_types(CType *type1, CType *type2, int unqualified)
  2542. {
  2543. int bt1, t1, t2;
  2544. t1 = type1->t & VT_TYPE;
  2545. t2 = type2->t & VT_TYPE;
  2546. if (unqualified) {
  2547. /* strip qualifiers before comparing */
  2548. t1 &= ~(VT_CONSTANT | VT_VOLATILE);
  2549. t2 &= ~(VT_CONSTANT | VT_VOLATILE);
  2550. }
  2551. /* Default Vs explicit signedness only matters for char */
  2552. if ((t1 & VT_BTYPE) != VT_BYTE) {
  2553. t1 &= ~VT_DEFSIGN;
  2554. t2 &= ~VT_DEFSIGN;
  2555. }
  2556. /* XXX: bitfields ? */
  2557. if (t1 != t2)
  2558. return 0;
  2559. /* test more complicated cases */
  2560. bt1 = t1 & VT_BTYPE;
  2561. if (bt1 == VT_PTR) {
  2562. type1 = pointed_type(type1);
  2563. type2 = pointed_type(type2);
  2564. return is_compatible_types(type1, type2);
  2565. } else if (bt1 == VT_STRUCT) {
  2566. return (type1->ref == type2->ref);
  2567. } else if (bt1 == VT_FUNC) {
  2568. return is_compatible_func(type1, type2);
  2569. } else {
  2570. return 1;
  2571. }
  2572. }
  2573. /* return true if type1 and type2 are exactly the same (including
  2574. qualifiers).
  2575. */
  2576. static int is_compatible_types(CType *type1, CType *type2)
  2577. {
  2578. return compare_types(type1,type2,0);
  2579. }
  2580. /* return true if type1 and type2 are the same (ignoring qualifiers).
  2581. */
  2582. static int is_compatible_unqualified_types(CType *type1, CType *type2)
  2583. {
  2584. return compare_types(type1,type2,1);
  2585. }
  2586. /* print a type. If 'varstr' is not NULL, then the variable is also
  2587. printed in the type */
  2588. /* XXX: union */
  2589. /* XXX: add array and function pointers */
  2590. static void type_to_str(char *buf, int buf_size,
  2591. CType *type, const char *varstr)
  2592. {
  2593. int bt, v, t;
  2594. Sym *s, *sa;
  2595. char buf1[256];
  2596. const char *tstr;
  2597. t = type->t;
  2598. bt = t & VT_BTYPE;
  2599. buf[0] = '\0';
  2600. if (t & VT_EXTERN)
  2601. pstrcat(buf, buf_size, "extern ");
  2602. if (t & VT_STATIC)
  2603. pstrcat(buf, buf_size, "static ");
  2604. if (t & VT_TYPEDEF)
  2605. pstrcat(buf, buf_size, "typedef ");
  2606. if (t & VT_INLINE)
  2607. pstrcat(buf, buf_size, "inline ");
  2608. if (t & VT_VOLATILE)
  2609. pstrcat(buf, buf_size, "volatile ");
  2610. if (t & VT_CONSTANT)
  2611. pstrcat(buf, buf_size, "const ");
  2612. if (((t & VT_DEFSIGN) && bt == VT_BYTE)
  2613. || ((t & VT_UNSIGNED)
  2614. && (bt == VT_SHORT || bt == VT_INT || bt == VT_LLONG)
  2615. && !IS_ENUM(t)
  2616. ))
  2617. pstrcat(buf, buf_size, (t & VT_UNSIGNED) ? "unsigned " : "signed ");
  2618. buf_size -= strlen(buf);
  2619. buf += strlen(buf);
  2620. switch(bt) {
  2621. case VT_VOID:
  2622. tstr = "void";
  2623. goto add_tstr;
  2624. case VT_BOOL:
  2625. tstr = "_Bool";
  2626. goto add_tstr;
  2627. case VT_BYTE:
  2628. tstr = "char";
  2629. goto add_tstr;
  2630. case VT_SHORT:
  2631. tstr = "short";
  2632. goto add_tstr;
  2633. case VT_INT:
  2634. tstr = "int";
  2635. goto maybe_long;
  2636. case VT_LLONG:
  2637. tstr = "long long";
  2638. maybe_long:
  2639. if (t & VT_LONG)
  2640. tstr = "long";
  2641. if (!IS_ENUM(t))
  2642. goto add_tstr;
  2643. tstr = "enum ";
  2644. goto tstruct;
  2645. case VT_FLOAT:
  2646. tstr = "float";
  2647. goto add_tstr;
  2648. case VT_DOUBLE:
  2649. tstr = "double";
  2650. goto add_tstr;
  2651. case VT_LDOUBLE:
  2652. tstr = "long double";
  2653. add_tstr:
  2654. pstrcat(buf, buf_size, tstr);
  2655. break;
  2656. case VT_STRUCT:
  2657. tstr = "struct ";
  2658. if (IS_UNION(t))
  2659. tstr = "union ";
  2660. tstruct:
  2661. pstrcat(buf, buf_size, tstr);
  2662. v = type->ref->v & ~SYM_STRUCT;
  2663. if (v >= SYM_FIRST_ANOM)
  2664. pstrcat(buf, buf_size, "<anonymous>");
  2665. else
  2666. pstrcat(buf, buf_size, get_tok_str(v, NULL));
  2667. break;
  2668. case VT_FUNC:
  2669. s = type->ref;
  2670. type_to_str(buf, buf_size, &s->type, varstr);
  2671. pstrcat(buf, buf_size, "(");
  2672. sa = s->next;
  2673. while (sa != NULL) {
  2674. type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
  2675. pstrcat(buf, buf_size, buf1);
  2676. sa = sa->next;
  2677. if (sa)
  2678. pstrcat(buf, buf_size, ", ");
  2679. }
  2680. pstrcat(buf, buf_size, ")");
  2681. goto no_var;
  2682. case VT_PTR:
  2683. s = type->ref;
  2684. if (t & VT_ARRAY) {
  2685. snprintf(buf1, sizeof(buf1), "%s[%d]", varstr ? varstr : "", s->c);
  2686. type_to_str(buf, buf_size, &s->type, buf1);
  2687. goto no_var;
  2688. }
  2689. pstrcpy(buf1, sizeof(buf1), "*");
  2690. if (t & VT_CONSTANT)
  2691. pstrcat(buf1, buf_size, "const ");
  2692. if (t & VT_VOLATILE)
  2693. pstrcat(buf1, buf_size, "volatile ");
  2694. if (varstr)
  2695. pstrcat(buf1, sizeof(buf1), varstr);
  2696. type_to_str(buf, buf_size, &s->type, buf1);
  2697. goto no_var;
  2698. }
  2699. if (varstr) {
  2700. pstrcat(buf, buf_size, " ");
  2701. pstrcat(buf, buf_size, varstr);
  2702. }
  2703. no_var: ;
  2704. }
  2705. /* verify type compatibility to store vtop in 'dt' type, and generate
  2706. casts if needed. */
  2707. static void gen_assign_cast(CType *dt)
  2708. {
  2709. CType *st, *type1, *type2;
  2710. char buf1[256], buf2[256];
  2711. int dbt, sbt;
  2712. st = &vtop->type; /* source type */
  2713. dbt = dt->t & VT_BTYPE;
  2714. sbt = st->t & VT_BTYPE;
  2715. if (sbt == VT_VOID || dbt == VT_VOID) {
  2716. if (sbt == VT_VOID && dbt == VT_VOID)
  2717. ; /*
  2718. It is Ok if both are void
  2719. A test program:
  2720. void func1() {}
  2721. void func2() {
  2722. return func1();
  2723. }
  2724. gcc accepts this program
  2725. */
  2726. else
  2727. tcc_error("cannot cast from/to void");
  2728. }
  2729. if (dt->t & VT_CONSTANT)
  2730. tcc_warning("assignment of read-only location");
  2731. switch(dbt) {
  2732. case VT_PTR:
  2733. /* special cases for pointers */
  2734. /* '0' can also be a pointer */
  2735. if (is_null_pointer(vtop))
  2736. goto type_ok;
  2737. /* accept implicit pointer to integer cast with warning */
  2738. if (is_integer_btype(sbt)) {
  2739. tcc_warning("assignment makes pointer from integer without a cast");
  2740. goto type_ok;
  2741. }
  2742. type1 = pointed_type(dt);
  2743. /* a function is implicitly a function pointer */
  2744. if (sbt == VT_FUNC) {
  2745. if ((type1->t & VT_BTYPE) != VT_VOID &&
  2746. !is_compatible_types(pointed_type(dt), st))
  2747. tcc_warning("assignment from incompatible pointer type");
  2748. goto type_ok;
  2749. }
  2750. if (sbt != VT_PTR)
  2751. goto error;
  2752. type2 = pointed_type(st);
  2753. if ((type1->t & VT_BTYPE) == VT_VOID ||
  2754. (type2->t & VT_BTYPE) == VT_VOID) {
  2755. /* void * can match anything */
  2756. } else {
  2757. //printf("types %08x %08x\n", type1->t, type2->t);
  2758. /* exact type match, except for qualifiers */
  2759. if (!is_compatible_unqualified_types(type1, type2)) {
  2760. /* Like GCC don't warn by default for merely changes
  2761. in pointer target signedness. Do warn for different
  2762. base types, though, in particular for unsigned enums
  2763. and signed int targets. */
  2764. if ((type1->t & (VT_BTYPE|VT_LONG)) != (type2->t & (VT_BTYPE|VT_LONG))
  2765. || IS_ENUM(type1->t) || IS_ENUM(type2->t)
  2766. )
  2767. tcc_warning("assignment from incompatible pointer type");
  2768. }
  2769. }
  2770. /* check const and volatile */
  2771. if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
  2772. (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
  2773. tcc_warning("assignment discards qualifiers from pointer target type");
  2774. break;
  2775. case VT_BYTE:
  2776. case VT_SHORT:
  2777. case VT_INT:
  2778. case VT_LLONG:
  2779. if (sbt == VT_PTR || sbt == VT_FUNC) {
  2780. tcc_warning("assignment makes integer from pointer without a cast");
  2781. } else if (sbt == VT_STRUCT) {
  2782. goto case_VT_STRUCT;
  2783. }
  2784. /* XXX: more tests */
  2785. break;
  2786. case VT_STRUCT:
  2787. case_VT_STRUCT:
  2788. if (!is_compatible_unqualified_types(dt, st)) {
  2789. error:
  2790. type_to_str(buf1, sizeof(buf1), st, NULL);
  2791. type_to_str(buf2, sizeof(buf2), dt, NULL);
  2792. tcc_error("cannot cast '%s' to '%s'", buf1, buf2);
  2793. }
  2794. break;
  2795. }
  2796. type_ok:
  2797. gen_cast(dt);
  2798. }
  2799. /* store vtop in lvalue pushed on stack */
  2800. ST_FUNC void vstore(void)
  2801. {
  2802. int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
  2803. ft = vtop[-1].type.t;
  2804. sbt = vtop->type.t & VT_BTYPE;
  2805. dbt = ft & VT_BTYPE;
  2806. if ((((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
  2807. (sbt == VT_INT && dbt == VT_SHORT))
  2808. && !(vtop->type.t & VT_BITFIELD)) {
  2809. /* optimize char/short casts */
  2810. delayed_cast = VT_MUSTCAST;
  2811. vtop->type.t = ft & VT_TYPE;
  2812. /* XXX: factorize */
  2813. if (ft & VT_CONSTANT)
  2814. tcc_warning("assignment of read-only location");
  2815. } else {
  2816. delayed_cast = 0;
  2817. if (!(ft & VT_BITFIELD))
  2818. gen_assign_cast(&vtop[-1].type);
  2819. }
  2820. if (sbt == VT_STRUCT) {
  2821. /* if structure, only generate pointer */
  2822. /* structure assignment : generate memcpy */
  2823. /* XXX: optimize if small size */
  2824. size = type_size(&vtop->type, &align);
  2825. /* destination */
  2826. vswap();
  2827. vtop->type.t = VT_PTR;
  2828. gaddrof();
  2829. /* address of memcpy() */
  2830. #ifdef TCC_ARM_EABI
  2831. if(!(align & 7))
  2832. vpush_global_sym(&func_old_type, TOK_memcpy8);
  2833. else if(!(align & 3))
  2834. vpush_global_sym(&func_old_type, TOK_memcpy4);
  2835. else
  2836. #endif
  2837. /* Use memmove, rather than memcpy, as dest and src may be same: */
  2838. vpush_global_sym(&func_old_type, TOK_memmove);
  2839. vswap();
  2840. /* source */
  2841. vpushv(vtop - 2);
  2842. vtop->type.t = VT_PTR;
  2843. gaddrof();
  2844. /* type size */
  2845. vpushi(size);
  2846. gfunc_call(3);
  2847. /* leave source on stack */
  2848. } else if (ft & VT_BITFIELD) {
  2849. /* bitfield store handling */
  2850. /* save lvalue as expression result (example: s.b = s.a = n;) */
  2851. vdup(), vtop[-1] = vtop[-2];
  2852. bit_pos = BIT_POS(ft);
  2853. bit_size = BIT_SIZE(ft);
  2854. /* remove bit field info to avoid loops */
  2855. vtop[-1].type.t = ft & ~VT_STRUCT_MASK;
  2856. if ((ft & VT_BTYPE) == VT_BOOL) {
  2857. gen_cast(&vtop[-1].type);
  2858. vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
  2859. }
  2860. r = adjust_bf(vtop - 1, bit_pos, bit_size);
  2861. if (r == VT_STRUCT) {
  2862. gen_cast_s((ft & VT_BTYPE) == VT_LLONG ? VT_LLONG : VT_INT);
  2863. store_packed_bf(bit_pos, bit_size);
  2864. } else {
  2865. unsigned long long mask = (1ULL << bit_size) - 1;
  2866. if ((ft & VT_BTYPE) != VT_BOOL) {
  2867. /* mask source */
  2868. if ((vtop[-1].type.t & VT_BTYPE) == VT_LLONG)
  2869. vpushll(mask);
  2870. else
  2871. vpushi((unsigned)mask);
  2872. gen_op('&');
  2873. }
  2874. /* shift source */
  2875. vpushi(bit_pos);
  2876. gen_op(TOK_SHL);
  2877. vswap();
  2878. /* duplicate destination */
  2879. vdup();
  2880. vrott(3);
  2881. /* load destination, mask and or with source */
  2882. if ((vtop->type.t & VT_BTYPE) == VT_LLONG)
  2883. vpushll(~(mask << bit_pos));
  2884. else
  2885. vpushi(~((unsigned)mask << bit_pos));
  2886. gen_op('&');
  2887. gen_op('|');
  2888. /* store result */
  2889. vstore();
  2890. /* ... and discard */
  2891. vpop();
  2892. }
  2893. } else if (dbt == VT_VOID) {
  2894. --vtop;
  2895. } else {
  2896. #ifdef CONFIG_TCC_BCHECK
  2897. /* bound check case */
  2898. if (vtop[-1].r & VT_MUSTBOUND) {
  2899. vswap();
  2900. gbound();
  2901. vswap();
  2902. }
  2903. #endif
  2904. rc = RC_INT;
  2905. if (is_float(ft)) {
  2906. rc = RC_FLOAT;
  2907. #ifdef TCC_TARGET_X86_64
  2908. if ((ft & VT_BTYPE) == VT_LDOUBLE) {
  2909. rc = RC_ST0;
  2910. } else if ((ft & VT_BTYPE) == VT_QFLOAT) {
  2911. rc = RC_FRET;
  2912. }
  2913. #endif
  2914. }
  2915. r = gv(rc); /* generate value */
  2916. /* if lvalue was saved on stack, must read it */
  2917. if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
  2918. SValue sv;
  2919. t = get_reg(RC_INT);
  2920. #if PTR_SIZE == 8
  2921. sv.type.t = VT_PTR;
  2922. #else
  2923. sv.type.t = VT_INT;
  2924. #endif
  2925. sv.r = VT_LOCAL | VT_LVAL;
  2926. sv.c.i = vtop[-1].c.i;
  2927. load(t, &sv);
  2928. vtop[-1].r = t | VT_LVAL;
  2929. }
  2930. /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
  2931. #if PTR_SIZE == 8
  2932. if (((ft & VT_BTYPE) == VT_QLONG) || ((ft & VT_BTYPE) == VT_QFLOAT)) {
  2933. int addr_type = VT_LLONG, load_size = 8, load_type = ((vtop->type.t & VT_BTYPE) == VT_QLONG) ? VT_LLONG : VT_DOUBLE;
  2934. #else
  2935. if ((ft & VT_BTYPE) == VT_LLONG) {
  2936. int addr_type = VT_INT, load_size = 4, load_type = VT_INT;
  2937. #endif
  2938. vtop[-1].type.t = load_type;
  2939. store(r, vtop - 1);
  2940. vswap();
  2941. /* convert to int to increment easily */
  2942. vtop->type.t = addr_type;
  2943. gaddrof();
  2944. vpushi(load_size);
  2945. gen_op('+');
  2946. vtop->r |= VT_LVAL;
  2947. vswap();
  2948. vtop[-1].type.t = load_type;
  2949. /* XXX: it works because r2 is spilled last ! */
  2950. store(vtop->r2, vtop - 1);
  2951. } else {
  2952. store(r, vtop - 1);
  2953. }
  2954. vswap();
  2955. vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
  2956. vtop->r |= delayed_cast;
  2957. }
  2958. }
  2959. /* post defines POST/PRE add. c is the token ++ or -- */
  2960. ST_FUNC void inc(int post, int c)
  2961. {
  2962. test_lvalue();
  2963. vdup(); /* save lvalue */
  2964. if (post) {
  2965. gv_dup(); /* duplicate value */
  2966. vrotb(3);
  2967. vrotb(3);
  2968. }
  2969. /* add constant */
  2970. vpushi(c - TOK_MID);
  2971. gen_op('+');
  2972. vstore(); /* store value */
  2973. if (post)
  2974. vpop(); /* if post op, return saved value */
  2975. }
  2976. ST_FUNC void parse_mult_str (CString *astr, const char *msg)
  2977. {
  2978. /* read the string */
  2979. if (tok != TOK_STR)
  2980. expect(msg);
  2981. cstr_new(astr);
  2982. while (tok == TOK_STR) {
  2983. /* XXX: add \0 handling too ? */
  2984. cstr_cat(astr, tokc.str.data, -1);
  2985. next();
  2986. }
  2987. cstr_ccat(astr, '\0');
  2988. }
  2989. /* If I is >= 1 and a power of two, returns log2(i)+1.
  2990. If I is 0 returns 0. */
  2991. static int exact_log2p1(int i)
  2992. {
  2993. int ret;
  2994. if (!i)
  2995. return 0;
  2996. for (ret = 1; i >= 1 << 8; ret += 8)
  2997. i >>= 8;
  2998. if (i >= 1 << 4)
  2999. ret += 4, i >>= 4;
  3000. if (i >= 1 << 2)
  3001. ret += 2, i >>= 2;
  3002. if (i >= 1 << 1)
  3003. ret++;
  3004. return ret;
  3005. }
  3006. /* Parse __attribute__((...)) GNUC extension. */
  3007. static void parse_attribute(AttributeDef *ad)
  3008. {
  3009. int t, n;
  3010. CString astr;
  3011. redo:
  3012. if (tok != TOK_ATTRIBUTE1 && tok != TOK_ATTRIBUTE2)
  3013. return;
  3014. next();
  3015. skip('(');
  3016. skip('(');
  3017. while (tok != ')') {
  3018. if (tok < TOK_IDENT)
  3019. expect("attribute name");
  3020. t = tok;
  3021. next();
  3022. switch(t) {
  3023. case TOK_SECTION1:
  3024. case TOK_SECTION2:
  3025. skip('(');
  3026. parse_mult_str(&astr, "section name");
  3027. ad->section = find_section(tcc_state, (char *)astr.data);
  3028. skip(')');
  3029. cstr_free(&astr);
  3030. break;
  3031. case TOK_ALIAS1:
  3032. case TOK_ALIAS2:
  3033. skip('(');
  3034. parse_mult_str(&astr, "alias(\"target\")");
  3035. ad->alias_target = /* save string as token, for later */
  3036. tok_alloc((char*)astr.data, astr.size-1)->tok;
  3037. skip(')');
  3038. cstr_free(&astr);
  3039. break;
  3040. case TOK_VISIBILITY1:
  3041. case TOK_VISIBILITY2:
  3042. skip('(');
  3043. parse_mult_str(&astr,
  3044. "visibility(\"default|hidden|internal|protected\")");
  3045. if (!strcmp (astr.data, "default"))
  3046. ad->a.visibility = STV_DEFAULT;
  3047. else if (!strcmp (astr.data, "hidden"))
  3048. ad->a.visibility = STV_HIDDEN;
  3049. else if (!strcmp (astr.data, "internal"))
  3050. ad->a.visibility = STV_INTERNAL;
  3051. else if (!strcmp (astr.data, "protected"))
  3052. ad->a.visibility = STV_PROTECTED;
  3053. else
  3054. expect("visibility(\"default|hidden|internal|protected\")");
  3055. skip(')');
  3056. cstr_free(&astr);
  3057. break;
  3058. case TOK_ALIGNED1:
  3059. case TOK_ALIGNED2:
  3060. if (tok == '(') {
  3061. next();
  3062. n = expr_const();
  3063. if (n <= 0 || (n & (n - 1)) != 0)
  3064. tcc_error("alignment must be a positive power of two");
  3065. skip(')');
  3066. } else {
  3067. n = MAX_ALIGN;
  3068. }
  3069. ad->a.aligned = exact_log2p1(n);
  3070. if (n != 1 << (ad->a.aligned - 1))
  3071. tcc_error("alignment of %d is larger than implemented", n);
  3072. break;
  3073. case TOK_PACKED1:
  3074. case TOK_PACKED2:
  3075. ad->a.packed = 1;
  3076. break;
  3077. case TOK_WEAK1:
  3078. case TOK_WEAK2:
  3079. ad->a.weak = 1;
  3080. break;
  3081. case TOK_UNUSED1:
  3082. case TOK_UNUSED2:
  3083. /* currently, no need to handle it because tcc does not
  3084. track unused objects */
  3085. break;
  3086. case TOK_NORETURN1:
  3087. case TOK_NORETURN2:
  3088. /* currently, no need to handle it because tcc does not
  3089. track unused objects */
  3090. break;
  3091. case TOK_CDECL1:
  3092. case TOK_CDECL2:
  3093. case TOK_CDECL3:
  3094. ad->f.func_call = FUNC_CDECL;
  3095. break;
  3096. case TOK_STDCALL1:
  3097. case TOK_STDCALL2:
  3098. case TOK_STDCALL3:
  3099. ad->f.func_call = FUNC_STDCALL;
  3100. break;
  3101. #ifdef TCC_TARGET_I386
  3102. case TOK_REGPARM1:
  3103. case TOK_REGPARM2:
  3104. skip('(');
  3105. n = expr_const();
  3106. if (n > 3)
  3107. n = 3;
  3108. else if (n < 0)
  3109. n = 0;
  3110. if (n > 0)
  3111. ad->f.func_call = FUNC_FASTCALL1 + n - 1;
  3112. skip(')');
  3113. break;
  3114. case TOK_FASTCALL1:
  3115. case TOK_FASTCALL2:
  3116. case TOK_FASTCALL3:
  3117. ad->f.func_call = FUNC_FASTCALLW;
  3118. break;
  3119. #endif
  3120. case TOK_MODE:
  3121. skip('(');
  3122. switch(tok) {
  3123. case TOK_MODE_DI:
  3124. ad->attr_mode = VT_LLONG + 1;
  3125. break;
  3126. case TOK_MODE_QI:
  3127. ad->attr_mode = VT_BYTE + 1;
  3128. break;
  3129. case TOK_MODE_HI:
  3130. ad->attr_mode = VT_SHORT + 1;
  3131. break;
  3132. case TOK_MODE_SI:
  3133. case TOK_MODE_word:
  3134. ad->attr_mode = VT_INT + 1;
  3135. break;
  3136. default:
  3137. tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok, NULL));
  3138. break;
  3139. }
  3140. next();
  3141. skip(')');
  3142. break;
  3143. case TOK_DLLEXPORT:
  3144. ad->a.dllexport = 1;
  3145. break;
  3146. case TOK_DLLIMPORT:
  3147. ad->a.dllimport = 1;
  3148. break;
  3149. default:
  3150. if (tcc_state->warn_unsupported)
  3151. tcc_warning("'%s' attribute ignored", get_tok_str(t, NULL));
  3152. /* skip parameters */
  3153. if (tok == '(') {
  3154. int parenthesis = 0;
  3155. do {
  3156. if (tok == '(')
  3157. parenthesis++;
  3158. else if (tok == ')')
  3159. parenthesis--;
  3160. next();
  3161. } while (parenthesis && tok != -1);
  3162. }
  3163. break;
  3164. }
  3165. if (tok != ',')
  3166. break;
  3167. next();
  3168. }
  3169. skip(')');
  3170. skip(')');
  3171. goto redo;
  3172. }
  3173. static Sym * find_field (CType *type, int v)
  3174. {
  3175. Sym *s = type->ref;
  3176. v |= SYM_FIELD;
  3177. while ((s = s->next) != NULL) {
  3178. if ((s->v & SYM_FIELD) &&
  3179. (s->type.t & VT_BTYPE) == VT_STRUCT &&
  3180. (s->v & ~SYM_FIELD) >= SYM_FIRST_ANOM) {
  3181. Sym *ret = find_field (&s->type, v);
  3182. if (ret)
  3183. return ret;
  3184. }
  3185. if (s->v == v)
  3186. break;
  3187. }
  3188. return s;
  3189. }
  3190. static void struct_add_offset (Sym *s, int offset)
  3191. {
  3192. while ((s = s->next) != NULL) {
  3193. if ((s->v & SYM_FIELD) &&
  3194. (s->type.t & VT_BTYPE) == VT_STRUCT &&
  3195. (s->v & ~SYM_FIELD) >= SYM_FIRST_ANOM) {
  3196. struct_add_offset(s->type.ref, offset);
  3197. } else
  3198. s->c += offset;
  3199. }
  3200. }
  3201. static void struct_layout(CType *type, AttributeDef *ad)
  3202. {
  3203. int size, align, maxalign, offset, c, bit_pos, bit_size;
  3204. int packed, a, bt, prevbt, prev_bit_size;
  3205. int pcc = !tcc_state->ms_bitfields;
  3206. int pragma_pack = *tcc_state->pack_stack_ptr;
  3207. Sym *f;
  3208. maxalign = 1;
  3209. offset = 0;
  3210. c = 0;
  3211. bit_pos = 0;
  3212. prevbt = VT_STRUCT; /* make it never match */
  3213. prev_bit_size = 0;
  3214. //#define BF_DEBUG
  3215. for (f = type->ref->next; f; f = f->next) {
  3216. if (f->type.t & VT_BITFIELD)
  3217. bit_size = BIT_SIZE(f->type.t);
  3218. else
  3219. bit_size = -1;
  3220. size = type_size(&f->type, &align);
  3221. a = f->a.aligned ? 1 << (f->a.aligned - 1) : 0;
  3222. packed = 0;
  3223. if (pcc && bit_size == 0) {
  3224. /* in pcc mode, packing does not affect zero-width bitfields */
  3225. } else {
  3226. /* in pcc mode, attribute packed overrides if set. */
  3227. if (pcc && (f->a.packed || ad->a.packed))
  3228. align = packed = 1;
  3229. /* pragma pack overrides align if lesser and packs bitfields always */
  3230. if (pragma_pack) {
  3231. packed = 1;
  3232. if (pragma_pack < align)
  3233. align = pragma_pack;
  3234. /* in pcc mode pragma pack also overrides individual align */
  3235. if (pcc && pragma_pack < a)
  3236. a = 0;
  3237. }
  3238. }
  3239. /* some individual align was specified */
  3240. if (a)
  3241. align = a;
  3242. if (type->ref->type.t == VT_UNION) {
  3243. if (pcc && bit_size >= 0)
  3244. size = (bit_size + 7) >> 3;
  3245. offset = 0;
  3246. if (size > c)
  3247. c = size;
  3248. } else if (bit_size < 0) {
  3249. if (pcc)
  3250. c += (bit_pos + 7) >> 3;
  3251. c = (c + align - 1) & -align;
  3252. offset = c;
  3253. if (size > 0)
  3254. c += size;
  3255. bit_pos = 0;
  3256. prevbt = VT_STRUCT;
  3257. prev_bit_size = 0;
  3258. } else {
  3259. /* A bit-field. Layout is more complicated. There are two
  3260. options: PCC (GCC) compatible and MS compatible */
  3261. if (pcc) {
  3262. /* In PCC layout a bit-field is placed adjacent to the
  3263. preceding bit-fields, except if:
  3264. - it has zero-width
  3265. - an individual alignment was given
  3266. - it would overflow its base type container and
  3267. there is no packing */
  3268. if (bit_size == 0) {
  3269. new_field:
  3270. c = (c + ((bit_pos + 7) >> 3) + align - 1) & -align;
  3271. bit_pos = 0;
  3272. } else if (f->a.aligned) {
  3273. goto new_field;
  3274. } else if (!packed) {
  3275. int a8 = align * 8;
  3276. int ofs = ((c * 8 + bit_pos) % a8 + bit_size + a8 - 1) / a8;
  3277. if (ofs > size / align)
  3278. goto new_field;
  3279. }
  3280. /* in pcc mode, long long bitfields have type int if they fit */
  3281. if (size == 8 && bit_size <= 32)
  3282. f->type.t = (f->type.t & ~VT_BTYPE) | VT_INT, size = 4;
  3283. while (bit_pos >= align * 8)
  3284. c += align, bit_pos -= align * 8;
  3285. offset = c;
  3286. /* In PCC layout named bit-fields influence the alignment
  3287. of the containing struct using the base types alignment,
  3288. except for packed fields (which here have correct align). */
  3289. if (f->v & SYM_FIRST_ANOM
  3290. // && bit_size // ??? gcc on ARM/rpi does that
  3291. )
  3292. align = 1;
  3293. } else {
  3294. bt = f->type.t & VT_BTYPE;
  3295. if ((bit_pos + bit_size > size * 8)
  3296. || (bit_size > 0) == (bt != prevbt)
  3297. ) {
  3298. c = (c + align - 1) & -align;
  3299. offset = c;
  3300. bit_pos = 0;
  3301. /* In MS bitfield mode a bit-field run always uses
  3302. at least as many bits as the underlying type.
  3303. To start a new run it's also required that this
  3304. or the last bit-field had non-zero width. */
  3305. if (bit_size || prev_bit_size)
  3306. c += size;
  3307. }
  3308. /* In MS layout the records alignment is normally
  3309. influenced by the field, except for a zero-width
  3310. field at the start of a run (but by further zero-width
  3311. fields it is again). */
  3312. if (bit_size == 0 && prevbt != bt)
  3313. align = 1;
  3314. prevbt = bt;
  3315. prev_bit_size = bit_size;
  3316. }
  3317. f->type.t = (f->type.t & ~(0x3f << VT_STRUCT_SHIFT))
  3318. | (bit_pos << VT_STRUCT_SHIFT);
  3319. bit_pos += bit_size;
  3320. }
  3321. if (align > maxalign)
  3322. maxalign = align;
  3323. #ifdef BF_DEBUG
  3324. printf("set field %s offset %-2d size %-2d align %-2d",
  3325. get_tok_str(f->v & ~SYM_FIELD, NULL), offset, size, align);
  3326. if (f->type.t & VT_BITFIELD) {
  3327. printf(" pos %-2d bits %-2d",
  3328. BIT_POS(f->type.t),
  3329. BIT_SIZE(f->type.t)
  3330. );
  3331. }
  3332. printf("\n");
  3333. #endif
  3334. if (f->v & SYM_FIRST_ANOM && (f->type.t & VT_BTYPE) == VT_STRUCT) {
  3335. Sym *ass;
  3336. /* An anonymous struct/union. Adjust member offsets
  3337. to reflect the real offset of our containing struct.
  3338. Also set the offset of this anon member inside
  3339. the outer struct to be zero. Via this it
  3340. works when accessing the field offset directly
  3341. (from base object), as well as when recursing
  3342. members in initializer handling. */
  3343. int v2 = f->type.ref->v;
  3344. if (!(v2 & SYM_FIELD) &&
  3345. (v2 & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
  3346. Sym **pps;
  3347. /* This happens only with MS extensions. The
  3348. anon member has a named struct type, so it
  3349. potentially is shared with other references.
  3350. We need to unshare members so we can modify
  3351. them. */
  3352. ass = f->type.ref;
  3353. f->type.ref = sym_push(anon_sym++ | SYM_FIELD,
  3354. &f->type.ref->type, 0,
  3355. f->type.ref->c);
  3356. pps = &f->type.ref->next;
  3357. while ((ass = ass->next) != NULL) {
  3358. *pps = sym_push(ass->v, &ass->type, 0, ass->c);
  3359. pps = &((*pps)->next);
  3360. }
  3361. *pps = NULL;
  3362. }
  3363. struct_add_offset(f->type.ref, offset);
  3364. f->c = 0;
  3365. } else {
  3366. f->c = offset;
  3367. }
  3368. f->r = 0;
  3369. }
  3370. if (pcc)
  3371. c += (bit_pos + 7) >> 3;
  3372. /* store size and alignment */
  3373. a = bt = ad->a.aligned ? 1 << (ad->a.aligned - 1) : 1;
  3374. if (a < maxalign)
  3375. a = maxalign;
  3376. type->ref->r = a;
  3377. if (pragma_pack && pragma_pack < maxalign && 0 == pcc) {
  3378. /* can happen if individual align for some member was given. In
  3379. this case MSVC ignores maxalign when aligning the size */
  3380. a = pragma_pack;
  3381. if (a < bt)
  3382. a = bt;
  3383. }
  3384. c = (c + a - 1) & -a;
  3385. type->ref->c = c;
  3386. #ifdef BF_DEBUG
  3387. printf("struct size %-2d align %-2d\n\n", c, a), fflush(stdout);
  3388. #endif
  3389. /* check whether we can access bitfields by their type */
  3390. for (f = type->ref->next; f; f = f->next) {
  3391. int s, px, cx, c0;
  3392. CType t;
  3393. if (0 == (f->type.t & VT_BITFIELD))
  3394. continue;
  3395. f->type.ref = f;
  3396. f->auxtype = -1;
  3397. bit_size = BIT_SIZE(f->type.t);
  3398. if (bit_size == 0)
  3399. continue;
  3400. bit_pos = BIT_POS(f->type.t);
  3401. size = type_size(&f->type, &align);
  3402. if (bit_pos + bit_size <= size * 8 && f->c + size <= c)
  3403. continue;
  3404. /* try to access the field using a different type */
  3405. c0 = -1, s = align = 1;
  3406. for (;;) {
  3407. px = f->c * 8 + bit_pos;
  3408. cx = (px >> 3) & -align;
  3409. px = px - (cx << 3);
  3410. if (c0 == cx)
  3411. break;
  3412. s = (px + bit_size + 7) >> 3;
  3413. if (s > 4) {
  3414. t.t = VT_LLONG;
  3415. } else if (s > 2) {
  3416. t.t = VT_INT;
  3417. } else if (s > 1) {
  3418. t.t = VT_SHORT;
  3419. } else {
  3420. t.t = VT_BYTE;
  3421. }
  3422. s = type_size(&t, &align);
  3423. c0 = cx;
  3424. }
  3425. if (px + bit_size <= s * 8 && cx + s <= c) {
  3426. /* update offset and bit position */
  3427. f->c = cx;
  3428. bit_pos = px;
  3429. f->type.t = (f->type.t & ~(0x3f << VT_STRUCT_SHIFT))
  3430. | (bit_pos << VT_STRUCT_SHIFT);
  3431. if (s != size)
  3432. f->auxtype = t.t;
  3433. #ifdef BF_DEBUG
  3434. printf("FIX field %s offset %-2d size %-2d align %-2d "
  3435. "pos %-2d bits %-2d\n",
  3436. get_tok_str(f->v & ~SYM_FIELD, NULL),
  3437. cx, s, align, px, bit_size);
  3438. #endif
  3439. } else {
  3440. /* fall back to load/store single-byte wise */
  3441. f->auxtype = VT_STRUCT;
  3442. #ifdef BF_DEBUG
  3443. printf("FIX field %s : load byte-wise\n",
  3444. get_tok_str(f->v & ~SYM_FIELD, NULL));
  3445. #endif
  3446. }
  3447. }
  3448. }
  3449. /* enum/struct/union declaration. u is VT_ENUM/VT_STRUCT/VT_UNION */
  3450. static void struct_decl(CType *type, int u)
  3451. {
  3452. int v, c, size, align, flexible;
  3453. int bit_size, bsize, bt;
  3454. Sym *s, *ss, **ps;
  3455. AttributeDef ad, ad1;
  3456. CType type1, btype;
  3457. memset(&ad, 0, sizeof ad);
  3458. next();
  3459. parse_attribute(&ad);
  3460. if (tok != '{') {
  3461. v = tok;
  3462. next();
  3463. /* struct already defined ? return it */
  3464. if (v < TOK_IDENT)
  3465. expect("struct/union/enum name");
  3466. s = struct_find(v);
  3467. if (s && (s->sym_scope == local_scope || tok != '{')) {
  3468. if (u == s->type.t)
  3469. goto do_decl;
  3470. if (u == VT_ENUM && IS_ENUM(s->type.t))
  3471. goto do_decl;
  3472. tcc_error("redefinition of '%s'", get_tok_str(v, NULL));
  3473. }
  3474. } else {
  3475. v = anon_sym++;
  3476. }
  3477. /* Record the original enum/struct/union token. */
  3478. type1.t = u == VT_ENUM ? u | VT_INT | VT_UNSIGNED : u;
  3479. type1.ref = NULL;
  3480. /* we put an undefined size for struct/union */
  3481. s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
  3482. s->r = 0; /* default alignment is zero as gcc */
  3483. do_decl:
  3484. type->t = s->type.t;
  3485. type->ref = s;
  3486. if (tok == '{') {
  3487. next();
  3488. if (s->c != -1)
  3489. tcc_error("struct/union/enum already defined");
  3490. /* cannot be empty */
  3491. /* non empty enums are not allowed */
  3492. ps = &s->next;
  3493. if (u == VT_ENUM) {
  3494. long long ll = 0, pl = 0, nl = 0;
  3495. CType t;
  3496. t.ref = s;
  3497. /* enum symbols have static storage */
  3498. t.t = VT_INT|VT_STATIC|VT_ENUM_VAL;
  3499. for(;;) {
  3500. v = tok;
  3501. if (v < TOK_UIDENT)
  3502. expect("identifier");
  3503. ss = sym_find(v);
  3504. if (ss && !local_stack)
  3505. tcc_error("redefinition of enumerator '%s'",
  3506. get_tok_str(v, NULL));
  3507. next();
  3508. if (tok == '=') {
  3509. next();
  3510. ll = expr_const64();
  3511. }
  3512. ss = sym_push(v, &t, VT_CONST, 0);
  3513. ss->enum_val = ll;
  3514. *ps = ss, ps = &ss->next;
  3515. if (ll < nl)
  3516. nl = ll;
  3517. if (ll > pl)
  3518. pl = ll;
  3519. if (tok != ',')
  3520. break;
  3521. next();
  3522. ll++;
  3523. /* NOTE: we accept a trailing comma */
  3524. if (tok == '}')
  3525. break;
  3526. }
  3527. skip('}');
  3528. /* set integral type of the enum */
  3529. t.t = VT_INT;
  3530. if (nl >= 0) {
  3531. if (pl != (unsigned)pl)
  3532. t.t = (LONG_SIZE==8 ? VT_LLONG|VT_LONG : VT_LLONG);
  3533. t.t |= VT_UNSIGNED;
  3534. } else if (pl != (int)pl || nl != (int)nl)
  3535. t.t = (LONG_SIZE==8 ? VT_LLONG|VT_LONG : VT_LLONG);
  3536. s->type.t = type->t = t.t | VT_ENUM;
  3537. s->c = 0;
  3538. /* set type for enum members */
  3539. for (ss = s->next; ss; ss = ss->next) {
  3540. ll = ss->enum_val;
  3541. if (ll == (int)ll) /* default is int if it fits */
  3542. continue;
  3543. if (t.t & VT_UNSIGNED) {
  3544. ss->type.t |= VT_UNSIGNED;
  3545. if (ll == (unsigned)ll)
  3546. continue;
  3547. }
  3548. ss->type.t = (ss->type.t & ~VT_BTYPE)
  3549. | (LONG_SIZE==8 ? VT_LLONG|VT_LONG : VT_LLONG);
  3550. }
  3551. } else {
  3552. c = 0;
  3553. flexible = 0;
  3554. while (tok != '}') {
  3555. if (!parse_btype(&btype, &ad1)) {
  3556. skip(';');
  3557. continue;
  3558. }
  3559. while (1) {
  3560. if (flexible)
  3561. tcc_error("flexible array member '%s' not at the end of struct",
  3562. get_tok_str(v, NULL));
  3563. bit_size = -1;
  3564. v = 0;
  3565. type1 = btype;
  3566. if (tok != ':') {
  3567. if (tok != ';')
  3568. type_decl(&type1, &ad1, &v, TYPE_DIRECT);
  3569. if (v == 0) {
  3570. if ((type1.t & VT_BTYPE) != VT_STRUCT)
  3571. expect("identifier");
  3572. else {
  3573. int v = btype.ref->v;
  3574. if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
  3575. if (tcc_state->ms_extensions == 0)
  3576. expect("identifier");
  3577. }
  3578. }
  3579. }
  3580. if (type_size(&type1, &align) < 0) {
  3581. if ((u == VT_STRUCT) && (type1.t & VT_ARRAY) && c)
  3582. flexible = 1;
  3583. else
  3584. tcc_error("field '%s' has incomplete type",
  3585. get_tok_str(v, NULL));
  3586. }
  3587. if ((type1.t & VT_BTYPE) == VT_FUNC ||
  3588. (type1.t & VT_STORAGE))
  3589. tcc_error("invalid type for '%s'",
  3590. get_tok_str(v, NULL));
  3591. }
  3592. if (tok == ':') {
  3593. next();
  3594. bit_size = expr_const();
  3595. /* XXX: handle v = 0 case for messages */
  3596. if (bit_size < 0)
  3597. tcc_error("negative width in bit-field '%s'",
  3598. get_tok_str(v, NULL));
  3599. if (v && bit_size == 0)
  3600. tcc_error("zero width for bit-field '%s'",
  3601. get_tok_str(v, NULL));
  3602. parse_attribute(&ad1);
  3603. }
  3604. size = type_size(&type1, &align);
  3605. if (bit_size >= 0) {
  3606. bt = type1.t & VT_BTYPE;
  3607. if (bt != VT_INT &&
  3608. bt != VT_BYTE &&
  3609. bt != VT_SHORT &&
  3610. bt != VT_BOOL &&
  3611. bt != VT_LLONG)
  3612. tcc_error("bitfields must have scalar type");
  3613. bsize = size * 8;
  3614. if (bit_size > bsize) {
  3615. tcc_error("width of '%s' exceeds its type",
  3616. get_tok_str(v, NULL));
  3617. } else if (bit_size == bsize
  3618. && !ad.a.packed && !ad1.a.packed) {
  3619. /* no need for bit fields */
  3620. ;
  3621. } else if (bit_size == 64) {
  3622. tcc_error("field width 64 not implemented");
  3623. } else {
  3624. type1.t = (type1.t & ~VT_STRUCT_MASK)
  3625. | VT_BITFIELD
  3626. | (bit_size << (VT_STRUCT_SHIFT + 6));
  3627. }
  3628. }
  3629. if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
  3630. /* Remember we've seen a real field to check
  3631. for placement of flexible array member. */
  3632. c = 1;
  3633. }
  3634. /* If member is a struct or bit-field, enforce
  3635. placing into the struct (as anonymous). */
  3636. if (v == 0 &&
  3637. ((type1.t & VT_BTYPE) == VT_STRUCT ||
  3638. bit_size >= 0)) {
  3639. v = anon_sym++;
  3640. }
  3641. if (v) {
  3642. ss = sym_push(v | SYM_FIELD, &type1, 0, 0);
  3643. ss->a = ad1.a;
  3644. *ps = ss;
  3645. ps = &ss->next;
  3646. }
  3647. if (tok == ';' || tok == TOK_EOF)
  3648. break;
  3649. skip(',');
  3650. }
  3651. skip(';');
  3652. }
  3653. skip('}');
  3654. parse_attribute(&ad);
  3655. struct_layout(type, &ad);
  3656. }
  3657. }
  3658. }
  3659. static void sym_to_attr(AttributeDef *ad, Sym *s)
  3660. {
  3661. if (s->a.aligned && 0 == ad->a.aligned)
  3662. ad->a.aligned = s->a.aligned;
  3663. if (s->f.func_call && 0 == ad->f.func_call)
  3664. ad->f.func_call = s->f.func_call;
  3665. if (s->f.func_type && 0 == ad->f.func_type)
  3666. ad->f.func_type = s->f.func_type;
  3667. if (s->a.packed)
  3668. ad->a.packed = 1;
  3669. }
  3670. /* Add type qualifiers to a type. If the type is an array then the qualifiers
  3671. are added to the element type, copied because it could be a typedef. */
  3672. static void parse_btype_qualify(CType *type, int qualifiers)
  3673. {
  3674. while (type->t & VT_ARRAY) {
  3675. type->ref = sym_push(SYM_FIELD, &type->ref->type, 0, type->ref->c);
  3676. type = &type->ref->type;
  3677. }
  3678. type->t |= qualifiers;
  3679. }
  3680. /* return 0 if no type declaration. otherwise, return the basic type
  3681. and skip it.
  3682. */
  3683. static int parse_btype(CType *type, AttributeDef *ad)
  3684. {
  3685. int t, u, bt, st, type_found, typespec_found, g;
  3686. Sym *s;
  3687. CType type1;
  3688. memset(ad, 0, sizeof(AttributeDef));
  3689. type_found = 0;
  3690. typespec_found = 0;
  3691. t = VT_INT;
  3692. bt = st = -1;
  3693. type->ref = NULL;
  3694. while(1) {
  3695. switch(tok) {
  3696. case TOK_EXTENSION:
  3697. /* currently, we really ignore extension */
  3698. next();
  3699. continue;
  3700. /* basic types */
  3701. case TOK_CHAR:
  3702. u = VT_BYTE;
  3703. basic_type:
  3704. next();
  3705. basic_type1:
  3706. if (u == VT_SHORT || u == VT_LONG) {
  3707. if (st != -1 || (bt != -1 && bt != VT_INT))
  3708. tmbt: tcc_error("too many basic types");
  3709. st = u;
  3710. } else {
  3711. if (bt != -1 || (st != -1 && u != VT_INT))
  3712. goto tmbt;
  3713. bt = u;
  3714. }
  3715. if (u != VT_INT)
  3716. t = (t & ~(VT_BTYPE|VT_LONG)) | u;
  3717. typespec_found = 1;
  3718. break;
  3719. case TOK_VOID:
  3720. u = VT_VOID;
  3721. goto basic_type;
  3722. case TOK_SHORT:
  3723. u = VT_SHORT;
  3724. goto basic_type;
  3725. case TOK_INT:
  3726. u = VT_INT;
  3727. goto basic_type;
  3728. case TOK_LONG:
  3729. if ((t & VT_BTYPE) == VT_DOUBLE) {
  3730. t = (t & ~(VT_BTYPE|VT_LONG)) | VT_LDOUBLE;
  3731. } else if ((t & (VT_BTYPE|VT_LONG)) == VT_LONG) {
  3732. t = (t & ~(VT_BTYPE|VT_LONG)) | VT_LLONG;
  3733. } else {
  3734. u = VT_LONG;
  3735. goto basic_type;
  3736. }
  3737. next();
  3738. break;
  3739. #ifdef TCC_TARGET_ARM64
  3740. case TOK_UINT128:
  3741. /* GCC's __uint128_t appears in some Linux header files. Make it a
  3742. synonym for long double to get the size and alignment right. */
  3743. u = VT_LDOUBLE;
  3744. goto basic_type;
  3745. #endif
  3746. case TOK_BOOL:
  3747. u = VT_BOOL;
  3748. goto basic_type;
  3749. case TOK_FLOAT:
  3750. u = VT_FLOAT;
  3751. goto basic_type;
  3752. case TOK_DOUBLE:
  3753. if ((t & (VT_BTYPE|VT_LONG)) == VT_LONG) {
  3754. t = (t & ~(VT_BTYPE|VT_LONG)) | VT_LDOUBLE;
  3755. } else {
  3756. u = VT_DOUBLE;
  3757. goto basic_type;
  3758. }
  3759. next();
  3760. break;
  3761. case TOK_ENUM:
  3762. struct_decl(&type1, VT_ENUM);
  3763. basic_type2:
  3764. u = type1.t;
  3765. type->ref = type1.ref;
  3766. goto basic_type1;
  3767. case TOK_STRUCT:
  3768. struct_decl(&type1, VT_STRUCT);
  3769. goto basic_type2;
  3770. case TOK_UNION:
  3771. struct_decl(&type1, VT_UNION);
  3772. goto basic_type2;
  3773. /* type modifiers */
  3774. case TOK_CONST1:
  3775. case TOK_CONST2:
  3776. case TOK_CONST3:
  3777. type->t = t;
  3778. parse_btype_qualify(type, VT_CONSTANT);
  3779. t = type->t;
  3780. next();
  3781. break;
  3782. case TOK_VOLATILE1:
  3783. case TOK_VOLATILE2:
  3784. case TOK_VOLATILE3:
  3785. type->t = t;
  3786. parse_btype_qualify(type, VT_VOLATILE);
  3787. t = type->t;
  3788. next();
  3789. break;
  3790. case TOK_SIGNED1:
  3791. case TOK_SIGNED2:
  3792. case TOK_SIGNED3:
  3793. if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == (VT_DEFSIGN|VT_UNSIGNED))
  3794. tcc_error("signed and unsigned modifier");
  3795. t |= VT_DEFSIGN;
  3796. next();
  3797. typespec_found = 1;
  3798. break;
  3799. case TOK_REGISTER:
  3800. case TOK_AUTO:
  3801. case TOK_RESTRICT1:
  3802. case TOK_RESTRICT2:
  3803. case TOK_RESTRICT3:
  3804. next();
  3805. break;
  3806. case TOK_UNSIGNED:
  3807. if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == VT_DEFSIGN)
  3808. tcc_error("signed and unsigned modifier");
  3809. t |= VT_DEFSIGN | VT_UNSIGNED;
  3810. next();
  3811. typespec_found = 1;
  3812. break;
  3813. /* storage */
  3814. case TOK_EXTERN:
  3815. g = VT_EXTERN;
  3816. goto storage;
  3817. case TOK_STATIC:
  3818. g = VT_STATIC;
  3819. goto storage;
  3820. case TOK_TYPEDEF:
  3821. g = VT_TYPEDEF;
  3822. goto storage;
  3823. storage:
  3824. if (t & (VT_EXTERN|VT_STATIC|VT_TYPEDEF) & ~g)
  3825. tcc_error("multiple storage classes");
  3826. t |= g;
  3827. next();
  3828. break;
  3829. case TOK_INLINE1:
  3830. case TOK_INLINE2:
  3831. case TOK_INLINE3:
  3832. t |= VT_INLINE;
  3833. next();
  3834. break;
  3835. /* GNUC attribute */
  3836. case TOK_ATTRIBUTE1:
  3837. case TOK_ATTRIBUTE2:
  3838. parse_attribute(ad);
  3839. if (ad->attr_mode) {
  3840. u = ad->attr_mode -1;
  3841. t = (t & ~(VT_BTYPE|VT_LONG)) | u;
  3842. }
  3843. break;
  3844. /* GNUC typeof */
  3845. case TOK_TYPEOF1:
  3846. case TOK_TYPEOF2:
  3847. case TOK_TYPEOF3:
  3848. next();
  3849. parse_expr_type(&type1);
  3850. /* remove all storage modifiers except typedef */
  3851. type1.t &= ~(VT_STORAGE&~VT_TYPEDEF);
  3852. if (type1.ref)
  3853. sym_to_attr(ad, type1.ref);
  3854. goto basic_type2;
  3855. default:
  3856. if (typespec_found)
  3857. goto the_end;
  3858. s = sym_find(tok);
  3859. if (!s || !(s->type.t & VT_TYPEDEF))
  3860. goto the_end;
  3861. t &= ~(VT_BTYPE|VT_LONG);
  3862. u = t & ~(VT_CONSTANT | VT_VOLATILE), t ^= u;
  3863. type->t = (s->type.t & ~VT_TYPEDEF) | u;
  3864. type->ref = s->type.ref;
  3865. if (t)
  3866. parse_btype_qualify(type, t);
  3867. t = type->t;
  3868. /* get attributes from typedef */
  3869. sym_to_attr(ad, s);
  3870. next();
  3871. typespec_found = 1;
  3872. st = bt = -2;
  3873. break;
  3874. }
  3875. type_found = 1;
  3876. }
  3877. the_end:
  3878. if (tcc_state->char_is_unsigned) {
  3879. if ((t & (VT_DEFSIGN|VT_BTYPE)) == VT_BYTE)
  3880. t |= VT_UNSIGNED;
  3881. }
  3882. /* VT_LONG is used just as a modifier for VT_INT / VT_LLONG */
  3883. bt = t & (VT_BTYPE|VT_LONG);
  3884. if (bt == VT_LONG)
  3885. t |= LONG_SIZE == 8 ? VT_LLONG : VT_INT;
  3886. #ifdef TCC_TARGET_PE
  3887. if (bt == VT_LDOUBLE)
  3888. t = (t & ~(VT_BTYPE|VT_LONG)) | VT_DOUBLE;
  3889. #endif
  3890. type->t = t;
  3891. return type_found;
  3892. }
  3893. /* convert a function parameter type (array to pointer and function to
  3894. function pointer) */
  3895. static inline void convert_parameter_type(CType *pt)
  3896. {
  3897. /* remove const and volatile qualifiers (XXX: const could be used
  3898. to indicate a const function parameter */
  3899. pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
  3900. /* array must be transformed to pointer according to ANSI C */
  3901. pt->t &= ~VT_ARRAY;
  3902. if ((pt->t & VT_BTYPE) == VT_FUNC) {
  3903. mk_pointer(pt);
  3904. }
  3905. }
  3906. ST_FUNC void parse_asm_str(CString *astr)
  3907. {
  3908. skip('(');
  3909. parse_mult_str(astr, "string constant");
  3910. }
  3911. /* Parse an asm label and return the token */
  3912. static int asm_label_instr(void)
  3913. {
  3914. int v;
  3915. CString astr;
  3916. next();
  3917. parse_asm_str(&astr);
  3918. skip(')');
  3919. #ifdef ASM_DEBUG
  3920. printf("asm_alias: \"%s\"\n", (char *)astr.data);
  3921. #endif
  3922. v = tok_alloc(astr.data, astr.size - 1)->tok;
  3923. cstr_free(&astr);
  3924. return v;
  3925. }
  3926. static int post_type(CType *type, AttributeDef *ad, int storage, int td)
  3927. {
  3928. int n, l, t1, arg_size, align;
  3929. Sym **plast, *s, *first;
  3930. AttributeDef ad1;
  3931. CType pt;
  3932. if (tok == '(') {
  3933. /* function type, or recursive declarator (return if so) */
  3934. next();
  3935. if (td && !(td & TYPE_ABSTRACT))
  3936. return 0;
  3937. if (tok == ')')
  3938. l = 0;
  3939. else if (parse_btype(&pt, &ad1))
  3940. l = FUNC_NEW;
  3941. else if (td)
  3942. return 0;
  3943. else
  3944. l = FUNC_OLD;
  3945. first = NULL;
  3946. plast = &first;
  3947. arg_size = 0;
  3948. if (l) {
  3949. for(;;) {
  3950. /* read param name and compute offset */
  3951. if (l != FUNC_OLD) {
  3952. if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
  3953. break;
  3954. type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
  3955. if ((pt.t & VT_BTYPE) == VT_VOID)
  3956. tcc_error("parameter declared as void");
  3957. arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE;
  3958. } else {
  3959. n = tok;
  3960. if (n < TOK_UIDENT)
  3961. expect("identifier");
  3962. pt.t = VT_VOID; /* invalid type */
  3963. next();
  3964. }
  3965. convert_parameter_type(&pt);
  3966. s = sym_push(n | SYM_FIELD, &pt, 0, 0);
  3967. *plast = s;
  3968. plast = &s->next;
  3969. if (tok == ')')
  3970. break;
  3971. skip(',');
  3972. if (l == FUNC_NEW && tok == TOK_DOTS) {
  3973. l = FUNC_ELLIPSIS;
  3974. next();
  3975. break;
  3976. }
  3977. if (l == FUNC_NEW && !parse_btype(&pt, &ad1))
  3978. tcc_error("invalid type");
  3979. }
  3980. } else
  3981. /* if no parameters, then old type prototype */
  3982. l = FUNC_OLD;
  3983. skip(')');
  3984. /* NOTE: const is ignored in returned type as it has a special
  3985. meaning in gcc / C++ */
  3986. type->t &= ~VT_CONSTANT;
  3987. /* some ancient pre-K&R C allows a function to return an array
  3988. and the array brackets to be put after the arguments, such
  3989. that "int c()[]" means something like "int[] c()" */
  3990. if (tok == '[') {
  3991. next();
  3992. skip(']'); /* only handle simple "[]" */
  3993. mk_pointer(type);
  3994. }
  3995. /* we push a anonymous symbol which will contain the function prototype */
  3996. ad->f.func_args = arg_size;
  3997. ad->f.func_type = l;
  3998. s = sym_push(SYM_FIELD, type, 0, 0);
  3999. s->a = ad->a;
  4000. s->f = ad->f;
  4001. s->next = first;
  4002. type->t = VT_FUNC;
  4003. type->ref = s;
  4004. } else if (tok == '[') {
  4005. int saved_nocode_wanted = nocode_wanted;
  4006. /* array definition */
  4007. next();
  4008. if (tok == TOK_RESTRICT1)
  4009. next();
  4010. n = -1;
  4011. t1 = 0;
  4012. if (tok != ']') {
  4013. if (!local_stack || (storage & VT_STATIC))
  4014. vpushi(expr_const());
  4015. else {
  4016. /* VLAs (which can only happen with local_stack && !VT_STATIC)
  4017. length must always be evaluated, even under nocode_wanted,
  4018. so that its size slot is initialized (e.g. under sizeof
  4019. or typeof). */
  4020. nocode_wanted = 0;
  4021. gexpr();
  4022. }
  4023. if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
  4024. n = vtop->c.i;
  4025. if (n < 0)
  4026. tcc_error("invalid array size");
  4027. } else {
  4028. if (!is_integer_btype(vtop->type.t & VT_BTYPE))
  4029. tcc_error("size of variable length array should be an integer");
  4030. t1 = VT_VLA;
  4031. }
  4032. }
  4033. skip(']');
  4034. /* parse next post type */
  4035. post_type(type, ad, storage, 0);
  4036. if (type->t == VT_FUNC)
  4037. tcc_error("declaration of an array of functions");
  4038. t1 |= type->t & VT_VLA;
  4039. if (t1 & VT_VLA) {
  4040. loc -= type_size(&int_type, &align);
  4041. loc &= -align;
  4042. n = loc;
  4043. vla_runtime_type_size(type, &align);
  4044. gen_op('*');
  4045. vset(&int_type, VT_LOCAL|VT_LVAL, n);
  4046. vswap();
  4047. vstore();
  4048. }
  4049. if (n != -1)
  4050. vpop();
  4051. nocode_wanted = saved_nocode_wanted;
  4052. /* we push an anonymous symbol which will contain the array
  4053. element type */
  4054. s = sym_push(SYM_FIELD, type, 0, n);
  4055. type->t = (t1 ? VT_VLA : VT_ARRAY) | VT_PTR;
  4056. type->ref = s;
  4057. }
  4058. return 1;
  4059. }
  4060. /* Parse a type declarator (except basic type), and return the type
  4061. in 'type'. 'td' is a bitmask indicating which kind of type decl is
  4062. expected. 'type' should contain the basic type. 'ad' is the
  4063. attribute definition of the basic type. It can be modified by
  4064. type_decl(). If this (possibly abstract) declarator is a pointer chain
  4065. it returns the innermost pointed to type (equals *type, but is a different
  4066. pointer), otherwise returns type itself, that's used for recursive calls. */
  4067. static CType *type_decl(CType *type, AttributeDef *ad, int *v, int td)
  4068. {
  4069. CType *post, *ret;
  4070. int qualifiers, storage;
  4071. /* recursive type, remove storage bits first, apply them later again */
  4072. storage = type->t & VT_STORAGE;
  4073. type->t &= ~VT_STORAGE;
  4074. post = ret = type;
  4075. while (tok == '*') {
  4076. qualifiers = 0;
  4077. redo:
  4078. next();
  4079. switch(tok) {
  4080. case TOK_CONST1:
  4081. case TOK_CONST2:
  4082. case TOK_CONST3:
  4083. qualifiers |= VT_CONSTANT;
  4084. goto redo;
  4085. case TOK_VOLATILE1:
  4086. case TOK_VOLATILE2:
  4087. case TOK_VOLATILE3:
  4088. qualifiers |= VT_VOLATILE;
  4089. goto redo;
  4090. case TOK_RESTRICT1:
  4091. case TOK_RESTRICT2:
  4092. case TOK_RESTRICT3:
  4093. goto redo;
  4094. /* XXX: clarify attribute handling */
  4095. case TOK_ATTRIBUTE1:
  4096. case TOK_ATTRIBUTE2:
  4097. parse_attribute(ad);
  4098. break;
  4099. }
  4100. mk_pointer(type);
  4101. type->t |= qualifiers;
  4102. if (ret == type)
  4103. /* innermost pointed to type is the one for the first derivation */
  4104. ret = pointed_type(type);
  4105. }
  4106. if (tok == '(') {
  4107. /* This is possibly a parameter type list for abstract declarators
  4108. ('int ()'), use post_type for testing this. */
  4109. if (!post_type(type, ad, 0, td)) {
  4110. /* It's not, so it's a nested declarator, and the post operations
  4111. apply to the innermost pointed to type (if any). */
  4112. /* XXX: this is not correct to modify 'ad' at this point, but
  4113. the syntax is not clear */
  4114. parse_attribute(ad);
  4115. post = type_decl(type, ad, v, td);
  4116. skip(')');
  4117. }
  4118. } else if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
  4119. /* type identifier */
  4120. *v = tok;
  4121. next();
  4122. } else {
  4123. if (!(td & TYPE_ABSTRACT))
  4124. expect("identifier");
  4125. *v = 0;
  4126. }
  4127. post_type(post, ad, storage, 0);
  4128. parse_attribute(ad);
  4129. type->t |= storage;
  4130. return ret;
  4131. }
  4132. /* compute the lvalue VT_LVAL_xxx needed to match type t. */
  4133. ST_FUNC int lvalue_type(int t)
  4134. {
  4135. int bt, r;
  4136. r = VT_LVAL;
  4137. bt = t & VT_BTYPE;
  4138. if (bt == VT_BYTE || bt == VT_BOOL)
  4139. r |= VT_LVAL_BYTE;
  4140. else if (bt == VT_SHORT)
  4141. r |= VT_LVAL_SHORT;
  4142. else
  4143. return r;
  4144. if (t & VT_UNSIGNED)
  4145. r |= VT_LVAL_UNSIGNED;
  4146. return r;
  4147. }
  4148. /* indirection with full error checking and bound check */
  4149. ST_FUNC void indir(void)
  4150. {
  4151. if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
  4152. if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
  4153. return;
  4154. expect("pointer");
  4155. }
  4156. if (vtop->r & VT_LVAL)
  4157. gv(RC_INT);
  4158. vtop->type = *pointed_type(&vtop->type);
  4159. /* Arrays and functions are never lvalues */
  4160. if (!(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_VLA)
  4161. && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
  4162. vtop->r |= lvalue_type(vtop->type.t);
  4163. /* if bound checking, the referenced pointer must be checked */
  4164. #ifdef CONFIG_TCC_BCHECK
  4165. if (tcc_state->do_bounds_check)
  4166. vtop->r |= VT_MUSTBOUND;
  4167. #endif
  4168. }
  4169. }
  4170. /* pass a parameter to a function and do type checking and casting */
  4171. static void gfunc_param_typed(Sym *func, Sym *arg)
  4172. {
  4173. int func_type;
  4174. CType type;
  4175. func_type = func->f.func_type;
  4176. if (func_type == FUNC_OLD ||
  4177. (func_type == FUNC_ELLIPSIS && arg == NULL)) {
  4178. /* default casting : only need to convert float to double */
  4179. if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
  4180. gen_cast_s(VT_DOUBLE);
  4181. } else if (vtop->type.t & VT_BITFIELD) {
  4182. type.t = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
  4183. type.ref = vtop->type.ref;
  4184. gen_cast(&type);
  4185. }
  4186. } else if (arg == NULL) {
  4187. tcc_error("too many arguments to function");
  4188. } else {
  4189. type = arg->type;
  4190. type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
  4191. gen_assign_cast(&type);
  4192. }
  4193. }
  4194. /* parse an expression and return its type without any side effect. */
  4195. static void expr_type(CType *type, void (*expr_fn)(void))
  4196. {
  4197. nocode_wanted++;
  4198. expr_fn();
  4199. *type = vtop->type;
  4200. vpop();
  4201. nocode_wanted--;
  4202. }
  4203. /* parse an expression of the form '(type)' or '(expr)' and return its
  4204. type */
  4205. static void parse_expr_type(CType *type)
  4206. {
  4207. int n;
  4208. AttributeDef ad;
  4209. skip('(');
  4210. if (parse_btype(type, &ad)) {
  4211. type_decl(type, &ad, &n, TYPE_ABSTRACT);
  4212. } else {
  4213. expr_type(type, gexpr);
  4214. }
  4215. skip(')');
  4216. }
  4217. static void parse_type(CType *type)
  4218. {
  4219. AttributeDef ad;
  4220. int n;
  4221. if (!parse_btype(type, &ad)) {
  4222. expect("type");
  4223. }
  4224. type_decl(type, &ad, &n, TYPE_ABSTRACT);
  4225. }
  4226. static void parse_builtin_params(int nc, const char *args)
  4227. {
  4228. char c, sep = '(';
  4229. CType t;
  4230. if (nc)
  4231. nocode_wanted++;
  4232. next();
  4233. while ((c = *args++)) {
  4234. skip(sep);
  4235. sep = ',';
  4236. switch (c) {
  4237. case 'e': expr_eq(); continue;
  4238. case 't': parse_type(&t); vpush(&t); continue;
  4239. default: tcc_error("internal error"); break;
  4240. }
  4241. }
  4242. skip(')');
  4243. if (nc)
  4244. nocode_wanted--;
  4245. }
  4246. ST_FUNC void unary(void)
  4247. {
  4248. int n, t, align, size, r, sizeof_caller;
  4249. CType type;
  4250. Sym *s;
  4251. AttributeDef ad;
  4252. sizeof_caller = in_sizeof;
  4253. in_sizeof = 0;
  4254. type.ref = NULL;
  4255. /* XXX: GCC 2.95.3 does not generate a table although it should be
  4256. better here */
  4257. tok_next:
  4258. switch(tok) {
  4259. case TOK_EXTENSION:
  4260. next();
  4261. goto tok_next;
  4262. case TOK_LCHAR:
  4263. #ifdef TCC_TARGET_PE
  4264. t = VT_SHORT|VT_UNSIGNED;
  4265. goto push_tokc;
  4266. #endif
  4267. case TOK_CINT:
  4268. case TOK_CCHAR:
  4269. t = VT_INT;
  4270. push_tokc:
  4271. type.t = t;
  4272. vsetc(&type, VT_CONST, &tokc);
  4273. next();
  4274. break;
  4275. case TOK_CUINT:
  4276. t = VT_INT | VT_UNSIGNED;
  4277. goto push_tokc;
  4278. case TOK_CLLONG:
  4279. t = VT_LLONG;
  4280. goto push_tokc;
  4281. case TOK_CULLONG:
  4282. t = VT_LLONG | VT_UNSIGNED;
  4283. goto push_tokc;
  4284. case TOK_CFLOAT:
  4285. t = VT_FLOAT;
  4286. goto push_tokc;
  4287. case TOK_CDOUBLE:
  4288. t = VT_DOUBLE;
  4289. goto push_tokc;
  4290. case TOK_CLDOUBLE:
  4291. t = VT_LDOUBLE;
  4292. goto push_tokc;
  4293. case TOK_CLONG:
  4294. t = (LONG_SIZE == 8 ? VT_LLONG : VT_INT) | VT_LONG;
  4295. goto push_tokc;
  4296. case TOK_CULONG:
  4297. t = (LONG_SIZE == 8 ? VT_LLONG : VT_INT) | VT_LONG | VT_UNSIGNED;
  4298. goto push_tokc;
  4299. case TOK___FUNCTION__:
  4300. if (!gnu_ext)
  4301. goto tok_identifier;
  4302. /* fall thru */
  4303. case TOK___FUNC__:
  4304. {
  4305. void *ptr;
  4306. int len;
  4307. /* special function name identifier */
  4308. len = strlen(funcname) + 1;
  4309. /* generate char[len] type */
  4310. type.t = VT_BYTE;
  4311. mk_pointer(&type);
  4312. type.t |= VT_ARRAY;
  4313. type.ref->c = len;
  4314. vpush_ref(&type, data_section, data_section->data_offset, len);
  4315. if (!NODATA_WANTED) {
  4316. ptr = section_ptr_add(data_section, len);
  4317. memcpy(ptr, funcname, len);
  4318. }
  4319. next();
  4320. }
  4321. break;
  4322. case TOK_LSTR:
  4323. #ifdef TCC_TARGET_PE
  4324. t = VT_SHORT | VT_UNSIGNED;
  4325. #else
  4326. t = VT_INT;
  4327. #endif
  4328. goto str_init;
  4329. case TOK_STR:
  4330. /* string parsing */
  4331. t = VT_BYTE;
  4332. if (tcc_state->char_is_unsigned)
  4333. t = VT_BYTE | VT_UNSIGNED;
  4334. str_init:
  4335. if (tcc_state->warn_write_strings)
  4336. t |= VT_CONSTANT;
  4337. type.t = t;
  4338. mk_pointer(&type);
  4339. type.t |= VT_ARRAY;
  4340. memset(&ad, 0, sizeof(AttributeDef));
  4341. decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
  4342. break;
  4343. case '(':
  4344. next();
  4345. /* cast ? */
  4346. if (parse_btype(&type, &ad)) {
  4347. type_decl(&type, &ad, &n, TYPE_ABSTRACT);
  4348. skip(')');
  4349. /* check ISOC99 compound literal */
  4350. if (tok == '{') {
  4351. /* data is allocated locally by default */
  4352. if (global_expr)
  4353. r = VT_CONST;
  4354. else
  4355. r = VT_LOCAL;
  4356. /* all except arrays are lvalues */
  4357. if (!(type.t & VT_ARRAY))
  4358. r |= lvalue_type(type.t);
  4359. memset(&ad, 0, sizeof(AttributeDef));
  4360. decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
  4361. } else {
  4362. if (sizeof_caller) {
  4363. vpush(&type);
  4364. return;
  4365. }
  4366. unary();
  4367. gen_cast(&type);
  4368. }
  4369. } else if (tok == '{') {
  4370. int saved_nocode_wanted = nocode_wanted;
  4371. if (const_wanted)
  4372. tcc_error("expected constant");
  4373. /* save all registers */
  4374. save_regs(0);
  4375. /* statement expression : we do not accept break/continue
  4376. inside as GCC does. We do retain the nocode_wanted state,
  4377. as statement expressions can't ever be entered from the
  4378. outside, so any reactivation of code emission (from labels
  4379. or loop heads) can be disabled again after the end of it. */
  4380. block(NULL, NULL, 1);
  4381. nocode_wanted = saved_nocode_wanted;
  4382. skip(')');
  4383. } else {
  4384. gexpr();
  4385. skip(')');
  4386. }
  4387. break;
  4388. case '*':
  4389. next();
  4390. unary();
  4391. indir();
  4392. break;
  4393. case '&':
  4394. next();
  4395. unary();
  4396. /* functions names must be treated as function pointers,
  4397. except for unary '&' and sizeof. Since we consider that
  4398. functions are not lvalues, we only have to handle it
  4399. there and in function calls. */
  4400. /* arrays can also be used although they are not lvalues */
  4401. if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
  4402. !(vtop->type.t & VT_ARRAY))
  4403. test_lvalue();
  4404. mk_pointer(&vtop->type);
  4405. gaddrof();
  4406. break;
  4407. case '!':
  4408. next();
  4409. unary();
  4410. if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
  4411. gen_cast_s(VT_BOOL);
  4412. vtop->c.i = !vtop->c.i;
  4413. } else if ((vtop->r & VT_VALMASK) == VT_CMP)
  4414. vtop->c.i ^= 1;
  4415. else {
  4416. save_regs(1);
  4417. vseti(VT_JMP, gvtst(1, 0));
  4418. }
  4419. break;
  4420. case '~':
  4421. next();
  4422. unary();
  4423. vpushi(-1);
  4424. gen_op('^');
  4425. break;
  4426. case '+':
  4427. next();
  4428. unary();
  4429. if ((vtop->type.t & VT_BTYPE) == VT_PTR)
  4430. tcc_error("pointer not accepted for unary plus");
  4431. /* In order to force cast, we add zero, except for floating point
  4432. where we really need an noop (otherwise -0.0 will be transformed
  4433. into +0.0). */
  4434. if (!is_float(vtop->type.t)) {
  4435. vpushi(0);
  4436. gen_op('+');
  4437. }
  4438. break;
  4439. case TOK_SIZEOF:
  4440. case TOK_ALIGNOF1:
  4441. case TOK_ALIGNOF2:
  4442. t = tok;
  4443. next();
  4444. in_sizeof++;
  4445. expr_type(&type, unary); /* Perform a in_sizeof = 0; */
  4446. s = vtop[1].sym; /* hack: accessing previous vtop */
  4447. size = type_size(&type, &align);
  4448. if (s && s->a.aligned)
  4449. align = 1 << (s->a.aligned - 1);
  4450. if (t == TOK_SIZEOF) {
  4451. if (!(type.t & VT_VLA)) {
  4452. if (size < 0)
  4453. tcc_error("sizeof applied to an incomplete type");
  4454. vpushs(size);
  4455. } else {
  4456. vla_runtime_type_size(&type, &align);
  4457. }
  4458. } else {
  4459. vpushs(align);
  4460. }
  4461. vtop->type.t |= VT_UNSIGNED;
  4462. break;
  4463. case TOK_builtin_expect:
  4464. /* __builtin_expect is a no-op for now */
  4465. parse_builtin_params(0, "ee");
  4466. vpop();
  4467. break;
  4468. case TOK_builtin_types_compatible_p:
  4469. parse_builtin_params(0, "tt");
  4470. vtop[-1].type.t &= ~(VT_CONSTANT | VT_VOLATILE);
  4471. vtop[0].type.t &= ~(VT_CONSTANT | VT_VOLATILE);
  4472. n = is_compatible_types(&vtop[-1].type, &vtop[0].type);
  4473. vtop -= 2;
  4474. vpushi(n);
  4475. break;
  4476. case TOK_builtin_choose_expr:
  4477. {
  4478. int64_t c;
  4479. next();
  4480. skip('(');
  4481. c = expr_const64();
  4482. skip(',');
  4483. if (!c) {
  4484. nocode_wanted++;
  4485. }
  4486. expr_eq();
  4487. if (!c) {
  4488. vpop();
  4489. nocode_wanted--;
  4490. }
  4491. skip(',');
  4492. if (c) {
  4493. nocode_wanted++;
  4494. }
  4495. expr_eq();
  4496. if (c) {
  4497. vpop();
  4498. nocode_wanted--;
  4499. }
  4500. skip(')');
  4501. }
  4502. break;
  4503. case TOK_builtin_constant_p:
  4504. parse_builtin_params(1, "e");
  4505. n = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
  4506. vtop--;
  4507. vpushi(n);
  4508. break;
  4509. case TOK_builtin_frame_address:
  4510. case TOK_builtin_return_address:
  4511. {
  4512. int tok1 = tok;
  4513. int level;
  4514. next();
  4515. skip('(');
  4516. if (tok != TOK_CINT) {
  4517. tcc_error("%s only takes positive integers",
  4518. tok1 == TOK_builtin_return_address ?
  4519. "__builtin_return_address" :
  4520. "__builtin_frame_address");
  4521. }
  4522. level = (uint32_t)tokc.i;
  4523. next();
  4524. skip(')');
  4525. type.t = VT_VOID;
  4526. mk_pointer(&type);
  4527. vset(&type, VT_LOCAL, 0); /* local frame */
  4528. while (level--) {
  4529. mk_pointer(&vtop->type);
  4530. indir(); /* -> parent frame */
  4531. }
  4532. if (tok1 == TOK_builtin_return_address) {
  4533. // assume return address is just above frame pointer on stack
  4534. vpushi(PTR_SIZE);
  4535. gen_op('+');
  4536. mk_pointer(&vtop->type);
  4537. indir();
  4538. }
  4539. }
  4540. break;
  4541. #ifdef TCC_TARGET_X86_64
  4542. #ifdef TCC_TARGET_PE
  4543. case TOK_builtin_va_start:
  4544. parse_builtin_params(0, "ee");
  4545. r = vtop->r & VT_VALMASK;
  4546. if (r == VT_LLOCAL)
  4547. r = VT_LOCAL;
  4548. if (r != VT_LOCAL)
  4549. tcc_error("__builtin_va_start expects a local variable");
  4550. vtop->r = r;
  4551. vtop->type = char_pointer_type;
  4552. vtop->c.i += 8;
  4553. vstore();
  4554. break;
  4555. #else
  4556. case TOK_builtin_va_arg_types:
  4557. parse_builtin_params(0, "t");
  4558. vpushi(classify_x86_64_va_arg(&vtop->type));
  4559. vswap();
  4560. vpop();
  4561. break;
  4562. #endif
  4563. #endif
  4564. #ifdef TCC_TARGET_ARM64
  4565. case TOK___va_start: {
  4566. parse_builtin_params(0, "ee");
  4567. //xx check types
  4568. gen_va_start();
  4569. vpushi(0);
  4570. vtop->type.t = VT_VOID;
  4571. break;
  4572. }
  4573. case TOK___va_arg: {
  4574. parse_builtin_params(0, "et");
  4575. type = vtop->type;
  4576. vpop();
  4577. //xx check types
  4578. gen_va_arg(&type);
  4579. vtop->type = type;
  4580. break;
  4581. }
  4582. case TOK___arm64_clear_cache: {
  4583. parse_builtin_params(0, "ee");
  4584. gen_clear_cache();
  4585. vpushi(0);
  4586. vtop->type.t = VT_VOID;
  4587. break;
  4588. }
  4589. #endif
  4590. /* pre operations */
  4591. case TOK_INC:
  4592. case TOK_DEC:
  4593. t = tok;
  4594. next();
  4595. unary();
  4596. inc(0, t);
  4597. break;
  4598. case '-':
  4599. next();
  4600. unary();
  4601. t = vtop->type.t & VT_BTYPE;
  4602. if (is_float(t)) {
  4603. /* In IEEE negate(x) isn't subtract(0,x), but rather
  4604. subtract(-0, x). */
  4605. vpush(&vtop->type);
  4606. if (t == VT_FLOAT)
  4607. vtop->c.f = -1.0 * 0.0;
  4608. else if (t == VT_DOUBLE)
  4609. vtop->c.d = -1.0 * 0.0;
  4610. else
  4611. vtop->c.ld = -1.0 * 0.0;
  4612. } else
  4613. vpushi(0);
  4614. vswap();
  4615. gen_op('-');
  4616. break;
  4617. case TOK_LAND:
  4618. if (!gnu_ext)
  4619. goto tok_identifier;
  4620. next();
  4621. /* allow to take the address of a label */
  4622. if (tok < TOK_UIDENT)
  4623. expect("label identifier");
  4624. s = label_find(tok);
  4625. if (!s) {
  4626. s = label_push(&global_label_stack, tok, LABEL_FORWARD);
  4627. } else {
  4628. if (s->r == LABEL_DECLARED)
  4629. s->r = LABEL_FORWARD;
  4630. }
  4631. if (!s->type.t) {
  4632. s->type.t = VT_VOID;
  4633. mk_pointer(&s->type);
  4634. s->type.t |= VT_STATIC;
  4635. }
  4636. vpushsym(&s->type, s);
  4637. next();
  4638. break;
  4639. case TOK_GENERIC:
  4640. {
  4641. CType controlling_type;
  4642. int has_default = 0;
  4643. int has_match = 0;
  4644. int learn = 0;
  4645. TokenString *str = NULL;
  4646. next();
  4647. skip('(');
  4648. expr_type(&controlling_type, expr_eq);
  4649. controlling_type.t &= ~(VT_CONSTANT | VT_VOLATILE | VT_ARRAY);
  4650. for (;;) {
  4651. learn = 0;
  4652. skip(',');
  4653. if (tok == TOK_DEFAULT) {
  4654. if (has_default)
  4655. tcc_error("too many 'default'");
  4656. has_default = 1;
  4657. if (!has_match)
  4658. learn = 1;
  4659. next();
  4660. } else {
  4661. AttributeDef ad_tmp;
  4662. int itmp;
  4663. CType cur_type;
  4664. parse_btype(&cur_type, &ad_tmp);
  4665. type_decl(&cur_type, &ad_tmp, &itmp, TYPE_ABSTRACT);
  4666. if (compare_types(&controlling_type, &cur_type, 0)) {
  4667. if (has_match) {
  4668. tcc_error("type match twice");
  4669. }
  4670. has_match = 1;
  4671. learn = 1;
  4672. }
  4673. }
  4674. skip(':');
  4675. if (learn) {
  4676. if (str)
  4677. tok_str_free(str);
  4678. skip_or_save_block(&str);
  4679. } else {
  4680. skip_or_save_block(NULL);
  4681. }
  4682. if (tok == ')')
  4683. break;
  4684. }
  4685. if (!str) {
  4686. char buf[60];
  4687. type_to_str(buf, sizeof buf, &controlling_type, NULL);
  4688. tcc_error("type '%s' does not match any association", buf);
  4689. }
  4690. begin_macro(str, 1);
  4691. next();
  4692. expr_eq();
  4693. if (tok != TOK_EOF)
  4694. expect(",");
  4695. end_macro();
  4696. next();
  4697. break;
  4698. }
  4699. // special qnan , snan and infinity values
  4700. case TOK___NAN__:
  4701. vpush64(VT_DOUBLE, 0x7ff8000000000000ULL);
  4702. next();
  4703. break;
  4704. case TOK___SNAN__:
  4705. vpush64(VT_DOUBLE, 0x7ff0000000000001ULL);
  4706. next();
  4707. break;
  4708. case TOK___INF__:
  4709. vpush64(VT_DOUBLE, 0x7ff0000000000000ULL);
  4710. next();
  4711. break;
  4712. default:
  4713. tok_identifier:
  4714. t = tok;
  4715. next();
  4716. if (t < TOK_UIDENT)
  4717. expect("identifier");
  4718. s = sym_find(t);
  4719. if (!s) {
  4720. const char *name = get_tok_str(t, NULL);
  4721. if (tok != '(')
  4722. tcc_error("'%s' undeclared", name);
  4723. /* for simple function calls, we tolerate undeclared
  4724. external reference to int() function */
  4725. if (tcc_state->warn_implicit_function_declaration
  4726. #ifdef TCC_TARGET_PE
  4727. /* people must be warned about using undeclared WINAPI functions
  4728. (which usually start with uppercase letter) */
  4729. || (name[0] >= 'A' && name[0] <= 'Z')
  4730. #endif
  4731. )
  4732. tcc_warning("implicit declaration of function '%s'", name);
  4733. s = external_global_sym(t, &func_old_type, 0);
  4734. }
  4735. r = s->r;
  4736. /* A symbol that has a register is a local register variable,
  4737. which starts out as VT_LOCAL value. */
  4738. if ((r & VT_VALMASK) < VT_CONST)
  4739. r = (r & ~VT_VALMASK) | VT_LOCAL;
  4740. vset(&s->type, r, s->c);
  4741. /* Point to s as backpointer (even without r&VT_SYM).
  4742. Will be used by at least the x86 inline asm parser for
  4743. regvars. */
  4744. vtop->sym = s;
  4745. if (r & VT_SYM) {
  4746. vtop->c.i = 0;
  4747. } else if (r == VT_CONST && IS_ENUM_VAL(s->type.t)) {
  4748. vtop->c.i = s->enum_val;
  4749. }
  4750. break;
  4751. }
  4752. /* post operations */
  4753. while (1) {
  4754. if (tok == TOK_INC || tok == TOK_DEC) {
  4755. inc(1, tok);
  4756. next();
  4757. } else if (tok == '.' || tok == TOK_ARROW || tok == TOK_CDOUBLE) {
  4758. int qualifiers;
  4759. /* field */
  4760. if (tok == TOK_ARROW)
  4761. indir();
  4762. qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
  4763. test_lvalue();
  4764. gaddrof();
  4765. /* expect pointer on structure */
  4766. if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
  4767. expect("struct or union");
  4768. if (tok == TOK_CDOUBLE)
  4769. expect("field name");
  4770. next();
  4771. if (tok == TOK_CINT || tok == TOK_CUINT)
  4772. expect("field name");
  4773. s = find_field(&vtop->type, tok);
  4774. if (!s)
  4775. tcc_error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, &tokc));
  4776. /* add field offset to pointer */
  4777. vtop->type = char_pointer_type; /* change type to 'char *' */
  4778. vpushi(s->c);
  4779. gen_op('+');
  4780. /* change type to field type, and set to lvalue */
  4781. vtop->type = s->type;
  4782. vtop->type.t |= qualifiers;
  4783. /* an array is never an lvalue */
  4784. if (!(vtop->type.t & VT_ARRAY)) {
  4785. vtop->r |= lvalue_type(vtop->type.t);
  4786. #ifdef CONFIG_TCC_BCHECK
  4787. /* if bound checking, the referenced pointer must be checked */
  4788. if (tcc_state->do_bounds_check && (vtop->r & VT_VALMASK) != VT_LOCAL)
  4789. vtop->r |= VT_MUSTBOUND;
  4790. #endif
  4791. }
  4792. next();
  4793. } else if (tok == '[') {
  4794. next();
  4795. gexpr();
  4796. gen_op('+');
  4797. indir();
  4798. skip(']');
  4799. } else if (tok == '(') {
  4800. SValue ret;
  4801. Sym *sa;
  4802. int nb_args, ret_nregs, ret_align, regsize, variadic;
  4803. /* function call */
  4804. if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
  4805. /* pointer test (no array accepted) */
  4806. if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
  4807. vtop->type = *pointed_type(&vtop->type);
  4808. if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
  4809. goto error_func;
  4810. } else {
  4811. error_func:
  4812. expect("function pointer");
  4813. }
  4814. } else {
  4815. vtop->r &= ~VT_LVAL; /* no lvalue */
  4816. }
  4817. /* get return type */
  4818. s = vtop->type.ref;
  4819. next();
  4820. sa = s->next; /* first parameter */
  4821. nb_args = regsize = 0;
  4822. ret.r2 = VT_CONST;
  4823. /* compute first implicit argument if a structure is returned */
  4824. if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
  4825. variadic = (s->f.func_type == FUNC_ELLIPSIS);
  4826. ret_nregs = gfunc_sret(&s->type, variadic, &ret.type,
  4827. &ret_align, &regsize);
  4828. if (!ret_nregs) {
  4829. /* get some space for the returned structure */
  4830. size = type_size(&s->type, &align);
  4831. #ifdef TCC_TARGET_ARM64
  4832. /* On arm64, a small struct is return in registers.
  4833. It is much easier to write it to memory if we know
  4834. that we are allowed to write some extra bytes, so
  4835. round the allocated space up to a power of 2: */
  4836. if (size < 16)
  4837. while (size & (size - 1))
  4838. size = (size | (size - 1)) + 1;
  4839. #endif
  4840. loc = (loc - size) & -align;
  4841. ret.type = s->type;
  4842. ret.r = VT_LOCAL | VT_LVAL;
  4843. /* pass it as 'int' to avoid structure arg passing
  4844. problems */
  4845. vseti(VT_LOCAL, loc);
  4846. ret.c = vtop->c;
  4847. nb_args++;
  4848. }
  4849. } else {
  4850. ret_nregs = 1;
  4851. ret.type = s->type;
  4852. }
  4853. if (ret_nregs) {
  4854. /* return in register */
  4855. if (is_float(ret.type.t)) {
  4856. ret.r = reg_fret(ret.type.t);
  4857. #ifdef TCC_TARGET_X86_64
  4858. if ((ret.type.t & VT_BTYPE) == VT_QFLOAT)
  4859. ret.r2 = REG_QRET;
  4860. #endif
  4861. } else {
  4862. #ifndef TCC_TARGET_ARM64
  4863. #ifdef TCC_TARGET_X86_64
  4864. if ((ret.type.t & VT_BTYPE) == VT_QLONG)
  4865. #else
  4866. if ((ret.type.t & VT_BTYPE) == VT_LLONG)
  4867. #endif
  4868. ret.r2 = REG_LRET;
  4869. #endif
  4870. ret.r = REG_IRET;
  4871. }
  4872. ret.c.i = 0;
  4873. }
  4874. if (tok != ')') {
  4875. for(;;) {
  4876. expr_eq();
  4877. gfunc_param_typed(s, sa);
  4878. nb_args++;
  4879. if (sa)
  4880. sa = sa->next;
  4881. if (tok == ')')
  4882. break;
  4883. skip(',');
  4884. }
  4885. }
  4886. if (sa)
  4887. tcc_error("too few arguments to function");
  4888. skip(')');
  4889. gfunc_call(nb_args);
  4890. /* return value */
  4891. for (r = ret.r + ret_nregs + !ret_nregs; r-- > ret.r;) {
  4892. vsetc(&ret.type, r, &ret.c);
  4893. vtop->r2 = ret.r2; /* Loop only happens when r2 is VT_CONST */
  4894. }
  4895. /* handle packed struct return */
  4896. if (((s->type.t & VT_BTYPE) == VT_STRUCT) && ret_nregs) {
  4897. int addr, offset;
  4898. size = type_size(&s->type, &align);
  4899. /* We're writing whole regs often, make sure there's enough
  4900. space. Assume register size is power of 2. */
  4901. if (regsize > align)
  4902. align = regsize;
  4903. loc = (loc - size) & -align;
  4904. addr = loc;
  4905. offset = 0;
  4906. for (;;) {
  4907. vset(&ret.type, VT_LOCAL | VT_LVAL, addr + offset);
  4908. vswap();
  4909. vstore();
  4910. vtop--;
  4911. if (--ret_nregs == 0)
  4912. break;
  4913. offset += regsize;
  4914. }
  4915. vset(&s->type, VT_LOCAL | VT_LVAL, addr);
  4916. }
  4917. } else {
  4918. break;
  4919. }
  4920. }
  4921. }
  4922. ST_FUNC void expr_prod(void)
  4923. {
  4924. int t;
  4925. unary();
  4926. while (tok == '*' || tok == '/' || tok == '%') {
  4927. t = tok;
  4928. next();
  4929. unary();
  4930. gen_op(t);
  4931. }
  4932. }
  4933. ST_FUNC void expr_sum(void)
  4934. {
  4935. int t;
  4936. expr_prod();
  4937. while (tok == '+' || tok == '-') {
  4938. t = tok;
  4939. next();
  4940. expr_prod();
  4941. gen_op(t);
  4942. }
  4943. }
  4944. static void expr_shift(void)
  4945. {
  4946. int t;
  4947. expr_sum();
  4948. while (tok == TOK_SHL || tok == TOK_SAR) {
  4949. t = tok;
  4950. next();
  4951. expr_sum();
  4952. gen_op(t);
  4953. }
  4954. }
  4955. static void expr_cmp(void)
  4956. {
  4957. int t;
  4958. expr_shift();
  4959. while ((tok >= TOK_ULE && tok <= TOK_GT) ||
  4960. tok == TOK_ULT || tok == TOK_UGE) {
  4961. t = tok;
  4962. next();
  4963. expr_shift();
  4964. gen_op(t);
  4965. }
  4966. }
  4967. static void expr_cmpeq(void)
  4968. {
  4969. int t;
  4970. expr_cmp();
  4971. while (tok == TOK_EQ || tok == TOK_NE) {
  4972. t = tok;
  4973. next();
  4974. expr_cmp();
  4975. gen_op(t);
  4976. }
  4977. }
  4978. static void expr_and(void)
  4979. {
  4980. expr_cmpeq();
  4981. while (tok == '&') {
  4982. next();
  4983. expr_cmpeq();
  4984. gen_op('&');
  4985. }
  4986. }
  4987. static void expr_xor(void)
  4988. {
  4989. expr_and();
  4990. while (tok == '^') {
  4991. next();
  4992. expr_and();
  4993. gen_op('^');
  4994. }
  4995. }
  4996. static void expr_or(void)
  4997. {
  4998. expr_xor();
  4999. while (tok == '|') {
  5000. next();
  5001. expr_xor();
  5002. gen_op('|');
  5003. }
  5004. }
  5005. static void expr_land(void)
  5006. {
  5007. expr_or();
  5008. if (tok == TOK_LAND) {
  5009. int t = 0;
  5010. for(;;) {
  5011. if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
  5012. gen_cast_s(VT_BOOL);
  5013. if (vtop->c.i) {
  5014. vpop();
  5015. } else {
  5016. nocode_wanted++;
  5017. while (tok == TOK_LAND) {
  5018. next();
  5019. expr_or();
  5020. vpop();
  5021. }
  5022. nocode_wanted--;
  5023. if (t)
  5024. gsym(t);
  5025. gen_cast_s(VT_INT);
  5026. break;
  5027. }
  5028. } else {
  5029. if (!t)
  5030. save_regs(1);
  5031. t = gvtst(1, t);
  5032. }
  5033. if (tok != TOK_LAND) {
  5034. if (t)
  5035. vseti(VT_JMPI, t);
  5036. else
  5037. vpushi(1);
  5038. break;
  5039. }
  5040. next();
  5041. expr_or();
  5042. }
  5043. }
  5044. }
  5045. static void expr_lor(void)
  5046. {
  5047. expr_land();
  5048. if (tok == TOK_LOR) {
  5049. int t = 0;
  5050. for(;;) {
  5051. if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
  5052. gen_cast_s(VT_BOOL);
  5053. if (!vtop->c.i) {
  5054. vpop();
  5055. } else {
  5056. nocode_wanted++;
  5057. while (tok == TOK_LOR) {
  5058. next();
  5059. expr_land();
  5060. vpop();
  5061. }
  5062. nocode_wanted--;
  5063. if (t)
  5064. gsym(t);
  5065. gen_cast_s(VT_INT);
  5066. break;
  5067. }
  5068. } else {
  5069. if (!t)
  5070. save_regs(1);
  5071. t = gvtst(0, t);
  5072. }
  5073. if (tok != TOK_LOR) {
  5074. if (t)
  5075. vseti(VT_JMP, t);
  5076. else
  5077. vpushi(0);
  5078. break;
  5079. }
  5080. next();
  5081. expr_land();
  5082. }
  5083. }
  5084. }
  5085. /* Assuming vtop is a value used in a conditional context
  5086. (i.e. compared with zero) return 0 if it's false, 1 if
  5087. true and -1 if it can't be statically determined. */
  5088. static int condition_3way(void)
  5089. {
  5090. int c = -1;
  5091. if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST &&
  5092. (!(vtop->r & VT_SYM) || !vtop->sym->a.weak)) {
  5093. vdup();
  5094. gen_cast_s(VT_BOOL);
  5095. c = vtop->c.i;
  5096. vpop();
  5097. }
  5098. return c;
  5099. }
  5100. static void expr_cond(void)
  5101. {
  5102. int tt, u, r1, r2, rc, t1, t2, bt1, bt2, islv, c, g;
  5103. SValue sv;
  5104. CType type, type1, type2;
  5105. expr_lor();
  5106. if (tok == '?') {
  5107. next();
  5108. c = condition_3way();
  5109. g = (tok == ':' && gnu_ext);
  5110. if (c < 0) {
  5111. /* needed to avoid having different registers saved in
  5112. each branch */
  5113. if (is_float(vtop->type.t)) {
  5114. rc = RC_FLOAT;
  5115. #ifdef TCC_TARGET_X86_64
  5116. if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
  5117. rc = RC_ST0;
  5118. }
  5119. #endif
  5120. } else
  5121. rc = RC_INT;
  5122. gv(rc);
  5123. save_regs(1);
  5124. if (g)
  5125. gv_dup();
  5126. tt = gvtst(1, 0);
  5127. } else {
  5128. if (!g)
  5129. vpop();
  5130. tt = 0;
  5131. }
  5132. if (1) {
  5133. if (c == 0)
  5134. nocode_wanted++;
  5135. if (!g)
  5136. gexpr();
  5137. type1 = vtop->type;
  5138. sv = *vtop; /* save value to handle it later */
  5139. vtop--; /* no vpop so that FP stack is not flushed */
  5140. skip(':');
  5141. u = 0;
  5142. if (c < 0)
  5143. u = gjmp(0);
  5144. gsym(tt);
  5145. if (c == 0)
  5146. nocode_wanted--;
  5147. if (c == 1)
  5148. nocode_wanted++;
  5149. expr_cond();
  5150. if (c == 1)
  5151. nocode_wanted--;
  5152. type2 = vtop->type;
  5153. t1 = type1.t;
  5154. bt1 = t1 & VT_BTYPE;
  5155. t2 = type2.t;
  5156. bt2 = t2 & VT_BTYPE;
  5157. type.ref = NULL;
  5158. /* cast operands to correct type according to ISOC rules */
  5159. if (is_float(bt1) || is_float(bt2)) {
  5160. if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
  5161. type.t = VT_LDOUBLE;
  5162. } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
  5163. type.t = VT_DOUBLE;
  5164. } else {
  5165. type.t = VT_FLOAT;
  5166. }
  5167. } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
  5168. /* cast to biggest op */
  5169. type.t = VT_LLONG | VT_LONG;
  5170. if (bt1 == VT_LLONG)
  5171. type.t &= t1;
  5172. if (bt2 == VT_LLONG)
  5173. type.t &= t2;
  5174. /* convert to unsigned if it does not fit in a long long */
  5175. if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_LLONG | VT_UNSIGNED) ||
  5176. (t2 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_LLONG | VT_UNSIGNED))
  5177. type.t |= VT_UNSIGNED;
  5178. } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
  5179. /* If one is a null ptr constant the result type
  5180. is the other. */
  5181. if (is_null_pointer (vtop))
  5182. type = type1;
  5183. else if (is_null_pointer (&sv))
  5184. type = type2;
  5185. /* XXX: test pointer compatibility, C99 has more elaborate
  5186. rules here. */
  5187. else
  5188. type = type1;
  5189. } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
  5190. /* XXX: test function pointer compatibility */
  5191. type = bt1 == VT_FUNC ? type1 : type2;
  5192. } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
  5193. /* XXX: test structure compatibility */
  5194. type = bt1 == VT_STRUCT ? type1 : type2;
  5195. } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
  5196. /* NOTE: as an extension, we accept void on only one side */
  5197. type.t = VT_VOID;
  5198. } else {
  5199. /* integer operations */
  5200. type.t = VT_INT | (VT_LONG & (t1 | t2));
  5201. /* convert to unsigned if it does not fit in an integer */
  5202. if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_INT | VT_UNSIGNED) ||
  5203. (t2 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_INT | VT_UNSIGNED))
  5204. type.t |= VT_UNSIGNED;
  5205. }
  5206. /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
  5207. that `(expr ? a : b).mem` does not error with "lvalue expected" */
  5208. islv = (vtop->r & VT_LVAL) && (sv.r & VT_LVAL) && VT_STRUCT == (type.t & VT_BTYPE);
  5209. islv &= c < 0;
  5210. /* now we convert second operand */
  5211. if (c != 1) {
  5212. gen_cast(&type);
  5213. if (islv) {
  5214. mk_pointer(&vtop->type);
  5215. gaddrof();
  5216. } else if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
  5217. gaddrof();
  5218. }
  5219. rc = RC_INT;
  5220. if (is_float(type.t)) {
  5221. rc = RC_FLOAT;
  5222. #ifdef TCC_TARGET_X86_64
  5223. if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
  5224. rc = RC_ST0;
  5225. }
  5226. #endif
  5227. } else if ((type.t & VT_BTYPE) == VT_LLONG) {
  5228. /* for long longs, we use fixed registers to avoid having
  5229. to handle a complicated move */
  5230. rc = RC_IRET;
  5231. }
  5232. tt = r2 = 0;
  5233. if (c < 0) {
  5234. r2 = gv(rc);
  5235. tt = gjmp(0);
  5236. }
  5237. gsym(u);
  5238. /* this is horrible, but we must also convert first
  5239. operand */
  5240. if (c != 0) {
  5241. *vtop = sv;
  5242. gen_cast(&type);
  5243. if (islv) {
  5244. mk_pointer(&vtop->type);
  5245. gaddrof();
  5246. } else if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
  5247. gaddrof();
  5248. }
  5249. if (c < 0) {
  5250. r1 = gv(rc);
  5251. move_reg(r2, r1, type.t);
  5252. vtop->r = r2;
  5253. gsym(tt);
  5254. if (islv)
  5255. indir();
  5256. }
  5257. }
  5258. }
  5259. }
  5260. static void expr_eq(void)
  5261. {
  5262. int t;
  5263. expr_cond();
  5264. if (tok == '=' ||
  5265. (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
  5266. tok == TOK_A_XOR || tok == TOK_A_OR ||
  5267. tok == TOK_A_SHL || tok == TOK_A_SAR) {
  5268. test_lvalue();
  5269. t = tok;
  5270. next();
  5271. if (t == '=') {
  5272. expr_eq();
  5273. } else {
  5274. vdup();
  5275. expr_eq();
  5276. gen_op(t & 0x7f);
  5277. }
  5278. vstore();
  5279. }
  5280. }
  5281. ST_FUNC void gexpr(void)
  5282. {
  5283. while (1) {
  5284. expr_eq();
  5285. if (tok != ',')
  5286. break;
  5287. vpop();
  5288. next();
  5289. }
  5290. }
  5291. /* parse a constant expression and return value in vtop. */
  5292. static void expr_const1(void)
  5293. {
  5294. const_wanted++;
  5295. nocode_wanted++;
  5296. expr_cond();
  5297. nocode_wanted--;
  5298. const_wanted--;
  5299. }
  5300. /* parse an integer constant and return its value. */
  5301. static inline int64_t expr_const64(void)
  5302. {
  5303. int64_t c;
  5304. expr_const1();
  5305. if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
  5306. expect("constant expression");
  5307. c = vtop->c.i;
  5308. vpop();
  5309. return c;
  5310. }
  5311. /* parse an integer constant and return its value.
  5312. Complain if it doesn't fit 32bit (signed or unsigned). */
  5313. ST_FUNC int expr_const(void)
  5314. {
  5315. int c;
  5316. int64_t wc = expr_const64();
  5317. c = wc;
  5318. if (c != wc && (unsigned)c != wc)
  5319. tcc_error("constant exceeds 32 bit");
  5320. return c;
  5321. }
  5322. /* return the label token if current token is a label, otherwise
  5323. return zero */
  5324. static int is_label(void)
  5325. {
  5326. int last_tok;
  5327. /* fast test first */
  5328. if (tok < TOK_UIDENT)
  5329. return 0;
  5330. /* no need to save tokc because tok is an identifier */
  5331. last_tok = tok;
  5332. next();
  5333. if (tok == ':') {
  5334. return last_tok;
  5335. } else {
  5336. unget_tok(last_tok);
  5337. return 0;
  5338. }
  5339. }
  5340. #ifndef TCC_TARGET_ARM64
  5341. static void gfunc_return(CType *func_type)
  5342. {
  5343. if ((func_type->t & VT_BTYPE) == VT_STRUCT) {
  5344. CType type, ret_type;
  5345. int ret_align, ret_nregs, regsize;
  5346. ret_nregs = gfunc_sret(func_type, func_var, &ret_type,
  5347. &ret_align, &regsize);
  5348. if (0 == ret_nregs) {
  5349. /* if returning structure, must copy it to implicit
  5350. first pointer arg location */
  5351. type = *func_type;
  5352. mk_pointer(&type);
  5353. vset(&type, VT_LOCAL | VT_LVAL, func_vc);
  5354. indir();
  5355. vswap();
  5356. /* copy structure value to pointer */
  5357. vstore();
  5358. } else {
  5359. /* returning structure packed into registers */
  5360. int r, size, addr, align;
  5361. size = type_size(func_type,&align);
  5362. if ((vtop->r != (VT_LOCAL | VT_LVAL) ||
  5363. (vtop->c.i & (ret_align-1)))
  5364. && (align & (ret_align-1))) {
  5365. loc = (loc - size) & -ret_align;
  5366. addr = loc;
  5367. type = *func_type;
  5368. vset(&type, VT_LOCAL | VT_LVAL, addr);
  5369. vswap();
  5370. vstore();
  5371. vpop();
  5372. vset(&ret_type, VT_LOCAL | VT_LVAL, addr);
  5373. }
  5374. vtop->type = ret_type;
  5375. if (is_float(ret_type.t))
  5376. r = rc_fret(ret_type.t);
  5377. else
  5378. r = RC_IRET;
  5379. if (ret_nregs == 1)
  5380. gv(r);
  5381. else {
  5382. for (;;) {
  5383. vdup();
  5384. gv(r);
  5385. vpop();
  5386. if (--ret_nregs == 0)
  5387. break;
  5388. /* We assume that when a structure is returned in multiple
  5389. registers, their classes are consecutive values of the
  5390. suite s(n) = 2^n */
  5391. r <<= 1;
  5392. vtop->c.i += regsize;
  5393. }
  5394. }
  5395. }
  5396. } else if (is_float(func_type->t)) {
  5397. gv(rc_fret(func_type->t));
  5398. } else {
  5399. gv(RC_IRET);
  5400. }
  5401. vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
  5402. }
  5403. #endif
  5404. static int case_cmp(const void *pa, const void *pb)
  5405. {
  5406. int64_t a = (*(struct case_t**) pa)->v1;
  5407. int64_t b = (*(struct case_t**) pb)->v1;
  5408. return a < b ? -1 : a > b;
  5409. }
  5410. static void gcase(struct case_t **base, int len, int *bsym)
  5411. {
  5412. struct case_t *p;
  5413. int e;
  5414. int ll = (vtop->type.t & VT_BTYPE) == VT_LLONG;
  5415. gv(RC_INT);
  5416. while (len > 4) {
  5417. /* binary search */
  5418. p = base[len/2];
  5419. vdup();
  5420. if (ll)
  5421. vpushll(p->v2);
  5422. else
  5423. vpushi(p->v2);
  5424. gen_op(TOK_LE);
  5425. e = gtst(1, 0);
  5426. vdup();
  5427. if (ll)
  5428. vpushll(p->v1);
  5429. else
  5430. vpushi(p->v1);
  5431. gen_op(TOK_GE);
  5432. gtst_addr(0, p->sym); /* v1 <= x <= v2 */
  5433. /* x < v1 */
  5434. gcase(base, len/2, bsym);
  5435. if (cur_switch->def_sym)
  5436. gjmp_addr(cur_switch->def_sym);
  5437. else
  5438. *bsym = gjmp(*bsym);
  5439. /* x > v2 */
  5440. gsym(e);
  5441. e = len/2 + 1;
  5442. base += e; len -= e;
  5443. }
  5444. /* linear scan */
  5445. while (len--) {
  5446. p = *base++;
  5447. vdup();
  5448. if (ll)
  5449. vpushll(p->v2);
  5450. else
  5451. vpushi(p->v2);
  5452. if (p->v1 == p->v2) {
  5453. gen_op(TOK_EQ);
  5454. gtst_addr(0, p->sym);
  5455. } else {
  5456. gen_op(TOK_LE);
  5457. e = gtst(1, 0);
  5458. vdup();
  5459. if (ll)
  5460. vpushll(p->v1);
  5461. else
  5462. vpushi(p->v1);
  5463. gen_op(TOK_GE);
  5464. gtst_addr(0, p->sym);
  5465. gsym(e);
  5466. }
  5467. }
  5468. }
  5469. static void block(int *bsym, int *csym, int is_expr)
  5470. {
  5471. int a, b, c, d, cond;
  5472. Sym *s;
  5473. /* generate line number info */
  5474. if (tcc_state->do_debug)
  5475. tcc_debug_line(tcc_state);
  5476. if (is_expr) {
  5477. /* default return value is (void) */
  5478. vpushi(0);
  5479. vtop->type.t = VT_VOID;
  5480. }
  5481. if (tok == TOK_IF) {
  5482. /* if test */
  5483. int saved_nocode_wanted = nocode_wanted;
  5484. next();
  5485. skip('(');
  5486. gexpr();
  5487. skip(')');
  5488. cond = condition_3way();
  5489. if (cond == 1)
  5490. a = 0, vpop();
  5491. else
  5492. a = gvtst(1, 0);
  5493. if (cond == 0)
  5494. nocode_wanted |= 0x20000000;
  5495. block(bsym, csym, 0);
  5496. if (cond != 1)
  5497. nocode_wanted = saved_nocode_wanted;
  5498. c = tok;
  5499. if (c == TOK_ELSE) {
  5500. next();
  5501. d = gjmp(0);
  5502. gsym(a);
  5503. if (cond == 1)
  5504. nocode_wanted |= 0x20000000;
  5505. block(bsym, csym, 0);
  5506. gsym(d); /* patch else jmp */
  5507. if (cond != 0)
  5508. nocode_wanted = saved_nocode_wanted;
  5509. } else
  5510. gsym(a);
  5511. } else if (tok == TOK_WHILE) {
  5512. int saved_nocode_wanted;
  5513. nocode_wanted &= ~0x20000000;
  5514. next();
  5515. d = ind;
  5516. vla_sp_restore();
  5517. skip('(');
  5518. gexpr();
  5519. skip(')');
  5520. a = gvtst(1, 0);
  5521. b = 0;
  5522. ++local_scope;
  5523. saved_nocode_wanted = nocode_wanted;
  5524. block(&a, &b, 0);
  5525. nocode_wanted = saved_nocode_wanted;
  5526. --local_scope;
  5527. gjmp_addr(d);
  5528. gsym(a);
  5529. gsym_addr(b, d);
  5530. } else if (tok == '{') {
  5531. Sym *llabel;
  5532. int block_vla_sp_loc = vla_sp_loc, saved_vlas_in_scope = vlas_in_scope;
  5533. next();
  5534. /* record local declaration stack position */
  5535. s = local_stack;
  5536. llabel = local_label_stack;
  5537. ++local_scope;
  5538. /* handle local labels declarations */
  5539. if (tok == TOK_LABEL) {
  5540. next();
  5541. for(;;) {
  5542. if (tok < TOK_UIDENT)
  5543. expect("label identifier");
  5544. label_push(&local_label_stack, tok, LABEL_DECLARED);
  5545. next();
  5546. if (tok == ',') {
  5547. next();
  5548. } else {
  5549. skip(';');
  5550. break;
  5551. }
  5552. }
  5553. }
  5554. while (tok != '}') {
  5555. if ((a = is_label()))
  5556. unget_tok(a);
  5557. else
  5558. decl(VT_LOCAL);
  5559. if (tok != '}') {
  5560. if (is_expr)
  5561. vpop();
  5562. block(bsym, csym, is_expr);
  5563. }
  5564. }
  5565. /* pop locally defined labels */
  5566. label_pop(&local_label_stack, llabel, is_expr);
  5567. /* pop locally defined symbols */
  5568. --local_scope;
  5569. /* In the is_expr case (a statement expression is finished here),
  5570. vtop might refer to symbols on the local_stack. Either via the
  5571. type or via vtop->sym. We can't pop those nor any that in turn
  5572. might be referred to. To make it easier we don't roll back
  5573. any symbols in that case; some upper level call to block() will
  5574. do that. We do have to remove such symbols from the lookup
  5575. tables, though. sym_pop will do that. */
  5576. sym_pop(&local_stack, s, is_expr);
  5577. /* Pop VLA frames and restore stack pointer if required */
  5578. if (vlas_in_scope > saved_vlas_in_scope) {
  5579. vla_sp_loc = saved_vlas_in_scope ? block_vla_sp_loc : vla_sp_root_loc;
  5580. vla_sp_restore();
  5581. }
  5582. vlas_in_scope = saved_vlas_in_scope;
  5583. next();
  5584. } else if (tok == TOK_RETURN) {
  5585. next();
  5586. if (tok != ';') {
  5587. gexpr();
  5588. gen_assign_cast(&func_vt);
  5589. if ((func_vt.t & VT_BTYPE) == VT_VOID)
  5590. vtop--;
  5591. else
  5592. gfunc_return(&func_vt);
  5593. }
  5594. skip(';');
  5595. /* jump unless last stmt in top-level block */
  5596. if (tok != '}' || local_scope != 1)
  5597. rsym = gjmp(rsym);
  5598. nocode_wanted |= 0x20000000;
  5599. } else if (tok == TOK_BREAK) {
  5600. /* compute jump */
  5601. if (!bsym)
  5602. tcc_error("cannot break");
  5603. *bsym = gjmp(*bsym);
  5604. next();
  5605. skip(';');
  5606. nocode_wanted |= 0x20000000;
  5607. } else if (tok == TOK_CONTINUE) {
  5608. /* compute jump */
  5609. if (!csym)
  5610. tcc_error("cannot continue");
  5611. vla_sp_restore_root();
  5612. *csym = gjmp(*csym);
  5613. next();
  5614. skip(';');
  5615. } else if (tok == TOK_FOR) {
  5616. int e;
  5617. int saved_nocode_wanted;
  5618. nocode_wanted &= ~0x20000000;
  5619. next();
  5620. skip('(');
  5621. s = local_stack;
  5622. ++local_scope;
  5623. if (tok != ';') {
  5624. /* c99 for-loop init decl? */
  5625. if (!decl0(VT_LOCAL, 1, NULL)) {
  5626. /* no, regular for-loop init expr */
  5627. gexpr();
  5628. vpop();
  5629. }
  5630. }
  5631. skip(';');
  5632. d = ind;
  5633. c = ind;
  5634. vla_sp_restore();
  5635. a = 0;
  5636. b = 0;
  5637. if (tok != ';') {
  5638. gexpr();
  5639. a = gvtst(1, 0);
  5640. }
  5641. skip(';');
  5642. if (tok != ')') {
  5643. e = gjmp(0);
  5644. c = ind;
  5645. vla_sp_restore();
  5646. gexpr();
  5647. vpop();
  5648. gjmp_addr(d);
  5649. gsym(e);
  5650. }
  5651. skip(')');
  5652. saved_nocode_wanted = nocode_wanted;
  5653. block(&a, &b, 0);
  5654. nocode_wanted = saved_nocode_wanted;
  5655. gjmp_addr(c);
  5656. gsym(a);
  5657. gsym_addr(b, c);
  5658. --local_scope;
  5659. sym_pop(&local_stack, s, 0);
  5660. } else
  5661. if (tok == TOK_DO) {
  5662. int saved_nocode_wanted;
  5663. nocode_wanted &= ~0x20000000;
  5664. next();
  5665. a = 0;
  5666. b = 0;
  5667. d = ind;
  5668. vla_sp_restore();
  5669. saved_nocode_wanted = nocode_wanted;
  5670. block(&a, &b, 0);
  5671. skip(TOK_WHILE);
  5672. skip('(');
  5673. gsym(b);
  5674. gexpr();
  5675. c = gvtst(0, 0);
  5676. gsym_addr(c, d);
  5677. nocode_wanted = saved_nocode_wanted;
  5678. skip(')');
  5679. gsym(a);
  5680. skip(';');
  5681. } else
  5682. if (tok == TOK_SWITCH) {
  5683. struct switch_t *saved, sw;
  5684. int saved_nocode_wanted = nocode_wanted;
  5685. SValue switchval;
  5686. next();
  5687. skip('(');
  5688. gexpr();
  5689. skip(')');
  5690. switchval = *vtop--;
  5691. a = 0;
  5692. b = gjmp(0); /* jump to first case */
  5693. sw.p = NULL; sw.n = 0; sw.def_sym = 0;
  5694. saved = cur_switch;
  5695. cur_switch = &sw;
  5696. block(&a, csym, 0);
  5697. nocode_wanted = saved_nocode_wanted;
  5698. a = gjmp(a); /* add implicit break */
  5699. /* case lookup */
  5700. gsym(b);
  5701. qsort(sw.p, sw.n, sizeof(void*), case_cmp);
  5702. for (b = 1; b < sw.n; b++)
  5703. if (sw.p[b - 1]->v2 >= sw.p[b]->v1)
  5704. tcc_error("duplicate case value");
  5705. /* Our switch table sorting is signed, so the compared
  5706. value needs to be as well when it's 64bit. */
  5707. if ((switchval.type.t & VT_BTYPE) == VT_LLONG)
  5708. switchval.type.t &= ~VT_UNSIGNED;
  5709. vpushv(&switchval);
  5710. gcase(sw.p, sw.n, &a);
  5711. vpop();
  5712. if (sw.def_sym)
  5713. gjmp_addr(sw.def_sym);
  5714. dynarray_reset(&sw.p, &sw.n);
  5715. cur_switch = saved;
  5716. /* break label */
  5717. gsym(a);
  5718. } else
  5719. if (tok == TOK_CASE) {
  5720. struct case_t *cr = tcc_malloc(sizeof(struct case_t));
  5721. if (!cur_switch)
  5722. expect("switch");
  5723. nocode_wanted &= ~0x20000000;
  5724. next();
  5725. cr->v1 = cr->v2 = expr_const64();
  5726. if (gnu_ext && tok == TOK_DOTS) {
  5727. next();
  5728. cr->v2 = expr_const64();
  5729. if (cr->v2 < cr->v1)
  5730. tcc_warning("empty case range");
  5731. }
  5732. cr->sym = ind;
  5733. dynarray_add(&cur_switch->p, &cur_switch->n, cr);
  5734. skip(':');
  5735. is_expr = 0;
  5736. goto block_after_label;
  5737. } else
  5738. if (tok == TOK_DEFAULT) {
  5739. next();
  5740. skip(':');
  5741. if (!cur_switch)
  5742. expect("switch");
  5743. if (cur_switch->def_sym)
  5744. tcc_error("too many 'default'");
  5745. cur_switch->def_sym = ind;
  5746. is_expr = 0;
  5747. goto block_after_label;
  5748. } else
  5749. if (tok == TOK_GOTO) {
  5750. next();
  5751. if (tok == '*' && gnu_ext) {
  5752. /* computed goto */
  5753. next();
  5754. gexpr();
  5755. if ((vtop->type.t & VT_BTYPE) != VT_PTR)
  5756. expect("pointer");
  5757. ggoto();
  5758. } else if (tok >= TOK_UIDENT) {
  5759. s = label_find(tok);
  5760. /* put forward definition if needed */
  5761. if (!s) {
  5762. s = label_push(&global_label_stack, tok, LABEL_FORWARD);
  5763. } else {
  5764. if (s->r == LABEL_DECLARED)
  5765. s->r = LABEL_FORWARD;
  5766. }
  5767. vla_sp_restore_root();
  5768. if (s->r & LABEL_FORWARD)
  5769. s->jnext = gjmp(s->jnext);
  5770. else
  5771. gjmp_addr(s->jnext);
  5772. next();
  5773. } else {
  5774. expect("label identifier");
  5775. }
  5776. skip(';');
  5777. } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
  5778. asm_instr();
  5779. } else {
  5780. b = is_label();
  5781. if (b) {
  5782. /* label case */
  5783. next();
  5784. s = label_find(b);
  5785. if (s) {
  5786. if (s->r == LABEL_DEFINED)
  5787. tcc_error("duplicate label '%s'", get_tok_str(s->v, NULL));
  5788. gsym(s->jnext);
  5789. s->r = LABEL_DEFINED;
  5790. } else {
  5791. s = label_push(&global_label_stack, b, LABEL_DEFINED);
  5792. }
  5793. s->jnext = ind;
  5794. vla_sp_restore();
  5795. /* we accept this, but it is a mistake */
  5796. block_after_label:
  5797. nocode_wanted &= ~0x20000000;
  5798. if (tok == '}') {
  5799. tcc_warning("deprecated use of label at end of compound statement");
  5800. } else {
  5801. if (is_expr)
  5802. vpop();
  5803. block(bsym, csym, is_expr);
  5804. }
  5805. } else {
  5806. /* expression case */
  5807. if (tok != ';') {
  5808. if (is_expr) {
  5809. vpop();
  5810. gexpr();
  5811. } else {
  5812. gexpr();
  5813. vpop();
  5814. }
  5815. }
  5816. skip(';');
  5817. }
  5818. }
  5819. }
  5820. /* This skips over a stream of tokens containing balanced {} and ()
  5821. pairs, stopping at outer ',' ';' and '}' (or matching '}' if we started
  5822. with a '{'). If STR then allocates and stores the skipped tokens
  5823. in *STR. This doesn't check if () and {} are nested correctly,
  5824. i.e. "({)}" is accepted. */
  5825. static void skip_or_save_block(TokenString **str)
  5826. {
  5827. int braces = tok == '{';
  5828. int level = 0;
  5829. if (str)
  5830. *str = tok_str_alloc();
  5831. while ((level > 0 || (tok != '}' && tok != ',' && tok != ';' && tok != ')'))) {
  5832. int t;
  5833. if (tok == TOK_EOF) {
  5834. if (str || level > 0)
  5835. tcc_error("unexpected end of file");
  5836. else
  5837. break;
  5838. }
  5839. if (str)
  5840. tok_str_add_tok(*str);
  5841. t = tok;
  5842. next();
  5843. if (t == '{' || t == '(') {
  5844. level++;
  5845. } else if (t == '}' || t == ')') {
  5846. level--;
  5847. if (level == 0 && braces && t == '}')
  5848. break;
  5849. }
  5850. }
  5851. if (str) {
  5852. tok_str_add(*str, -1);
  5853. tok_str_add(*str, 0);
  5854. }
  5855. }
  5856. #define EXPR_CONST 1
  5857. #define EXPR_ANY 2
  5858. static void parse_init_elem(int expr_type)
  5859. {
  5860. int saved_global_expr;
  5861. switch(expr_type) {
  5862. case EXPR_CONST:
  5863. /* compound literals must be allocated globally in this case */
  5864. saved_global_expr = global_expr;
  5865. global_expr = 1;
  5866. expr_const1();
  5867. global_expr = saved_global_expr;
  5868. /* NOTE: symbols are accepted, as well as lvalue for anon symbols
  5869. (compound literals). */
  5870. if (((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST
  5871. && ((vtop->r & (VT_SYM|VT_LVAL)) != (VT_SYM|VT_LVAL)
  5872. || vtop->sym->v < SYM_FIRST_ANOM))
  5873. #ifdef TCC_TARGET_PE
  5874. || ((vtop->r & VT_SYM) && vtop->sym->a.dllimport)
  5875. #endif
  5876. )
  5877. tcc_error("initializer element is not constant");
  5878. break;
  5879. case EXPR_ANY:
  5880. expr_eq();
  5881. break;
  5882. }
  5883. }
  5884. /* put zeros for variable based init */
  5885. static void init_putz(Section *sec, unsigned long c, int size)
  5886. {
  5887. if (sec) {
  5888. /* nothing to do because globals are already set to zero */
  5889. } else {
  5890. vpush_global_sym(&func_old_type, TOK_memset);
  5891. vseti(VT_LOCAL, c);
  5892. #ifdef TCC_TARGET_ARM
  5893. vpushs(size);
  5894. vpushi(0);
  5895. #else
  5896. vpushi(0);
  5897. vpushs(size);
  5898. #endif
  5899. gfunc_call(3);
  5900. }
  5901. }
  5902. /* t is the array or struct type. c is the array or struct
  5903. address. cur_field is the pointer to the current
  5904. field, for arrays the 'c' member contains the current start
  5905. index. 'size_only' is true if only size info is needed (only used
  5906. in arrays). al contains the already initialized length of the
  5907. current container (starting at c). This returns the new length of that. */
  5908. static int decl_designator(CType *type, Section *sec, unsigned long c,
  5909. Sym **cur_field, int size_only, int al)
  5910. {
  5911. Sym *s, *f;
  5912. int index, index_last, align, l, nb_elems, elem_size;
  5913. unsigned long corig = c;
  5914. elem_size = 0;
  5915. nb_elems = 1;
  5916. if (gnu_ext && (l = is_label()) != 0)
  5917. goto struct_field;
  5918. /* NOTE: we only support ranges for last designator */
  5919. while (nb_elems == 1 && (tok == '[' || tok == '.')) {
  5920. if (tok == '[') {
  5921. if (!(type->t & VT_ARRAY))
  5922. expect("array type");
  5923. next();
  5924. index = index_last = expr_const();
  5925. if (tok == TOK_DOTS && gnu_ext) {
  5926. next();
  5927. index_last = expr_const();
  5928. }
  5929. skip(']');
  5930. s = type->ref;
  5931. if (index < 0 || (s->c >= 0 && index_last >= s->c) ||
  5932. index_last < index)
  5933. tcc_error("invalid index");
  5934. if (cur_field)
  5935. (*cur_field)->c = index_last;
  5936. type = pointed_type(type);
  5937. elem_size = type_size(type, &align);
  5938. c += index * elem_size;
  5939. nb_elems = index_last - index + 1;
  5940. } else {
  5941. next();
  5942. l = tok;
  5943. struct_field:
  5944. next();
  5945. if ((type->t & VT_BTYPE) != VT_STRUCT)
  5946. expect("struct/union type");
  5947. f = find_field(type, l);
  5948. if (!f)
  5949. expect("field");
  5950. if (cur_field)
  5951. *cur_field = f;
  5952. type = &f->type;
  5953. c += f->c;
  5954. }
  5955. cur_field = NULL;
  5956. }
  5957. if (!cur_field) {
  5958. if (tok == '=') {
  5959. next();
  5960. } else if (!gnu_ext) {
  5961. expect("=");
  5962. }
  5963. } else {
  5964. if (type->t & VT_ARRAY) {
  5965. index = (*cur_field)->c;
  5966. if (type->ref->c >= 0 && index >= type->ref->c)
  5967. tcc_error("index too large");
  5968. type = pointed_type(type);
  5969. c += index * type_size(type, &align);
  5970. } else {
  5971. f = *cur_field;
  5972. while (f && (f->v & SYM_FIRST_ANOM) && (f->type.t & VT_BITFIELD))
  5973. *cur_field = f = f->next;
  5974. if (!f)
  5975. tcc_error("too many field init");
  5976. type = &f->type;
  5977. c += f->c;
  5978. }
  5979. }
  5980. /* must put zero in holes (note that doing it that way
  5981. ensures that it even works with designators) */
  5982. if (!size_only && c - corig > al)
  5983. init_putz(sec, corig + al, c - corig - al);
  5984. decl_initializer(type, sec, c, 0, size_only);
  5985. /* XXX: make it more general */
  5986. if (!size_only && nb_elems > 1) {
  5987. unsigned long c_end;
  5988. uint8_t *src, *dst;
  5989. int i;
  5990. if (!sec) {
  5991. vset(type, VT_LOCAL|VT_LVAL, c);
  5992. for (i = 1; i < nb_elems; i++) {
  5993. vset(type, VT_LOCAL|VT_LVAL, c + elem_size * i);
  5994. vswap();
  5995. vstore();
  5996. }
  5997. vpop();
  5998. } else if (!NODATA_WANTED) {
  5999. c_end = c + nb_elems * elem_size;
  6000. if (c_end > sec->data_allocated)
  6001. section_realloc(sec, c_end);
  6002. src = sec->data + c;
  6003. dst = src;
  6004. for(i = 1; i < nb_elems; i++) {
  6005. dst += elem_size;
  6006. memcpy(dst, src, elem_size);
  6007. }
  6008. }
  6009. }
  6010. c += nb_elems * type_size(type, &align);
  6011. if (c - corig > al)
  6012. al = c - corig;
  6013. return al;
  6014. }
  6015. /* store a value or an expression directly in global data or in local array */
  6016. static void init_putv(CType *type, Section *sec, unsigned long c)
  6017. {
  6018. int bt;
  6019. void *ptr;
  6020. CType dtype;
  6021. dtype = *type;
  6022. dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
  6023. if (sec) {
  6024. int size, align;
  6025. /* XXX: not portable */
  6026. /* XXX: generate error if incorrect relocation */
  6027. gen_assign_cast(&dtype);
  6028. bt = type->t & VT_BTYPE;
  6029. if ((vtop->r & VT_SYM)
  6030. && bt != VT_PTR
  6031. && bt != VT_FUNC
  6032. && (bt != (PTR_SIZE == 8 ? VT_LLONG : VT_INT)
  6033. || (type->t & VT_BITFIELD))
  6034. && !((vtop->r & VT_CONST) && vtop->sym->v >= SYM_FIRST_ANOM)
  6035. )
  6036. tcc_error("initializer element is not computable at load time");
  6037. if (NODATA_WANTED) {
  6038. vtop--;
  6039. return;
  6040. }
  6041. size = type_size(type, &align);
  6042. section_reserve(sec, c + size);
  6043. ptr = sec->data + c;
  6044. /* XXX: make code faster ? */
  6045. if ((vtop->r & (VT_SYM|VT_CONST)) == (VT_SYM|VT_CONST) &&
  6046. vtop->sym->v >= SYM_FIRST_ANOM &&
  6047. /* XXX This rejects compound literals like
  6048. '(void *){ptr}'. The problem is that '&sym' is
  6049. represented the same way, which would be ruled out
  6050. by the SYM_FIRST_ANOM check above, but also '"string"'
  6051. in 'char *p = "string"' is represented the same
  6052. with the type being VT_PTR and the symbol being an
  6053. anonymous one. That is, there's no difference in vtop
  6054. between '(void *){x}' and '&(void *){x}'. Ignore
  6055. pointer typed entities here. Hopefully no real code
  6056. will every use compound literals with scalar type. */
  6057. (vtop->type.t & VT_BTYPE) != VT_PTR) {
  6058. /* These come from compound literals, memcpy stuff over. */
  6059. Section *ssec;
  6060. ElfW(Sym) *esym;
  6061. ElfW_Rel *rel;
  6062. esym = &((ElfW(Sym) *)symtab_section->data)[vtop->sym->c];
  6063. ssec = tcc_state->sections[esym->st_shndx];
  6064. memmove (ptr, ssec->data + esym->st_value, size);
  6065. if (ssec->reloc) {
  6066. /* We need to copy over all memory contents, and that
  6067. includes relocations. Use the fact that relocs are
  6068. created it order, so look from the end of relocs
  6069. until we hit one before the copied region. */
  6070. int num_relocs = ssec->reloc->data_offset / sizeof(*rel);
  6071. rel = (ElfW_Rel*)(ssec->reloc->data + ssec->reloc->data_offset);
  6072. while (num_relocs--) {
  6073. rel--;
  6074. if (rel->r_offset >= esym->st_value + size)
  6075. continue;
  6076. if (rel->r_offset < esym->st_value)
  6077. break;
  6078. /* Note: if the same fields are initialized multiple
  6079. times (possible with designators) then we possibly
  6080. add multiple relocations for the same offset here.
  6081. That would lead to wrong code, the last reloc needs
  6082. to win. We clean this up later after the whole
  6083. initializer is parsed. */
  6084. put_elf_reloca(symtab_section, sec,
  6085. c + rel->r_offset - esym->st_value,
  6086. ELFW(R_TYPE)(rel->r_info),
  6087. ELFW(R_SYM)(rel->r_info),
  6088. #if PTR_SIZE == 8
  6089. rel->r_addend
  6090. #else
  6091. 0
  6092. #endif
  6093. );
  6094. }
  6095. }
  6096. } else {
  6097. if (type->t & VT_BITFIELD) {
  6098. int bit_pos, bit_size, bits, n;
  6099. unsigned char *p, v, m;
  6100. bit_pos = BIT_POS(vtop->type.t);
  6101. bit_size = BIT_SIZE(vtop->type.t);
  6102. p = (unsigned char*)ptr + (bit_pos >> 3);
  6103. bit_pos &= 7, bits = 0;
  6104. while (bit_size) {
  6105. n = 8 - bit_pos;
  6106. if (n > bit_size)
  6107. n = bit_size;
  6108. v = vtop->c.i >> bits << bit_pos;
  6109. m = ((1 << n) - 1) << bit_pos;
  6110. *p = (*p & ~m) | (v & m);
  6111. bits += n, bit_size -= n, bit_pos = 0, ++p;
  6112. }
  6113. } else
  6114. switch(bt) {
  6115. /* XXX: when cross-compiling we assume that each type has the
  6116. same representation on host and target, which is likely to
  6117. be wrong in the case of long double */
  6118. case VT_BOOL:
  6119. vtop->c.i = vtop->c.i != 0;
  6120. case VT_BYTE:
  6121. *(char *)ptr |= vtop->c.i;
  6122. break;
  6123. case VT_SHORT:
  6124. *(short *)ptr |= vtop->c.i;
  6125. break;
  6126. case VT_FLOAT:
  6127. *(float*)ptr = vtop->c.f;
  6128. break;
  6129. case VT_DOUBLE:
  6130. *(double *)ptr = vtop->c.d;
  6131. break;
  6132. case VT_LDOUBLE:
  6133. #if defined TCC_IS_NATIVE_387
  6134. if (sizeof (long double) >= 10) /* zero pad ten-byte LD */
  6135. memcpy(ptr, &vtop->c.ld, 10);
  6136. #ifdef __TINYC__
  6137. else if (sizeof (long double) == sizeof (double))
  6138. __asm__("fldl %1\nfstpt %0\n" : "=m" (ptr) : "m" (vtop->c.ld));
  6139. #endif
  6140. else
  6141. #endif
  6142. if (sizeof(long double) == LDOUBLE_SIZE)
  6143. *(long double*)ptr = vtop->c.ld;
  6144. else if (sizeof(double) == LDOUBLE_SIZE)
  6145. *(double *)ptr = (double)vtop->c.ld;
  6146. else
  6147. tcc_error("can't cross compile long double constants");
  6148. break;
  6149. #if PTR_SIZE != 8
  6150. case VT_LLONG:
  6151. *(long long *)ptr |= vtop->c.i;
  6152. break;
  6153. #else
  6154. case VT_LLONG:
  6155. #endif
  6156. case VT_PTR:
  6157. {
  6158. addr_t val = vtop->c.i;
  6159. #if PTR_SIZE == 8
  6160. if (vtop->r & VT_SYM)
  6161. greloca(sec, vtop->sym, c, R_DATA_PTR, val);
  6162. else
  6163. *(addr_t *)ptr |= val;
  6164. #else
  6165. if (vtop->r & VT_SYM)
  6166. greloc(sec, vtop->sym, c, R_DATA_PTR);
  6167. *(addr_t *)ptr |= val;
  6168. #endif
  6169. break;
  6170. }
  6171. default:
  6172. {
  6173. int val = vtop->c.i;
  6174. #if PTR_SIZE == 8
  6175. if (vtop->r & VT_SYM)
  6176. greloca(sec, vtop->sym, c, R_DATA_PTR, val);
  6177. else
  6178. *(int *)ptr |= val;
  6179. #else
  6180. if (vtop->r & VT_SYM)
  6181. greloc(sec, vtop->sym, c, R_DATA_PTR);
  6182. *(int *)ptr |= val;
  6183. #endif
  6184. break;
  6185. }
  6186. }
  6187. }
  6188. vtop--;
  6189. } else {
  6190. vset(&dtype, VT_LOCAL|VT_LVAL, c);
  6191. vswap();
  6192. vstore();
  6193. vpop();
  6194. }
  6195. }
  6196. /* 't' contains the type and storage info. 'c' is the offset of the
  6197. object in section 'sec'. If 'sec' is NULL, it means stack based
  6198. allocation. 'first' is true if array '{' must be read (multi
  6199. dimension implicit array init handling). 'size_only' is true if
  6200. size only evaluation is wanted (only for arrays). */
  6201. static void decl_initializer(CType *type, Section *sec, unsigned long c,
  6202. int first, int size_only)
  6203. {
  6204. int len, n, no_oblock, nb, i;
  6205. int size1, align1;
  6206. int have_elem;
  6207. Sym *s, *f;
  6208. Sym indexsym;
  6209. CType *t1;
  6210. /* If we currently are at an '}' or ',' we have read an initializer
  6211. element in one of our callers, and not yet consumed it. */
  6212. have_elem = tok == '}' || tok == ',';
  6213. if (!have_elem && tok != '{' &&
  6214. /* In case of strings we have special handling for arrays, so
  6215. don't consume them as initializer value (which would commit them
  6216. to some anonymous symbol). */
  6217. tok != TOK_LSTR && tok != TOK_STR &&
  6218. !size_only) {
  6219. parse_init_elem(!sec ? EXPR_ANY : EXPR_CONST);
  6220. have_elem = 1;
  6221. }
  6222. if (have_elem &&
  6223. !(type->t & VT_ARRAY) &&
  6224. /* Use i_c_parameter_t, to strip toplevel qualifiers.
  6225. The source type might have VT_CONSTANT set, which is
  6226. of course assignable to non-const elements. */
  6227. is_compatible_unqualified_types(type, &vtop->type)) {
  6228. init_putv(type, sec, c);
  6229. } else if (type->t & VT_ARRAY) {
  6230. s = type->ref;
  6231. n = s->c;
  6232. t1 = pointed_type(type);
  6233. size1 = type_size(t1, &align1);
  6234. no_oblock = 1;
  6235. if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
  6236. tok == '{') {
  6237. if (tok != '{')
  6238. tcc_error("character array initializer must be a literal,"
  6239. " optionally enclosed in braces");
  6240. skip('{');
  6241. no_oblock = 0;
  6242. }
  6243. /* only parse strings here if correct type (otherwise: handle
  6244. them as ((w)char *) expressions */
  6245. if ((tok == TOK_LSTR &&
  6246. #ifdef TCC_TARGET_PE
  6247. (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
  6248. #else
  6249. (t1->t & VT_BTYPE) == VT_INT
  6250. #endif
  6251. ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
  6252. len = 0;
  6253. while (tok == TOK_STR || tok == TOK_LSTR) {
  6254. int cstr_len, ch;
  6255. /* compute maximum number of chars wanted */
  6256. if (tok == TOK_STR)
  6257. cstr_len = tokc.str.size;
  6258. else
  6259. cstr_len = tokc.str.size / sizeof(nwchar_t);
  6260. cstr_len--;
  6261. nb = cstr_len;
  6262. if (n >= 0 && nb > (n - len))
  6263. nb = n - len;
  6264. if (!size_only) {
  6265. if (cstr_len > nb)
  6266. tcc_warning("initializer-string for array is too long");
  6267. /* in order to go faster for common case (char
  6268. string in global variable, we handle it
  6269. specifically */
  6270. if (sec && tok == TOK_STR && size1 == 1) {
  6271. if (!NODATA_WANTED)
  6272. memcpy(sec->data + c + len, tokc.str.data, nb);
  6273. } else {
  6274. for(i=0;i<nb;i++) {
  6275. if (tok == TOK_STR)
  6276. ch = ((unsigned char *)tokc.str.data)[i];
  6277. else
  6278. ch = ((nwchar_t *)tokc.str.data)[i];
  6279. vpushi(ch);
  6280. init_putv(t1, sec, c + (len + i) * size1);
  6281. }
  6282. }
  6283. }
  6284. len += nb;
  6285. next();
  6286. }
  6287. /* only add trailing zero if enough storage (no
  6288. warning in this case since it is standard) */
  6289. if (n < 0 || len < n) {
  6290. if (!size_only) {
  6291. vpushi(0);
  6292. init_putv(t1, sec, c + (len * size1));
  6293. }
  6294. len++;
  6295. }
  6296. len *= size1;
  6297. } else {
  6298. indexsym.c = 0;
  6299. f = &indexsym;
  6300. do_init_list:
  6301. len = 0;
  6302. while (tok != '}' || have_elem) {
  6303. len = decl_designator(type, sec, c, &f, size_only, len);
  6304. have_elem = 0;
  6305. if (type->t & VT_ARRAY) {
  6306. ++indexsym.c;
  6307. /* special test for multi dimensional arrays (may not
  6308. be strictly correct if designators are used at the
  6309. same time) */
  6310. if (no_oblock && len >= n*size1)
  6311. break;
  6312. } else {
  6313. if (s->type.t == VT_UNION)
  6314. f = NULL;
  6315. else
  6316. f = f->next;
  6317. if (no_oblock && f == NULL)
  6318. break;
  6319. }
  6320. if (tok == '}')
  6321. break;
  6322. skip(',');
  6323. }
  6324. }
  6325. /* put zeros at the end */
  6326. if (!size_only && len < n*size1)
  6327. init_putz(sec, c + len, n*size1 - len);
  6328. if (!no_oblock)
  6329. skip('}');
  6330. /* patch type size if needed, which happens only for array types */
  6331. if (n < 0)
  6332. s->c = size1 == 1 ? len : ((len + size1 - 1)/size1);
  6333. } else if ((type->t & VT_BTYPE) == VT_STRUCT) {
  6334. size1 = 1;
  6335. no_oblock = 1;
  6336. if (first || tok == '{') {
  6337. skip('{');
  6338. no_oblock = 0;
  6339. }
  6340. s = type->ref;
  6341. f = s->next;
  6342. n = s->c;
  6343. goto do_init_list;
  6344. } else if (tok == '{') {
  6345. next();
  6346. decl_initializer(type, sec, c, first, size_only);
  6347. skip('}');
  6348. } else if (size_only) {
  6349. /* If we supported only ISO C we wouldn't have to accept calling
  6350. this on anything than an array size_only==1 (and even then
  6351. only on the outermost level, so no recursion would be needed),
  6352. because initializing a flex array member isn't supported.
  6353. But GNU C supports it, so we need to recurse even into
  6354. subfields of structs and arrays when size_only is set. */
  6355. /* just skip expression */
  6356. skip_or_save_block(NULL);
  6357. } else {
  6358. if (!have_elem) {
  6359. /* This should happen only when we haven't parsed
  6360. the init element above for fear of committing a
  6361. string constant to memory too early. */
  6362. if (tok != TOK_STR && tok != TOK_LSTR)
  6363. expect("string constant");
  6364. parse_init_elem(!sec ? EXPR_ANY : EXPR_CONST);
  6365. }
  6366. init_putv(type, sec, c);
  6367. }
  6368. }
  6369. /* parse an initializer for type 't' if 'has_init' is non zero, and
  6370. allocate space in local or global data space ('r' is either
  6371. VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
  6372. variable 'v' of scope 'scope' is declared before initializers
  6373. are parsed. If 'v' is zero, then a reference to the new object
  6374. is put in the value stack. If 'has_init' is 2, a special parsing
  6375. is done to handle string constants. */
  6376. static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
  6377. int has_init, int v, int scope)
  6378. {
  6379. int size, align, addr;
  6380. TokenString *init_str = NULL;
  6381. Section *sec;
  6382. Sym *flexible_array;
  6383. Sym *sym = NULL;
  6384. int saved_nocode_wanted = nocode_wanted;
  6385. #ifdef CONFIG_TCC_BCHECK
  6386. int bcheck = tcc_state->do_bounds_check && !NODATA_WANTED;
  6387. #endif
  6388. if (type->t & VT_STATIC)
  6389. nocode_wanted |= NODATA_WANTED ? 0x40000000 : 0x80000000;
  6390. flexible_array = NULL;
  6391. if ((type->t & VT_BTYPE) == VT_STRUCT) {
  6392. Sym *field = type->ref->next;
  6393. if (field) {
  6394. while (field->next)
  6395. field = field->next;
  6396. if (field->type.t & VT_ARRAY && field->type.ref->c < 0)
  6397. flexible_array = field;
  6398. }
  6399. }
  6400. size = type_size(type, &align);
  6401. /* If unknown size, we must evaluate it before
  6402. evaluating initializers because
  6403. initializers can generate global data too
  6404. (e.g. string pointers or ISOC99 compound
  6405. literals). It also simplifies local
  6406. initializers handling */
  6407. if (size < 0 || (flexible_array && has_init)) {
  6408. if (!has_init)
  6409. tcc_error("unknown type size");
  6410. /* get all init string */
  6411. if (has_init == 2) {
  6412. init_str = tok_str_alloc();
  6413. /* only get strings */
  6414. while (tok == TOK_STR || tok == TOK_LSTR) {
  6415. tok_str_add_tok(init_str);
  6416. next();
  6417. }
  6418. tok_str_add(init_str, -1);
  6419. tok_str_add(init_str, 0);
  6420. } else {
  6421. skip_or_save_block(&init_str);
  6422. }
  6423. unget_tok(0);
  6424. /* compute size */
  6425. begin_macro(init_str, 1);
  6426. next();
  6427. decl_initializer(type, NULL, 0, 1, 1);
  6428. /* prepare second initializer parsing */
  6429. macro_ptr = init_str->str;
  6430. next();
  6431. /* if still unknown size, error */
  6432. size = type_size(type, &align);
  6433. if (size < 0)
  6434. tcc_error("unknown type size");
  6435. }
  6436. /* If there's a flex member and it was used in the initializer
  6437. adjust size. */
  6438. if (flexible_array &&
  6439. flexible_array->type.ref->c > 0)
  6440. size += flexible_array->type.ref->c
  6441. * pointed_size(&flexible_array->type);
  6442. /* take into account specified alignment if bigger */
  6443. if (ad->a.aligned) {
  6444. int speca = 1 << (ad->a.aligned - 1);
  6445. if (speca > align)
  6446. align = speca;
  6447. } else if (ad->a.packed) {
  6448. align = 1;
  6449. }
  6450. if (NODATA_WANTED)
  6451. size = 0, align = 1;
  6452. if ((r & VT_VALMASK) == VT_LOCAL) {
  6453. sec = NULL;
  6454. #ifdef CONFIG_TCC_BCHECK
  6455. if (bcheck && (type->t & VT_ARRAY)) {
  6456. loc--;
  6457. }
  6458. #endif
  6459. loc = (loc - size) & -align;
  6460. addr = loc;
  6461. #ifdef CONFIG_TCC_BCHECK
  6462. /* handles bounds */
  6463. /* XXX: currently, since we do only one pass, we cannot track
  6464. '&' operators, so we add only arrays */
  6465. if (bcheck && (type->t & VT_ARRAY)) {
  6466. addr_t *bounds_ptr;
  6467. /* add padding between regions */
  6468. loc--;
  6469. /* then add local bound info */
  6470. bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(addr_t));
  6471. bounds_ptr[0] = addr;
  6472. bounds_ptr[1] = size;
  6473. }
  6474. #endif
  6475. if (v) {
  6476. /* local variable */
  6477. #ifdef CONFIG_TCC_ASM
  6478. if (ad->asm_label) {
  6479. int reg = asm_parse_regvar(ad->asm_label);
  6480. if (reg >= 0)
  6481. r = (r & ~VT_VALMASK) | reg;
  6482. }
  6483. #endif
  6484. sym = sym_push(v, type, r, addr);
  6485. sym->a = ad->a;
  6486. } else {
  6487. /* push local reference */
  6488. vset(type, r, addr);
  6489. }
  6490. } else {
  6491. if (v && scope == VT_CONST) {
  6492. /* see if the symbol was already defined */
  6493. sym = sym_find(v);
  6494. if (sym) {
  6495. patch_storage(sym, ad, type);
  6496. if (sym->type.t & VT_EXTERN) {
  6497. /* if the variable is extern, it was not allocated */
  6498. sym->type.t &= ~VT_EXTERN;
  6499. /* set array size if it was omitted in extern
  6500. declaration */
  6501. if ((sym->type.t & VT_ARRAY) &&
  6502. sym->type.ref->c < 0 &&
  6503. type->ref->c >= 0)
  6504. sym->type.ref->c = type->ref->c;
  6505. } else if (!has_init) {
  6506. /* we accept several definitions of the same
  6507. global variable. this is tricky, because we
  6508. must play with the SHN_COMMON type of the symbol */
  6509. /* no init data, we won't add more to the symbol */
  6510. goto no_alloc;
  6511. } else if (sym->c) {
  6512. ElfW(Sym) *esym;
  6513. esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
  6514. if (esym->st_shndx == data_section->sh_num)
  6515. tcc_error("redefinition of '%s'", get_tok_str(v, NULL));
  6516. }
  6517. }
  6518. }
  6519. /* allocate symbol in corresponding section */
  6520. sec = ad->section;
  6521. if (!sec) {
  6522. if (has_init)
  6523. sec = data_section;
  6524. else if (tcc_state->nocommon)
  6525. sec = bss_section;
  6526. }
  6527. if (sec) {
  6528. addr = section_add(sec, size, align);
  6529. #ifdef CONFIG_TCC_BCHECK
  6530. /* add padding if bound check */
  6531. if (bcheck)
  6532. section_add(sec, 1, 1);
  6533. #endif
  6534. } else {
  6535. addr = align; /* SHN_COMMON is special, symbol value is align */
  6536. sec = common_section;
  6537. }
  6538. if (v) {
  6539. if (!sym) {
  6540. sym = sym_push(v, type, r | VT_SYM, 0);
  6541. patch_storage(sym, ad, NULL);
  6542. }
  6543. /* Local statics have a scope until now (for
  6544. warnings), remove it here. */
  6545. sym->sym_scope = 0;
  6546. /* update symbol definition */
  6547. put_extern_sym(sym, sec, addr, size);
  6548. } else {
  6549. /* push global reference */
  6550. sym = get_sym_ref(type, sec, addr, size);
  6551. vpushsym(type, sym);
  6552. vtop->r |= r;
  6553. }
  6554. #ifdef CONFIG_TCC_BCHECK
  6555. /* handles bounds now because the symbol must be defined
  6556. before for the relocation */
  6557. if (bcheck) {
  6558. addr_t *bounds_ptr;
  6559. greloca(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR, 0);
  6560. /* then add global bound info */
  6561. bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(addr_t));
  6562. bounds_ptr[0] = 0; /* relocated */
  6563. bounds_ptr[1] = size;
  6564. }
  6565. #endif
  6566. }
  6567. if (type->t & VT_VLA) {
  6568. int a;
  6569. if (NODATA_WANTED)
  6570. goto no_alloc;
  6571. /* save current stack pointer */
  6572. if (vlas_in_scope == 0) {
  6573. if (vla_sp_root_loc == -1)
  6574. vla_sp_root_loc = (loc -= PTR_SIZE);
  6575. gen_vla_sp_save(vla_sp_root_loc);
  6576. }
  6577. vla_runtime_type_size(type, &a);
  6578. gen_vla_alloc(type, a);
  6579. gen_vla_sp_save(addr);
  6580. vla_sp_loc = addr;
  6581. vlas_in_scope++;
  6582. } else if (has_init) {
  6583. size_t oldreloc_offset = 0;
  6584. if (sec && sec->reloc)
  6585. oldreloc_offset = sec->reloc->data_offset;
  6586. decl_initializer(type, sec, addr, 1, 0);
  6587. if (sec && sec->reloc)
  6588. squeeze_multi_relocs(sec, oldreloc_offset);
  6589. /* patch flexible array member size back to -1, */
  6590. /* for possible subsequent similar declarations */
  6591. if (flexible_array)
  6592. flexible_array->type.ref->c = -1;
  6593. }
  6594. no_alloc:
  6595. /* restore parse state if needed */
  6596. if (init_str) {
  6597. end_macro();
  6598. next();
  6599. }
  6600. nocode_wanted = saved_nocode_wanted;
  6601. }
  6602. /* parse a function defined by symbol 'sym' and generate its code in
  6603. 'cur_text_section' */
  6604. static void gen_function(Sym *sym)
  6605. {
  6606. nocode_wanted = 0;
  6607. ind = cur_text_section->data_offset;
  6608. /* NOTE: we patch the symbol size later */
  6609. put_extern_sym(sym, cur_text_section, ind, 0);
  6610. funcname = get_tok_str(sym->v, NULL);
  6611. func_ind = ind;
  6612. /* Initialize VLA state */
  6613. vla_sp_loc = -1;
  6614. vla_sp_root_loc = -1;
  6615. /* put debug symbol */
  6616. tcc_debug_funcstart(tcc_state, sym);
  6617. /* push a dummy symbol to enable local sym storage */
  6618. sym_push2(&local_stack, SYM_FIELD, 0, 0);
  6619. local_scope = 1; /* for function parameters */
  6620. gfunc_prolog(&sym->type);
  6621. local_scope = 0;
  6622. rsym = 0;
  6623. block(NULL, NULL, 0);
  6624. nocode_wanted = 0;
  6625. gsym(rsym);
  6626. gfunc_epilog();
  6627. cur_text_section->data_offset = ind;
  6628. label_pop(&global_label_stack, NULL, 0);
  6629. /* reset local stack */
  6630. local_scope = 0;
  6631. sym_pop(&local_stack, NULL, 0);
  6632. /* end of function */
  6633. /* patch symbol size */
  6634. ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
  6635. ind - func_ind;
  6636. tcc_debug_funcend(tcc_state, ind - func_ind);
  6637. /* It's better to crash than to generate wrong code */
  6638. cur_text_section = NULL;
  6639. funcname = ""; /* for safety */
  6640. func_vt.t = VT_VOID; /* for safety */
  6641. func_var = 0; /* for safety */
  6642. ind = 0; /* for safety */
  6643. nocode_wanted = 0x80000000;
  6644. check_vstack();
  6645. }
  6646. static void gen_inline_functions(TCCState *s)
  6647. {
  6648. Sym *sym;
  6649. int inline_generated, i, ln;
  6650. struct InlineFunc *fn;
  6651. ln = file->line_num;
  6652. /* iterate while inline function are referenced */
  6653. do {
  6654. inline_generated = 0;
  6655. for (i = 0; i < s->nb_inline_fns; ++i) {
  6656. fn = s->inline_fns[i];
  6657. sym = fn->sym;
  6658. if (sym && sym->c) {
  6659. /* the function was used: generate its code and
  6660. convert it to a normal function */
  6661. fn->sym = NULL;
  6662. if (file)
  6663. pstrcpy(file->filename, sizeof file->filename, fn->filename);
  6664. sym->type.t &= ~VT_INLINE;
  6665. begin_macro(fn->func_str, 1);
  6666. next();
  6667. cur_text_section = text_section;
  6668. gen_function(sym);
  6669. end_macro();
  6670. inline_generated = 1;
  6671. }
  6672. }
  6673. } while (inline_generated);
  6674. file->line_num = ln;
  6675. }
  6676. ST_FUNC void free_inline_functions(TCCState *s)
  6677. {
  6678. int i;
  6679. /* free tokens of unused inline functions */
  6680. for (i = 0; i < s->nb_inline_fns; ++i) {
  6681. struct InlineFunc *fn = s->inline_fns[i];
  6682. if (fn->sym)
  6683. tok_str_free(fn->func_str);
  6684. }
  6685. dynarray_reset(&s->inline_fns, &s->nb_inline_fns);
  6686. }
  6687. /* 'l' is VT_LOCAL or VT_CONST to define default storage type, or VT_CMP
  6688. if parsing old style parameter decl list (and FUNC_SYM is set then) */
  6689. static int decl0(int l, int is_for_loop_init, Sym *func_sym)
  6690. {
  6691. int v, has_init, r;
  6692. CType type, btype;
  6693. Sym *sym;
  6694. AttributeDef ad;
  6695. while (1) {
  6696. if (!parse_btype(&btype, &ad)) {
  6697. if (is_for_loop_init)
  6698. return 0;
  6699. /* skip redundant ';' if not in old parameter decl scope */
  6700. if (tok == ';' && l != VT_CMP) {
  6701. next();
  6702. continue;
  6703. }
  6704. if (l != VT_CONST)
  6705. break;
  6706. if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
  6707. /* global asm block */
  6708. asm_global_instr();
  6709. continue;
  6710. }
  6711. if (tok >= TOK_UIDENT) {
  6712. /* special test for old K&R protos without explicit int
  6713. type. Only accepted when defining global data */
  6714. btype.t = VT_INT;
  6715. } else {
  6716. if (tok != TOK_EOF)
  6717. expect("declaration");
  6718. break;
  6719. }
  6720. }
  6721. if (tok == ';') {
  6722. if ((btype.t & VT_BTYPE) == VT_STRUCT) {
  6723. int v = btype.ref->v;
  6724. if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) >= SYM_FIRST_ANOM)
  6725. tcc_warning("unnamed struct/union that defines no instances");
  6726. next();
  6727. continue;
  6728. }
  6729. if (IS_ENUM(btype.t)) {
  6730. next();
  6731. continue;
  6732. }
  6733. }
  6734. while (1) { /* iterate thru each declaration */
  6735. type = btype;
  6736. /* If the base type itself was an array type of unspecified
  6737. size (like in 'typedef int arr[]; arr x = {1};') then
  6738. we will overwrite the unknown size by the real one for
  6739. this decl. We need to unshare the ref symbol holding
  6740. that size. */
  6741. if ((type.t & VT_ARRAY) && type.ref->c < 0) {
  6742. type.ref = sym_push(SYM_FIELD, &type.ref->type, 0, type.ref->c);
  6743. }
  6744. type_decl(&type, &ad, &v, TYPE_DIRECT);
  6745. #if 0
  6746. {
  6747. char buf[500];
  6748. type_to_str(buf, sizeof(buf), &type, get_tok_str(v, NULL));
  6749. printf("type = '%s'\n", buf);
  6750. }
  6751. #endif
  6752. if ((type.t & VT_BTYPE) == VT_FUNC) {
  6753. if ((type.t & VT_STATIC) && (l == VT_LOCAL)) {
  6754. tcc_error("function without file scope cannot be static");
  6755. }
  6756. /* if old style function prototype, we accept a
  6757. declaration list */
  6758. sym = type.ref;
  6759. if (sym->f.func_type == FUNC_OLD && l == VT_CONST)
  6760. decl0(VT_CMP, 0, sym);
  6761. }
  6762. if (gnu_ext && (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
  6763. ad.asm_label = asm_label_instr();
  6764. /* parse one last attribute list, after asm label */
  6765. parse_attribute(&ad);
  6766. if (tok == '{')
  6767. expect(";");
  6768. }
  6769. #ifdef TCC_TARGET_PE
  6770. if (ad.a.dllimport || ad.a.dllexport) {
  6771. if (type.t & (VT_STATIC|VT_TYPEDEF))
  6772. tcc_error("cannot have dll linkage with static or typedef");
  6773. if (ad.a.dllimport) {
  6774. if ((type.t & VT_BTYPE) == VT_FUNC)
  6775. ad.a.dllimport = 0;
  6776. else
  6777. type.t |= VT_EXTERN;
  6778. }
  6779. }
  6780. #endif
  6781. if (tok == '{') {
  6782. if (l != VT_CONST)
  6783. tcc_error("cannot use local functions");
  6784. if ((type.t & VT_BTYPE) != VT_FUNC)
  6785. expect("function definition");
  6786. /* reject abstract declarators in function definition
  6787. make old style params without decl have int type */
  6788. sym = type.ref;
  6789. while ((sym = sym->next) != NULL) {
  6790. if (!(sym->v & ~SYM_FIELD))
  6791. expect("identifier");
  6792. if (sym->type.t == VT_VOID)
  6793. sym->type = int_type;
  6794. }
  6795. /* XXX: cannot do better now: convert extern line to static inline */
  6796. if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
  6797. type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
  6798. sym = sym_find(v);
  6799. if (sym) {
  6800. Sym *ref;
  6801. if ((sym->type.t & VT_BTYPE) != VT_FUNC)
  6802. goto func_error1;
  6803. ref = sym->type.ref;
  6804. /* use func_call from prototype if not defined */
  6805. if (ref->f.func_call != FUNC_CDECL
  6806. && type.ref->f.func_call == FUNC_CDECL)
  6807. type.ref->f.func_call = ref->f.func_call;
  6808. /* use static from prototype */
  6809. if (sym->type.t & VT_STATIC)
  6810. type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
  6811. /* If the definition has no visibility use the
  6812. one from prototype. */
  6813. if (!type.ref->a.visibility)
  6814. type.ref->a.visibility = ref->a.visibility;
  6815. /* apply other storage attributes from prototype */
  6816. type.ref->a.dllexport |= ref->a.dllexport;
  6817. type.ref->a.weak |= ref->a.weak;
  6818. if (!is_compatible_types(&sym->type, &type)) {
  6819. func_error1:
  6820. tcc_error("incompatible types for redefinition of '%s'",
  6821. get_tok_str(v, NULL));
  6822. }
  6823. if (ref->f.func_body)
  6824. tcc_error("redefinition of '%s'", get_tok_str(v, NULL));
  6825. /* if symbol is already defined, then put complete type */
  6826. sym->type = type;
  6827. } else {
  6828. /* put function symbol */
  6829. sym = global_identifier_push(v, type.t, 0);
  6830. sym->type.ref = type.ref;
  6831. }
  6832. sym->type.ref->f.func_body = 1;
  6833. sym->r = VT_SYM | VT_CONST;
  6834. patch_storage(sym, &ad, NULL);
  6835. /* static inline functions are just recorded as a kind
  6836. of macro. Their code will be emitted at the end of
  6837. the compilation unit only if they are used */
  6838. if ((type.t & (VT_INLINE | VT_STATIC)) ==
  6839. (VT_INLINE | VT_STATIC)) {
  6840. struct InlineFunc *fn;
  6841. const char *filename;
  6842. filename = file ? file->filename : "";
  6843. fn = tcc_malloc(sizeof *fn + strlen(filename));
  6844. strcpy(fn->filename, filename);
  6845. fn->sym = sym;
  6846. skip_or_save_block(&fn->func_str);
  6847. dynarray_add(&tcc_state->inline_fns,
  6848. &tcc_state->nb_inline_fns, fn);
  6849. } else {
  6850. /* compute text section */
  6851. cur_text_section = ad.section;
  6852. if (!cur_text_section)
  6853. cur_text_section = text_section;
  6854. gen_function(sym);
  6855. }
  6856. break;
  6857. } else {
  6858. if (l == VT_CMP) {
  6859. /* find parameter in function parameter list */
  6860. for (sym = func_sym->next; sym; sym = sym->next)
  6861. if ((sym->v & ~SYM_FIELD) == v)
  6862. goto found;
  6863. tcc_error("declaration for parameter '%s' but no such parameter",
  6864. get_tok_str(v, NULL));
  6865. found:
  6866. if (type.t & VT_STORAGE) /* 'register' is okay */
  6867. tcc_error("storage class specified for '%s'",
  6868. get_tok_str(v, NULL));
  6869. if (sym->type.t != VT_VOID)
  6870. tcc_error("redefinition of parameter '%s'",
  6871. get_tok_str(v, NULL));
  6872. convert_parameter_type(&type);
  6873. sym->type = type;
  6874. } else if (type.t & VT_TYPEDEF) {
  6875. /* save typedefed type */
  6876. /* XXX: test storage specifiers ? */
  6877. sym = sym_find(v);
  6878. if (sym && sym->sym_scope == local_scope) {
  6879. if (!is_compatible_types(&sym->type, &type)
  6880. || !(sym->type.t & VT_TYPEDEF))
  6881. tcc_error("incompatible redefinition of '%s'",
  6882. get_tok_str(v, NULL));
  6883. sym->type = type;
  6884. } else {
  6885. sym = sym_push(v, &type, 0, 0);
  6886. }
  6887. sym->a = ad.a;
  6888. sym->f = ad.f;
  6889. } else {
  6890. r = 0;
  6891. if ((type.t & VT_BTYPE) == VT_FUNC) {
  6892. /* external function definition */
  6893. /* specific case for func_call attribute */
  6894. type.ref->f = ad.f;
  6895. } else if (!(type.t & VT_ARRAY)) {
  6896. /* not lvalue if array */
  6897. r |= lvalue_type(type.t);
  6898. }
  6899. has_init = (tok == '=');
  6900. if (has_init && (type.t & VT_VLA))
  6901. tcc_error("variable length array cannot be initialized");
  6902. if (((type.t & VT_EXTERN) && (!has_init || l != VT_CONST)) ||
  6903. ((type.t & VT_BTYPE) == VT_FUNC) ||
  6904. ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
  6905. !has_init && l == VT_CONST && type.ref->c < 0)) {
  6906. /* external variable or function */
  6907. /* NOTE: as GCC, uninitialized global static
  6908. arrays of null size are considered as
  6909. extern */
  6910. sym = external_sym(v, &type, r, &ad);
  6911. if (ad.alias_target) {
  6912. Section tsec;
  6913. ElfW(Sym) *esym;
  6914. Sym *alias_target;
  6915. alias_target = sym_find(ad.alias_target);
  6916. if (!alias_target || !alias_target->c)
  6917. tcc_error("unsupported forward __alias__ attribute");
  6918. esym = &((ElfW(Sym) *)symtab_section->data)[alias_target->c];
  6919. tsec.sh_num = esym->st_shndx;
  6920. /* Local statics have a scope until now (for
  6921. warnings), remove it here. */
  6922. sym->sym_scope = 0;
  6923. put_extern_sym2(sym, &tsec, esym->st_value, esym->st_size, 0);
  6924. }
  6925. } else {
  6926. if (type.t & VT_STATIC)
  6927. r |= VT_CONST;
  6928. else
  6929. r |= l;
  6930. if (has_init)
  6931. next();
  6932. decl_initializer_alloc(&type, &ad, r, has_init, v, l);
  6933. }
  6934. }
  6935. if (tok != ',') {
  6936. if (is_for_loop_init)
  6937. return 1;
  6938. skip(';');
  6939. break;
  6940. }
  6941. next();
  6942. }
  6943. ad.a.aligned = 0;
  6944. }
  6945. }
  6946. return 0;
  6947. }
  6948. static void decl(int l)
  6949. {
  6950. decl0(l, 0, NULL);
  6951. }
  6952. /* ------------------------------------------------------------------------- */