tccgen.c 218 KB

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