tccgen.c 151 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123
  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. void swap(int *p, int *q)
  21. {
  22. int t;
  23. t = *p;
  24. *p = *q;
  25. *q = t;
  26. }
  27. void vsetc(CType *type, int r, CValue *vc)
  28. {
  29. int v;
  30. if (vtop >= vstack + (VSTACK_SIZE - 1))
  31. error("memory full");
  32. /* cannot let cpu flags if other instruction are generated. Also
  33. avoid leaving VT_JMP anywhere except on the top of the stack
  34. because it would complicate the code generator. */
  35. if (vtop >= vstack) {
  36. v = vtop->r & VT_VALMASK;
  37. if (v == VT_CMP || (v & ~1) == VT_JMP)
  38. gv(RC_INT);
  39. }
  40. vtop++;
  41. vtop->type = *type;
  42. vtop->r = r;
  43. vtop->r2 = VT_CONST;
  44. vtop->c = *vc;
  45. }
  46. /* push integer constant */
  47. void vpushi(int v)
  48. {
  49. CValue cval;
  50. cval.i = v;
  51. vsetc(&int_type, VT_CONST, &cval);
  52. }
  53. /* push long long constant */
  54. void vpushll(long long v)
  55. {
  56. CValue cval;
  57. CType ctype;
  58. ctype.t = VT_LLONG;
  59. cval.ull = v;
  60. vsetc(&ctype, VT_CONST, &cval);
  61. }
  62. /* Return a static symbol pointing to a section */
  63. static Sym *get_sym_ref(CType *type, Section *sec,
  64. unsigned long offset, unsigned long size)
  65. {
  66. int v;
  67. Sym *sym;
  68. v = anon_sym++;
  69. sym = global_identifier_push(v, type->t | VT_STATIC, 0);
  70. sym->type.ref = type->ref;
  71. sym->r = VT_CONST | VT_SYM;
  72. put_extern_sym(sym, sec, offset, size);
  73. return sym;
  74. }
  75. /* push a reference to a section offset by adding a dummy symbol */
  76. static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
  77. {
  78. CValue cval;
  79. cval.ul = 0;
  80. vsetc(type, VT_CONST | VT_SYM, &cval);
  81. vtop->sym = get_sym_ref(type, sec, offset, size);
  82. }
  83. /* define a new external reference to a symbol 'v' of type 'u' */
  84. static Sym *external_global_sym(int v, CType *type, int r)
  85. {
  86. Sym *s;
  87. s = sym_find(v);
  88. if (!s) {
  89. /* push forward reference */
  90. s = global_identifier_push(v, type->t | VT_EXTERN, 0);
  91. s->type.ref = type->ref;
  92. s->r = r | VT_CONST | VT_SYM;
  93. }
  94. return s;
  95. }
  96. /* define a new external reference to a symbol 'v' of type 'u' */
  97. static Sym *external_sym(int v, CType *type, int r)
  98. {
  99. Sym *s;
  100. s = sym_find(v);
  101. if (!s) {
  102. /* push forward reference */
  103. s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
  104. s->type.t |= VT_EXTERN;
  105. } else {
  106. if (!is_compatible_types(&s->type, type))
  107. error("incompatible types for redefinition of '%s'",
  108. get_tok_str(v, NULL));
  109. }
  110. return s;
  111. }
  112. /* push a reference to global symbol v */
  113. static void vpush_global_sym(CType *type, int v)
  114. {
  115. Sym *sym;
  116. CValue cval;
  117. sym = external_global_sym(v, type, 0);
  118. cval.ul = 0;
  119. vsetc(type, VT_CONST | VT_SYM, &cval);
  120. vtop->sym = sym;
  121. }
  122. void vset(CType *type, int r, int v)
  123. {
  124. CValue cval;
  125. cval.i = v;
  126. vsetc(type, r, &cval);
  127. }
  128. void vseti(int r, int v)
  129. {
  130. CType type;
  131. type.t = VT_INT;
  132. vset(&type, r, v);
  133. }
  134. void vswap(void)
  135. {
  136. SValue tmp;
  137. tmp = vtop[0];
  138. vtop[0] = vtop[-1];
  139. vtop[-1] = tmp;
  140. }
  141. void vpushv(SValue *v)
  142. {
  143. if (vtop >= vstack + (VSTACK_SIZE - 1))
  144. error("memory full");
  145. vtop++;
  146. *vtop = *v;
  147. }
  148. void vdup(void)
  149. {
  150. vpushv(vtop);
  151. }
  152. /* save r to the memory stack, and mark it as being free */
  153. void save_reg(int r)
  154. {
  155. int l, saved, size, align;
  156. SValue *p, sv;
  157. CType *type;
  158. /* modify all stack values */
  159. saved = 0;
  160. l = 0;
  161. for(p=vstack;p<=vtop;p++) {
  162. if ((p->r & VT_VALMASK) == r ||
  163. ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
  164. /* must save value on stack if not already done */
  165. if (!saved) {
  166. /* NOTE: must reload 'r' because r might be equal to r2 */
  167. r = p->r & VT_VALMASK;
  168. /* store register in the stack */
  169. type = &p->type;
  170. if ((p->r & VT_LVAL) ||
  171. (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
  172. #ifdef TCC_TARGET_X86_64
  173. type = &char_pointer_type;
  174. #else
  175. type = &int_type;
  176. #endif
  177. size = type_size(type, &align);
  178. loc = (loc - size) & -align;
  179. sv.type.t = type->t;
  180. sv.r = VT_LOCAL | VT_LVAL;
  181. sv.c.ul = loc;
  182. store(r, &sv);
  183. #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
  184. /* x86 specific: need to pop fp register ST0 if saved */
  185. if (r == TREG_ST0) {
  186. o(0xd9dd); /* fstp %st(1) */
  187. }
  188. #endif
  189. #ifndef TCC_TARGET_X86_64
  190. /* special long long case */
  191. if ((type->t & VT_BTYPE) == VT_LLONG) {
  192. sv.c.ul += 4;
  193. store(p->r2, &sv);
  194. }
  195. #endif
  196. l = loc;
  197. saved = 1;
  198. }
  199. /* mark that stack entry as being saved on the stack */
  200. if (p->r & VT_LVAL) {
  201. /* also clear the bounded flag because the
  202. relocation address of the function was stored in
  203. p->c.ul */
  204. p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
  205. } else {
  206. p->r = lvalue_type(p->type.t) | VT_LOCAL;
  207. }
  208. p->r2 = VT_CONST;
  209. p->c.ul = l;
  210. }
  211. }
  212. }
  213. /* find a register of class 'rc2' with at most one reference on stack.
  214. * If none, call get_reg(rc) */
  215. int get_reg_ex(int rc, int rc2)
  216. {
  217. int r;
  218. SValue *p;
  219. for(r=0;r<NB_REGS;r++) {
  220. if (reg_classes[r] & rc2) {
  221. int n;
  222. n=0;
  223. for(p = vstack; p <= vtop; p++) {
  224. if ((p->r & VT_VALMASK) == r ||
  225. (p->r2 & VT_VALMASK) == r)
  226. n++;
  227. }
  228. if (n <= 1)
  229. return r;
  230. }
  231. }
  232. return get_reg(rc);
  233. }
  234. /* find a free register of class 'rc'. If none, save one register */
  235. int get_reg(int rc)
  236. {
  237. int r;
  238. SValue *p;
  239. /* find a free register */
  240. for(r=0;r<NB_REGS;r++) {
  241. if (reg_classes[r] & rc) {
  242. for(p=vstack;p<=vtop;p++) {
  243. if ((p->r & VT_VALMASK) == r ||
  244. (p->r2 & VT_VALMASK) == r)
  245. goto notfound;
  246. }
  247. return r;
  248. }
  249. notfound: ;
  250. }
  251. /* no register left : free the first one on the stack (VERY
  252. IMPORTANT to start from the bottom to ensure that we don't
  253. spill registers used in gen_opi()) */
  254. for(p=vstack;p<=vtop;p++) {
  255. r = p->r & VT_VALMASK;
  256. if (r < VT_CONST && (reg_classes[r] & rc))
  257. goto save_found;
  258. /* also look at second register (if long long) */
  259. r = p->r2 & VT_VALMASK;
  260. if (r < VT_CONST && (reg_classes[r] & rc)) {
  261. save_found:
  262. save_reg(r);
  263. return r;
  264. }
  265. }
  266. /* Should never comes here */
  267. return -1;
  268. }
  269. /* save registers up to (vtop - n) stack entry */
  270. void save_regs(int n)
  271. {
  272. int r;
  273. SValue *p, *p1;
  274. p1 = vtop - n;
  275. for(p = vstack;p <= p1; p++) {
  276. r = p->r & VT_VALMASK;
  277. if (r < VT_CONST) {
  278. save_reg(r);
  279. }
  280. }
  281. }
  282. /* move register 's' to 'r', and flush previous value of r to memory
  283. if needed */
  284. void move_reg(int r, int s)
  285. {
  286. SValue sv;
  287. if (r != s) {
  288. save_reg(r);
  289. sv.type.t = VT_INT;
  290. sv.r = s;
  291. sv.c.ul = 0;
  292. load(r, &sv);
  293. }
  294. }
  295. /* get address of vtop (vtop MUST BE an lvalue) */
  296. void gaddrof(void)
  297. {
  298. vtop->r &= ~VT_LVAL;
  299. /* tricky: if saved lvalue, then we can go back to lvalue */
  300. if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
  301. vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
  302. }
  303. #ifdef CONFIG_TCC_BCHECK
  304. /* generate lvalue bound code */
  305. void gbound(void)
  306. {
  307. int lval_type;
  308. CType type1;
  309. vtop->r &= ~VT_MUSTBOUND;
  310. /* if lvalue, then use checking code before dereferencing */
  311. if (vtop->r & VT_LVAL) {
  312. /* if not VT_BOUNDED value, then make one */
  313. if (!(vtop->r & VT_BOUNDED)) {
  314. lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
  315. /* must save type because we must set it to int to get pointer */
  316. type1 = vtop->type;
  317. vtop->type.t = VT_INT;
  318. gaddrof();
  319. vpushi(0);
  320. gen_bounded_ptr_add();
  321. vtop->r |= lval_type;
  322. vtop->type = type1;
  323. }
  324. /* then check for dereferencing */
  325. gen_bounded_ptr_deref();
  326. }
  327. }
  328. #endif
  329. /* store vtop a register belonging to class 'rc'. lvalues are
  330. converted to values. Cannot be used if cannot be converted to
  331. register value (such as structures). */
  332. int gv(int rc)
  333. {
  334. int r, rc2, bit_pos, bit_size, size, align, i;
  335. /* NOTE: get_reg can modify vstack[] */
  336. if (vtop->type.t & VT_BITFIELD) {
  337. CType type;
  338. int bits = 32;
  339. bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
  340. bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
  341. /* remove bit field info to avoid loops */
  342. vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
  343. /* cast to int to propagate signedness in following ops */
  344. if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
  345. type.t = VT_LLONG;
  346. bits = 64;
  347. } else
  348. type.t = VT_INT;
  349. if((vtop->type.t & VT_UNSIGNED) ||
  350. (vtop->type.t & VT_BTYPE) == VT_BOOL)
  351. type.t |= VT_UNSIGNED;
  352. gen_cast(&type);
  353. /* generate shifts */
  354. vpushi(bits - (bit_pos + bit_size));
  355. gen_op(TOK_SHL);
  356. vpushi(bits - bit_size);
  357. /* NOTE: transformed to SHR if unsigned */
  358. gen_op(TOK_SAR);
  359. r = gv(rc);
  360. } else {
  361. if (is_float(vtop->type.t) &&
  362. (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
  363. Sym *sym;
  364. int *ptr;
  365. unsigned long offset;
  366. #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
  367. CValue check;
  368. #endif
  369. /* XXX: unify with initializers handling ? */
  370. /* CPUs usually cannot use float constants, so we store them
  371. generically in data segment */
  372. size = type_size(&vtop->type, &align);
  373. offset = (data_section->data_offset + align - 1) & -align;
  374. data_section->data_offset = offset;
  375. /* XXX: not portable yet */
  376. #if defined(__i386__) || defined(__x86_64__)
  377. /* Zero pad x87 tenbyte long doubles */
  378. if (size == LDOUBLE_SIZE)
  379. vtop->c.tab[2] &= 0xffff;
  380. #endif
  381. ptr = section_ptr_add(data_section, size);
  382. size = size >> 2;
  383. #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
  384. check.d = 1;
  385. if(check.tab[0])
  386. for(i=0;i<size;i++)
  387. ptr[i] = vtop->c.tab[size-1-i];
  388. else
  389. #endif
  390. for(i=0;i<size;i++)
  391. ptr[i] = vtop->c.tab[i];
  392. sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
  393. vtop->r |= VT_LVAL | VT_SYM;
  394. vtop->sym = sym;
  395. vtop->c.ul = 0;
  396. }
  397. #ifdef CONFIG_TCC_BCHECK
  398. if (vtop->r & VT_MUSTBOUND)
  399. gbound();
  400. #endif
  401. r = vtop->r & VT_VALMASK;
  402. rc2 = RC_INT;
  403. if (rc == RC_IRET)
  404. rc2 = RC_LRET;
  405. /* need to reload if:
  406. - constant
  407. - lvalue (need to dereference pointer)
  408. - already a register, but not in the right class */
  409. if (r >= VT_CONST ||
  410. (vtop->r & VT_LVAL) ||
  411. !(reg_classes[r] & rc) ||
  412. ((vtop->type.t & VT_BTYPE) == VT_LLONG &&
  413. !(reg_classes[vtop->r2] & rc2))) {
  414. r = get_reg(rc);
  415. #ifndef TCC_TARGET_X86_64
  416. if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
  417. int r2;
  418. unsigned long long ll;
  419. /* two register type load : expand to two words
  420. temporarily */
  421. if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
  422. /* load constant */
  423. ll = vtop->c.ull;
  424. vtop->c.ui = ll; /* first word */
  425. load(r, vtop);
  426. vtop->r = r; /* save register value */
  427. vpushi(ll >> 32); /* second word */
  428. } else if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
  429. (vtop->r & VT_LVAL)) {
  430. /* We do not want to modifier the long long
  431. pointer here, so the safest (and less
  432. efficient) is to save all the other registers
  433. in the stack. XXX: totally inefficient. */
  434. save_regs(1);
  435. /* load from memory */
  436. load(r, vtop);
  437. vdup();
  438. vtop[-1].r = r; /* save register value */
  439. /* increment pointer to get second word */
  440. vtop->type.t = VT_INT;
  441. gaddrof();
  442. vpushi(4);
  443. gen_op('+');
  444. vtop->r |= VT_LVAL;
  445. } else {
  446. /* move registers */
  447. load(r, vtop);
  448. vdup();
  449. vtop[-1].r = r; /* save register value */
  450. vtop->r = vtop[-1].r2;
  451. }
  452. /* allocate second register */
  453. r2 = get_reg(rc2);
  454. load(r2, vtop);
  455. vpop();
  456. /* write second register */
  457. vtop->r2 = r2;
  458. } else
  459. #endif
  460. if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
  461. int t1, t;
  462. /* lvalue of scalar type : need to use lvalue type
  463. because of possible cast */
  464. t = vtop->type.t;
  465. t1 = t;
  466. /* compute memory access type */
  467. if (vtop->r & VT_LVAL_BYTE)
  468. t = VT_BYTE;
  469. else if (vtop->r & VT_LVAL_SHORT)
  470. t = VT_SHORT;
  471. if (vtop->r & VT_LVAL_UNSIGNED)
  472. t |= VT_UNSIGNED;
  473. vtop->type.t = t;
  474. load(r, vtop);
  475. /* restore wanted type */
  476. vtop->type.t = t1;
  477. } else {
  478. /* one register type load */
  479. load(r, vtop);
  480. }
  481. }
  482. vtop->r = r;
  483. #ifdef TCC_TARGET_C67
  484. /* uses register pairs for doubles */
  485. if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
  486. vtop->r2 = r+1;
  487. #endif
  488. }
  489. return r;
  490. }
  491. /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
  492. void gv2(int rc1, int rc2)
  493. {
  494. int v;
  495. /* generate more generic register first. But VT_JMP or VT_CMP
  496. values must be generated first in all cases to avoid possible
  497. reload errors */
  498. v = vtop[0].r & VT_VALMASK;
  499. if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
  500. vswap();
  501. gv(rc1);
  502. vswap();
  503. gv(rc2);
  504. /* test if reload is needed for first register */
  505. if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
  506. vswap();
  507. gv(rc1);
  508. vswap();
  509. }
  510. } else {
  511. gv(rc2);
  512. vswap();
  513. gv(rc1);
  514. vswap();
  515. /* test if reload is needed for first register */
  516. if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
  517. gv(rc2);
  518. }
  519. }
  520. }
  521. /* wrapper around RC_FRET to return a register by type */
  522. int rc_fret(int t)
  523. {
  524. #ifdef TCC_TARGET_X86_64
  525. if (t == VT_LDOUBLE) {
  526. return RC_ST0;
  527. }
  528. #endif
  529. return RC_FRET;
  530. }
  531. /* wrapper around REG_FRET to return a register by type */
  532. int reg_fret(int t)
  533. {
  534. #ifdef TCC_TARGET_X86_64
  535. if (t == VT_LDOUBLE) {
  536. return TREG_ST0;
  537. }
  538. #endif
  539. return REG_FRET;
  540. }
  541. /* expand long long on stack in two int registers */
  542. void lexpand(void)
  543. {
  544. int u;
  545. u = vtop->type.t & VT_UNSIGNED;
  546. gv(RC_INT);
  547. vdup();
  548. vtop[0].r = vtop[-1].r2;
  549. vtop[0].r2 = VT_CONST;
  550. vtop[-1].r2 = VT_CONST;
  551. vtop[0].type.t = VT_INT | u;
  552. vtop[-1].type.t = VT_INT | u;
  553. }
  554. #ifdef TCC_TARGET_ARM
  555. /* expand long long on stack */
  556. void lexpand_nr(void)
  557. {
  558. int u,v;
  559. u = vtop->type.t & VT_UNSIGNED;
  560. vdup();
  561. vtop->r2 = VT_CONST;
  562. vtop->type.t = VT_INT | u;
  563. v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
  564. if (v == VT_CONST) {
  565. vtop[-1].c.ui = vtop->c.ull;
  566. vtop->c.ui = vtop->c.ull >> 32;
  567. vtop->r = VT_CONST;
  568. } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
  569. vtop->c.ui += 4;
  570. vtop->r = vtop[-1].r;
  571. } else if (v > VT_CONST) {
  572. vtop--;
  573. lexpand();
  574. } else
  575. vtop->r = vtop[-1].r2;
  576. vtop[-1].r2 = VT_CONST;
  577. vtop[-1].type.t = VT_INT | u;
  578. }
  579. #endif
  580. /* build a long long from two ints */
  581. void lbuild(int t)
  582. {
  583. gv2(RC_INT, RC_INT);
  584. vtop[-1].r2 = vtop[0].r;
  585. vtop[-1].type.t = t;
  586. vpop();
  587. }
  588. /* rotate n first stack elements to the bottom
  589. I1 ... In -> I2 ... In I1 [top is right]
  590. */
  591. void vrotb(int n)
  592. {
  593. int i;
  594. SValue tmp;
  595. tmp = vtop[-n + 1];
  596. for(i=-n+1;i!=0;i++)
  597. vtop[i] = vtop[i+1];
  598. vtop[0] = tmp;
  599. }
  600. /* rotate n first stack elements to the top
  601. I1 ... In -> In I1 ... I(n-1) [top is right]
  602. */
  603. void vrott(int n)
  604. {
  605. int i;
  606. SValue tmp;
  607. tmp = vtop[0];
  608. for(i = 0;i < n - 1; i++)
  609. vtop[-i] = vtop[-i - 1];
  610. vtop[-n + 1] = tmp;
  611. }
  612. #ifdef TCC_TARGET_ARM
  613. /* like vrott but in other direction
  614. In ... I1 -> I(n-1) ... I1 In [top is right]
  615. */
  616. void vnrott(int n)
  617. {
  618. int i;
  619. SValue tmp;
  620. tmp = vtop[-n + 1];
  621. for(i = n - 1; i > 0; i--)
  622. vtop[-i] = vtop[-i + 1];
  623. vtop[0] = tmp;
  624. }
  625. #endif
  626. /* pop stack value */
  627. void vpop(void)
  628. {
  629. int v;
  630. v = vtop->r & VT_VALMASK;
  631. #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
  632. /* for x86, we need to pop the FP stack */
  633. if (v == TREG_ST0 && !nocode_wanted) {
  634. o(0xd9dd); /* fstp %st(1) */
  635. } else
  636. #endif
  637. if (v == VT_JMP || v == VT_JMPI) {
  638. /* need to put correct jump if && or || without test */
  639. gsym(vtop->c.ul);
  640. }
  641. vtop--;
  642. }
  643. /* convert stack entry to register and duplicate its value in another
  644. register */
  645. void gv_dup(void)
  646. {
  647. int rc, t, r, r1;
  648. SValue sv;
  649. t = vtop->type.t;
  650. if ((t & VT_BTYPE) == VT_LLONG) {
  651. lexpand();
  652. gv_dup();
  653. vswap();
  654. vrotb(3);
  655. gv_dup();
  656. vrotb(4);
  657. /* stack: H L L1 H1 */
  658. lbuild(t);
  659. vrotb(3);
  660. vrotb(3);
  661. vswap();
  662. lbuild(t);
  663. vswap();
  664. } else {
  665. /* duplicate value */
  666. rc = RC_INT;
  667. sv.type.t = VT_INT;
  668. if (is_float(t)) {
  669. rc = RC_FLOAT;
  670. #ifdef TCC_TARGET_X86_64
  671. if ((t & VT_BTYPE) == VT_LDOUBLE) {
  672. rc = RC_ST0;
  673. }
  674. #endif
  675. sv.type.t = t;
  676. }
  677. r = gv(rc);
  678. r1 = get_reg(rc);
  679. sv.r = r;
  680. sv.c.ul = 0;
  681. load(r1, &sv); /* move r to r1 */
  682. vdup();
  683. /* duplicates value */
  684. vtop->r = r1;
  685. }
  686. }
  687. #ifndef TCC_TARGET_X86_64
  688. /* generate CPU independent (unsigned) long long operations */
  689. void gen_opl(int op)
  690. {
  691. int t, a, b, op1, c, i;
  692. int func;
  693. unsigned short reg_iret = REG_IRET;
  694. unsigned short reg_lret = REG_LRET;
  695. SValue tmp;
  696. switch(op) {
  697. case '/':
  698. case TOK_PDIV:
  699. func = TOK___divdi3;
  700. goto gen_func;
  701. case TOK_UDIV:
  702. func = TOK___udivdi3;
  703. goto gen_func;
  704. case '%':
  705. func = TOK___moddi3;
  706. goto gen_mod_func;
  707. case TOK_UMOD:
  708. func = TOK___umoddi3;
  709. gen_mod_func:
  710. #ifdef TCC_ARM_EABI
  711. reg_iret = TREG_R2;
  712. reg_lret = TREG_R3;
  713. #endif
  714. gen_func:
  715. /* call generic long long function */
  716. vpush_global_sym(&func_old_type, func);
  717. vrott(3);
  718. gfunc_call(2);
  719. vpushi(0);
  720. vtop->r = reg_iret;
  721. vtop->r2 = reg_lret;
  722. break;
  723. case '^':
  724. case '&':
  725. case '|':
  726. case '*':
  727. case '+':
  728. case '-':
  729. t = vtop->type.t;
  730. vswap();
  731. lexpand();
  732. vrotb(3);
  733. lexpand();
  734. /* stack: L1 H1 L2 H2 */
  735. tmp = vtop[0];
  736. vtop[0] = vtop[-3];
  737. vtop[-3] = tmp;
  738. tmp = vtop[-2];
  739. vtop[-2] = vtop[-3];
  740. vtop[-3] = tmp;
  741. vswap();
  742. /* stack: H1 H2 L1 L2 */
  743. if (op == '*') {
  744. vpushv(vtop - 1);
  745. vpushv(vtop - 1);
  746. gen_op(TOK_UMULL);
  747. lexpand();
  748. /* stack: H1 H2 L1 L2 ML MH */
  749. for(i=0;i<4;i++)
  750. vrotb(6);
  751. /* stack: ML MH H1 H2 L1 L2 */
  752. tmp = vtop[0];
  753. vtop[0] = vtop[-2];
  754. vtop[-2] = tmp;
  755. /* stack: ML MH H1 L2 H2 L1 */
  756. gen_op('*');
  757. vrotb(3);
  758. vrotb(3);
  759. gen_op('*');
  760. /* stack: ML MH M1 M2 */
  761. gen_op('+');
  762. gen_op('+');
  763. } else if (op == '+' || op == '-') {
  764. /* XXX: add non carry method too (for MIPS or alpha) */
  765. if (op == '+')
  766. op1 = TOK_ADDC1;
  767. else
  768. op1 = TOK_SUBC1;
  769. gen_op(op1);
  770. /* stack: H1 H2 (L1 op L2) */
  771. vrotb(3);
  772. vrotb(3);
  773. gen_op(op1 + 1); /* TOK_xxxC2 */
  774. } else {
  775. gen_op(op);
  776. /* stack: H1 H2 (L1 op L2) */
  777. vrotb(3);
  778. vrotb(3);
  779. /* stack: (L1 op L2) H1 H2 */
  780. gen_op(op);
  781. /* stack: (L1 op L2) (H1 op H2) */
  782. }
  783. /* stack: L H */
  784. lbuild(t);
  785. break;
  786. case TOK_SAR:
  787. case TOK_SHR:
  788. case TOK_SHL:
  789. if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
  790. t = vtop[-1].type.t;
  791. vswap();
  792. lexpand();
  793. vrotb(3);
  794. /* stack: L H shift */
  795. c = (int)vtop->c.i;
  796. /* constant: simpler */
  797. /* NOTE: all comments are for SHL. the other cases are
  798. done by swaping words */
  799. vpop();
  800. if (op != TOK_SHL)
  801. vswap();
  802. if (c >= 32) {
  803. /* stack: L H */
  804. vpop();
  805. if (c > 32) {
  806. vpushi(c - 32);
  807. gen_op(op);
  808. }
  809. if (op != TOK_SAR) {
  810. vpushi(0);
  811. } else {
  812. gv_dup();
  813. vpushi(31);
  814. gen_op(TOK_SAR);
  815. }
  816. vswap();
  817. } else {
  818. vswap();
  819. gv_dup();
  820. /* stack: H L L */
  821. vpushi(c);
  822. gen_op(op);
  823. vswap();
  824. vpushi(32 - c);
  825. if (op == TOK_SHL)
  826. gen_op(TOK_SHR);
  827. else
  828. gen_op(TOK_SHL);
  829. vrotb(3);
  830. /* stack: L L H */
  831. vpushi(c);
  832. if (op == TOK_SHL)
  833. gen_op(TOK_SHL);
  834. else
  835. gen_op(TOK_SHR);
  836. gen_op('|');
  837. }
  838. if (op != TOK_SHL)
  839. vswap();
  840. lbuild(t);
  841. } else {
  842. /* XXX: should provide a faster fallback on x86 ? */
  843. switch(op) {
  844. case TOK_SAR:
  845. func = TOK___ashrdi3;
  846. goto gen_func;
  847. case TOK_SHR:
  848. func = TOK___lshrdi3;
  849. goto gen_func;
  850. case TOK_SHL:
  851. func = TOK___ashldi3;
  852. goto gen_func;
  853. }
  854. }
  855. break;
  856. default:
  857. /* compare operations */
  858. t = vtop->type.t;
  859. vswap();
  860. lexpand();
  861. vrotb(3);
  862. lexpand();
  863. /* stack: L1 H1 L2 H2 */
  864. tmp = vtop[-1];
  865. vtop[-1] = vtop[-2];
  866. vtop[-2] = tmp;
  867. /* stack: L1 L2 H1 H2 */
  868. /* compare high */
  869. op1 = op;
  870. /* when values are equal, we need to compare low words. since
  871. the jump is inverted, we invert the test too. */
  872. if (op1 == TOK_LT)
  873. op1 = TOK_LE;
  874. else if (op1 == TOK_GT)
  875. op1 = TOK_GE;
  876. else if (op1 == TOK_ULT)
  877. op1 = TOK_ULE;
  878. else if (op1 == TOK_UGT)
  879. op1 = TOK_UGE;
  880. a = 0;
  881. b = 0;
  882. gen_op(op1);
  883. if (op1 != TOK_NE) {
  884. a = gtst(1, 0);
  885. }
  886. if (op != TOK_EQ) {
  887. /* generate non equal test */
  888. /* XXX: NOT PORTABLE yet */
  889. if (a == 0) {
  890. b = gtst(0, 0);
  891. } else {
  892. #if defined(TCC_TARGET_I386)
  893. b = psym(0x850f, 0);
  894. #elif defined(TCC_TARGET_ARM)
  895. b = ind;
  896. o(0x1A000000 | encbranch(ind, 0, 1));
  897. #elif defined(TCC_TARGET_C67)
  898. error("not implemented");
  899. #else
  900. #error not supported
  901. #endif
  902. }
  903. }
  904. /* compare low. Always unsigned */
  905. op1 = op;
  906. if (op1 == TOK_LT)
  907. op1 = TOK_ULT;
  908. else if (op1 == TOK_LE)
  909. op1 = TOK_ULE;
  910. else if (op1 == TOK_GT)
  911. op1 = TOK_UGT;
  912. else if (op1 == TOK_GE)
  913. op1 = TOK_UGE;
  914. gen_op(op1);
  915. a = gtst(1, a);
  916. gsym(b);
  917. vseti(VT_JMPI, a);
  918. break;
  919. }
  920. }
  921. #endif
  922. /* handle integer constant optimizations and various machine
  923. independent opt */
  924. void gen_opic(int op)
  925. {
  926. int c1, c2, t1, t2, n;
  927. SValue *v1, *v2;
  928. long long l1, l2;
  929. typedef unsigned long long U;
  930. v1 = vtop - 1;
  931. v2 = vtop;
  932. t1 = v1->type.t & VT_BTYPE;
  933. t2 = v2->type.t & VT_BTYPE;
  934. if (t1 == VT_LLONG)
  935. l1 = v1->c.ll;
  936. else if (v1->type.t & VT_UNSIGNED)
  937. l1 = v1->c.ui;
  938. else
  939. l1 = v1->c.i;
  940. if (t2 == VT_LLONG)
  941. l2 = v2->c.ll;
  942. else if (v2->type.t & VT_UNSIGNED)
  943. l2 = v2->c.ui;
  944. else
  945. l2 = v2->c.i;
  946. /* currently, we cannot do computations with forward symbols */
  947. c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
  948. c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
  949. if (c1 && c2) {
  950. switch(op) {
  951. case '+': l1 += l2; break;
  952. case '-': l1 -= l2; break;
  953. case '&': l1 &= l2; break;
  954. case '^': l1 ^= l2; break;
  955. case '|': l1 |= l2; break;
  956. case '*': l1 *= l2; break;
  957. case TOK_PDIV:
  958. case '/':
  959. case '%':
  960. case TOK_UDIV:
  961. case TOK_UMOD:
  962. /* if division by zero, generate explicit division */
  963. if (l2 == 0) {
  964. if (const_wanted)
  965. error("division by zero in constant");
  966. goto general_case;
  967. }
  968. switch(op) {
  969. default: l1 /= l2; break;
  970. case '%': l1 %= l2; break;
  971. case TOK_UDIV: l1 = (U)l1 / l2; break;
  972. case TOK_UMOD: l1 = (U)l1 % l2; break;
  973. }
  974. break;
  975. case TOK_SHL: l1 <<= l2; break;
  976. case TOK_SHR: l1 = (U)l1 >> l2; break;
  977. case TOK_SAR: l1 >>= l2; break;
  978. /* tests */
  979. case TOK_ULT: l1 = (U)l1 < (U)l2; break;
  980. case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
  981. case TOK_EQ: l1 = l1 == l2; break;
  982. case TOK_NE: l1 = l1 != l2; break;
  983. case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
  984. case TOK_UGT: l1 = (U)l1 > (U)l2; break;
  985. case TOK_LT: l1 = l1 < l2; break;
  986. case TOK_GE: l1 = l1 >= l2; break;
  987. case TOK_LE: l1 = l1 <= l2; break;
  988. case TOK_GT: l1 = l1 > l2; break;
  989. /* logical */
  990. case TOK_LAND: l1 = l1 && l2; break;
  991. case TOK_LOR: l1 = l1 || l2; break;
  992. default:
  993. goto general_case;
  994. }
  995. v1->c.ll = l1;
  996. vtop--;
  997. } else {
  998. /* if commutative ops, put c2 as constant */
  999. if (c1 && (op == '+' || op == '&' || op == '^' ||
  1000. op == '|' || op == '*')) {
  1001. vswap();
  1002. c2 = c1; //c = c1, c1 = c2, c2 = c;
  1003. l2 = l1; //l = l1, l1 = l2, l2 = l;
  1004. }
  1005. /* Filter out NOP operations like x*1, x-0, x&-1... */
  1006. if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
  1007. op == TOK_PDIV) &&
  1008. l2 == 1) ||
  1009. ((op == '+' || op == '-' || op == '|' || op == '^' ||
  1010. op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
  1011. l2 == 0) ||
  1012. (op == '&' &&
  1013. l2 == -1))) {
  1014. /* nothing to do */
  1015. vtop--;
  1016. } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
  1017. /* try to use shifts instead of muls or divs */
  1018. if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
  1019. n = -1;
  1020. while (l2) {
  1021. l2 >>= 1;
  1022. n++;
  1023. }
  1024. vtop->c.ll = n;
  1025. if (op == '*')
  1026. op = TOK_SHL;
  1027. else if (op == TOK_PDIV)
  1028. op = TOK_SAR;
  1029. else
  1030. op = TOK_SHR;
  1031. }
  1032. goto general_case;
  1033. } else if (c2 && (op == '+' || op == '-') &&
  1034. ((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) ==
  1035. (VT_CONST | VT_SYM) ||
  1036. (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
  1037. /* symbol + constant case */
  1038. if (op == '-')
  1039. l2 = -l2;
  1040. vtop--;
  1041. vtop->c.ll += l2;
  1042. } else {
  1043. general_case:
  1044. if (!nocode_wanted) {
  1045. /* call low level op generator */
  1046. if (t1 == VT_LLONG || t2 == VT_LLONG)
  1047. gen_opl(op);
  1048. else
  1049. gen_opi(op);
  1050. } else {
  1051. vtop--;
  1052. }
  1053. }
  1054. }
  1055. }
  1056. /* generate a floating point operation with constant propagation */
  1057. void gen_opif(int op)
  1058. {
  1059. int c1, c2;
  1060. SValue *v1, *v2;
  1061. long double f1, f2;
  1062. v1 = vtop - 1;
  1063. v2 = vtop;
  1064. /* currently, we cannot do computations with forward symbols */
  1065. c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
  1066. c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
  1067. if (c1 && c2) {
  1068. if (v1->type.t == VT_FLOAT) {
  1069. f1 = v1->c.f;
  1070. f2 = v2->c.f;
  1071. } else if (v1->type.t == VT_DOUBLE) {
  1072. f1 = v1->c.d;
  1073. f2 = v2->c.d;
  1074. } else {
  1075. f1 = v1->c.ld;
  1076. f2 = v2->c.ld;
  1077. }
  1078. /* NOTE: we only do constant propagation if finite number (not
  1079. NaN or infinity) (ANSI spec) */
  1080. if (!ieee_finite(f1) || !ieee_finite(f2))
  1081. goto general_case;
  1082. switch(op) {
  1083. case '+': f1 += f2; break;
  1084. case '-': f1 -= f2; break;
  1085. case '*': f1 *= f2; break;
  1086. case '/':
  1087. if (f2 == 0.0) {
  1088. if (const_wanted)
  1089. error("division by zero in constant");
  1090. goto general_case;
  1091. }
  1092. f1 /= f2;
  1093. break;
  1094. /* XXX: also handles tests ? */
  1095. default:
  1096. goto general_case;
  1097. }
  1098. /* XXX: overflow test ? */
  1099. if (v1->type.t == VT_FLOAT) {
  1100. v1->c.f = f1;
  1101. } else if (v1->type.t == VT_DOUBLE) {
  1102. v1->c.d = f1;
  1103. } else {
  1104. v1->c.ld = f1;
  1105. }
  1106. vtop--;
  1107. } else {
  1108. general_case:
  1109. if (!nocode_wanted) {
  1110. gen_opf(op);
  1111. } else {
  1112. vtop--;
  1113. }
  1114. }
  1115. }
  1116. static int pointed_size(CType *type)
  1117. {
  1118. int align;
  1119. return type_size(pointed_type(type), &align);
  1120. }
  1121. static inline int is_null_pointer(SValue *p)
  1122. {
  1123. if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
  1124. return 0;
  1125. return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
  1126. ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0);
  1127. }
  1128. static inline int is_integer_btype(int bt)
  1129. {
  1130. return (bt == VT_BYTE || bt == VT_SHORT ||
  1131. bt == VT_INT || bt == VT_LLONG);
  1132. }
  1133. /* check types for comparison or subtraction of pointers */
  1134. static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
  1135. {
  1136. CType *type1, *type2, tmp_type1, tmp_type2;
  1137. int bt1, bt2;
  1138. /* null pointers are accepted for all comparisons as gcc */
  1139. if (is_null_pointer(p1) || is_null_pointer(p2))
  1140. return;
  1141. type1 = &p1->type;
  1142. type2 = &p2->type;
  1143. bt1 = type1->t & VT_BTYPE;
  1144. bt2 = type2->t & VT_BTYPE;
  1145. /* accept comparison between pointer and integer with a warning */
  1146. if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
  1147. if (op != TOK_LOR && op != TOK_LAND )
  1148. warning("comparison between pointer and integer");
  1149. return;
  1150. }
  1151. /* both must be pointers or implicit function pointers */
  1152. if (bt1 == VT_PTR) {
  1153. type1 = pointed_type(type1);
  1154. } else if (bt1 != VT_FUNC)
  1155. goto invalid_operands;
  1156. if (bt2 == VT_PTR) {
  1157. type2 = pointed_type(type2);
  1158. } else if (bt2 != VT_FUNC) {
  1159. invalid_operands:
  1160. error("invalid operands to binary %s", get_tok_str(op, NULL));
  1161. }
  1162. if ((type1->t & VT_BTYPE) == VT_VOID ||
  1163. (type2->t & VT_BTYPE) == VT_VOID)
  1164. return;
  1165. tmp_type1 = *type1;
  1166. tmp_type2 = *type2;
  1167. tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
  1168. tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
  1169. if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
  1170. /* gcc-like error if '-' is used */
  1171. if (op == '-')
  1172. goto invalid_operands;
  1173. else
  1174. warning("comparison of distinct pointer types lacks a cast");
  1175. }
  1176. }
  1177. /* generic gen_op: handles types problems */
  1178. void gen_op(int op)
  1179. {
  1180. int u, t1, t2, bt1, bt2, t;
  1181. CType type1;
  1182. t1 = vtop[-1].type.t;
  1183. t2 = vtop[0].type.t;
  1184. bt1 = t1 & VT_BTYPE;
  1185. bt2 = t2 & VT_BTYPE;
  1186. if (bt1 == VT_PTR || bt2 == VT_PTR) {
  1187. /* at least one operand is a pointer */
  1188. /* relationnal op: must be both pointers */
  1189. if (op >= TOK_ULT && op <= TOK_LOR) {
  1190. check_comparison_pointer_types(vtop - 1, vtop, op);
  1191. /* pointers are handled are unsigned */
  1192. #ifdef TCC_TARGET_X86_64
  1193. t = VT_LLONG | VT_UNSIGNED;
  1194. #else
  1195. t = VT_INT | VT_UNSIGNED;
  1196. #endif
  1197. goto std_op;
  1198. }
  1199. /* if both pointers, then it must be the '-' op */
  1200. if (bt1 == VT_PTR && bt2 == VT_PTR) {
  1201. if (op != '-')
  1202. error("cannot use pointers here");
  1203. check_comparison_pointer_types(vtop - 1, vtop, op);
  1204. /* XXX: check that types are compatible */
  1205. u = pointed_size(&vtop[-1].type);
  1206. gen_opic(op);
  1207. /* set to integer type */
  1208. #ifdef TCC_TARGET_X86_64
  1209. vtop->type.t = VT_LLONG;
  1210. #else
  1211. vtop->type.t = VT_INT;
  1212. #endif
  1213. vpushi(u);
  1214. gen_op(TOK_PDIV);
  1215. } else {
  1216. /* exactly one pointer : must be '+' or '-'. */
  1217. if (op != '-' && op != '+')
  1218. error("cannot use pointers here");
  1219. /* Put pointer as first operand */
  1220. if (bt2 == VT_PTR) {
  1221. vswap();
  1222. swap(&t1, &t2);
  1223. }
  1224. type1 = vtop[-1].type;
  1225. #ifdef TCC_TARGET_X86_64
  1226. vpushll(pointed_size(&vtop[-1].type));
  1227. #else
  1228. /* XXX: cast to int ? (long long case) */
  1229. vpushi(pointed_size(&vtop[-1].type));
  1230. #endif
  1231. gen_op('*');
  1232. #ifdef CONFIG_TCC_BCHECK
  1233. /* if evaluating constant expression, no code should be
  1234. generated, so no bound check */
  1235. if (tcc_state->do_bounds_check && !const_wanted) {
  1236. /* if bounded pointers, we generate a special code to
  1237. test bounds */
  1238. if (op == '-') {
  1239. vpushi(0);
  1240. vswap();
  1241. gen_op('-');
  1242. }
  1243. gen_bounded_ptr_add();
  1244. } else
  1245. #endif
  1246. {
  1247. gen_opic(op);
  1248. }
  1249. /* put again type if gen_opic() swaped operands */
  1250. vtop->type = type1;
  1251. }
  1252. } else if (is_float(bt1) || is_float(bt2)) {
  1253. /* compute bigger type and do implicit casts */
  1254. if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
  1255. t = VT_LDOUBLE;
  1256. } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
  1257. t = VT_DOUBLE;
  1258. } else {
  1259. t = VT_FLOAT;
  1260. }
  1261. /* floats can only be used for a few operations */
  1262. if (op != '+' && op != '-' && op != '*' && op != '/' &&
  1263. (op < TOK_ULT || op > TOK_GT))
  1264. error("invalid operands for binary operation");
  1265. goto std_op;
  1266. } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
  1267. /* cast to biggest op */
  1268. t = VT_LLONG;
  1269. /* convert to unsigned if it does not fit in a long long */
  1270. if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
  1271. (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
  1272. t |= VT_UNSIGNED;
  1273. goto std_op;
  1274. } else {
  1275. /* integer operations */
  1276. t = VT_INT;
  1277. /* convert to unsigned if it does not fit in an integer */
  1278. if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
  1279. (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
  1280. t |= VT_UNSIGNED;
  1281. std_op:
  1282. /* XXX: currently, some unsigned operations are explicit, so
  1283. we modify them here */
  1284. if (t & VT_UNSIGNED) {
  1285. if (op == TOK_SAR)
  1286. op = TOK_SHR;
  1287. else if (op == '/')
  1288. op = TOK_UDIV;
  1289. else if (op == '%')
  1290. op = TOK_UMOD;
  1291. else if (op == TOK_LT)
  1292. op = TOK_ULT;
  1293. else if (op == TOK_GT)
  1294. op = TOK_UGT;
  1295. else if (op == TOK_LE)
  1296. op = TOK_ULE;
  1297. else if (op == TOK_GE)
  1298. op = TOK_UGE;
  1299. }
  1300. vswap();
  1301. type1.t = t;
  1302. gen_cast(&type1);
  1303. vswap();
  1304. /* special case for shifts and long long: we keep the shift as
  1305. an integer */
  1306. if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
  1307. type1.t = VT_INT;
  1308. gen_cast(&type1);
  1309. if (is_float(t))
  1310. gen_opif(op);
  1311. else
  1312. gen_opic(op);
  1313. if (op >= TOK_ULT && op <= TOK_GT) {
  1314. /* relationnal op: the result is an int */
  1315. vtop->type.t = VT_INT;
  1316. } else {
  1317. vtop->type.t = t;
  1318. }
  1319. }
  1320. }
  1321. #ifndef TCC_TARGET_ARM
  1322. /* generic itof for unsigned long long case */
  1323. void gen_cvt_itof1(int t)
  1324. {
  1325. if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
  1326. (VT_LLONG | VT_UNSIGNED)) {
  1327. if (t == VT_FLOAT)
  1328. vpush_global_sym(&func_old_type, TOK___floatundisf);
  1329. #if LDOUBLE_SIZE != 8
  1330. else if (t == VT_LDOUBLE)
  1331. vpush_global_sym(&func_old_type, TOK___floatundixf);
  1332. #endif
  1333. else
  1334. vpush_global_sym(&func_old_type, TOK___floatundidf);
  1335. vrott(2);
  1336. gfunc_call(1);
  1337. vpushi(0);
  1338. vtop->r = reg_fret(t);
  1339. } else {
  1340. gen_cvt_itof(t);
  1341. }
  1342. }
  1343. #endif
  1344. /* generic ftoi for unsigned long long case */
  1345. void gen_cvt_ftoi1(int t)
  1346. {
  1347. int st;
  1348. if (t == (VT_LLONG | VT_UNSIGNED)) {
  1349. /* not handled natively */
  1350. st = vtop->type.t & VT_BTYPE;
  1351. if (st == VT_FLOAT)
  1352. vpush_global_sym(&func_old_type, TOK___fixunssfdi);
  1353. #if LDOUBLE_SIZE != 8
  1354. else if (st == VT_LDOUBLE)
  1355. vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
  1356. #endif
  1357. else
  1358. vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
  1359. vrott(2);
  1360. gfunc_call(1);
  1361. vpushi(0);
  1362. vtop->r = REG_IRET;
  1363. vtop->r2 = REG_LRET;
  1364. } else {
  1365. gen_cvt_ftoi(t);
  1366. }
  1367. }
  1368. /* force char or short cast */
  1369. void force_charshort_cast(int t)
  1370. {
  1371. int bits, dbt;
  1372. dbt = t & VT_BTYPE;
  1373. /* XXX: add optimization if lvalue : just change type and offset */
  1374. if (dbt == VT_BYTE)
  1375. bits = 8;
  1376. else
  1377. bits = 16;
  1378. if (t & VT_UNSIGNED) {
  1379. vpushi((1 << bits) - 1);
  1380. gen_op('&');
  1381. } else {
  1382. bits = 32 - bits;
  1383. vpushi(bits);
  1384. gen_op(TOK_SHL);
  1385. /* result must be signed or the SAR is converted to an SHL
  1386. This was not the case when "t" was a signed short
  1387. and the last value on the stack was an unsigned int */
  1388. vtop->type.t &= ~VT_UNSIGNED;
  1389. vpushi(bits);
  1390. gen_op(TOK_SAR);
  1391. }
  1392. }
  1393. /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
  1394. static void gen_cast(CType *type)
  1395. {
  1396. int sbt, dbt, sf, df, c, p;
  1397. /* special delayed cast for char/short */
  1398. /* XXX: in some cases (multiple cascaded casts), it may still
  1399. be incorrect */
  1400. if (vtop->r & VT_MUSTCAST) {
  1401. vtop->r &= ~VT_MUSTCAST;
  1402. force_charshort_cast(vtop->type.t);
  1403. }
  1404. /* bitfields first get cast to ints */
  1405. if (vtop->type.t & VT_BITFIELD) {
  1406. gv(RC_INT);
  1407. }
  1408. dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
  1409. sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
  1410. if (sbt != dbt) {
  1411. sf = is_float(sbt);
  1412. df = is_float(dbt);
  1413. c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
  1414. p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
  1415. if (c) {
  1416. /* constant case: we can do it now */
  1417. /* XXX: in ISOC, cannot do it if error in convert */
  1418. if (sbt == VT_FLOAT)
  1419. vtop->c.ld = vtop->c.f;
  1420. else if (sbt == VT_DOUBLE)
  1421. vtop->c.ld = vtop->c.d;
  1422. if (df) {
  1423. if ((sbt & VT_BTYPE) == VT_LLONG) {
  1424. if (sbt & VT_UNSIGNED)
  1425. vtop->c.ld = vtop->c.ull;
  1426. else
  1427. vtop->c.ld = vtop->c.ll;
  1428. } else if(!sf) {
  1429. if (sbt & VT_UNSIGNED)
  1430. vtop->c.ld = vtop->c.ui;
  1431. else
  1432. vtop->c.ld = vtop->c.i;
  1433. }
  1434. if (dbt == VT_FLOAT)
  1435. vtop->c.f = (float)vtop->c.ld;
  1436. else if (dbt == VT_DOUBLE)
  1437. vtop->c.d = (double)vtop->c.ld;
  1438. } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
  1439. vtop->c.ull = (unsigned long long)vtop->c.ld;
  1440. } else if (sf && dbt == VT_BOOL) {
  1441. vtop->c.i = (vtop->c.ld != 0);
  1442. } else {
  1443. if(sf)
  1444. vtop->c.ll = (long long)vtop->c.ld;
  1445. else if (sbt == (VT_LLONG|VT_UNSIGNED))
  1446. vtop->c.ll = vtop->c.ull;
  1447. else if (sbt & VT_UNSIGNED)
  1448. vtop->c.ll = vtop->c.ui;
  1449. else if (sbt != VT_LLONG)
  1450. vtop->c.ll = vtop->c.i;
  1451. if (dbt == (VT_LLONG|VT_UNSIGNED))
  1452. vtop->c.ull = vtop->c.ll;
  1453. else if (dbt == VT_BOOL)
  1454. vtop->c.i = (vtop->c.ll != 0);
  1455. else if (dbt != VT_LLONG) {
  1456. int s = 0;
  1457. if ((dbt & VT_BTYPE) == VT_BYTE)
  1458. s = 24;
  1459. else if ((dbt & VT_BTYPE) == VT_SHORT)
  1460. s = 16;
  1461. if(dbt & VT_UNSIGNED)
  1462. vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
  1463. else
  1464. vtop->c.i = ((int)vtop->c.ll << s) >> s;
  1465. }
  1466. }
  1467. } else if (p && dbt == VT_BOOL) {
  1468. vtop->r = VT_CONST;
  1469. vtop->c.i = 1;
  1470. } else if (!nocode_wanted) {
  1471. /* non constant case: generate code */
  1472. if (sf && df) {
  1473. /* convert from fp to fp */
  1474. gen_cvt_ftof(dbt);
  1475. } else if (df) {
  1476. /* convert int to fp */
  1477. gen_cvt_itof1(dbt);
  1478. } else if (sf) {
  1479. /* convert fp to int */
  1480. if (dbt == VT_BOOL) {
  1481. vpushi(0);
  1482. gen_op(TOK_NE);
  1483. } else {
  1484. /* we handle char/short/etc... with generic code */
  1485. if (dbt != (VT_INT | VT_UNSIGNED) &&
  1486. dbt != (VT_LLONG | VT_UNSIGNED) &&
  1487. dbt != VT_LLONG)
  1488. dbt = VT_INT;
  1489. gen_cvt_ftoi1(dbt);
  1490. if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
  1491. /* additional cast for char/short... */
  1492. vtop->type.t = dbt;
  1493. gen_cast(type);
  1494. }
  1495. }
  1496. #ifndef TCC_TARGET_X86_64
  1497. } else if ((dbt & VT_BTYPE) == VT_LLONG) {
  1498. if ((sbt & VT_BTYPE) != VT_LLONG) {
  1499. /* scalar to long long */
  1500. /* machine independent conversion */
  1501. gv(RC_INT);
  1502. /* generate high word */
  1503. if (sbt == (VT_INT | VT_UNSIGNED)) {
  1504. vpushi(0);
  1505. gv(RC_INT);
  1506. } else {
  1507. if (sbt == VT_PTR) {
  1508. /* cast from pointer to int before we apply
  1509. shift operation, which pointers don't support*/
  1510. gen_cast(&int_type);
  1511. }
  1512. gv_dup();
  1513. vpushi(31);
  1514. gen_op(TOK_SAR);
  1515. }
  1516. /* patch second register */
  1517. vtop[-1].r2 = vtop->r;
  1518. vpop();
  1519. }
  1520. #else
  1521. } else if ((dbt & VT_BTYPE) == VT_LLONG ||
  1522. (dbt & VT_BTYPE) == VT_PTR) {
  1523. /* XXX: not sure if this is perfect... need more tests */
  1524. if ((sbt & VT_BTYPE) != VT_LLONG) {
  1525. int r = gv(RC_INT);
  1526. if (sbt != (VT_INT | VT_UNSIGNED) &&
  1527. sbt != VT_PTR && sbt != VT_FUNC) {
  1528. /* x86_64 specific: movslq */
  1529. o(0x6348);
  1530. o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
  1531. }
  1532. }
  1533. #endif
  1534. } else if (dbt == VT_BOOL) {
  1535. /* scalar to bool */
  1536. vpushi(0);
  1537. gen_op(TOK_NE);
  1538. } else if ((dbt & VT_BTYPE) == VT_BYTE ||
  1539. (dbt & VT_BTYPE) == VT_SHORT) {
  1540. if (sbt == VT_PTR) {
  1541. vtop->type.t = VT_INT;
  1542. warning("nonportable conversion from pointer to char/short");
  1543. }
  1544. force_charshort_cast(dbt);
  1545. } else if ((dbt & VT_BTYPE) == VT_INT) {
  1546. /* scalar to int */
  1547. if (sbt == VT_LLONG) {
  1548. /* from long long: just take low order word */
  1549. lexpand();
  1550. vpop();
  1551. }
  1552. /* if lvalue and single word type, nothing to do because
  1553. the lvalue already contains the real type size (see
  1554. VT_LVAL_xxx constants) */
  1555. }
  1556. }
  1557. } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
  1558. /* if we are casting between pointer types,
  1559. we must update the VT_LVAL_xxx size */
  1560. vtop->r = (vtop->r & ~VT_LVAL_TYPE)
  1561. | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
  1562. }
  1563. vtop->type = *type;
  1564. }
  1565. /* return type size. Put alignment at 'a' */
  1566. static int type_size(CType *type, int *a)
  1567. {
  1568. Sym *s;
  1569. int bt;
  1570. bt = type->t & VT_BTYPE;
  1571. if (bt == VT_STRUCT) {
  1572. /* struct/union */
  1573. s = type->ref;
  1574. *a = s->r;
  1575. return s->c;
  1576. } else if (bt == VT_PTR) {
  1577. if (type->t & VT_ARRAY) {
  1578. int ts;
  1579. s = type->ref;
  1580. ts = type_size(&s->type, a);
  1581. if (ts < 0 && s->c < 0)
  1582. ts = -ts;
  1583. return ts * s->c;
  1584. } else {
  1585. *a = PTR_SIZE;
  1586. return PTR_SIZE;
  1587. }
  1588. } else if (bt == VT_LDOUBLE) {
  1589. *a = LDOUBLE_ALIGN;
  1590. return LDOUBLE_SIZE;
  1591. } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
  1592. #ifdef TCC_TARGET_I386
  1593. #ifdef TCC_TARGET_PE
  1594. *a = 8;
  1595. #else
  1596. *a = 4;
  1597. #endif
  1598. #elif defined(TCC_TARGET_ARM)
  1599. #ifdef TCC_ARM_EABI
  1600. *a = 8;
  1601. #else
  1602. *a = 4;
  1603. #endif
  1604. #else
  1605. *a = 8;
  1606. #endif
  1607. return 8;
  1608. } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
  1609. *a = 4;
  1610. return 4;
  1611. } else if (bt == VT_SHORT) {
  1612. *a = 2;
  1613. return 2;
  1614. } else {
  1615. /* char, void, function, _Bool */
  1616. *a = 1;
  1617. return 1;
  1618. }
  1619. }
  1620. /* return the pointed type of t */
  1621. static inline CType *pointed_type(CType *type)
  1622. {
  1623. return &type->ref->type;
  1624. }
  1625. /* modify type so that its it is a pointer to type. */
  1626. static void mk_pointer(CType *type)
  1627. {
  1628. Sym *s;
  1629. s = sym_push(SYM_FIELD, type, 0, -1);
  1630. type->t = VT_PTR | (type->t & ~VT_TYPE);
  1631. type->ref = s;
  1632. }
  1633. /* compare function types. OLD functions match any new functions */
  1634. static int is_compatible_func(CType *type1, CType *type2)
  1635. {
  1636. Sym *s1, *s2;
  1637. s1 = type1->ref;
  1638. s2 = type2->ref;
  1639. if (!is_compatible_types(&s1->type, &s2->type))
  1640. return 0;
  1641. /* check func_call */
  1642. if (FUNC_CALL(s1->r) != FUNC_CALL(s2->r))
  1643. return 0;
  1644. /* XXX: not complete */
  1645. if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
  1646. return 1;
  1647. if (s1->c != s2->c)
  1648. return 0;
  1649. while (s1 != NULL) {
  1650. if (s2 == NULL)
  1651. return 0;
  1652. if (!is_compatible_parameter_types(&s1->type, &s2->type))
  1653. return 0;
  1654. s1 = s1->next;
  1655. s2 = s2->next;
  1656. }
  1657. if (s2)
  1658. return 0;
  1659. return 1;
  1660. }
  1661. /* return true if type1 and type2 are the same. If unqualified is
  1662. true, qualifiers on the types are ignored.
  1663. - enums are not checked as gcc __builtin_types_compatible_p ()
  1664. */
  1665. static int compare_types(CType *type1, CType *type2, int unqualified)
  1666. {
  1667. int bt1, t1, t2;
  1668. t1 = type1->t & VT_TYPE;
  1669. t2 = type2->t & VT_TYPE;
  1670. if (unqualified) {
  1671. /* strip qualifiers before comparing */
  1672. t1 &= ~(VT_CONSTANT | VT_VOLATILE);
  1673. t2 &= ~(VT_CONSTANT | VT_VOLATILE);
  1674. }
  1675. /* XXX: bitfields ? */
  1676. if (t1 != t2)
  1677. return 0;
  1678. /* test more complicated cases */
  1679. bt1 = t1 & VT_BTYPE;
  1680. if (bt1 == VT_PTR) {
  1681. type1 = pointed_type(type1);
  1682. type2 = pointed_type(type2);
  1683. return is_compatible_types(type1, type2);
  1684. } else if (bt1 == VT_STRUCT) {
  1685. return (type1->ref == type2->ref);
  1686. } else if (bt1 == VT_FUNC) {
  1687. return is_compatible_func(type1, type2);
  1688. } else {
  1689. return 1;
  1690. }
  1691. }
  1692. /* return true if type1 and type2 are exactly the same (including
  1693. qualifiers).
  1694. */
  1695. static int is_compatible_types(CType *type1, CType *type2)
  1696. {
  1697. return compare_types(type1,type2,0);
  1698. }
  1699. /* return true if type1 and type2 are the same (ignoring qualifiers).
  1700. */
  1701. static int is_compatible_parameter_types(CType *type1, CType *type2)
  1702. {
  1703. return compare_types(type1,type2,1);
  1704. }
  1705. /* print a type. If 'varstr' is not NULL, then the variable is also
  1706. printed in the type */
  1707. /* XXX: union */
  1708. /* XXX: add array and function pointers */
  1709. void type_to_str(char *buf, int buf_size,
  1710. CType *type, const char *varstr)
  1711. {
  1712. int bt, v, t;
  1713. Sym *s, *sa;
  1714. char buf1[256];
  1715. const char *tstr;
  1716. t = type->t & VT_TYPE;
  1717. bt = t & VT_BTYPE;
  1718. buf[0] = '\0';
  1719. if (t & VT_CONSTANT)
  1720. pstrcat(buf, buf_size, "const ");
  1721. if (t & VT_VOLATILE)
  1722. pstrcat(buf, buf_size, "volatile ");
  1723. if (t & VT_UNSIGNED)
  1724. pstrcat(buf, buf_size, "unsigned ");
  1725. switch(bt) {
  1726. case VT_VOID:
  1727. tstr = "void";
  1728. goto add_tstr;
  1729. case VT_BOOL:
  1730. tstr = "_Bool";
  1731. goto add_tstr;
  1732. case VT_BYTE:
  1733. tstr = "char";
  1734. goto add_tstr;
  1735. case VT_SHORT:
  1736. tstr = "short";
  1737. goto add_tstr;
  1738. case VT_INT:
  1739. tstr = "int";
  1740. goto add_tstr;
  1741. case VT_LONG:
  1742. tstr = "long";
  1743. goto add_tstr;
  1744. case VT_LLONG:
  1745. tstr = "long long";
  1746. goto add_tstr;
  1747. case VT_FLOAT:
  1748. tstr = "float";
  1749. goto add_tstr;
  1750. case VT_DOUBLE:
  1751. tstr = "double";
  1752. goto add_tstr;
  1753. case VT_LDOUBLE:
  1754. tstr = "long double";
  1755. add_tstr:
  1756. pstrcat(buf, buf_size, tstr);
  1757. break;
  1758. case VT_ENUM:
  1759. case VT_STRUCT:
  1760. if (bt == VT_STRUCT)
  1761. tstr = "struct ";
  1762. else
  1763. tstr = "enum ";
  1764. pstrcat(buf, buf_size, tstr);
  1765. v = type->ref->v & ~SYM_STRUCT;
  1766. if (v >= SYM_FIRST_ANOM)
  1767. pstrcat(buf, buf_size, "<anonymous>");
  1768. else
  1769. pstrcat(buf, buf_size, get_tok_str(v, NULL));
  1770. break;
  1771. case VT_FUNC:
  1772. s = type->ref;
  1773. type_to_str(buf, buf_size, &s->type, varstr);
  1774. pstrcat(buf, buf_size, "(");
  1775. sa = s->next;
  1776. while (sa != NULL) {
  1777. type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
  1778. pstrcat(buf, buf_size, buf1);
  1779. sa = sa->next;
  1780. if (sa)
  1781. pstrcat(buf, buf_size, ", ");
  1782. }
  1783. pstrcat(buf, buf_size, ")");
  1784. goto no_var;
  1785. case VT_PTR:
  1786. s = type->ref;
  1787. pstrcpy(buf1, sizeof(buf1), "*");
  1788. if (varstr)
  1789. pstrcat(buf1, sizeof(buf1), varstr);
  1790. type_to_str(buf, buf_size, &s->type, buf1);
  1791. goto no_var;
  1792. }
  1793. if (varstr) {
  1794. pstrcat(buf, buf_size, " ");
  1795. pstrcat(buf, buf_size, varstr);
  1796. }
  1797. no_var: ;
  1798. }
  1799. /* verify type compatibility to store vtop in 'dt' type, and generate
  1800. casts if needed. */
  1801. static void gen_assign_cast(CType *dt)
  1802. {
  1803. CType *st, *type1, *type2, tmp_type1, tmp_type2;
  1804. char buf1[256], buf2[256];
  1805. int dbt, sbt;
  1806. st = &vtop->type; /* source type */
  1807. dbt = dt->t & VT_BTYPE;
  1808. sbt = st->t & VT_BTYPE;
  1809. if (dt->t & VT_CONSTANT)
  1810. warning("assignment of read-only location");
  1811. switch(dbt) {
  1812. case VT_PTR:
  1813. /* special cases for pointers */
  1814. /* '0' can also be a pointer */
  1815. if (is_null_pointer(vtop))
  1816. goto type_ok;
  1817. /* accept implicit pointer to integer cast with warning */
  1818. if (is_integer_btype(sbt)) {
  1819. warning("assignment makes pointer from integer without a cast");
  1820. goto type_ok;
  1821. }
  1822. type1 = pointed_type(dt);
  1823. /* a function is implicitly a function pointer */
  1824. if (sbt == VT_FUNC) {
  1825. if ((type1->t & VT_BTYPE) != VT_VOID &&
  1826. !is_compatible_types(pointed_type(dt), st))
  1827. goto error;
  1828. else
  1829. goto type_ok;
  1830. }
  1831. if (sbt != VT_PTR)
  1832. goto error;
  1833. type2 = pointed_type(st);
  1834. if ((type1->t & VT_BTYPE) == VT_VOID ||
  1835. (type2->t & VT_BTYPE) == VT_VOID) {
  1836. /* void * can match anything */
  1837. } else {
  1838. /* exact type match, except for unsigned */
  1839. tmp_type1 = *type1;
  1840. tmp_type2 = *type2;
  1841. tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
  1842. tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
  1843. if (!is_compatible_types(&tmp_type1, &tmp_type2))
  1844. warning("assignment from incompatible pointer type");
  1845. }
  1846. /* check const and volatile */
  1847. if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
  1848. (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
  1849. warning("assignment discards qualifiers from pointer target type");
  1850. break;
  1851. case VT_BYTE:
  1852. case VT_SHORT:
  1853. case VT_INT:
  1854. case VT_LLONG:
  1855. if (sbt == VT_PTR || sbt == VT_FUNC) {
  1856. warning("assignment makes integer from pointer without a cast");
  1857. }
  1858. /* XXX: more tests */
  1859. break;
  1860. case VT_STRUCT:
  1861. tmp_type1 = *dt;
  1862. tmp_type2 = *st;
  1863. tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
  1864. tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
  1865. if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
  1866. error:
  1867. type_to_str(buf1, sizeof(buf1), st, NULL);
  1868. type_to_str(buf2, sizeof(buf2), dt, NULL);
  1869. error("cannot cast '%s' to '%s'", buf1, buf2);
  1870. }
  1871. break;
  1872. }
  1873. type_ok:
  1874. gen_cast(dt);
  1875. }
  1876. /* store vtop in lvalue pushed on stack */
  1877. void vstore(void)
  1878. {
  1879. int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
  1880. ft = vtop[-1].type.t;
  1881. sbt = vtop->type.t & VT_BTYPE;
  1882. dbt = ft & VT_BTYPE;
  1883. if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
  1884. (sbt == VT_INT && dbt == VT_SHORT)) {
  1885. /* optimize char/short casts */
  1886. delayed_cast = VT_MUSTCAST;
  1887. vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
  1888. /* XXX: factorize */
  1889. if (ft & VT_CONSTANT)
  1890. warning("assignment of read-only location");
  1891. } else {
  1892. delayed_cast = 0;
  1893. if (!(ft & VT_BITFIELD))
  1894. gen_assign_cast(&vtop[-1].type);
  1895. }
  1896. if (sbt == VT_STRUCT) {
  1897. /* if structure, only generate pointer */
  1898. /* structure assignment : generate memcpy */
  1899. /* XXX: optimize if small size */
  1900. if (!nocode_wanted) {
  1901. size = type_size(&vtop->type, &align);
  1902. #ifdef TCC_ARM_EABI
  1903. if(!(align & 7))
  1904. vpush_global_sym(&func_old_type, TOK_memcpy8);
  1905. else if(!(align & 3))
  1906. vpush_global_sym(&func_old_type, TOK_memcpy4);
  1907. else
  1908. #endif
  1909. vpush_global_sym(&func_old_type, TOK_memcpy);
  1910. /* destination */
  1911. vpushv(vtop - 2);
  1912. vtop->type.t = VT_PTR;
  1913. gaddrof();
  1914. /* source */
  1915. vpushv(vtop - 2);
  1916. vtop->type.t = VT_PTR;
  1917. gaddrof();
  1918. /* type size */
  1919. vpushi(size);
  1920. gfunc_call(3);
  1921. vswap();
  1922. vpop();
  1923. } else {
  1924. vswap();
  1925. vpop();
  1926. }
  1927. /* leave source on stack */
  1928. } else if (ft & VT_BITFIELD) {
  1929. /* bitfield store handling */
  1930. bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
  1931. bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
  1932. /* remove bit field info to avoid loops */
  1933. vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
  1934. /* duplicate source into other register */
  1935. gv_dup();
  1936. vswap();
  1937. vrott(3);
  1938. if((ft & VT_BTYPE) == VT_BOOL) {
  1939. gen_cast(&vtop[-1].type);
  1940. vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
  1941. }
  1942. /* duplicate destination */
  1943. vdup();
  1944. vtop[-1] = vtop[-2];
  1945. /* mask and shift source */
  1946. if((ft & VT_BTYPE) != VT_BOOL) {
  1947. if((ft & VT_BTYPE) == VT_LLONG) {
  1948. vpushll((1ULL << bit_size) - 1ULL);
  1949. } else {
  1950. vpushi((1 << bit_size) - 1);
  1951. }
  1952. gen_op('&');
  1953. }
  1954. vpushi(bit_pos);
  1955. gen_op(TOK_SHL);
  1956. /* load destination, mask and or with source */
  1957. vswap();
  1958. if((ft & VT_BTYPE) == VT_LLONG) {
  1959. vpushll(~(((1ULL << bit_size) - 1ULL) << bit_pos));
  1960. } else {
  1961. vpushi(~(((1 << bit_size) - 1) << bit_pos));
  1962. }
  1963. gen_op('&');
  1964. gen_op('|');
  1965. /* store result */
  1966. vstore();
  1967. /* pop off shifted source from "duplicate source..." above */
  1968. vpop();
  1969. } else {
  1970. #ifdef CONFIG_TCC_BCHECK
  1971. /* bound check case */
  1972. if (vtop[-1].r & VT_MUSTBOUND) {
  1973. vswap();
  1974. gbound();
  1975. vswap();
  1976. }
  1977. #endif
  1978. if (!nocode_wanted) {
  1979. rc = RC_INT;
  1980. if (is_float(ft)) {
  1981. rc = RC_FLOAT;
  1982. #ifdef TCC_TARGET_X86_64
  1983. if ((ft & VT_BTYPE) == VT_LDOUBLE) {
  1984. rc = RC_ST0;
  1985. }
  1986. #endif
  1987. }
  1988. r = gv(rc); /* generate value */
  1989. /* if lvalue was saved on stack, must read it */
  1990. if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
  1991. SValue sv;
  1992. t = get_reg(RC_INT);
  1993. #ifdef TCC_TARGET_X86_64
  1994. sv.type.t = VT_PTR;
  1995. #else
  1996. sv.type.t = VT_INT;
  1997. #endif
  1998. sv.r = VT_LOCAL | VT_LVAL;
  1999. sv.c.ul = vtop[-1].c.ul;
  2000. load(t, &sv);
  2001. vtop[-1].r = t | VT_LVAL;
  2002. }
  2003. store(r, vtop - 1);
  2004. #ifndef TCC_TARGET_X86_64
  2005. /* two word case handling : store second register at word + 4 */
  2006. if ((ft & VT_BTYPE) == VT_LLONG) {
  2007. vswap();
  2008. /* convert to int to increment easily */
  2009. vtop->type.t = VT_INT;
  2010. gaddrof();
  2011. vpushi(4);
  2012. gen_op('+');
  2013. vtop->r |= VT_LVAL;
  2014. vswap();
  2015. /* XXX: it works because r2 is spilled last ! */
  2016. store(vtop->r2, vtop - 1);
  2017. }
  2018. #endif
  2019. }
  2020. vswap();
  2021. vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
  2022. vtop->r |= delayed_cast;
  2023. }
  2024. }
  2025. /* post defines POST/PRE add. c is the token ++ or -- */
  2026. void inc(int post, int c)
  2027. {
  2028. test_lvalue();
  2029. vdup(); /* save lvalue */
  2030. if (post) {
  2031. gv_dup(); /* duplicate value */
  2032. vrotb(3);
  2033. vrotb(3);
  2034. }
  2035. /* add constant */
  2036. vpushi(c - TOK_MID);
  2037. gen_op('+');
  2038. vstore(); /* store value */
  2039. if (post)
  2040. vpop(); /* if post op, return saved value */
  2041. }
  2042. /* Parse GNUC __attribute__ extension. Currently, the following
  2043. extensions are recognized:
  2044. - aligned(n) : set data/function alignment.
  2045. - packed : force data alignment to 1
  2046. - section(x) : generate data/code in this section.
  2047. - unused : currently ignored, but may be used someday.
  2048. - regparm(n) : pass function parameters in registers (i386 only)
  2049. */
  2050. static void parse_attribute(AttributeDef *ad)
  2051. {
  2052. int t, n;
  2053. while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
  2054. next();
  2055. skip('(');
  2056. skip('(');
  2057. while (tok != ')') {
  2058. if (tok < TOK_IDENT)
  2059. expect("attribute name");
  2060. t = tok;
  2061. next();
  2062. switch(t) {
  2063. case TOK_SECTION1:
  2064. case TOK_SECTION2:
  2065. skip('(');
  2066. if (tok != TOK_STR)
  2067. expect("section name");
  2068. ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
  2069. next();
  2070. skip(')');
  2071. break;
  2072. case TOK_ALIGNED1:
  2073. case TOK_ALIGNED2:
  2074. if (tok == '(') {
  2075. next();
  2076. n = expr_const();
  2077. if (n <= 0 || (n & (n - 1)) != 0)
  2078. error("alignment must be a positive power of two");
  2079. skip(')');
  2080. } else {
  2081. n = MAX_ALIGN;
  2082. }
  2083. ad->aligned = n;
  2084. break;
  2085. case TOK_PACKED1:
  2086. case TOK_PACKED2:
  2087. ad->packed = 1;
  2088. break;
  2089. case TOK_UNUSED1:
  2090. case TOK_UNUSED2:
  2091. /* currently, no need to handle it because tcc does not
  2092. track unused objects */
  2093. break;
  2094. case TOK_NORETURN1:
  2095. case TOK_NORETURN2:
  2096. /* currently, no need to handle it because tcc does not
  2097. track unused objects */
  2098. break;
  2099. case TOK_CDECL1:
  2100. case TOK_CDECL2:
  2101. case TOK_CDECL3:
  2102. FUNC_CALL(ad->func_attr) = FUNC_CDECL;
  2103. break;
  2104. case TOK_STDCALL1:
  2105. case TOK_STDCALL2:
  2106. case TOK_STDCALL3:
  2107. FUNC_CALL(ad->func_attr) = FUNC_STDCALL;
  2108. break;
  2109. #ifdef TCC_TARGET_I386
  2110. case TOK_REGPARM1:
  2111. case TOK_REGPARM2:
  2112. skip('(');
  2113. n = expr_const();
  2114. if (n > 3)
  2115. n = 3;
  2116. else if (n < 0)
  2117. n = 0;
  2118. if (n > 0)
  2119. FUNC_CALL(ad->func_attr) = FUNC_FASTCALL1 + n - 1;
  2120. skip(')');
  2121. break;
  2122. case TOK_FASTCALL1:
  2123. case TOK_FASTCALL2:
  2124. case TOK_FASTCALL3:
  2125. FUNC_CALL(ad->func_attr) = FUNC_FASTCALLW;
  2126. break;
  2127. #endif
  2128. case TOK_DLLEXPORT:
  2129. FUNC_EXPORT(ad->func_attr) = 1;
  2130. break;
  2131. default:
  2132. if (tcc_state->warn_unsupported)
  2133. warning("'%s' attribute ignored", get_tok_str(t, NULL));
  2134. /* skip parameters */
  2135. if (tok == '(') {
  2136. int parenthesis = 0;
  2137. do {
  2138. if (tok == '(')
  2139. parenthesis++;
  2140. else if (tok == ')')
  2141. parenthesis--;
  2142. next();
  2143. } while (parenthesis && tok != -1);
  2144. }
  2145. break;
  2146. }
  2147. if (tok != ',')
  2148. break;
  2149. next();
  2150. }
  2151. skip(')');
  2152. skip(')');
  2153. }
  2154. }
  2155. /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
  2156. static void struct_decl(CType *type, int u)
  2157. {
  2158. int a, v, size, align, maxalign, c, offset;
  2159. int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
  2160. Sym *s, *ss, *ass, **ps;
  2161. AttributeDef ad;
  2162. CType type1, btype;
  2163. a = tok; /* save decl type */
  2164. next();
  2165. if (tok != '{') {
  2166. v = tok;
  2167. next();
  2168. /* struct already defined ? return it */
  2169. if (v < TOK_IDENT)
  2170. expect("struct/union/enum name");
  2171. s = struct_find(v);
  2172. if (s) {
  2173. if (s->type.t != a)
  2174. error("invalid type");
  2175. goto do_decl;
  2176. }
  2177. } else {
  2178. v = anon_sym++;
  2179. }
  2180. type1.t = a;
  2181. /* we put an undefined size for struct/union */
  2182. s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
  2183. s->r = 0; /* default alignment is zero as gcc */
  2184. /* put struct/union/enum name in type */
  2185. do_decl:
  2186. type->t = u;
  2187. type->ref = s;
  2188. if (tok == '{') {
  2189. next();
  2190. if (s->c != -1)
  2191. error("struct/union/enum already defined");
  2192. /* cannot be empty */
  2193. c = 0;
  2194. /* non empty enums are not allowed */
  2195. if (a == TOK_ENUM) {
  2196. for(;;) {
  2197. v = tok;
  2198. if (v < TOK_UIDENT)
  2199. expect("identifier");
  2200. next();
  2201. if (tok == '=') {
  2202. next();
  2203. c = expr_const();
  2204. }
  2205. /* enum symbols have static storage */
  2206. ss = sym_push(v, &int_type, VT_CONST, c);
  2207. ss->type.t |= VT_STATIC;
  2208. if (tok != ',')
  2209. break;
  2210. next();
  2211. c++;
  2212. /* NOTE: we accept a trailing comma */
  2213. if (tok == '}')
  2214. break;
  2215. }
  2216. skip('}');
  2217. } else {
  2218. maxalign = 1;
  2219. ps = &s->next;
  2220. prevbt = VT_INT;
  2221. bit_pos = 0;
  2222. offset = 0;
  2223. while (tok != '}') {
  2224. parse_btype(&btype, &ad);
  2225. while (1) {
  2226. bit_size = -1;
  2227. v = 0;
  2228. type1 = btype;
  2229. if (tok != ':') {
  2230. type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
  2231. if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
  2232. expect("identifier");
  2233. if ((type1.t & VT_BTYPE) == VT_FUNC ||
  2234. (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
  2235. error("invalid type for '%s'",
  2236. get_tok_str(v, NULL));
  2237. }
  2238. if (tok == ':') {
  2239. next();
  2240. bit_size = expr_const();
  2241. /* XXX: handle v = 0 case for messages */
  2242. if (bit_size < 0)
  2243. error("negative width in bit-field '%s'",
  2244. get_tok_str(v, NULL));
  2245. if (v && bit_size == 0)
  2246. error("zero width for bit-field '%s'",
  2247. get_tok_str(v, NULL));
  2248. }
  2249. size = type_size(&type1, &align);
  2250. if (ad.aligned) {
  2251. if (align < ad.aligned)
  2252. align = ad.aligned;
  2253. } else if (ad.packed) {
  2254. align = 1;
  2255. } else if (*tcc_state->pack_stack_ptr) {
  2256. if (align > *tcc_state->pack_stack_ptr)
  2257. align = *tcc_state->pack_stack_ptr;
  2258. }
  2259. lbit_pos = 0;
  2260. if (bit_size >= 0) {
  2261. bt = type1.t & VT_BTYPE;
  2262. if (bt != VT_INT &&
  2263. bt != VT_BYTE &&
  2264. bt != VT_SHORT &&
  2265. bt != VT_BOOL &&
  2266. bt != VT_ENUM &&
  2267. bt != VT_LLONG)
  2268. error("bitfields must have scalar type");
  2269. bsize = size * 8;
  2270. if (bit_size > bsize) {
  2271. error("width of '%s' exceeds its type",
  2272. get_tok_str(v, NULL));
  2273. } else if (bit_size == bsize) {
  2274. /* no need for bit fields */
  2275. bit_pos = 0;
  2276. } else if (bit_size == 0) {
  2277. /* XXX: what to do if only padding in a
  2278. structure ? */
  2279. /* zero size: means to pad */
  2280. bit_pos = 0;
  2281. } else {
  2282. /* we do not have enough room ?
  2283. did the type change?
  2284. is it a union? */
  2285. if ((bit_pos + bit_size) > bsize ||
  2286. bt != prevbt || a == TOK_UNION)
  2287. bit_pos = 0;
  2288. lbit_pos = bit_pos;
  2289. /* XXX: handle LSB first */
  2290. type1.t |= VT_BITFIELD |
  2291. (bit_pos << VT_STRUCT_SHIFT) |
  2292. (bit_size << (VT_STRUCT_SHIFT + 6));
  2293. bit_pos += bit_size;
  2294. }
  2295. prevbt = bt;
  2296. } else {
  2297. bit_pos = 0;
  2298. }
  2299. if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
  2300. /* add new memory data only if starting
  2301. bit field */
  2302. if (lbit_pos == 0) {
  2303. if (a == TOK_STRUCT) {
  2304. c = (c + align - 1) & -align;
  2305. offset = c;
  2306. if (size > 0)
  2307. c += size;
  2308. } else {
  2309. offset = 0;
  2310. if (size > c)
  2311. c = size;
  2312. }
  2313. if (align > maxalign)
  2314. maxalign = align;
  2315. }
  2316. #if 0
  2317. printf("add field %s offset=%d",
  2318. get_tok_str(v, NULL), offset);
  2319. if (type1.t & VT_BITFIELD) {
  2320. printf(" pos=%d size=%d",
  2321. (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
  2322. (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
  2323. }
  2324. printf("\n");
  2325. #endif
  2326. }
  2327. if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
  2328. ass = type1.ref;
  2329. while ((ass = ass->next) != NULL) {
  2330. ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
  2331. *ps = ss;
  2332. ps = &ss->next;
  2333. }
  2334. } else if (v) {
  2335. ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
  2336. *ps = ss;
  2337. ps = &ss->next;
  2338. }
  2339. if (tok == ';' || tok == TOK_EOF)
  2340. break;
  2341. skip(',');
  2342. }
  2343. skip(';');
  2344. }
  2345. skip('}');
  2346. /* store size and alignment */
  2347. s->c = (c + maxalign - 1) & -maxalign;
  2348. s->r = maxalign;
  2349. }
  2350. }
  2351. }
  2352. /* return 0 if no type declaration. otherwise, return the basic type
  2353. and skip it.
  2354. */
  2355. static int parse_btype(CType *type, AttributeDef *ad)
  2356. {
  2357. int t, u, type_found, typespec_found, typedef_found;
  2358. Sym *s;
  2359. CType type1;
  2360. memset(ad, 0, sizeof(AttributeDef));
  2361. type_found = 0;
  2362. typespec_found = 0;
  2363. typedef_found = 0;
  2364. t = 0;
  2365. while(1) {
  2366. switch(tok) {
  2367. case TOK_EXTENSION:
  2368. /* currently, we really ignore extension */
  2369. next();
  2370. continue;
  2371. /* basic types */
  2372. case TOK_CHAR:
  2373. u = VT_BYTE;
  2374. basic_type:
  2375. next();
  2376. basic_type1:
  2377. if ((t & VT_BTYPE) != 0)
  2378. error("too many basic types");
  2379. t |= u;
  2380. typespec_found = 1;
  2381. break;
  2382. case TOK_VOID:
  2383. u = VT_VOID;
  2384. goto basic_type;
  2385. case TOK_SHORT:
  2386. u = VT_SHORT;
  2387. goto basic_type;
  2388. case TOK_INT:
  2389. next();
  2390. typespec_found = 1;
  2391. break;
  2392. case TOK_LONG:
  2393. next();
  2394. if ((t & VT_BTYPE) == VT_DOUBLE) {
  2395. t = (t & ~VT_BTYPE) | VT_LDOUBLE;
  2396. } else if ((t & VT_BTYPE) == VT_LONG) {
  2397. t = (t & ~VT_BTYPE) | VT_LLONG;
  2398. } else {
  2399. u = VT_LONG;
  2400. goto basic_type1;
  2401. }
  2402. break;
  2403. case TOK_BOOL:
  2404. u = VT_BOOL;
  2405. goto basic_type;
  2406. case TOK_FLOAT:
  2407. u = VT_FLOAT;
  2408. goto basic_type;
  2409. case TOK_DOUBLE:
  2410. next();
  2411. if ((t & VT_BTYPE) == VT_LONG) {
  2412. t = (t & ~VT_BTYPE) | VT_LDOUBLE;
  2413. } else {
  2414. u = VT_DOUBLE;
  2415. goto basic_type1;
  2416. }
  2417. break;
  2418. case TOK_ENUM:
  2419. struct_decl(&type1, VT_ENUM);
  2420. basic_type2:
  2421. u = type1.t;
  2422. type->ref = type1.ref;
  2423. goto basic_type1;
  2424. case TOK_STRUCT:
  2425. case TOK_UNION:
  2426. struct_decl(&type1, VT_STRUCT);
  2427. goto basic_type2;
  2428. /* type modifiers */
  2429. case TOK_CONST1:
  2430. case TOK_CONST2:
  2431. case TOK_CONST3:
  2432. t |= VT_CONSTANT;
  2433. next();
  2434. break;
  2435. case TOK_VOLATILE1:
  2436. case TOK_VOLATILE2:
  2437. case TOK_VOLATILE3:
  2438. t |= VT_VOLATILE;
  2439. next();
  2440. break;
  2441. case TOK_SIGNED1:
  2442. case TOK_SIGNED2:
  2443. case TOK_SIGNED3:
  2444. typespec_found = 1;
  2445. t |= VT_SIGNED;
  2446. next();
  2447. break;
  2448. case TOK_REGISTER:
  2449. case TOK_AUTO:
  2450. case TOK_RESTRICT1:
  2451. case TOK_RESTRICT2:
  2452. case TOK_RESTRICT3:
  2453. next();
  2454. break;
  2455. case TOK_UNSIGNED:
  2456. t |= VT_UNSIGNED;
  2457. next();
  2458. typespec_found = 1;
  2459. break;
  2460. /* storage */
  2461. case TOK_EXTERN:
  2462. t |= VT_EXTERN;
  2463. next();
  2464. break;
  2465. case TOK_STATIC:
  2466. t |= VT_STATIC;
  2467. next();
  2468. break;
  2469. case TOK_TYPEDEF:
  2470. t |= VT_TYPEDEF;
  2471. next();
  2472. break;
  2473. case TOK_INLINE1:
  2474. case TOK_INLINE2:
  2475. case TOK_INLINE3:
  2476. t |= VT_INLINE;
  2477. next();
  2478. break;
  2479. /* GNUC attribute */
  2480. case TOK_ATTRIBUTE1:
  2481. case TOK_ATTRIBUTE2:
  2482. parse_attribute(ad);
  2483. break;
  2484. /* GNUC typeof */
  2485. case TOK_TYPEOF1:
  2486. case TOK_TYPEOF2:
  2487. case TOK_TYPEOF3:
  2488. next();
  2489. parse_expr_type(&type1);
  2490. goto basic_type2;
  2491. default:
  2492. if (typespec_found || typedef_found)
  2493. goto the_end;
  2494. s = sym_find(tok);
  2495. if (!s || !(s->type.t & VT_TYPEDEF))
  2496. goto the_end;
  2497. typedef_found = 1;
  2498. t |= (s->type.t & ~VT_TYPEDEF);
  2499. type->ref = s->type.ref;
  2500. next();
  2501. typespec_found = 1;
  2502. break;
  2503. }
  2504. type_found = 1;
  2505. }
  2506. the_end:
  2507. if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
  2508. error("signed and unsigned modifier");
  2509. if (tcc_state->char_is_unsigned) {
  2510. if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
  2511. t |= VT_UNSIGNED;
  2512. }
  2513. t &= ~VT_SIGNED;
  2514. /* long is never used as type */
  2515. if ((t & VT_BTYPE) == VT_LONG)
  2516. #ifndef TCC_TARGET_X86_64
  2517. t = (t & ~VT_BTYPE) | VT_INT;
  2518. #else
  2519. t = (t & ~VT_BTYPE) | VT_LLONG;
  2520. #endif
  2521. type->t = t;
  2522. return type_found;
  2523. }
  2524. /* convert a function parameter type (array to pointer and function to
  2525. function pointer) */
  2526. static inline void convert_parameter_type(CType *pt)
  2527. {
  2528. /* remove const and volatile qualifiers (XXX: const could be used
  2529. to indicate a const function parameter */
  2530. pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
  2531. /* array must be transformed to pointer according to ANSI C */
  2532. pt->t &= ~VT_ARRAY;
  2533. if ((pt->t & VT_BTYPE) == VT_FUNC) {
  2534. mk_pointer(pt);
  2535. }
  2536. }
  2537. static void post_type(CType *type, AttributeDef *ad)
  2538. {
  2539. int n, l, t1, arg_size, align;
  2540. Sym **plast, *s, *first;
  2541. AttributeDef ad1;
  2542. CType pt;
  2543. if (tok == '(') {
  2544. /* function declaration */
  2545. next();
  2546. l = 0;
  2547. first = NULL;
  2548. plast = &first;
  2549. arg_size = 0;
  2550. if (tok != ')') {
  2551. for(;;) {
  2552. /* read param name and compute offset */
  2553. if (l != FUNC_OLD) {
  2554. if (!parse_btype(&pt, &ad1)) {
  2555. if (l) {
  2556. error("invalid type");
  2557. } else {
  2558. l = FUNC_OLD;
  2559. goto old_proto;
  2560. }
  2561. }
  2562. l = FUNC_NEW;
  2563. if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
  2564. break;
  2565. type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
  2566. if ((pt.t & VT_BTYPE) == VT_VOID)
  2567. error("parameter declared as void");
  2568. arg_size += (type_size(&pt, &align) + 3) & ~3;
  2569. } else {
  2570. old_proto:
  2571. n = tok;
  2572. if (n < TOK_UIDENT)
  2573. expect("identifier");
  2574. pt.t = VT_INT;
  2575. next();
  2576. }
  2577. convert_parameter_type(&pt);
  2578. s = sym_push(n | SYM_FIELD, &pt, 0, 0);
  2579. *plast = s;
  2580. plast = &s->next;
  2581. if (tok == ')')
  2582. break;
  2583. skip(',');
  2584. if (l == FUNC_NEW && tok == TOK_DOTS) {
  2585. l = FUNC_ELLIPSIS;
  2586. next();
  2587. break;
  2588. }
  2589. }
  2590. }
  2591. /* if no parameters, then old type prototype */
  2592. if (l == 0)
  2593. l = FUNC_OLD;
  2594. skip(')');
  2595. t1 = type->t & VT_STORAGE;
  2596. /* NOTE: const is ignored in returned type as it has a special
  2597. meaning in gcc / C++ */
  2598. type->t &= ~(VT_STORAGE | VT_CONSTANT);
  2599. post_type(type, ad);
  2600. /* we push a anonymous symbol which will contain the function prototype */
  2601. FUNC_ARGS(ad->func_attr) = arg_size;
  2602. s = sym_push(SYM_FIELD, type, ad->func_attr, l);
  2603. s->next = first;
  2604. type->t = t1 | VT_FUNC;
  2605. type->ref = s;
  2606. } else if (tok == '[') {
  2607. /* array definition */
  2608. next();
  2609. if (tok == TOK_RESTRICT1)
  2610. next();
  2611. n = -1;
  2612. if (tok != ']') {
  2613. n = expr_const();
  2614. if (n < 0)
  2615. error("invalid array size");
  2616. }
  2617. skip(']');
  2618. /* parse next post type */
  2619. t1 = type->t & VT_STORAGE;
  2620. type->t &= ~VT_STORAGE;
  2621. post_type(type, ad);
  2622. /* we push a anonymous symbol which will contain the array
  2623. element type */
  2624. s = sym_push(SYM_FIELD, type, 0, n);
  2625. type->t = t1 | VT_ARRAY | VT_PTR;
  2626. type->ref = s;
  2627. }
  2628. }
  2629. /* Parse a type declaration (except basic type), and return the type
  2630. in 'type'. 'td' is a bitmask indicating which kind of type decl is
  2631. expected. 'type' should contain the basic type. 'ad' is the
  2632. attribute definition of the basic type. It can be modified by
  2633. type_decl().
  2634. */
  2635. static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
  2636. {
  2637. Sym *s;
  2638. CType type1, *type2;
  2639. int qualifiers;
  2640. while (tok == '*') {
  2641. qualifiers = 0;
  2642. redo:
  2643. next();
  2644. switch(tok) {
  2645. case TOK_CONST1:
  2646. case TOK_CONST2:
  2647. case TOK_CONST3:
  2648. qualifiers |= VT_CONSTANT;
  2649. goto redo;
  2650. case TOK_VOLATILE1:
  2651. case TOK_VOLATILE2:
  2652. case TOK_VOLATILE3:
  2653. qualifiers |= VT_VOLATILE;
  2654. goto redo;
  2655. case TOK_RESTRICT1:
  2656. case TOK_RESTRICT2:
  2657. case TOK_RESTRICT3:
  2658. goto redo;
  2659. }
  2660. mk_pointer(type);
  2661. type->t |= qualifiers;
  2662. }
  2663. /* XXX: clarify attribute handling */
  2664. if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
  2665. parse_attribute(ad);
  2666. /* recursive type */
  2667. /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
  2668. type1.t = 0; /* XXX: same as int */
  2669. if (tok == '(') {
  2670. next();
  2671. /* XXX: this is not correct to modify 'ad' at this point, but
  2672. the syntax is not clear */
  2673. if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
  2674. parse_attribute(ad);
  2675. type_decl(&type1, ad, v, td);
  2676. skip(')');
  2677. } else {
  2678. /* type identifier */
  2679. if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
  2680. *v = tok;
  2681. next();
  2682. } else {
  2683. if (!(td & TYPE_ABSTRACT))
  2684. expect("identifier");
  2685. *v = 0;
  2686. }
  2687. }
  2688. post_type(type, ad);
  2689. if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
  2690. parse_attribute(ad);
  2691. if (!type1.t)
  2692. return;
  2693. /* append type at the end of type1 */
  2694. type2 = &type1;
  2695. for(;;) {
  2696. s = type2->ref;
  2697. type2 = &s->type;
  2698. if (!type2->t) {
  2699. *type2 = *type;
  2700. break;
  2701. }
  2702. }
  2703. *type = type1;
  2704. }
  2705. /* compute the lvalue VT_LVAL_xxx needed to match type t. */
  2706. static int lvalue_type(int t)
  2707. {
  2708. int bt, r;
  2709. r = VT_LVAL;
  2710. bt = t & VT_BTYPE;
  2711. if (bt == VT_BYTE || bt == VT_BOOL)
  2712. r |= VT_LVAL_BYTE;
  2713. else if (bt == VT_SHORT)
  2714. r |= VT_LVAL_SHORT;
  2715. else
  2716. return r;
  2717. if (t & VT_UNSIGNED)
  2718. r |= VT_LVAL_UNSIGNED;
  2719. return r;
  2720. }
  2721. /* indirection with full error checking and bound check */
  2722. static void indir(void)
  2723. {
  2724. if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
  2725. if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
  2726. return;
  2727. expect("pointer");
  2728. }
  2729. if ((vtop->r & VT_LVAL) && !nocode_wanted)
  2730. gv(RC_INT);
  2731. vtop->type = *pointed_type(&vtop->type);
  2732. /* Arrays and functions are never lvalues */
  2733. if (!(vtop->type.t & VT_ARRAY)
  2734. && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
  2735. vtop->r |= lvalue_type(vtop->type.t);
  2736. /* if bound checking, the referenced pointer must be checked */
  2737. if (tcc_state->do_bounds_check)
  2738. vtop->r |= VT_MUSTBOUND;
  2739. }
  2740. }
  2741. /* pass a parameter to a function and do type checking and casting */
  2742. static void gfunc_param_typed(Sym *func, Sym *arg)
  2743. {
  2744. int func_type;
  2745. CType type;
  2746. func_type = func->c;
  2747. if (func_type == FUNC_OLD ||
  2748. (func_type == FUNC_ELLIPSIS && arg == NULL)) {
  2749. /* default casting : only need to convert float to double */
  2750. if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
  2751. type.t = VT_DOUBLE;
  2752. gen_cast(&type);
  2753. }
  2754. } else if (arg == NULL) {
  2755. error("too many arguments to function");
  2756. } else {
  2757. type = arg->type;
  2758. type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
  2759. gen_assign_cast(&type);
  2760. }
  2761. }
  2762. /* parse an expression of the form '(type)' or '(expr)' and return its
  2763. type */
  2764. static void parse_expr_type(CType *type)
  2765. {
  2766. int n;
  2767. AttributeDef ad;
  2768. skip('(');
  2769. if (parse_btype(type, &ad)) {
  2770. type_decl(type, &ad, &n, TYPE_ABSTRACT);
  2771. } else {
  2772. expr_type(type);
  2773. }
  2774. skip(')');
  2775. }
  2776. static void parse_type(CType *type)
  2777. {
  2778. AttributeDef ad;
  2779. int n;
  2780. if (!parse_btype(type, &ad)) {
  2781. expect("type");
  2782. }
  2783. type_decl(type, &ad, &n, TYPE_ABSTRACT);
  2784. }
  2785. static void vpush_tokc(int t)
  2786. {
  2787. CType type;
  2788. type.t = t;
  2789. vsetc(&type, VT_CONST, &tokc);
  2790. }
  2791. static void unary(void)
  2792. {
  2793. int n, t, align, size, r;
  2794. CType type;
  2795. Sym *s;
  2796. AttributeDef ad;
  2797. /* XXX: GCC 2.95.3 does not generate a table although it should be
  2798. better here */
  2799. tok_next:
  2800. switch(tok) {
  2801. case TOK_EXTENSION:
  2802. next();
  2803. goto tok_next;
  2804. case TOK_CINT:
  2805. case TOK_CCHAR:
  2806. case TOK_LCHAR:
  2807. vpushi(tokc.i);
  2808. next();
  2809. break;
  2810. case TOK_CUINT:
  2811. vpush_tokc(VT_INT | VT_UNSIGNED);
  2812. next();
  2813. break;
  2814. case TOK_CLLONG:
  2815. vpush_tokc(VT_LLONG);
  2816. next();
  2817. break;
  2818. case TOK_CULLONG:
  2819. vpush_tokc(VT_LLONG | VT_UNSIGNED);
  2820. next();
  2821. break;
  2822. case TOK_CFLOAT:
  2823. vpush_tokc(VT_FLOAT);
  2824. next();
  2825. break;
  2826. case TOK_CDOUBLE:
  2827. vpush_tokc(VT_DOUBLE);
  2828. next();
  2829. break;
  2830. case TOK_CLDOUBLE:
  2831. vpush_tokc(VT_LDOUBLE);
  2832. next();
  2833. break;
  2834. case TOK___FUNCTION__:
  2835. if (!gnu_ext)
  2836. goto tok_identifier;
  2837. /* fall thru */
  2838. case TOK___FUNC__:
  2839. {
  2840. void *ptr;
  2841. int len;
  2842. /* special function name identifier */
  2843. len = strlen(funcname) + 1;
  2844. /* generate char[len] type */
  2845. type.t = VT_BYTE;
  2846. mk_pointer(&type);
  2847. type.t |= VT_ARRAY;
  2848. type.ref->c = len;
  2849. vpush_ref(&type, data_section, data_section->data_offset, len);
  2850. ptr = section_ptr_add(data_section, len);
  2851. memcpy(ptr, funcname, len);
  2852. next();
  2853. }
  2854. break;
  2855. case TOK_LSTR:
  2856. #ifdef TCC_TARGET_PE
  2857. t = VT_SHORT | VT_UNSIGNED;
  2858. #else
  2859. t = VT_INT;
  2860. #endif
  2861. goto str_init;
  2862. case TOK_STR:
  2863. /* string parsing */
  2864. t = VT_BYTE;
  2865. str_init:
  2866. if (tcc_state->warn_write_strings)
  2867. t |= VT_CONSTANT;
  2868. type.t = t;
  2869. mk_pointer(&type);
  2870. type.t |= VT_ARRAY;
  2871. memset(&ad, 0, sizeof(AttributeDef));
  2872. decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
  2873. break;
  2874. case '(':
  2875. next();
  2876. /* cast ? */
  2877. if (parse_btype(&type, &ad)) {
  2878. type_decl(&type, &ad, &n, TYPE_ABSTRACT);
  2879. skip(')');
  2880. /* check ISOC99 compound literal */
  2881. if (tok == '{') {
  2882. /* data is allocated locally by default */
  2883. if (global_expr)
  2884. r = VT_CONST;
  2885. else
  2886. r = VT_LOCAL;
  2887. /* all except arrays are lvalues */
  2888. if (!(type.t & VT_ARRAY))
  2889. r |= lvalue_type(type.t);
  2890. memset(&ad, 0, sizeof(AttributeDef));
  2891. decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
  2892. } else {
  2893. unary();
  2894. gen_cast(&type);
  2895. }
  2896. } else if (tok == '{') {
  2897. /* save all registers */
  2898. save_regs(0);
  2899. /* statement expression : we do not accept break/continue
  2900. inside as GCC does */
  2901. block(NULL, NULL, NULL, NULL, 0, 1);
  2902. skip(')');
  2903. } else {
  2904. gexpr();
  2905. skip(')');
  2906. }
  2907. break;
  2908. case '*':
  2909. next();
  2910. unary();
  2911. indir();
  2912. break;
  2913. case '&':
  2914. next();
  2915. unary();
  2916. /* functions names must be treated as function pointers,
  2917. except for unary '&' and sizeof. Since we consider that
  2918. functions are not lvalues, we only have to handle it
  2919. there and in function calls. */
  2920. /* arrays can also be used although they are not lvalues */
  2921. if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
  2922. !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
  2923. test_lvalue();
  2924. mk_pointer(&vtop->type);
  2925. gaddrof();
  2926. break;
  2927. case '!':
  2928. next();
  2929. unary();
  2930. if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
  2931. CType boolean;
  2932. boolean.t = VT_BOOL;
  2933. gen_cast(&boolean);
  2934. vtop->c.i = !vtop->c.i;
  2935. } else if ((vtop->r & VT_VALMASK) == VT_CMP)
  2936. vtop->c.i = vtop->c.i ^ 1;
  2937. else {
  2938. save_regs(1);
  2939. vseti(VT_JMP, gtst(1, 0));
  2940. }
  2941. break;
  2942. case '~':
  2943. next();
  2944. unary();
  2945. vpushi(-1);
  2946. gen_op('^');
  2947. break;
  2948. case '+':
  2949. next();
  2950. /* in order to force cast, we add zero */
  2951. unary();
  2952. if ((vtop->type.t & VT_BTYPE) == VT_PTR)
  2953. error("pointer not accepted for unary plus");
  2954. vpushi(0);
  2955. gen_op('+');
  2956. break;
  2957. case TOK_SIZEOF:
  2958. case TOK_ALIGNOF1:
  2959. case TOK_ALIGNOF2:
  2960. t = tok;
  2961. next();
  2962. if (tok == '(') {
  2963. parse_expr_type(&type);
  2964. } else {
  2965. unary_type(&type);
  2966. }
  2967. size = type_size(&type, &align);
  2968. if (t == TOK_SIZEOF) {
  2969. if (size < 0)
  2970. error("sizeof applied to an incomplete type");
  2971. vpushi(size);
  2972. } else {
  2973. vpushi(align);
  2974. }
  2975. vtop->type.t |= VT_UNSIGNED;
  2976. break;
  2977. case TOK_builtin_types_compatible_p:
  2978. {
  2979. CType type1, type2;
  2980. next();
  2981. skip('(');
  2982. parse_type(&type1);
  2983. skip(',');
  2984. parse_type(&type2);
  2985. skip(')');
  2986. type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
  2987. type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
  2988. vpushi(is_compatible_types(&type1, &type2));
  2989. }
  2990. break;
  2991. case TOK_builtin_constant_p:
  2992. {
  2993. int saved_nocode_wanted, res;
  2994. next();
  2995. skip('(');
  2996. saved_nocode_wanted = nocode_wanted;
  2997. nocode_wanted = 1;
  2998. gexpr();
  2999. res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
  3000. vpop();
  3001. nocode_wanted = saved_nocode_wanted;
  3002. skip(')');
  3003. vpushi(res);
  3004. }
  3005. break;
  3006. case TOK_builtin_frame_address:
  3007. {
  3008. CType type;
  3009. next();
  3010. skip('(');
  3011. if (tok != TOK_CINT) {
  3012. error("__builtin_frame_address only takes integers");
  3013. }
  3014. if (tokc.i != 0) {
  3015. error("TCC only supports __builtin_frame_address(0)");
  3016. }
  3017. next();
  3018. skip(')');
  3019. type.t = VT_VOID;
  3020. mk_pointer(&type);
  3021. vset(&type, VT_LOCAL, 0);
  3022. }
  3023. break;
  3024. #ifdef TCC_TARGET_X86_64
  3025. case TOK_builtin_malloc:
  3026. tok = TOK_malloc;
  3027. goto tok_identifier;
  3028. case TOK_builtin_free:
  3029. tok = TOK_free;
  3030. goto tok_identifier;
  3031. #endif
  3032. case TOK_INC:
  3033. case TOK_DEC:
  3034. t = tok;
  3035. next();
  3036. unary();
  3037. inc(0, t);
  3038. break;
  3039. case '-':
  3040. next();
  3041. vpushi(0);
  3042. unary();
  3043. gen_op('-');
  3044. break;
  3045. case TOK_LAND:
  3046. if (!gnu_ext)
  3047. goto tok_identifier;
  3048. next();
  3049. /* allow to take the address of a label */
  3050. if (tok < TOK_UIDENT)
  3051. expect("label identifier");
  3052. s = label_find(tok);
  3053. if (!s) {
  3054. s = label_push(&global_label_stack, tok, LABEL_FORWARD);
  3055. } else {
  3056. if (s->r == LABEL_DECLARED)
  3057. s->r = LABEL_FORWARD;
  3058. }
  3059. if (!s->type.t) {
  3060. s->type.t = VT_VOID;
  3061. mk_pointer(&s->type);
  3062. s->type.t |= VT_STATIC;
  3063. }
  3064. vset(&s->type, VT_CONST | VT_SYM, 0);
  3065. vtop->sym = s;
  3066. next();
  3067. break;
  3068. default:
  3069. tok_identifier:
  3070. t = tok;
  3071. next();
  3072. if (t < TOK_UIDENT)
  3073. expect("identifier");
  3074. s = sym_find(t);
  3075. if (!s) {
  3076. if (tok != '(')
  3077. error("'%s' undeclared", get_tok_str(t, NULL));
  3078. /* for simple function calls, we tolerate undeclared
  3079. external reference to int() function */
  3080. if (tcc_state->warn_implicit_function_declaration)
  3081. warning("implicit declaration of function '%s'",
  3082. get_tok_str(t, NULL));
  3083. s = external_global_sym(t, &func_old_type, 0);
  3084. }
  3085. if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
  3086. (VT_STATIC | VT_INLINE | VT_FUNC)) {
  3087. /* if referencing an inline function, then we generate a
  3088. symbol to it if not already done. It will have the
  3089. effect to generate code for it at the end of the
  3090. compilation unit. Inline function as always
  3091. generated in the text section. */
  3092. if (!s->c)
  3093. put_extern_sym(s, text_section, 0, 0);
  3094. r = VT_SYM | VT_CONST;
  3095. } else {
  3096. r = s->r;
  3097. }
  3098. vset(&s->type, r, s->c);
  3099. /* if forward reference, we must point to s */
  3100. if (vtop->r & VT_SYM) {
  3101. vtop->sym = s;
  3102. vtop->c.ul = 0;
  3103. }
  3104. break;
  3105. }
  3106. /* post operations */
  3107. while (1) {
  3108. if (tok == TOK_INC || tok == TOK_DEC) {
  3109. inc(1, tok);
  3110. next();
  3111. } else if (tok == '.' || tok == TOK_ARROW) {
  3112. /* field */
  3113. if (tok == TOK_ARROW)
  3114. indir();
  3115. test_lvalue();
  3116. gaddrof();
  3117. next();
  3118. /* expect pointer on structure */
  3119. if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
  3120. expect("struct or union");
  3121. s = vtop->type.ref;
  3122. /* find field */
  3123. tok |= SYM_FIELD;
  3124. while ((s = s->next) != NULL) {
  3125. if (s->v == tok)
  3126. break;
  3127. }
  3128. if (!s)
  3129. error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
  3130. /* add field offset to pointer */
  3131. vtop->type = char_pointer_type; /* change type to 'char *' */
  3132. vpushi(s->c);
  3133. gen_op('+');
  3134. /* change type to field type, and set to lvalue */
  3135. vtop->type = s->type;
  3136. /* an array is never an lvalue */
  3137. if (!(vtop->type.t & VT_ARRAY)) {
  3138. vtop->r |= lvalue_type(vtop->type.t);
  3139. /* if bound checking, the referenced pointer must be checked */
  3140. if (tcc_state->do_bounds_check)
  3141. vtop->r |= VT_MUSTBOUND;
  3142. }
  3143. next();
  3144. } else if (tok == '[') {
  3145. next();
  3146. gexpr();
  3147. gen_op('+');
  3148. indir();
  3149. skip(']');
  3150. } else if (tok == '(') {
  3151. SValue ret;
  3152. Sym *sa;
  3153. int nb_args;
  3154. /* function call */
  3155. if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
  3156. /* pointer test (no array accepted) */
  3157. if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
  3158. vtop->type = *pointed_type(&vtop->type);
  3159. if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
  3160. goto error_func;
  3161. } else {
  3162. error_func:
  3163. expect("function pointer");
  3164. }
  3165. } else {
  3166. vtop->r &= ~VT_LVAL; /* no lvalue */
  3167. }
  3168. /* get return type */
  3169. s = vtop->type.ref;
  3170. next();
  3171. sa = s->next; /* first parameter */
  3172. nb_args = 0;
  3173. ret.r2 = VT_CONST;
  3174. /* compute first implicit argument if a structure is returned */
  3175. if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
  3176. /* get some space for the returned structure */
  3177. size = type_size(&s->type, &align);
  3178. loc = (loc - size) & -align;
  3179. ret.type = s->type;
  3180. ret.r = VT_LOCAL | VT_LVAL;
  3181. /* pass it as 'int' to avoid structure arg passing
  3182. problems */
  3183. vseti(VT_LOCAL, loc);
  3184. ret.c = vtop->c;
  3185. nb_args++;
  3186. } else {
  3187. ret.type = s->type;
  3188. /* return in register */
  3189. if (is_float(ret.type.t)) {
  3190. ret.r = reg_fret(ret.type.t);
  3191. } else {
  3192. if ((ret.type.t & VT_BTYPE) == VT_LLONG)
  3193. ret.r2 = REG_LRET;
  3194. ret.r = REG_IRET;
  3195. }
  3196. ret.c.i = 0;
  3197. }
  3198. if (tok != ')') {
  3199. for(;;) {
  3200. expr_eq();
  3201. gfunc_param_typed(s, sa);
  3202. nb_args++;
  3203. if (sa)
  3204. sa = sa->next;
  3205. if (tok == ')')
  3206. break;
  3207. skip(',');
  3208. }
  3209. }
  3210. if (sa)
  3211. error("too few arguments to function");
  3212. skip(')');
  3213. if (!nocode_wanted) {
  3214. gfunc_call(nb_args);
  3215. } else {
  3216. vtop -= (nb_args + 1);
  3217. }
  3218. /* return value */
  3219. vsetc(&ret.type, ret.r, &ret.c);
  3220. vtop->r2 = ret.r2;
  3221. } else {
  3222. break;
  3223. }
  3224. }
  3225. }
  3226. static void uneq(void)
  3227. {
  3228. int t;
  3229. unary();
  3230. if (tok == '=' ||
  3231. (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
  3232. tok == TOK_A_XOR || tok == TOK_A_OR ||
  3233. tok == TOK_A_SHL || tok == TOK_A_SAR) {
  3234. test_lvalue();
  3235. t = tok;
  3236. next();
  3237. if (t == '=') {
  3238. expr_eq();
  3239. } else {
  3240. vdup();
  3241. expr_eq();
  3242. gen_op(t & 0x7f);
  3243. }
  3244. vstore();
  3245. }
  3246. }
  3247. static void expr_prod(void)
  3248. {
  3249. int t;
  3250. uneq();
  3251. while (tok == '*' || tok == '/' || tok == '%') {
  3252. t = tok;
  3253. next();
  3254. uneq();
  3255. gen_op(t);
  3256. }
  3257. }
  3258. static void expr_sum(void)
  3259. {
  3260. int t;
  3261. expr_prod();
  3262. while (tok == '+' || tok == '-') {
  3263. t = tok;
  3264. next();
  3265. expr_prod();
  3266. gen_op(t);
  3267. }
  3268. }
  3269. static void expr_shift(void)
  3270. {
  3271. int t;
  3272. expr_sum();
  3273. while (tok == TOK_SHL || tok == TOK_SAR) {
  3274. t = tok;
  3275. next();
  3276. expr_sum();
  3277. gen_op(t);
  3278. }
  3279. }
  3280. static void expr_cmp(void)
  3281. {
  3282. int t;
  3283. expr_shift();
  3284. while ((tok >= TOK_ULE && tok <= TOK_GT) ||
  3285. tok == TOK_ULT || tok == TOK_UGE) {
  3286. t = tok;
  3287. next();
  3288. expr_shift();
  3289. gen_op(t);
  3290. }
  3291. }
  3292. static void expr_cmpeq(void)
  3293. {
  3294. int t;
  3295. expr_cmp();
  3296. while (tok == TOK_EQ || tok == TOK_NE) {
  3297. t = tok;
  3298. next();
  3299. expr_cmp();
  3300. gen_op(t);
  3301. }
  3302. }
  3303. static void expr_and(void)
  3304. {
  3305. expr_cmpeq();
  3306. while (tok == '&') {
  3307. next();
  3308. expr_cmpeq();
  3309. gen_op('&');
  3310. }
  3311. }
  3312. static void expr_xor(void)
  3313. {
  3314. expr_and();
  3315. while (tok == '^') {
  3316. next();
  3317. expr_and();
  3318. gen_op('^');
  3319. }
  3320. }
  3321. static void expr_or(void)
  3322. {
  3323. expr_xor();
  3324. while (tok == '|') {
  3325. next();
  3326. expr_xor();
  3327. gen_op('|');
  3328. }
  3329. }
  3330. /* XXX: fix this mess */
  3331. static void expr_land_const(void)
  3332. {
  3333. expr_or();
  3334. while (tok == TOK_LAND) {
  3335. next();
  3336. expr_or();
  3337. gen_op(TOK_LAND);
  3338. }
  3339. }
  3340. /* XXX: fix this mess */
  3341. static void expr_lor_const(void)
  3342. {
  3343. expr_land_const();
  3344. while (tok == TOK_LOR) {
  3345. next();
  3346. expr_land_const();
  3347. gen_op(TOK_LOR);
  3348. }
  3349. }
  3350. /* only used if non constant */
  3351. static void expr_land(void)
  3352. {
  3353. int t;
  3354. expr_or();
  3355. if (tok == TOK_LAND) {
  3356. t = 0;
  3357. save_regs(1);
  3358. for(;;) {
  3359. t = gtst(1, t);
  3360. if (tok != TOK_LAND) {
  3361. vseti(VT_JMPI, t);
  3362. break;
  3363. }
  3364. next();
  3365. expr_or();
  3366. }
  3367. }
  3368. }
  3369. static void expr_lor(void)
  3370. {
  3371. int t;
  3372. expr_land();
  3373. if (tok == TOK_LOR) {
  3374. t = 0;
  3375. save_regs(1);
  3376. for(;;) {
  3377. t = gtst(0, t);
  3378. if (tok != TOK_LOR) {
  3379. vseti(VT_JMP, t);
  3380. break;
  3381. }
  3382. next();
  3383. expr_land();
  3384. }
  3385. }
  3386. }
  3387. /* XXX: better constant handling */
  3388. static void expr_eq(void)
  3389. {
  3390. int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
  3391. SValue sv;
  3392. CType type, type1, type2;
  3393. if (const_wanted) {
  3394. expr_lor_const();
  3395. if (tok == '?') {
  3396. CType boolean;
  3397. int c;
  3398. boolean.t = VT_BOOL;
  3399. vdup();
  3400. gen_cast(&boolean);
  3401. c = vtop->c.i;
  3402. vpop();
  3403. next();
  3404. if (tok != ':' || !gnu_ext) {
  3405. vpop();
  3406. gexpr();
  3407. }
  3408. if (!c)
  3409. vpop();
  3410. skip(':');
  3411. expr_eq();
  3412. if (c)
  3413. vpop();
  3414. }
  3415. } else {
  3416. expr_lor();
  3417. if (tok == '?') {
  3418. next();
  3419. if (vtop != vstack) {
  3420. /* needed to avoid having different registers saved in
  3421. each branch */
  3422. if (is_float(vtop->type.t)) {
  3423. rc = RC_FLOAT;
  3424. #ifdef TCC_TARGET_X86_64
  3425. if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
  3426. rc = RC_ST0;
  3427. }
  3428. #endif
  3429. }
  3430. else
  3431. rc = RC_INT;
  3432. gv(rc);
  3433. save_regs(1);
  3434. }
  3435. if (tok == ':' && gnu_ext) {
  3436. gv_dup();
  3437. tt = gtst(1, 0);
  3438. } else {
  3439. tt = gtst(1, 0);
  3440. gexpr();
  3441. }
  3442. type1 = vtop->type;
  3443. sv = *vtop; /* save value to handle it later */
  3444. vtop--; /* no vpop so that FP stack is not flushed */
  3445. skip(':');
  3446. u = gjmp(0);
  3447. gsym(tt);
  3448. expr_eq();
  3449. type2 = vtop->type;
  3450. t1 = type1.t;
  3451. bt1 = t1 & VT_BTYPE;
  3452. t2 = type2.t;
  3453. bt2 = t2 & VT_BTYPE;
  3454. /* cast operands to correct type according to ISOC rules */
  3455. if (is_float(bt1) || is_float(bt2)) {
  3456. if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
  3457. type.t = VT_LDOUBLE;
  3458. } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
  3459. type.t = VT_DOUBLE;
  3460. } else {
  3461. type.t = VT_FLOAT;
  3462. }
  3463. } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
  3464. /* cast to biggest op */
  3465. type.t = VT_LLONG;
  3466. /* convert to unsigned if it does not fit in a long long */
  3467. if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
  3468. (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
  3469. type.t |= VT_UNSIGNED;
  3470. } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
  3471. /* XXX: test pointer compatibility */
  3472. type = type1;
  3473. } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
  3474. /* XXX: test function pointer compatibility */
  3475. type = type1;
  3476. } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
  3477. /* XXX: test structure compatibility */
  3478. type = type1;
  3479. } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
  3480. /* NOTE: as an extension, we accept void on only one side */
  3481. type.t = VT_VOID;
  3482. } else {
  3483. /* integer operations */
  3484. type.t = VT_INT;
  3485. /* convert to unsigned if it does not fit in an integer */
  3486. if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
  3487. (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
  3488. type.t |= VT_UNSIGNED;
  3489. }
  3490. /* now we convert second operand */
  3491. gen_cast(&type);
  3492. if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
  3493. gaddrof();
  3494. rc = RC_INT;
  3495. if (is_float(type.t)) {
  3496. rc = RC_FLOAT;
  3497. #ifdef TCC_TARGET_X86_64
  3498. if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
  3499. rc = RC_ST0;
  3500. }
  3501. #endif
  3502. } else if ((type.t & VT_BTYPE) == VT_LLONG) {
  3503. /* for long longs, we use fixed registers to avoid having
  3504. to handle a complicated move */
  3505. rc = RC_IRET;
  3506. }
  3507. r2 = gv(rc);
  3508. /* this is horrible, but we must also convert first
  3509. operand */
  3510. tt = gjmp(0);
  3511. gsym(u);
  3512. /* put again first value and cast it */
  3513. *vtop = sv;
  3514. gen_cast(&type);
  3515. if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
  3516. gaddrof();
  3517. r1 = gv(rc);
  3518. move_reg(r2, r1);
  3519. vtop->r = r2;
  3520. gsym(tt);
  3521. }
  3522. }
  3523. }
  3524. static void gexpr(void)
  3525. {
  3526. while (1) {
  3527. expr_eq();
  3528. if (tok != ',')
  3529. break;
  3530. vpop();
  3531. next();
  3532. }
  3533. }
  3534. /* parse an expression and return its type without any side effect. */
  3535. static void expr_type(CType *type)
  3536. {
  3537. int saved_nocode_wanted;
  3538. saved_nocode_wanted = nocode_wanted;
  3539. nocode_wanted = 1;
  3540. gexpr();
  3541. *type = vtop->type;
  3542. vpop();
  3543. nocode_wanted = saved_nocode_wanted;
  3544. }
  3545. /* parse a unary expression and return its type without any side
  3546. effect. */
  3547. static void unary_type(CType *type)
  3548. {
  3549. int a;
  3550. a = nocode_wanted;
  3551. nocode_wanted = 1;
  3552. unary();
  3553. *type = vtop->type;
  3554. vpop();
  3555. nocode_wanted = a;
  3556. }
  3557. /* parse a constant expression and return value in vtop. */
  3558. static void expr_const1(void)
  3559. {
  3560. int a;
  3561. a = const_wanted;
  3562. const_wanted = 1;
  3563. expr_eq();
  3564. const_wanted = a;
  3565. }
  3566. /* parse an integer constant and return its value. */
  3567. static int expr_const(void)
  3568. {
  3569. int c;
  3570. expr_const1();
  3571. if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
  3572. expect("constant expression");
  3573. c = vtop->c.i;
  3574. vpop();
  3575. return c;
  3576. }
  3577. /* return the label token if current token is a label, otherwise
  3578. return zero */
  3579. static int is_label(void)
  3580. {
  3581. int last_tok;
  3582. /* fast test first */
  3583. if (tok < TOK_UIDENT)
  3584. return 0;
  3585. /* no need to save tokc because tok is an identifier */
  3586. last_tok = tok;
  3587. next();
  3588. if (tok == ':') {
  3589. next();
  3590. return last_tok;
  3591. } else {
  3592. unget_tok(last_tok);
  3593. return 0;
  3594. }
  3595. }
  3596. static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
  3597. int case_reg, int is_expr)
  3598. {
  3599. int a, b, c, d;
  3600. Sym *s;
  3601. /* generate line number info */
  3602. if (tcc_state->do_debug &&
  3603. (last_line_num != file->line_num || last_ind != ind)) {
  3604. put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
  3605. last_ind = ind;
  3606. last_line_num = file->line_num;
  3607. }
  3608. if (is_expr) {
  3609. /* default return value is (void) */
  3610. vpushi(0);
  3611. vtop->type.t = VT_VOID;
  3612. }
  3613. if (tok == TOK_IF) {
  3614. /* if test */
  3615. next();
  3616. skip('(');
  3617. gexpr();
  3618. skip(')');
  3619. a = gtst(1, 0);
  3620. block(bsym, csym, case_sym, def_sym, case_reg, 0);
  3621. c = tok;
  3622. if (c == TOK_ELSE) {
  3623. next();
  3624. d = gjmp(0);
  3625. gsym(a);
  3626. block(bsym, csym, case_sym, def_sym, case_reg, 0);
  3627. gsym(d); /* patch else jmp */
  3628. } else
  3629. gsym(a);
  3630. } else if (tok == TOK_WHILE) {
  3631. next();
  3632. d = ind;
  3633. skip('(');
  3634. gexpr();
  3635. skip(')');
  3636. a = gtst(1, 0);
  3637. b = 0;
  3638. block(&a, &b, case_sym, def_sym, case_reg, 0);
  3639. gjmp_addr(d);
  3640. gsym(a);
  3641. gsym_addr(b, d);
  3642. } else if (tok == '{') {
  3643. Sym *llabel;
  3644. next();
  3645. /* record local declaration stack position */
  3646. s = local_stack;
  3647. llabel = local_label_stack;
  3648. /* handle local labels declarations */
  3649. if (tok == TOK_LABEL) {
  3650. next();
  3651. for(;;) {
  3652. if (tok < TOK_UIDENT)
  3653. expect("label identifier");
  3654. label_push(&local_label_stack, tok, LABEL_DECLARED);
  3655. next();
  3656. if (tok == ',') {
  3657. next();
  3658. } else {
  3659. skip(';');
  3660. break;
  3661. }
  3662. }
  3663. }
  3664. while (tok != '}') {
  3665. decl(VT_LOCAL);
  3666. if (tok != '}') {
  3667. if (is_expr)
  3668. vpop();
  3669. block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
  3670. }
  3671. }
  3672. /* pop locally defined labels */
  3673. label_pop(&local_label_stack, llabel);
  3674. /* pop locally defined symbols */
  3675. if(is_expr) {
  3676. /* XXX: this solution makes only valgrind happy...
  3677. triggered by gcc.c-torture/execute/20000917-1.c */
  3678. Sym *p;
  3679. switch(vtop->type.t & VT_BTYPE) {
  3680. case VT_PTR:
  3681. case VT_STRUCT:
  3682. case VT_ENUM:
  3683. case VT_FUNC:
  3684. for(p=vtop->type.ref;p;p=p->prev)
  3685. if(p->prev==s)
  3686. error("unsupported expression type");
  3687. }
  3688. }
  3689. sym_pop(&local_stack, s);
  3690. next();
  3691. } else if (tok == TOK_RETURN) {
  3692. next();
  3693. if (tok != ';') {
  3694. gexpr();
  3695. gen_assign_cast(&func_vt);
  3696. if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
  3697. CType type;
  3698. /* if returning structure, must copy it to implicit
  3699. first pointer arg location */
  3700. #ifdef TCC_ARM_EABI
  3701. int align, size;
  3702. size = type_size(&func_vt,&align);
  3703. if(size <= 4)
  3704. {
  3705. if((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & 3))
  3706. && (align & 3))
  3707. {
  3708. int addr;
  3709. loc = (loc - size) & -4;
  3710. addr = loc;
  3711. type = func_vt;
  3712. vset(&type, VT_LOCAL | VT_LVAL, addr);
  3713. vswap();
  3714. vstore();
  3715. vset(&int_type, VT_LOCAL | VT_LVAL, addr);
  3716. }
  3717. vtop->type = int_type;
  3718. gv(RC_IRET);
  3719. } else {
  3720. #endif
  3721. type = func_vt;
  3722. mk_pointer(&type);
  3723. vset(&type, VT_LOCAL | VT_LVAL, func_vc);
  3724. indir();
  3725. vswap();
  3726. /* copy structure value to pointer */
  3727. vstore();
  3728. #ifdef TCC_ARM_EABI
  3729. }
  3730. #endif
  3731. } else if (is_float(func_vt.t)) {
  3732. gv(rc_fret(func_vt.t));
  3733. } else {
  3734. gv(RC_IRET);
  3735. }
  3736. vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
  3737. }
  3738. skip(';');
  3739. rsym = gjmp(rsym); /* jmp */
  3740. } else if (tok == TOK_BREAK) {
  3741. /* compute jump */
  3742. if (!bsym)
  3743. error("cannot break");
  3744. *bsym = gjmp(*bsym);
  3745. next();
  3746. skip(';');
  3747. } else if (tok == TOK_CONTINUE) {
  3748. /* compute jump */
  3749. if (!csym)
  3750. error("cannot continue");
  3751. *csym = gjmp(*csym);
  3752. next();
  3753. skip(';');
  3754. } else if (tok == TOK_FOR) {
  3755. int e;
  3756. next();
  3757. skip('(');
  3758. if (tok != ';') {
  3759. gexpr();
  3760. vpop();
  3761. }
  3762. skip(';');
  3763. d = ind;
  3764. c = ind;
  3765. a = 0;
  3766. b = 0;
  3767. if (tok != ';') {
  3768. gexpr();
  3769. a = gtst(1, 0);
  3770. }
  3771. skip(';');
  3772. if (tok != ')') {
  3773. e = gjmp(0);
  3774. c = ind;
  3775. gexpr();
  3776. vpop();
  3777. gjmp_addr(d);
  3778. gsym(e);
  3779. }
  3780. skip(')');
  3781. block(&a, &b, case_sym, def_sym, case_reg, 0);
  3782. gjmp_addr(c);
  3783. gsym(a);
  3784. gsym_addr(b, c);
  3785. } else
  3786. if (tok == TOK_DO) {
  3787. next();
  3788. a = 0;
  3789. b = 0;
  3790. d = ind;
  3791. block(&a, &b, case_sym, def_sym, case_reg, 0);
  3792. skip(TOK_WHILE);
  3793. skip('(');
  3794. gsym(b);
  3795. gexpr();
  3796. c = gtst(0, 0);
  3797. gsym_addr(c, d);
  3798. skip(')');
  3799. gsym(a);
  3800. skip(';');
  3801. } else
  3802. if (tok == TOK_SWITCH) {
  3803. next();
  3804. skip('(');
  3805. gexpr();
  3806. /* XXX: other types than integer */
  3807. case_reg = gv(RC_INT);
  3808. vpop();
  3809. skip(')');
  3810. a = 0;
  3811. b = gjmp(0); /* jump to first case */
  3812. c = 0;
  3813. block(&a, csym, &b, &c, case_reg, 0);
  3814. /* if no default, jmp after switch */
  3815. if (c == 0)
  3816. c = ind;
  3817. /* default label */
  3818. gsym_addr(b, c);
  3819. /* break label */
  3820. gsym(a);
  3821. } else
  3822. if (tok == TOK_CASE) {
  3823. int v1, v2;
  3824. if (!case_sym)
  3825. expect("switch");
  3826. next();
  3827. v1 = expr_const();
  3828. v2 = v1;
  3829. if (gnu_ext && tok == TOK_DOTS) {
  3830. next();
  3831. v2 = expr_const();
  3832. if (v2 < v1)
  3833. warning("empty case range");
  3834. }
  3835. /* since a case is like a label, we must skip it with a jmp */
  3836. b = gjmp(0);
  3837. gsym(*case_sym);
  3838. vseti(case_reg, 0);
  3839. vpushi(v1);
  3840. if (v1 == v2) {
  3841. gen_op(TOK_EQ);
  3842. *case_sym = gtst(1, 0);
  3843. } else {
  3844. gen_op(TOK_GE);
  3845. *case_sym = gtst(1, 0);
  3846. vseti(case_reg, 0);
  3847. vpushi(v2);
  3848. gen_op(TOK_LE);
  3849. *case_sym = gtst(1, *case_sym);
  3850. }
  3851. gsym(b);
  3852. skip(':');
  3853. is_expr = 0;
  3854. goto block_after_label;
  3855. } else
  3856. if (tok == TOK_DEFAULT) {
  3857. next();
  3858. skip(':');
  3859. if (!def_sym)
  3860. expect("switch");
  3861. if (*def_sym)
  3862. error("too many 'default'");
  3863. *def_sym = ind;
  3864. is_expr = 0;
  3865. goto block_after_label;
  3866. } else
  3867. if (tok == TOK_GOTO) {
  3868. next();
  3869. if (tok == '*' && gnu_ext) {
  3870. /* computed goto */
  3871. next();
  3872. gexpr();
  3873. if ((vtop->type.t & VT_BTYPE) != VT_PTR)
  3874. expect("pointer");
  3875. ggoto();
  3876. } else if (tok >= TOK_UIDENT) {
  3877. s = label_find(tok);
  3878. /* put forward definition if needed */
  3879. if (!s) {
  3880. s = label_push(&global_label_stack, tok, LABEL_FORWARD);
  3881. } else {
  3882. if (s->r == LABEL_DECLARED)
  3883. s->r = LABEL_FORWARD;
  3884. }
  3885. /* label already defined */
  3886. if (s->r & LABEL_FORWARD)
  3887. s->next = (void *)gjmp((long)s->next);
  3888. else
  3889. gjmp_addr((long)s->next);
  3890. next();
  3891. } else {
  3892. expect("label identifier");
  3893. }
  3894. skip(';');
  3895. } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
  3896. asm_instr();
  3897. } else {
  3898. b = is_label();
  3899. if (b) {
  3900. /* label case */
  3901. s = label_find(b);
  3902. if (s) {
  3903. if (s->r == LABEL_DEFINED)
  3904. error("duplicate label '%s'", get_tok_str(s->v, NULL));
  3905. gsym((long)s->next);
  3906. s->r = LABEL_DEFINED;
  3907. } else {
  3908. s = label_push(&global_label_stack, b, LABEL_DEFINED);
  3909. }
  3910. s->next = (void *)ind;
  3911. /* we accept this, but it is a mistake */
  3912. block_after_label:
  3913. if (tok == '}') {
  3914. warning("deprecated use of label at end of compound statement");
  3915. } else {
  3916. if (is_expr)
  3917. vpop();
  3918. block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
  3919. }
  3920. } else {
  3921. /* expression case */
  3922. if (tok != ';') {
  3923. if (is_expr) {
  3924. vpop();
  3925. gexpr();
  3926. } else {
  3927. gexpr();
  3928. vpop();
  3929. }
  3930. }
  3931. skip(';');
  3932. }
  3933. }
  3934. }
  3935. /* t is the array or struct type. c is the array or struct
  3936. address. cur_index/cur_field is the pointer to the current
  3937. value. 'size_only' is true if only size info is needed (only used
  3938. in arrays) */
  3939. static void decl_designator(CType *type, Section *sec, unsigned long c,
  3940. int *cur_index, Sym **cur_field,
  3941. int size_only)
  3942. {
  3943. Sym *s, *f;
  3944. int notfirst, index, index_last, align, l, nb_elems, elem_size;
  3945. CType type1;
  3946. notfirst = 0;
  3947. elem_size = 0;
  3948. nb_elems = 1;
  3949. if (gnu_ext && (l = is_label()) != 0)
  3950. goto struct_field;
  3951. while (tok == '[' || tok == '.') {
  3952. if (tok == '[') {
  3953. if (!(type->t & VT_ARRAY))
  3954. expect("array type");
  3955. s = type->ref;
  3956. next();
  3957. index = expr_const();
  3958. if (index < 0 || (s->c >= 0 && index >= s->c))
  3959. expect("invalid index");
  3960. if (tok == TOK_DOTS && gnu_ext) {
  3961. next();
  3962. index_last = expr_const();
  3963. if (index_last < 0 ||
  3964. (s->c >= 0 && index_last >= s->c) ||
  3965. index_last < index)
  3966. expect("invalid index");
  3967. } else {
  3968. index_last = index;
  3969. }
  3970. skip(']');
  3971. if (!notfirst)
  3972. *cur_index = index_last;
  3973. type = pointed_type(type);
  3974. elem_size = type_size(type, &align);
  3975. c += index * elem_size;
  3976. /* NOTE: we only support ranges for last designator */
  3977. nb_elems = index_last - index + 1;
  3978. if (nb_elems != 1) {
  3979. notfirst = 1;
  3980. break;
  3981. }
  3982. } else {
  3983. next();
  3984. l = tok;
  3985. next();
  3986. struct_field:
  3987. if ((type->t & VT_BTYPE) != VT_STRUCT)
  3988. expect("struct/union type");
  3989. s = type->ref;
  3990. l |= SYM_FIELD;
  3991. f = s->next;
  3992. while (f) {
  3993. if (f->v == l)
  3994. break;
  3995. f = f->next;
  3996. }
  3997. if (!f)
  3998. expect("field");
  3999. if (!notfirst)
  4000. *cur_field = f;
  4001. /* XXX: fix this mess by using explicit storage field */
  4002. type1 = f->type;
  4003. type1.t |= (type->t & ~VT_TYPE);
  4004. type = &type1;
  4005. c += f->c;
  4006. }
  4007. notfirst = 1;
  4008. }
  4009. if (notfirst) {
  4010. if (tok == '=') {
  4011. next();
  4012. } else {
  4013. if (!gnu_ext)
  4014. expect("=");
  4015. }
  4016. } else {
  4017. if (type->t & VT_ARRAY) {
  4018. index = *cur_index;
  4019. type = pointed_type(type);
  4020. c += index * type_size(type, &align);
  4021. } else {
  4022. f = *cur_field;
  4023. if (!f)
  4024. error("too many field init");
  4025. /* XXX: fix this mess by using explicit storage field */
  4026. type1 = f->type;
  4027. type1.t |= (type->t & ~VT_TYPE);
  4028. type = &type1;
  4029. c += f->c;
  4030. }
  4031. }
  4032. decl_initializer(type, sec, c, 0, size_only);
  4033. /* XXX: make it more general */
  4034. if (!size_only && nb_elems > 1) {
  4035. unsigned long c_end;
  4036. uint8_t *src, *dst;
  4037. int i;
  4038. if (!sec)
  4039. error("range init not supported yet for dynamic storage");
  4040. c_end = c + nb_elems * elem_size;
  4041. if (c_end > sec->data_allocated)
  4042. section_realloc(sec, c_end);
  4043. src = sec->data + c;
  4044. dst = src;
  4045. for(i = 1; i < nb_elems; i++) {
  4046. dst += elem_size;
  4047. memcpy(dst, src, elem_size);
  4048. }
  4049. }
  4050. }
  4051. #define EXPR_VAL 0
  4052. #define EXPR_CONST 1
  4053. #define EXPR_ANY 2
  4054. /* store a value or an expression directly in global data or in local array */
  4055. static void init_putv(CType *type, Section *sec, unsigned long c,
  4056. int v, int expr_type)
  4057. {
  4058. int saved_global_expr, bt, bit_pos, bit_size;
  4059. void *ptr;
  4060. unsigned long long bit_mask;
  4061. CType dtype;
  4062. switch(expr_type) {
  4063. case EXPR_VAL:
  4064. vpushi(v);
  4065. break;
  4066. case EXPR_CONST:
  4067. /* compound literals must be allocated globally in this case */
  4068. saved_global_expr = global_expr;
  4069. global_expr = 1;
  4070. expr_const1();
  4071. global_expr = saved_global_expr;
  4072. /* NOTE: symbols are accepted */
  4073. if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
  4074. error("initializer element is not constant");
  4075. break;
  4076. case EXPR_ANY:
  4077. expr_eq();
  4078. break;
  4079. }
  4080. dtype = *type;
  4081. dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
  4082. if (sec) {
  4083. /* XXX: not portable */
  4084. /* XXX: generate error if incorrect relocation */
  4085. gen_assign_cast(&dtype);
  4086. bt = type->t & VT_BTYPE;
  4087. /* we'll write at most 12 bytes */
  4088. if (c + 12 > sec->data_allocated) {
  4089. section_realloc(sec, c + 12);
  4090. }
  4091. ptr = sec->data + c;
  4092. /* XXX: make code faster ? */
  4093. if (!(type->t & VT_BITFIELD)) {
  4094. bit_pos = 0;
  4095. bit_size = 32;
  4096. bit_mask = -1LL;
  4097. } else {
  4098. bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
  4099. bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
  4100. bit_mask = (1LL << bit_size) - 1;
  4101. }
  4102. if ((vtop->r & VT_SYM) &&
  4103. (bt == VT_BYTE ||
  4104. bt == VT_SHORT ||
  4105. bt == VT_DOUBLE ||
  4106. bt == VT_LDOUBLE ||
  4107. bt == VT_LLONG ||
  4108. (bt == VT_INT && bit_size != 32)))
  4109. error("initializer element is not computable at load time");
  4110. switch(bt) {
  4111. case VT_BOOL:
  4112. vtop->c.i = (vtop->c.i != 0);
  4113. case VT_BYTE:
  4114. *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
  4115. break;
  4116. case VT_SHORT:
  4117. *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
  4118. break;
  4119. case VT_DOUBLE:
  4120. *(double *)ptr = vtop->c.d;
  4121. break;
  4122. case VT_LDOUBLE:
  4123. *(long double *)ptr = vtop->c.ld;
  4124. break;
  4125. case VT_LLONG:
  4126. *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
  4127. break;
  4128. default:
  4129. if (vtop->r & VT_SYM) {
  4130. greloc(sec, vtop->sym, c, R_DATA_32);
  4131. }
  4132. *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
  4133. break;
  4134. }
  4135. vtop--;
  4136. } else {
  4137. vset(&dtype, VT_LOCAL|VT_LVAL, c);
  4138. vswap();
  4139. vstore();
  4140. vpop();
  4141. }
  4142. }
  4143. /* put zeros for variable based init */
  4144. static void init_putz(CType *t, Section *sec, unsigned long c, int size)
  4145. {
  4146. if (sec) {
  4147. /* nothing to do because globals are already set to zero */
  4148. } else {
  4149. vpush_global_sym(&func_old_type, TOK_memset);
  4150. vseti(VT_LOCAL, c);
  4151. vpushi(0);
  4152. vpushi(size);
  4153. gfunc_call(3);
  4154. }
  4155. }
  4156. /* 't' contains the type and storage info. 'c' is the offset of the
  4157. object in section 'sec'. If 'sec' is NULL, it means stack based
  4158. allocation. 'first' is true if array '{' must be read (multi
  4159. dimension implicit array init handling). 'size_only' is true if
  4160. size only evaluation is wanted (only for arrays). */
  4161. static void decl_initializer(CType *type, Section *sec, unsigned long c,
  4162. int first, int size_only)
  4163. {
  4164. int index, array_length, n, no_oblock, nb, parlevel, i;
  4165. int size1, align1, expr_type;
  4166. Sym *s, *f;
  4167. CType *t1;
  4168. if (type->t & VT_ARRAY) {
  4169. s = type->ref;
  4170. n = s->c;
  4171. array_length = 0;
  4172. t1 = pointed_type(type);
  4173. size1 = type_size(t1, &align1);
  4174. no_oblock = 1;
  4175. if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
  4176. tok == '{') {
  4177. skip('{');
  4178. no_oblock = 0;
  4179. }
  4180. /* only parse strings here if correct type (otherwise: handle
  4181. them as ((w)char *) expressions */
  4182. if ((tok == TOK_LSTR &&
  4183. #ifdef TCC_TARGET_PE
  4184. (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
  4185. #else
  4186. (t1->t & VT_BTYPE) == VT_INT
  4187. #endif
  4188. ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
  4189. while (tok == TOK_STR || tok == TOK_LSTR) {
  4190. int cstr_len, ch;
  4191. CString *cstr;
  4192. cstr = tokc.cstr;
  4193. /* compute maximum number of chars wanted */
  4194. if (tok == TOK_STR)
  4195. cstr_len = cstr->size;
  4196. else
  4197. cstr_len = cstr->size / sizeof(nwchar_t);
  4198. cstr_len--;
  4199. nb = cstr_len;
  4200. if (n >= 0 && nb > (n - array_length))
  4201. nb = n - array_length;
  4202. if (!size_only) {
  4203. if (cstr_len > nb)
  4204. warning("initializer-string for array is too long");
  4205. /* in order to go faster for common case (char
  4206. string in global variable, we handle it
  4207. specifically */
  4208. if (sec && tok == TOK_STR && size1 == 1) {
  4209. memcpy(sec->data + c + array_length, cstr->data, nb);
  4210. } else {
  4211. for(i=0;i<nb;i++) {
  4212. if (tok == TOK_STR)
  4213. ch = ((unsigned char *)cstr->data)[i];
  4214. else
  4215. ch = ((nwchar_t *)cstr->data)[i];
  4216. init_putv(t1, sec, c + (array_length + i) * size1,
  4217. ch, EXPR_VAL);
  4218. }
  4219. }
  4220. }
  4221. array_length += nb;
  4222. next();
  4223. }
  4224. /* only add trailing zero if enough storage (no
  4225. warning in this case since it is standard) */
  4226. if (n < 0 || array_length < n) {
  4227. if (!size_only) {
  4228. init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
  4229. }
  4230. array_length++;
  4231. }
  4232. } else {
  4233. index = 0;
  4234. while (tok != '}') {
  4235. decl_designator(type, sec, c, &index, NULL, size_only);
  4236. if (n >= 0 && index >= n)
  4237. error("index too large");
  4238. /* must put zero in holes (note that doing it that way
  4239. ensures that it even works with designators) */
  4240. if (!size_only && array_length < index) {
  4241. init_putz(t1, sec, c + array_length * size1,
  4242. (index - array_length) * size1);
  4243. }
  4244. index++;
  4245. if (index > array_length)
  4246. array_length = index;
  4247. /* special test for multi dimensional arrays (may not
  4248. be strictly correct if designators are used at the
  4249. same time) */
  4250. if (index >= n && no_oblock)
  4251. break;
  4252. if (tok == '}')
  4253. break;
  4254. skip(',');
  4255. }
  4256. }
  4257. if (!no_oblock)
  4258. skip('}');
  4259. /* put zeros at the end */
  4260. if (!size_only && n >= 0 && array_length < n) {
  4261. init_putz(t1, sec, c + array_length * size1,
  4262. (n - array_length) * size1);
  4263. }
  4264. /* patch type size if needed */
  4265. if (n < 0)
  4266. s->c = array_length;
  4267. } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
  4268. (sec || !first || tok == '{')) {
  4269. int par_count;
  4270. /* NOTE: the previous test is a specific case for automatic
  4271. struct/union init */
  4272. /* XXX: union needs only one init */
  4273. /* XXX: this test is incorrect for local initializers
  4274. beginning with ( without {. It would be much more difficult
  4275. to do it correctly (ideally, the expression parser should
  4276. be used in all cases) */
  4277. par_count = 0;
  4278. if (tok == '(') {
  4279. AttributeDef ad1;
  4280. CType type1;
  4281. next();
  4282. while (tok == '(') {
  4283. par_count++;
  4284. next();
  4285. }
  4286. if (!parse_btype(&type1, &ad1))
  4287. expect("cast");
  4288. type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
  4289. #if 0
  4290. if (!is_assignable_types(type, &type1))
  4291. error("invalid type for cast");
  4292. #endif
  4293. skip(')');
  4294. }
  4295. no_oblock = 1;
  4296. if (first || tok == '{') {
  4297. skip('{');
  4298. no_oblock = 0;
  4299. }
  4300. s = type->ref;
  4301. f = s->next;
  4302. array_length = 0;
  4303. index = 0;
  4304. n = s->c;
  4305. while (tok != '}') {
  4306. decl_designator(type, sec, c, NULL, &f, size_only);
  4307. index = f->c;
  4308. if (!size_only && array_length < index) {
  4309. init_putz(type, sec, c + array_length,
  4310. index - array_length);
  4311. }
  4312. index = index + type_size(&f->type, &align1);
  4313. if (index > array_length)
  4314. array_length = index;
  4315. f = f->next;
  4316. if (no_oblock && f == NULL)
  4317. break;
  4318. if (tok == '}')
  4319. break;
  4320. skip(',');
  4321. }
  4322. /* put zeros at the end */
  4323. if (!size_only && array_length < n) {
  4324. init_putz(type, sec, c + array_length,
  4325. n - array_length);
  4326. }
  4327. if (!no_oblock)
  4328. skip('}');
  4329. while (par_count) {
  4330. skip(')');
  4331. par_count--;
  4332. }
  4333. } else if (tok == '{') {
  4334. next();
  4335. decl_initializer(type, sec, c, first, size_only);
  4336. skip('}');
  4337. } else if (size_only) {
  4338. /* just skip expression */
  4339. parlevel = 0;
  4340. while ((parlevel > 0 || (tok != '}' && tok != ',')) &&
  4341. tok != -1) {
  4342. if (tok == '(')
  4343. parlevel++;
  4344. else if (tok == ')')
  4345. parlevel--;
  4346. next();
  4347. }
  4348. } else {
  4349. /* currently, we always use constant expression for globals
  4350. (may change for scripting case) */
  4351. expr_type = EXPR_CONST;
  4352. if (!sec)
  4353. expr_type = EXPR_ANY;
  4354. init_putv(type, sec, c, 0, expr_type);
  4355. }
  4356. }
  4357. /* parse an initializer for type 't' if 'has_init' is non zero, and
  4358. allocate space in local or global data space ('r' is either
  4359. VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
  4360. variable 'v' of scope 'scope' is declared before initializers are
  4361. parsed. If 'v' is zero, then a reference to the new object is put
  4362. in the value stack. If 'has_init' is 2, a special parsing is done
  4363. to handle string constants. */
  4364. static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
  4365. int has_init, int v, int scope)
  4366. {
  4367. int size, align, addr, data_offset;
  4368. int level;
  4369. ParseState saved_parse_state = {0};
  4370. TokenString init_str;
  4371. Section *sec;
  4372. size = type_size(type, &align);
  4373. /* If unknown size, we must evaluate it before
  4374. evaluating initializers because
  4375. initializers can generate global data too
  4376. (e.g. string pointers or ISOC99 compound
  4377. literals). It also simplifies local
  4378. initializers handling */
  4379. tok_str_new(&init_str);
  4380. if (size < 0) {
  4381. if (!has_init)
  4382. error("unknown type size");
  4383. /* get all init string */
  4384. if (has_init == 2) {
  4385. /* only get strings */
  4386. while (tok == TOK_STR || tok == TOK_LSTR) {
  4387. tok_str_add_tok(&init_str);
  4388. next();
  4389. }
  4390. } else {
  4391. level = 0;
  4392. while (level > 0 || (tok != ',' && tok != ';')) {
  4393. if (tok < 0)
  4394. error("unexpected end of file in initializer");
  4395. tok_str_add_tok(&init_str);
  4396. if (tok == '{')
  4397. level++;
  4398. else if (tok == '}') {
  4399. level--;
  4400. if (level <= 0) {
  4401. next();
  4402. break;
  4403. }
  4404. }
  4405. next();
  4406. }
  4407. }
  4408. tok_str_add(&init_str, -1);
  4409. tok_str_add(&init_str, 0);
  4410. /* compute size */
  4411. save_parse_state(&saved_parse_state);
  4412. macro_ptr = init_str.str;
  4413. next();
  4414. decl_initializer(type, NULL, 0, 1, 1);
  4415. /* prepare second initializer parsing */
  4416. macro_ptr = init_str.str;
  4417. next();
  4418. /* if still unknown size, error */
  4419. size = type_size(type, &align);
  4420. if (size < 0)
  4421. error("unknown type size");
  4422. }
  4423. /* take into account specified alignment if bigger */
  4424. if (ad->aligned) {
  4425. if (ad->aligned > align)
  4426. align = ad->aligned;
  4427. } else if (ad->packed) {
  4428. align = 1;
  4429. }
  4430. if ((r & VT_VALMASK) == VT_LOCAL) {
  4431. sec = NULL;
  4432. if (tcc_state->do_bounds_check && (type->t & VT_ARRAY))
  4433. loc--;
  4434. loc = (loc - size) & -align;
  4435. addr = loc;
  4436. /* handles bounds */
  4437. /* XXX: currently, since we do only one pass, we cannot track
  4438. '&' operators, so we add only arrays */
  4439. if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
  4440. unsigned long *bounds_ptr;
  4441. /* add padding between regions */
  4442. loc--;
  4443. /* then add local bound info */
  4444. bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
  4445. bounds_ptr[0] = addr;
  4446. bounds_ptr[1] = size;
  4447. }
  4448. if (v) {
  4449. /* local variable */
  4450. sym_push(v, type, r, addr);
  4451. } else {
  4452. /* push local reference */
  4453. vset(type, r, addr);
  4454. }
  4455. } else {
  4456. Sym *sym;
  4457. sym = NULL;
  4458. if (v && scope == VT_CONST) {
  4459. /* see if the symbol was already defined */
  4460. sym = sym_find(v);
  4461. if (sym) {
  4462. if (!is_compatible_types(&sym->type, type))
  4463. error("incompatible types for redefinition of '%s'",
  4464. get_tok_str(v, NULL));
  4465. if (sym->type.t & VT_EXTERN) {
  4466. /* if the variable is extern, it was not allocated */
  4467. sym->type.t &= ~VT_EXTERN;
  4468. /* set array size if it was omitted in extern
  4469. declaration */
  4470. if ((sym->type.t & VT_ARRAY) &&
  4471. sym->type.ref->c < 0 &&
  4472. type->ref->c >= 0)
  4473. sym->type.ref->c = type->ref->c;
  4474. } else {
  4475. /* we accept several definitions of the same
  4476. global variable. this is tricky, because we
  4477. must play with the SHN_COMMON type of the symbol */
  4478. /* XXX: should check if the variable was already
  4479. initialized. It is incorrect to initialized it
  4480. twice */
  4481. /* no init data, we won't add more to the symbol */
  4482. if (!has_init)
  4483. goto no_alloc;
  4484. }
  4485. }
  4486. }
  4487. /* allocate symbol in corresponding section */
  4488. sec = ad->section;
  4489. if (!sec) {
  4490. if (has_init)
  4491. sec = data_section;
  4492. else if (tcc_state->nocommon)
  4493. sec = bss_section;
  4494. }
  4495. if (sec) {
  4496. data_offset = sec->data_offset;
  4497. data_offset = (data_offset + align - 1) & -align;
  4498. addr = data_offset;
  4499. /* very important to increment global pointer at this time
  4500. because initializers themselves can create new initializers */
  4501. data_offset += size;
  4502. /* add padding if bound check */
  4503. if (tcc_state->do_bounds_check)
  4504. data_offset++;
  4505. sec->data_offset = data_offset;
  4506. /* allocate section space to put the data */
  4507. if (sec->sh_type != SHT_NOBITS &&
  4508. data_offset > sec->data_allocated)
  4509. section_realloc(sec, data_offset);
  4510. /* align section if needed */
  4511. if (align > sec->sh_addralign)
  4512. sec->sh_addralign = align;
  4513. } else {
  4514. addr = 0; /* avoid warning */
  4515. }
  4516. if (v) {
  4517. if (scope != VT_CONST || !sym) {
  4518. sym = sym_push(v, type, r | VT_SYM, 0);
  4519. }
  4520. /* update symbol definition */
  4521. if (sec) {
  4522. put_extern_sym(sym, sec, addr, size);
  4523. } else {
  4524. ElfW(Sym) *esym;
  4525. /* put a common area */
  4526. put_extern_sym(sym, NULL, align, size);
  4527. /* XXX: find a nicer way */
  4528. esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
  4529. esym->st_shndx = SHN_COMMON;
  4530. }
  4531. } else {
  4532. CValue cval;
  4533. /* push global reference */
  4534. sym = get_sym_ref(type, sec, addr, size);
  4535. cval.ul = 0;
  4536. vsetc(type, VT_CONST | VT_SYM, &cval);
  4537. vtop->sym = sym;
  4538. }
  4539. /* handles bounds now because the symbol must be defined
  4540. before for the relocation */
  4541. if (tcc_state->do_bounds_check) {
  4542. unsigned long *bounds_ptr;
  4543. greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_32);
  4544. /* then add global bound info */
  4545. bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
  4546. bounds_ptr[0] = 0; /* relocated */
  4547. bounds_ptr[1] = size;
  4548. }
  4549. }
  4550. if (has_init) {
  4551. decl_initializer(type, sec, addr, 1, 0);
  4552. /* restore parse state if needed */
  4553. if (init_str.str) {
  4554. tok_str_free(init_str.str);
  4555. restore_parse_state(&saved_parse_state);
  4556. }
  4557. }
  4558. no_alloc: ;
  4559. }
  4560. void put_func_debug(Sym *sym)
  4561. {
  4562. char buf[512];
  4563. /* stabs info */
  4564. /* XXX: we put here a dummy type */
  4565. snprintf(buf, sizeof(buf), "%s:%c1",
  4566. funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
  4567. put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
  4568. cur_text_section, sym->c);
  4569. /* //gr gdb wants a line at the function */
  4570. put_stabn(N_SLINE, 0, file->line_num, 0);
  4571. last_ind = 0;
  4572. last_line_num = 0;
  4573. }
  4574. /* parse an old style function declaration list */
  4575. /* XXX: check multiple parameter */
  4576. static void func_decl_list(Sym *func_sym)
  4577. {
  4578. AttributeDef ad;
  4579. int v;
  4580. Sym *s;
  4581. CType btype, type;
  4582. /* parse each declaration */
  4583. while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF) {
  4584. if (!parse_btype(&btype, &ad))
  4585. expect("declaration list");
  4586. if (((btype.t & VT_BTYPE) == VT_ENUM ||
  4587. (btype.t & VT_BTYPE) == VT_STRUCT) &&
  4588. tok == ';') {
  4589. /* we accept no variable after */
  4590. } else {
  4591. for(;;) {
  4592. type = btype;
  4593. type_decl(&type, &ad, &v, TYPE_DIRECT);
  4594. /* find parameter in function parameter list */
  4595. s = func_sym->next;
  4596. while (s != NULL) {
  4597. if ((s->v & ~SYM_FIELD) == v)
  4598. goto found;
  4599. s = s->next;
  4600. }
  4601. error("declaration for parameter '%s' but no such parameter",
  4602. get_tok_str(v, NULL));
  4603. found:
  4604. /* check that no storage specifier except 'register' was given */
  4605. if (type.t & VT_STORAGE)
  4606. error("storage class specified for '%s'", get_tok_str(v, NULL));
  4607. convert_parameter_type(&type);
  4608. /* we can add the type (NOTE: it could be local to the function) */
  4609. s->type = type;
  4610. /* accept other parameters */
  4611. if (tok == ',')
  4612. next();
  4613. else
  4614. break;
  4615. }
  4616. }
  4617. skip(';');
  4618. }
  4619. }
  4620. /* parse a function defined by symbol 'sym' and generate its code in
  4621. 'cur_text_section' */
  4622. static void gen_function(Sym *sym)
  4623. {
  4624. int saved_nocode_wanted = nocode_wanted;
  4625. nocode_wanted = 0;
  4626. ind = cur_text_section->data_offset;
  4627. /* NOTE: we patch the symbol size later */
  4628. put_extern_sym(sym, cur_text_section, ind, 0);
  4629. funcname = get_tok_str(sym->v, NULL);
  4630. func_ind = ind;
  4631. /* put debug symbol */
  4632. if (tcc_state->do_debug)
  4633. put_func_debug(sym);
  4634. /* push a dummy symbol to enable local sym storage */
  4635. sym_push2(&local_stack, SYM_FIELD, 0, 0);
  4636. gfunc_prolog(&sym->type);
  4637. rsym = 0;
  4638. block(NULL, NULL, NULL, NULL, 0, 0);
  4639. gsym(rsym);
  4640. gfunc_epilog();
  4641. cur_text_section->data_offset = ind;
  4642. label_pop(&global_label_stack, NULL);
  4643. sym_pop(&local_stack, NULL); /* reset local stack */
  4644. /* end of function */
  4645. /* patch symbol size */
  4646. ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
  4647. ind - func_ind;
  4648. if (tcc_state->do_debug) {
  4649. put_stabn(N_FUN, 0, 0, ind - func_ind);
  4650. }
  4651. /* It's better to crash than to generate wrong code */
  4652. cur_text_section = NULL;
  4653. funcname = ""; /* for safety */
  4654. func_vt.t = VT_VOID; /* for safety */
  4655. ind = 0; /* for safety */
  4656. nocode_wanted = saved_nocode_wanted;
  4657. }
  4658. static void gen_inline_functions(void)
  4659. {
  4660. Sym *sym;
  4661. CType *type;
  4662. int *str, inline_generated;
  4663. /* iterate while inline function are referenced */
  4664. for(;;) {
  4665. inline_generated = 0;
  4666. for(sym = global_stack; sym != NULL; sym = sym->prev) {
  4667. type = &sym->type;
  4668. if (((type->t & VT_BTYPE) == VT_FUNC) &&
  4669. (type->t & (VT_STATIC | VT_INLINE)) ==
  4670. (VT_STATIC | VT_INLINE) &&
  4671. sym->c != 0) {
  4672. /* the function was used: generate its code and
  4673. convert it to a normal function */
  4674. str = INLINE_DEF(sym->r);
  4675. sym->r = VT_SYM | VT_CONST;
  4676. sym->type.t &= ~VT_INLINE;
  4677. macro_ptr = str;
  4678. next();
  4679. cur_text_section = text_section;
  4680. gen_function(sym);
  4681. macro_ptr = NULL; /* fail safe */
  4682. tok_str_free(str);
  4683. inline_generated = 1;
  4684. }
  4685. }
  4686. if (!inline_generated)
  4687. break;
  4688. }
  4689. /* free all remaining inline function tokens */
  4690. for(sym = global_stack; sym != NULL; sym = sym->prev) {
  4691. type = &sym->type;
  4692. if (((type->t & VT_BTYPE) == VT_FUNC) &&
  4693. (type->t & (VT_STATIC | VT_INLINE)) ==
  4694. (VT_STATIC | VT_INLINE)) {
  4695. //gr printf("sym %d %s\n", sym->r, get_tok_str(sym->v, NULL));
  4696. if (sym->r == (VT_SYM | VT_CONST)) //gr beware!
  4697. continue;
  4698. str = INLINE_DEF(sym->r);
  4699. tok_str_free(str);
  4700. sym->r = 0; /* fail safe */
  4701. }
  4702. }
  4703. }
  4704. /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
  4705. static void decl(int l)
  4706. {
  4707. int v, has_init, r;
  4708. CType type, btype;
  4709. Sym *sym;
  4710. AttributeDef ad;
  4711. while (1) {
  4712. if (!parse_btype(&btype, &ad)) {
  4713. /* skip redundant ';' */
  4714. /* XXX: find more elegant solution */
  4715. if (tok == ';') {
  4716. next();
  4717. continue;
  4718. }
  4719. if (l == VT_CONST &&
  4720. (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
  4721. /* global asm block */
  4722. asm_global_instr();
  4723. continue;
  4724. }
  4725. /* special test for old K&R protos without explicit int
  4726. type. Only accepted when defining global data */
  4727. if (l == VT_LOCAL || tok < TOK_DEFINE)
  4728. break;
  4729. btype.t = VT_INT;
  4730. }
  4731. if (((btype.t & VT_BTYPE) == VT_ENUM ||
  4732. (btype.t & VT_BTYPE) == VT_STRUCT) &&
  4733. tok == ';') {
  4734. /* we accept no variable after */
  4735. next();
  4736. continue;
  4737. }
  4738. while (1) { /* iterate thru each declaration */
  4739. type = btype;
  4740. type_decl(&type, &ad, &v, TYPE_DIRECT);
  4741. #if 0
  4742. {
  4743. char buf[500];
  4744. type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
  4745. printf("type = '%s'\n", buf);
  4746. }
  4747. #endif
  4748. if ((type.t & VT_BTYPE) == VT_FUNC) {
  4749. /* if old style function prototype, we accept a
  4750. declaration list */
  4751. sym = type.ref;
  4752. if (sym->c == FUNC_OLD)
  4753. func_decl_list(sym);
  4754. }
  4755. if (tok == '{') {
  4756. if (l == VT_LOCAL)
  4757. error("cannot use local functions");
  4758. if ((type.t & VT_BTYPE) != VT_FUNC)
  4759. expect("function definition");
  4760. /* reject abstract declarators in function definition */
  4761. sym = type.ref;
  4762. while ((sym = sym->next) != NULL)
  4763. if (!(sym->v & ~SYM_FIELD))
  4764. expect("identifier");
  4765. /* XXX: cannot do better now: convert extern line to static inline */
  4766. if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
  4767. type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
  4768. sym = sym_find(v);
  4769. if (sym) {
  4770. if ((sym->type.t & VT_BTYPE) != VT_FUNC)
  4771. goto func_error1;
  4772. /* specific case: if not func_call defined, we put
  4773. the one of the prototype */
  4774. /* XXX: should have default value */
  4775. r = sym->type.ref->r;
  4776. if (FUNC_CALL(r) != FUNC_CDECL
  4777. && FUNC_CALL(type.ref->r) == FUNC_CDECL)
  4778. FUNC_CALL(type.ref->r) = FUNC_CALL(r);
  4779. if (FUNC_EXPORT(r))
  4780. FUNC_EXPORT(type.ref->r) = 1;
  4781. if (!is_compatible_types(&sym->type, &type)) {
  4782. func_error1:
  4783. error("incompatible types for redefinition of '%s'",
  4784. get_tok_str(v, NULL));
  4785. }
  4786. /* if symbol is already defined, then put complete type */
  4787. sym->type = type;
  4788. } else {
  4789. /* put function symbol */
  4790. sym = global_identifier_push(v, type.t, 0);
  4791. sym->type.ref = type.ref;
  4792. }
  4793. /* static inline functions are just recorded as a kind
  4794. of macro. Their code will be emitted at the end of
  4795. the compilation unit only if they are used */
  4796. if ((type.t & (VT_INLINE | VT_STATIC)) ==
  4797. (VT_INLINE | VT_STATIC)) {
  4798. TokenString func_str;
  4799. int block_level;
  4800. tok_str_new(&func_str);
  4801. block_level = 0;
  4802. for(;;) {
  4803. int t;
  4804. if (tok == TOK_EOF)
  4805. error("unexpected end of file");
  4806. tok_str_add_tok(&func_str);
  4807. t = tok;
  4808. next();
  4809. if (t == '{') {
  4810. block_level++;
  4811. } else if (t == '}') {
  4812. block_level--;
  4813. if (block_level == 0)
  4814. break;
  4815. }
  4816. }
  4817. tok_str_add(&func_str, -1);
  4818. tok_str_add(&func_str, 0);
  4819. INLINE_DEF(sym->r) = func_str.str;
  4820. } else {
  4821. /* compute text section */
  4822. cur_text_section = ad.section;
  4823. if (!cur_text_section)
  4824. cur_text_section = text_section;
  4825. sym->r = VT_SYM | VT_CONST;
  4826. gen_function(sym);
  4827. }
  4828. break;
  4829. } else {
  4830. if (btype.t & VT_TYPEDEF) {
  4831. /* save typedefed type */
  4832. /* XXX: test storage specifiers ? */
  4833. sym = sym_push(v, &type, 0, 0);
  4834. sym->type.t |= VT_TYPEDEF;
  4835. } else if ((type.t & VT_BTYPE) == VT_FUNC) {
  4836. /* external function definition */
  4837. /* specific case for func_call attribute */
  4838. if (ad.func_attr)
  4839. type.ref->r = ad.func_attr;
  4840. external_sym(v, &type, 0);
  4841. } else {
  4842. /* not lvalue if array */
  4843. r = 0;
  4844. if (!(type.t & VT_ARRAY))
  4845. r |= lvalue_type(type.t);
  4846. has_init = (tok == '=');
  4847. if ((btype.t & VT_EXTERN) ||
  4848. ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
  4849. !has_init && l == VT_CONST && type.ref->c < 0)) {
  4850. /* external variable */
  4851. /* NOTE: as GCC, uninitialized global static
  4852. arrays of null size are considered as
  4853. extern */
  4854. external_sym(v, &type, r);
  4855. } else {
  4856. type.t |= (btype.t & VT_STATIC); /* Retain "static". */
  4857. if (type.t & VT_STATIC)
  4858. r |= VT_CONST;
  4859. else
  4860. r |= l;
  4861. if (has_init)
  4862. next();
  4863. decl_initializer_alloc(&type, &ad, r,
  4864. has_init, v, l);
  4865. }
  4866. }
  4867. if (tok != ',') {
  4868. skip(';');
  4869. break;
  4870. }
  4871. next();
  4872. }
  4873. }
  4874. }
  4875. }