tccpp.c 107 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904
  1. /*
  2. * TCC - Tiny C Compiler
  3. *
  4. * Copyright (c) 2001-2004 Fabrice Bellard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include "tcc.h"
  21. /********************************************************/
  22. /* global variables */
  23. ST_DATA int tok_flags;
  24. ST_DATA int parse_flags;
  25. ST_DATA struct BufferedFile *file;
  26. ST_DATA int ch, tok;
  27. ST_DATA CValue tokc;
  28. ST_DATA const int *macro_ptr;
  29. ST_DATA CString tokcstr; /* current parsed string, if any */
  30. /* display benchmark infos */
  31. ST_DATA int total_lines;
  32. ST_DATA int total_bytes;
  33. ST_DATA int tok_ident;
  34. ST_DATA TokenSym **table_ident;
  35. /* ------------------------------------------------------------------------- */
  36. static TokenSym *hash_ident[TOK_HASH_SIZE];
  37. static char token_buf[STRING_MAX_SIZE + 1];
  38. static CString cstr_buf;
  39. static CString macro_equal_buf;
  40. static TokenString tokstr_buf;
  41. static unsigned char isidnum_table[256 - CH_EOF];
  42. static int pp_debug_tok, pp_debug_symv;
  43. static int pp_once;
  44. static int pp_expr;
  45. static int pp_counter;
  46. static void tok_print(const char *msg, const int *str);
  47. static struct TinyAlloc *toksym_alloc;
  48. static struct TinyAlloc *tokstr_alloc;
  49. static struct TinyAlloc *cstr_alloc;
  50. static TokenString *macro_stack;
  51. static const char tcc_keywords[] =
  52. #define DEF(id, str) str "\0"
  53. #include "tcctok.h"
  54. #undef DEF
  55. ;
  56. /* WARNING: the content of this string encodes token numbers */
  57. static const unsigned char tok_two_chars[] =
  58. /* outdated -- gr
  59. "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253"
  60. "-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
  61. */{
  62. '<','=', TOK_LE,
  63. '>','=', TOK_GE,
  64. '!','=', TOK_NE,
  65. '&','&', TOK_LAND,
  66. '|','|', TOK_LOR,
  67. '+','+', TOK_INC,
  68. '-','-', TOK_DEC,
  69. '=','=', TOK_EQ,
  70. '<','<', TOK_SHL,
  71. '>','>', TOK_SAR,
  72. '+','=', TOK_A_ADD,
  73. '-','=', TOK_A_SUB,
  74. '*','=', TOK_A_MUL,
  75. '/','=', TOK_A_DIV,
  76. '%','=', TOK_A_MOD,
  77. '&','=', TOK_A_AND,
  78. '^','=', TOK_A_XOR,
  79. '|','=', TOK_A_OR,
  80. '-','>', TOK_ARROW,
  81. '.','.', TOK_TWODOTS,
  82. '#','#', TOK_TWOSHARPS,
  83. 0
  84. };
  85. static void next_nomacro_spc(void);
  86. ST_FUNC void skip(int c)
  87. {
  88. if (tok != c)
  89. tcc_error("'%c' expected (got \"%s\")", c, get_tok_str(tok, &tokc));
  90. next();
  91. }
  92. ST_FUNC void expect(const char *msg)
  93. {
  94. tcc_error("%s expected", msg);
  95. }
  96. /* ------------------------------------------------------------------------- */
  97. /* Custom allocator for tiny objects */
  98. #define USE_TAL
  99. #ifndef USE_TAL
  100. #define tal_free(al, p) tcc_free(p)
  101. #define tal_realloc(al, p, size) tcc_realloc(p, size)
  102. #define tal_new(a,b,c)
  103. #define tal_delete(a)
  104. #else
  105. #if !defined(MEM_DEBUG)
  106. #define tal_free(al, p) tal_free_impl(al, p)
  107. #define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size)
  108. #define TAL_DEBUG_PARAMS
  109. #else
  110. #define TAL_DEBUG 1
  111. //#define TAL_INFO 1 /* collect and dump allocators stats */
  112. #define tal_free(al, p) tal_free_impl(al, p, __FILE__, __LINE__)
  113. #define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size, __FILE__, __LINE__)
  114. #define TAL_DEBUG_PARAMS , const char *file, int line
  115. #define TAL_DEBUG_FILE_LEN 40
  116. #endif
  117. #define TOKSYM_TAL_SIZE (768 * 1024) /* allocator for tiny TokenSym in table_ident */
  118. #define TOKSTR_TAL_SIZE (768 * 1024) /* allocator for tiny TokenString instances */
  119. #define CSTR_TAL_SIZE (256 * 1024) /* allocator for tiny CString instances */
  120. #define TOKSYM_TAL_LIMIT 256 /* prefer unique limits to distinguish allocators debug msgs */
  121. #define TOKSTR_TAL_LIMIT 128 /* 32 * sizeof(int) */
  122. #define CSTR_TAL_LIMIT 1024
  123. typedef struct TinyAlloc {
  124. unsigned limit;
  125. unsigned size;
  126. uint8_t *buffer;
  127. uint8_t *p;
  128. unsigned nb_allocs;
  129. struct TinyAlloc *next, *top;
  130. #ifdef TAL_INFO
  131. unsigned nb_peak;
  132. unsigned nb_total;
  133. unsigned nb_missed;
  134. uint8_t *peak_p;
  135. #endif
  136. } TinyAlloc;
  137. typedef struct tal_header_t {
  138. unsigned size;
  139. #ifdef TAL_DEBUG
  140. int line_num; /* negative line_num used for double free check */
  141. char file_name[TAL_DEBUG_FILE_LEN + 1];
  142. #endif
  143. } tal_header_t;
  144. /* ------------------------------------------------------------------------- */
  145. static TinyAlloc *tal_new(TinyAlloc **pal, unsigned limit, unsigned size)
  146. {
  147. TinyAlloc *al = tcc_mallocz(sizeof(TinyAlloc));
  148. al->p = al->buffer = tcc_malloc(size);
  149. al->limit = limit;
  150. al->size = size;
  151. if (pal) *pal = al;
  152. return al;
  153. }
  154. static void tal_delete(TinyAlloc *al)
  155. {
  156. TinyAlloc *next;
  157. tail_call:
  158. if (!al)
  159. return;
  160. #ifdef TAL_INFO
  161. fprintf(stderr, "limit=%5d, size=%5g MB, nb_peak=%6d, nb_total=%8d, nb_missed=%6d, usage=%5.1f%%\n",
  162. al->limit, al->size / 1024.0 / 1024.0, al->nb_peak, al->nb_total, al->nb_missed,
  163. (al->peak_p - al->buffer) * 100.0 / al->size);
  164. #endif
  165. #ifdef TAL_DEBUG
  166. if (al->nb_allocs > 0) {
  167. uint8_t *p;
  168. fprintf(stderr, "TAL_DEBUG: memory leak %d chunk(s) (limit= %d)\n",
  169. al->nb_allocs, al->limit);
  170. p = al->buffer;
  171. while (p < al->p) {
  172. tal_header_t *header = (tal_header_t *)p;
  173. if (header->line_num > 0) {
  174. fprintf(stderr, "%s:%d: chunk of %d bytes leaked\n",
  175. header->file_name, header->line_num, header->size);
  176. }
  177. p += header->size + sizeof(tal_header_t);
  178. }
  179. #if MEM_DEBUG-0 == 2
  180. exit(2);
  181. #endif
  182. }
  183. #endif
  184. next = al->next;
  185. tcc_free(al->buffer);
  186. tcc_free(al);
  187. al = next;
  188. goto tail_call;
  189. }
  190. static void tal_free_impl(TinyAlloc *al, void *p TAL_DEBUG_PARAMS)
  191. {
  192. if (!p)
  193. return;
  194. tail_call:
  195. if (al->buffer <= (uint8_t *)p && (uint8_t *)p < al->buffer + al->size) {
  196. #ifdef TAL_DEBUG
  197. tal_header_t *header = (((tal_header_t *)p) - 1);
  198. if (header->line_num < 0) {
  199. fprintf(stderr, "%s:%d: TAL_DEBUG: double frees chunk from\n",
  200. file, line);
  201. fprintf(stderr, "%s:%d: %d bytes\n",
  202. header->file_name, (int)-header->line_num, (int)header->size);
  203. } else
  204. header->line_num = -header->line_num;
  205. #endif
  206. al->nb_allocs--;
  207. if (!al->nb_allocs)
  208. al->p = al->buffer;
  209. } else if (al->next) {
  210. al = al->next;
  211. goto tail_call;
  212. }
  213. else
  214. tcc_free(p);
  215. }
  216. static void *tal_realloc_impl(TinyAlloc **pal, void *p, unsigned size TAL_DEBUG_PARAMS)
  217. {
  218. tal_header_t *header;
  219. void *ret;
  220. int is_own;
  221. unsigned adj_size = (size + 3) & -4;
  222. TinyAlloc *al = *pal;
  223. tail_call:
  224. is_own = (al->buffer <= (uint8_t *)p && (uint8_t *)p < al->buffer + al->size);
  225. if ((!p || is_own) && size <= al->limit) {
  226. if (al->p + adj_size + sizeof(tal_header_t) < al->buffer + al->size) {
  227. header = (tal_header_t *)al->p;
  228. header->size = adj_size;
  229. #ifdef TAL_DEBUG
  230. { int ofs = strlen(file) - TAL_DEBUG_FILE_LEN;
  231. strncpy(header->file_name, file + (ofs > 0 ? ofs : 0), TAL_DEBUG_FILE_LEN);
  232. header->file_name[TAL_DEBUG_FILE_LEN] = 0;
  233. header->line_num = line; }
  234. #endif
  235. ret = al->p + sizeof(tal_header_t);
  236. al->p += adj_size + sizeof(tal_header_t);
  237. if (is_own) {
  238. header = (((tal_header_t *)p) - 1);
  239. memcpy(ret, p, header->size);
  240. #ifdef TAL_DEBUG
  241. header->line_num = -header->line_num;
  242. #endif
  243. } else {
  244. al->nb_allocs++;
  245. }
  246. #ifdef TAL_INFO
  247. if (al->nb_peak < al->nb_allocs)
  248. al->nb_peak = al->nb_allocs;
  249. if (al->peak_p < al->p)
  250. al->peak_p = al->p;
  251. al->nb_total++;
  252. #endif
  253. return ret;
  254. } else if (is_own) {
  255. al->nb_allocs--;
  256. ret = tal_realloc(*pal, 0, size);
  257. header = (((tal_header_t *)p) - 1);
  258. memcpy(ret, p, header->size);
  259. #ifdef TAL_DEBUG
  260. header->line_num = -header->line_num;
  261. #endif
  262. return ret;
  263. }
  264. if (al->next) {
  265. al = al->next;
  266. } else {
  267. TinyAlloc *bottom = al, *next = al->top ? al->top : al;
  268. al = tal_new(pal, next->limit, next->size * 2);
  269. al->next = next;
  270. bottom->top = al;
  271. }
  272. goto tail_call;
  273. }
  274. if (is_own) {
  275. al->nb_allocs--;
  276. ret = tcc_malloc(size);
  277. header = (((tal_header_t *)p) - 1);
  278. memcpy(ret, p, header->size);
  279. #ifdef TAL_DEBUG
  280. header->line_num = -header->line_num;
  281. #endif
  282. } else if (al->next) {
  283. al = al->next;
  284. goto tail_call;
  285. } else
  286. ret = tcc_realloc(p, size);
  287. #ifdef TAL_INFO
  288. al->nb_missed++;
  289. #endif
  290. return ret;
  291. }
  292. #endif /* USE_TAL */
  293. /* ------------------------------------------------------------------------- */
  294. /* CString handling */
  295. static void cstr_realloc(CString *cstr, int new_size)
  296. {
  297. int size;
  298. size = cstr->size_allocated;
  299. if (size < 8)
  300. size = 8; /* no need to allocate a too small first string */
  301. while (size < new_size)
  302. size = size * 2;
  303. cstr->data = tal_realloc(cstr_alloc, cstr->data, size);
  304. cstr->size_allocated = size;
  305. }
  306. /* add a byte */
  307. ST_INLN void cstr_ccat(CString *cstr, int ch)
  308. {
  309. int size;
  310. size = cstr->size + 1;
  311. if (size > cstr->size_allocated)
  312. cstr_realloc(cstr, size);
  313. ((unsigned char *)cstr->data)[size - 1] = ch;
  314. cstr->size = size;
  315. }
  316. ST_FUNC void cstr_cat(CString *cstr, const char *str, int len)
  317. {
  318. int size;
  319. if (len <= 0)
  320. len = strlen(str) + 1 + len;
  321. size = cstr->size + len;
  322. if (size > cstr->size_allocated)
  323. cstr_realloc(cstr, size);
  324. memmove(((unsigned char *)cstr->data) + cstr->size, str, len);
  325. cstr->size = size;
  326. }
  327. /* add a wide char */
  328. ST_FUNC void cstr_wccat(CString *cstr, int ch)
  329. {
  330. int size;
  331. size = cstr->size + sizeof(nwchar_t);
  332. if (size > cstr->size_allocated)
  333. cstr_realloc(cstr, size);
  334. *(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch;
  335. cstr->size = size;
  336. }
  337. ST_FUNC void cstr_new(CString *cstr)
  338. {
  339. memset(cstr, 0, sizeof(CString));
  340. }
  341. /* free string and reset it to NULL */
  342. ST_FUNC void cstr_free(CString *cstr)
  343. {
  344. tal_free(cstr_alloc, cstr->data);
  345. cstr_new(cstr);
  346. }
  347. /* reset string to empty */
  348. ST_FUNC void cstr_reset(CString *cstr)
  349. {
  350. cstr->size = 0;
  351. }
  352. /* XXX: unicode ? */
  353. static void add_char(CString *cstr, int c)
  354. {
  355. if (c == '\'' || c == '\"' || c == '\\') {
  356. /* XXX: could be more precise if char or string */
  357. cstr_ccat(cstr, '\\');
  358. }
  359. if (c >= 32 && c <= 126) {
  360. cstr_ccat(cstr, c);
  361. } else {
  362. cstr_ccat(cstr, '\\');
  363. if (c == '\n') {
  364. cstr_ccat(cstr, 'n');
  365. } else {
  366. cstr_ccat(cstr, '0' + ((c >> 6) & 7));
  367. cstr_ccat(cstr, '0' + ((c >> 3) & 7));
  368. cstr_ccat(cstr, '0' + (c & 7));
  369. }
  370. }
  371. }
  372. /* ------------------------------------------------------------------------- */
  373. /* allocate a new token */
  374. static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
  375. {
  376. TokenSym *ts, **ptable;
  377. int i;
  378. if (tok_ident >= SYM_FIRST_ANOM)
  379. tcc_error("memory full (symbols)");
  380. /* expand token table if needed */
  381. i = tok_ident - TOK_IDENT;
  382. if ((i % TOK_ALLOC_INCR) == 0) {
  383. ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
  384. table_ident = ptable;
  385. }
  386. ts = tal_realloc(toksym_alloc, 0, sizeof(TokenSym) + len);
  387. table_ident[i] = ts;
  388. ts->tok = tok_ident++;
  389. ts->sym_define = NULL;
  390. ts->sym_label = NULL;
  391. ts->sym_struct = NULL;
  392. ts->sym_identifier = NULL;
  393. ts->len = len;
  394. ts->hash_next = NULL;
  395. memcpy(ts->str, str, len);
  396. ts->str[len] = '\0';
  397. *pts = ts;
  398. return ts;
  399. }
  400. #define TOK_HASH_INIT 1
  401. #define TOK_HASH_FUNC(h, c) ((h) + ((h) << 5) + ((h) >> 27) + (c))
  402. /* find a token and add it if not found */
  403. ST_FUNC TokenSym *tok_alloc(const char *str, int len)
  404. {
  405. TokenSym *ts, **pts;
  406. int i;
  407. unsigned int h;
  408. h = TOK_HASH_INIT;
  409. for(i=0;i<len;i++)
  410. h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
  411. h &= (TOK_HASH_SIZE - 1);
  412. pts = &hash_ident[h];
  413. for(;;) {
  414. ts = *pts;
  415. if (!ts)
  416. break;
  417. if (ts->len == len && !memcmp(ts->str, str, len))
  418. return ts;
  419. pts = &(ts->hash_next);
  420. }
  421. return tok_alloc_new(pts, str, len);
  422. }
  423. /* XXX: buffer overflow */
  424. /* XXX: float tokens */
  425. ST_FUNC const char *get_tok_str(int v, CValue *cv)
  426. {
  427. char *p;
  428. int i, len;
  429. cstr_reset(&cstr_buf);
  430. p = cstr_buf.data;
  431. switch(v) {
  432. case TOK_CINT:
  433. case TOK_CUINT:
  434. case TOK_CLONG:
  435. case TOK_CULONG:
  436. case TOK_CLLONG:
  437. case TOK_CULLONG:
  438. /* XXX: not quite exact, but only useful for testing */
  439. #ifdef _WIN32
  440. sprintf(p, "%u", (unsigned)cv->i);
  441. #else
  442. sprintf(p, "%llu", (unsigned long long)cv->i);
  443. #endif
  444. break;
  445. case TOK_LCHAR:
  446. cstr_ccat(&cstr_buf, 'L');
  447. case TOK_CCHAR:
  448. cstr_ccat(&cstr_buf, '\'');
  449. add_char(&cstr_buf, cv->i);
  450. cstr_ccat(&cstr_buf, '\'');
  451. cstr_ccat(&cstr_buf, '\0');
  452. break;
  453. case TOK_PPNUM:
  454. case TOK_PPSTR:
  455. return (char*)cv->str.data;
  456. case TOK_LSTR:
  457. cstr_ccat(&cstr_buf, 'L');
  458. case TOK_STR:
  459. cstr_ccat(&cstr_buf, '\"');
  460. if (v == TOK_STR) {
  461. len = cv->str.size - 1;
  462. for(i=0;i<len;i++)
  463. add_char(&cstr_buf, ((unsigned char *)cv->str.data)[i]);
  464. } else {
  465. len = (cv->str.size / sizeof(nwchar_t)) - 1;
  466. for(i=0;i<len;i++)
  467. add_char(&cstr_buf, ((nwchar_t *)cv->str.data)[i]);
  468. }
  469. cstr_ccat(&cstr_buf, '\"');
  470. cstr_ccat(&cstr_buf, '\0');
  471. break;
  472. case TOK_CFLOAT:
  473. cstr_cat(&cstr_buf, "<float>", 0);
  474. break;
  475. case TOK_CDOUBLE:
  476. cstr_cat(&cstr_buf, "<double>", 0);
  477. break;
  478. case TOK_CLDOUBLE:
  479. cstr_cat(&cstr_buf, "<long double>", 0);
  480. break;
  481. case TOK_LINENUM:
  482. cstr_cat(&cstr_buf, "<linenumber>", 0);
  483. break;
  484. /* above tokens have value, the ones below don't */
  485. case TOK_LT:
  486. v = '<';
  487. goto addv;
  488. case TOK_GT:
  489. v = '>';
  490. goto addv;
  491. case TOK_DOTS:
  492. return strcpy(p, "...");
  493. case TOK_A_SHL:
  494. return strcpy(p, "<<=");
  495. case TOK_A_SAR:
  496. return strcpy(p, ">>=");
  497. case TOK_EOF:
  498. return strcpy(p, "<eof>");
  499. default:
  500. if (v < TOK_IDENT) {
  501. /* search in two bytes table */
  502. const unsigned char *q = tok_two_chars;
  503. while (*q) {
  504. if (q[2] == v) {
  505. *p++ = q[0];
  506. *p++ = q[1];
  507. *p = '\0';
  508. return cstr_buf.data;
  509. }
  510. q += 3;
  511. }
  512. if (v >= 127) {
  513. sprintf(cstr_buf.data, "<%02x>", v);
  514. return cstr_buf.data;
  515. }
  516. addv:
  517. *p++ = v;
  518. *p = '\0';
  519. } else if (v < tok_ident) {
  520. return table_ident[v - TOK_IDENT]->str;
  521. } else if (v >= SYM_FIRST_ANOM) {
  522. /* special name for anonymous symbol */
  523. sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
  524. } else {
  525. /* should never happen */
  526. return NULL;
  527. }
  528. break;
  529. }
  530. return cstr_buf.data;
  531. }
  532. /* return the current character, handling end of block if necessary
  533. (but not stray) */
  534. ST_FUNC int handle_eob(void)
  535. {
  536. BufferedFile *bf = file;
  537. int len;
  538. /* only tries to read if really end of buffer */
  539. if (bf->buf_ptr >= bf->buf_end) {
  540. if (bf->fd >= 0) {
  541. #if defined(PARSE_DEBUG)
  542. len = 1;
  543. #else
  544. len = IO_BUF_SIZE;
  545. #endif
  546. len = read(bf->fd, bf->buffer, len);
  547. if (len < 0)
  548. len = 0;
  549. } else {
  550. len = 0;
  551. }
  552. total_bytes += len;
  553. bf->buf_ptr = bf->buffer;
  554. bf->buf_end = bf->buffer + len;
  555. *bf->buf_end = CH_EOB;
  556. }
  557. if (bf->buf_ptr < bf->buf_end) {
  558. return bf->buf_ptr[0];
  559. } else {
  560. bf->buf_ptr = bf->buf_end;
  561. return CH_EOF;
  562. }
  563. }
  564. /* read next char from current input file and handle end of input buffer */
  565. ST_INLN void inp(void)
  566. {
  567. ch = *(++(file->buf_ptr));
  568. /* end of buffer/file handling */
  569. if (ch == CH_EOB)
  570. ch = handle_eob();
  571. }
  572. /* handle '\[\r]\n' */
  573. static int handle_stray_noerror(void)
  574. {
  575. while (ch == '\\') {
  576. inp();
  577. if (ch == '\n') {
  578. file->line_num++;
  579. inp();
  580. } else if (ch == '\r') {
  581. inp();
  582. if (ch != '\n')
  583. goto fail;
  584. file->line_num++;
  585. inp();
  586. } else {
  587. fail:
  588. return 1;
  589. }
  590. }
  591. return 0;
  592. }
  593. static void handle_stray(void)
  594. {
  595. if (handle_stray_noerror())
  596. tcc_error("stray '\\' in program");
  597. }
  598. /* skip the stray and handle the \\n case. Output an error if
  599. incorrect char after the stray */
  600. static int handle_stray1(uint8_t *p)
  601. {
  602. int c;
  603. file->buf_ptr = p;
  604. if (p >= file->buf_end) {
  605. c = handle_eob();
  606. if (c != '\\')
  607. return c;
  608. p = file->buf_ptr;
  609. }
  610. ch = *p;
  611. if (handle_stray_noerror()) {
  612. if (!(parse_flags & PARSE_FLAG_ACCEPT_STRAYS))
  613. tcc_error("stray '\\' in program");
  614. *--file->buf_ptr = '\\';
  615. }
  616. p = file->buf_ptr;
  617. c = *p;
  618. return c;
  619. }
  620. /* handle just the EOB case, but not stray */
  621. #define PEEKC_EOB(c, p)\
  622. {\
  623. p++;\
  624. c = *p;\
  625. if (c == '\\') {\
  626. file->buf_ptr = p;\
  627. c = handle_eob();\
  628. p = file->buf_ptr;\
  629. }\
  630. }
  631. /* handle the complicated stray case */
  632. #define PEEKC(c, p)\
  633. {\
  634. p++;\
  635. c = *p;\
  636. if (c == '\\') {\
  637. c = handle_stray1(p);\
  638. p = file->buf_ptr;\
  639. }\
  640. }
  641. /* input with '\[\r]\n' handling. Note that this function cannot
  642. handle other characters after '\', so you cannot call it inside
  643. strings or comments */
  644. ST_FUNC void minp(void)
  645. {
  646. inp();
  647. if (ch == '\\')
  648. handle_stray();
  649. }
  650. /* single line C++ comments */
  651. static uint8_t *parse_line_comment(uint8_t *p)
  652. {
  653. int c;
  654. p++;
  655. for(;;) {
  656. c = *p;
  657. redo:
  658. if (c == '\n' || c == CH_EOF) {
  659. break;
  660. } else if (c == '\\') {
  661. file->buf_ptr = p;
  662. c = handle_eob();
  663. p = file->buf_ptr;
  664. if (c == '\\') {
  665. PEEKC_EOB(c, p);
  666. if (c == '\n') {
  667. file->line_num++;
  668. PEEKC_EOB(c, p);
  669. } else if (c == '\r') {
  670. PEEKC_EOB(c, p);
  671. if (c == '\n') {
  672. file->line_num++;
  673. PEEKC_EOB(c, p);
  674. }
  675. }
  676. } else {
  677. goto redo;
  678. }
  679. } else {
  680. p++;
  681. }
  682. }
  683. return p;
  684. }
  685. /* C comments */
  686. ST_FUNC uint8_t *parse_comment(uint8_t *p)
  687. {
  688. int c;
  689. p++;
  690. for(;;) {
  691. /* fast skip loop */
  692. for(;;) {
  693. c = *p;
  694. if (c == '\n' || c == '*' || c == '\\')
  695. break;
  696. p++;
  697. c = *p;
  698. if (c == '\n' || c == '*' || c == '\\')
  699. break;
  700. p++;
  701. }
  702. /* now we can handle all the cases */
  703. if (c == '\n') {
  704. file->line_num++;
  705. p++;
  706. } else if (c == '*') {
  707. p++;
  708. for(;;) {
  709. c = *p;
  710. if (c == '*') {
  711. p++;
  712. } else if (c == '/') {
  713. goto end_of_comment;
  714. } else if (c == '\\') {
  715. file->buf_ptr = p;
  716. c = handle_eob();
  717. p = file->buf_ptr;
  718. if (c == CH_EOF)
  719. tcc_error("unexpected end of file in comment");
  720. if (c == '\\') {
  721. /* skip '\[\r]\n', otherwise just skip the stray */
  722. while (c == '\\') {
  723. PEEKC_EOB(c, p);
  724. if (c == '\n') {
  725. file->line_num++;
  726. PEEKC_EOB(c, p);
  727. } else if (c == '\r') {
  728. PEEKC_EOB(c, p);
  729. if (c == '\n') {
  730. file->line_num++;
  731. PEEKC_EOB(c, p);
  732. }
  733. } else {
  734. goto after_star;
  735. }
  736. }
  737. }
  738. } else {
  739. break;
  740. }
  741. }
  742. after_star: ;
  743. } else {
  744. /* stray, eob or eof */
  745. file->buf_ptr = p;
  746. c = handle_eob();
  747. p = file->buf_ptr;
  748. if (c == CH_EOF) {
  749. tcc_error("unexpected end of file in comment");
  750. } else if (c == '\\') {
  751. p++;
  752. }
  753. }
  754. }
  755. end_of_comment:
  756. p++;
  757. return p;
  758. }
  759. ST_FUNC int set_idnum(int c, int val)
  760. {
  761. int prev = isidnum_table[c - CH_EOF];
  762. isidnum_table[c - CH_EOF] = val;
  763. return prev;
  764. }
  765. #define cinp minp
  766. static inline void skip_spaces(void)
  767. {
  768. while (isidnum_table[ch - CH_EOF] & IS_SPC)
  769. cinp();
  770. }
  771. static inline int check_space(int t, int *spc)
  772. {
  773. if (t < 256 && (isidnum_table[t - CH_EOF] & IS_SPC)) {
  774. if (*spc)
  775. return 1;
  776. *spc = 1;
  777. } else
  778. *spc = 0;
  779. return 0;
  780. }
  781. /* parse a string without interpreting escapes */
  782. static uint8_t *parse_pp_string(uint8_t *p,
  783. int sep, CString *str)
  784. {
  785. int c;
  786. p++;
  787. for(;;) {
  788. c = *p;
  789. if (c == sep) {
  790. break;
  791. } else if (c == '\\') {
  792. file->buf_ptr = p;
  793. c = handle_eob();
  794. p = file->buf_ptr;
  795. if (c == CH_EOF) {
  796. unterminated_string:
  797. /* XXX: indicate line number of start of string */
  798. tcc_error("missing terminating %c character", sep);
  799. } else if (c == '\\') {
  800. /* escape : just skip \[\r]\n */
  801. PEEKC_EOB(c, p);
  802. if (c == '\n') {
  803. file->line_num++;
  804. p++;
  805. } else if (c == '\r') {
  806. PEEKC_EOB(c, p);
  807. if (c != '\n')
  808. expect("'\n' after '\r'");
  809. file->line_num++;
  810. p++;
  811. } else if (c == CH_EOF) {
  812. goto unterminated_string;
  813. } else {
  814. if (str) {
  815. cstr_ccat(str, '\\');
  816. cstr_ccat(str, c);
  817. }
  818. p++;
  819. }
  820. }
  821. } else if (c == '\n') {
  822. file->line_num++;
  823. goto add_char;
  824. } else if (c == '\r') {
  825. PEEKC_EOB(c, p);
  826. if (c != '\n') {
  827. if (str)
  828. cstr_ccat(str, '\r');
  829. } else {
  830. file->line_num++;
  831. goto add_char;
  832. }
  833. } else {
  834. add_char:
  835. if (str)
  836. cstr_ccat(str, c);
  837. p++;
  838. }
  839. }
  840. p++;
  841. return p;
  842. }
  843. /* skip block of text until #else, #elif or #endif. skip also pairs of
  844. #if/#endif */
  845. static void preprocess_skip(void)
  846. {
  847. int a, start_of_line, c, in_warn_or_error;
  848. uint8_t *p;
  849. p = file->buf_ptr;
  850. a = 0;
  851. redo_start:
  852. start_of_line = 1;
  853. in_warn_or_error = 0;
  854. for(;;) {
  855. redo_no_start:
  856. c = *p;
  857. switch(c) {
  858. case ' ':
  859. case '\t':
  860. case '\f':
  861. case '\v':
  862. case '\r':
  863. p++;
  864. goto redo_no_start;
  865. case '\n':
  866. file->line_num++;
  867. p++;
  868. goto redo_start;
  869. case '\\':
  870. file->buf_ptr = p;
  871. c = handle_eob();
  872. if (c == CH_EOF) {
  873. expect("#endif");
  874. } else if (c == '\\') {
  875. ch = file->buf_ptr[0];
  876. handle_stray_noerror();
  877. }
  878. p = file->buf_ptr;
  879. goto redo_no_start;
  880. /* skip strings */
  881. case '\"':
  882. case '\'':
  883. if (in_warn_or_error)
  884. goto _default;
  885. p = parse_pp_string(p, c, NULL);
  886. break;
  887. /* skip comments */
  888. case '/':
  889. if (in_warn_or_error)
  890. goto _default;
  891. file->buf_ptr = p;
  892. ch = *p;
  893. minp();
  894. p = file->buf_ptr;
  895. if (ch == '*') {
  896. p = parse_comment(p);
  897. } else if (ch == '/') {
  898. p = parse_line_comment(p);
  899. }
  900. break;
  901. case '#':
  902. p++;
  903. if (start_of_line) {
  904. file->buf_ptr = p;
  905. next_nomacro();
  906. p = file->buf_ptr;
  907. if (a == 0 &&
  908. (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
  909. goto the_end;
  910. if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
  911. a++;
  912. else if (tok == TOK_ENDIF)
  913. a--;
  914. else if( tok == TOK_ERROR || tok == TOK_WARNING)
  915. in_warn_or_error = 1;
  916. else if (tok == TOK_LINEFEED)
  917. goto redo_start;
  918. else if (parse_flags & PARSE_FLAG_ASM_FILE)
  919. p = parse_line_comment(p - 1);
  920. } else if (parse_flags & PARSE_FLAG_ASM_FILE)
  921. p = parse_line_comment(p - 1);
  922. break;
  923. _default:
  924. default:
  925. p++;
  926. break;
  927. }
  928. start_of_line = 0;
  929. }
  930. the_end: ;
  931. file->buf_ptr = p;
  932. }
  933. #if 0
  934. /* return the number of additional 'ints' necessary to store the
  935. token */
  936. static inline int tok_size(const int *p)
  937. {
  938. switch(*p) {
  939. /* 4 bytes */
  940. case TOK_CINT:
  941. case TOK_CUINT:
  942. case TOK_CCHAR:
  943. case TOK_LCHAR:
  944. case TOK_CFLOAT:
  945. case TOK_LINENUM:
  946. return 1 + 1;
  947. case TOK_STR:
  948. case TOK_LSTR:
  949. case TOK_PPNUM:
  950. case TOK_PPSTR:
  951. return 1 + ((sizeof(CString) + ((CString *)(p+1))->size + 3) >> 2);
  952. case TOK_CLONG:
  953. case TOK_CULONG:
  954. return 1 + LONG_SIZE / 4;
  955. case TOK_CDOUBLE:
  956. case TOK_CLLONG:
  957. case TOK_CULLONG:
  958. return 1 + 2;
  959. case TOK_CLDOUBLE:
  960. return 1 + LDOUBLE_SIZE / 4;
  961. default:
  962. return 1 + 0;
  963. }
  964. }
  965. #endif
  966. /* token string handling */
  967. ST_INLN void tok_str_new(TokenString *s)
  968. {
  969. s->str = NULL;
  970. s->len = s->lastlen = 0;
  971. s->allocated_len = 0;
  972. s->last_line_num = -1;
  973. }
  974. ST_FUNC TokenString *tok_str_alloc(void)
  975. {
  976. TokenString *str = tal_realloc(tokstr_alloc, 0, sizeof *str);
  977. tok_str_new(str);
  978. return str;
  979. }
  980. ST_FUNC int *tok_str_dup(TokenString *s)
  981. {
  982. int *str;
  983. str = tal_realloc(tokstr_alloc, 0, s->len * sizeof(int));
  984. memcpy(str, s->str, s->len * sizeof(int));
  985. return str;
  986. }
  987. ST_FUNC void tok_str_free_str(int *str)
  988. {
  989. tal_free(tokstr_alloc, str);
  990. }
  991. ST_FUNC void tok_str_free(TokenString *str)
  992. {
  993. tok_str_free_str(str->str);
  994. tal_free(tokstr_alloc, str);
  995. }
  996. ST_FUNC int *tok_str_realloc(TokenString *s, int new_size)
  997. {
  998. int *str, size;
  999. size = s->allocated_len;
  1000. if (size < 16)
  1001. size = 16;
  1002. while (size < new_size)
  1003. size = size * 2;
  1004. if (size > s->allocated_len) {
  1005. str = tal_realloc(tokstr_alloc, s->str, size * sizeof(int));
  1006. s->allocated_len = size;
  1007. s->str = str;
  1008. }
  1009. return s->str;
  1010. }
  1011. ST_FUNC void tok_str_add(TokenString *s, int t)
  1012. {
  1013. int len, *str;
  1014. len = s->len;
  1015. str = s->str;
  1016. if (len >= s->allocated_len)
  1017. str = tok_str_realloc(s, len + 1);
  1018. str[len++] = t;
  1019. s->len = len;
  1020. }
  1021. ST_FUNC void begin_macro(TokenString *str, int alloc)
  1022. {
  1023. str->alloc = alloc;
  1024. str->prev = macro_stack;
  1025. str->prev_ptr = macro_ptr;
  1026. str->save_line_num = file->line_num;
  1027. macro_ptr = str->str;
  1028. macro_stack = str;
  1029. }
  1030. ST_FUNC void end_macro(void)
  1031. {
  1032. TokenString *str = macro_stack;
  1033. macro_stack = str->prev;
  1034. macro_ptr = str->prev_ptr;
  1035. file->line_num = str->save_line_num;
  1036. if (str->alloc == 2) {
  1037. str->alloc = 3; /* just mark as finished */
  1038. } else {
  1039. tok_str_free(str);
  1040. }
  1041. }
  1042. static void tok_str_add2(TokenString *s, int t, CValue *cv)
  1043. {
  1044. int len, *str;
  1045. len = s->lastlen = s->len;
  1046. str = s->str;
  1047. /* allocate space for worst case */
  1048. if (len + TOK_MAX_SIZE >= s->allocated_len)
  1049. str = tok_str_realloc(s, len + TOK_MAX_SIZE + 1);
  1050. str[len++] = t;
  1051. switch(t) {
  1052. case TOK_CINT:
  1053. case TOK_CUINT:
  1054. case TOK_CCHAR:
  1055. case TOK_LCHAR:
  1056. case TOK_CFLOAT:
  1057. case TOK_LINENUM:
  1058. #if LONG_SIZE == 4
  1059. case TOK_CLONG:
  1060. case TOK_CULONG:
  1061. #endif
  1062. str[len++] = cv->tab[0];
  1063. break;
  1064. case TOK_PPNUM:
  1065. case TOK_PPSTR:
  1066. case TOK_STR:
  1067. case TOK_LSTR:
  1068. {
  1069. /* Insert the string into the int array. */
  1070. size_t nb_words =
  1071. 1 + (cv->str.size + sizeof(int) - 1) / sizeof(int);
  1072. if (len + nb_words >= s->allocated_len)
  1073. str = tok_str_realloc(s, len + nb_words + 1);
  1074. str[len] = cv->str.size;
  1075. memcpy(&str[len + 1], cv->str.data, cv->str.size);
  1076. len += nb_words;
  1077. }
  1078. break;
  1079. case TOK_CDOUBLE:
  1080. case TOK_CLLONG:
  1081. case TOK_CULLONG:
  1082. #if LONG_SIZE == 8
  1083. case TOK_CLONG:
  1084. case TOK_CULONG:
  1085. #endif
  1086. #if LDOUBLE_SIZE == 8
  1087. case TOK_CLDOUBLE:
  1088. #endif
  1089. str[len++] = cv->tab[0];
  1090. str[len++] = cv->tab[1];
  1091. break;
  1092. #if LDOUBLE_SIZE == 12
  1093. case TOK_CLDOUBLE:
  1094. str[len++] = cv->tab[0];
  1095. str[len++] = cv->tab[1];
  1096. str[len++] = cv->tab[2];
  1097. #elif LDOUBLE_SIZE == 16
  1098. case TOK_CLDOUBLE:
  1099. str[len++] = cv->tab[0];
  1100. str[len++] = cv->tab[1];
  1101. str[len++] = cv->tab[2];
  1102. str[len++] = cv->tab[3];
  1103. #elif LDOUBLE_SIZE != 8
  1104. #error add long double size support
  1105. #endif
  1106. break;
  1107. default:
  1108. break;
  1109. }
  1110. s->len = len;
  1111. }
  1112. /* add the current parse token in token string 's' */
  1113. ST_FUNC void tok_str_add_tok(TokenString *s)
  1114. {
  1115. CValue cval;
  1116. /* save line number info */
  1117. if (file->line_num != s->last_line_num) {
  1118. s->last_line_num = file->line_num;
  1119. cval.i = s->last_line_num;
  1120. tok_str_add2(s, TOK_LINENUM, &cval);
  1121. }
  1122. tok_str_add2(s, tok, &tokc);
  1123. }
  1124. /* get a token from an integer array and increment pointer
  1125. accordingly. we code it as a macro to avoid pointer aliasing. */
  1126. static inline void TOK_GET(int *t, const int **pp, CValue *cv)
  1127. {
  1128. const int *p = *pp;
  1129. int n, *tab;
  1130. tab = cv->tab;
  1131. switch(*t = *p++) {
  1132. #if LONG_SIZE == 4
  1133. case TOK_CLONG:
  1134. #endif
  1135. case TOK_CINT:
  1136. case TOK_CCHAR:
  1137. case TOK_LCHAR:
  1138. case TOK_LINENUM:
  1139. cv->i = *p++;
  1140. break;
  1141. #if LONG_SIZE == 4
  1142. case TOK_CULONG:
  1143. #endif
  1144. case TOK_CUINT:
  1145. cv->i = (unsigned)*p++;
  1146. break;
  1147. case TOK_CFLOAT:
  1148. tab[0] = *p++;
  1149. break;
  1150. case TOK_STR:
  1151. case TOK_LSTR:
  1152. case TOK_PPNUM:
  1153. case TOK_PPSTR:
  1154. cv->str.size = *p++;
  1155. cv->str.data = p;
  1156. p += (cv->str.size + sizeof(int) - 1) / sizeof(int);
  1157. break;
  1158. case TOK_CDOUBLE:
  1159. case TOK_CLLONG:
  1160. case TOK_CULLONG:
  1161. #if LONG_SIZE == 8
  1162. case TOK_CLONG:
  1163. case TOK_CULONG:
  1164. #endif
  1165. n = 2;
  1166. goto copy;
  1167. case TOK_CLDOUBLE:
  1168. #if LDOUBLE_SIZE == 16
  1169. n = 4;
  1170. #elif LDOUBLE_SIZE == 12
  1171. n = 3;
  1172. #elif LDOUBLE_SIZE == 8
  1173. n = 2;
  1174. #else
  1175. # error add long double size support
  1176. #endif
  1177. copy:
  1178. do
  1179. *tab++ = *p++;
  1180. while (--n);
  1181. break;
  1182. default:
  1183. break;
  1184. }
  1185. *pp = p;
  1186. }
  1187. static int macro_is_equal(const int *a, const int *b)
  1188. {
  1189. CValue cv;
  1190. int t;
  1191. if (!a || !b)
  1192. return 1;
  1193. while (*a && *b) {
  1194. /* first time preallocate macro_equal_buf, next time only reset position to start */
  1195. cstr_reset(&macro_equal_buf);
  1196. TOK_GET(&t, &a, &cv);
  1197. cstr_cat(&macro_equal_buf, get_tok_str(t, &cv), 0);
  1198. TOK_GET(&t, &b, &cv);
  1199. if (strcmp(macro_equal_buf.data, get_tok_str(t, &cv)))
  1200. return 0;
  1201. }
  1202. return !(*a || *b);
  1203. }
  1204. /* defines handling */
  1205. ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg)
  1206. {
  1207. Sym *s, *o;
  1208. o = define_find(v);
  1209. s = sym_push2(&define_stack, v, macro_type, 0);
  1210. s->d = str;
  1211. s->next = first_arg;
  1212. table_ident[v - TOK_IDENT]->sym_define = s;
  1213. if (o && !macro_is_equal(o->d, s->d))
  1214. tcc_warning("%s redefined", get_tok_str(v, NULL));
  1215. }
  1216. /* undefined a define symbol. Its name is just set to zero */
  1217. ST_FUNC void define_undef(Sym *s)
  1218. {
  1219. int v = s->v;
  1220. if (v >= TOK_IDENT && v < tok_ident)
  1221. table_ident[v - TOK_IDENT]->sym_define = NULL;
  1222. }
  1223. ST_INLN Sym *define_find(int v)
  1224. {
  1225. v -= TOK_IDENT;
  1226. if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
  1227. return NULL;
  1228. return table_ident[v]->sym_define;
  1229. }
  1230. /* free define stack until top reaches 'b' */
  1231. ST_FUNC void free_defines(Sym *b)
  1232. {
  1233. while (define_stack != b) {
  1234. Sym *top = define_stack;
  1235. define_stack = top->prev;
  1236. tok_str_free_str(top->d);
  1237. define_undef(top);
  1238. sym_free(top);
  1239. }
  1240. /* restore remaining (-D or predefined) symbols if they were
  1241. #undef'd in the file */
  1242. while (b) {
  1243. int v = b->v;
  1244. if (v >= TOK_IDENT && v < tok_ident) {
  1245. Sym **d = &table_ident[v - TOK_IDENT]->sym_define;
  1246. if (!*d)
  1247. *d = b;
  1248. }
  1249. b = b->prev;
  1250. }
  1251. }
  1252. /* label lookup */
  1253. ST_FUNC Sym *label_find(int v)
  1254. {
  1255. v -= TOK_IDENT;
  1256. if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
  1257. return NULL;
  1258. return table_ident[v]->sym_label;
  1259. }
  1260. ST_FUNC Sym *label_push(Sym **ptop, int v, int flags)
  1261. {
  1262. Sym *s, **ps;
  1263. s = sym_push2(ptop, v, 0, 0);
  1264. s->r = flags;
  1265. ps = &table_ident[v - TOK_IDENT]->sym_label;
  1266. if (ptop == &global_label_stack) {
  1267. /* modify the top most local identifier, so that
  1268. sym_identifier will point to 's' when popped */
  1269. while (*ps != NULL)
  1270. ps = &(*ps)->prev_tok;
  1271. }
  1272. s->prev_tok = *ps;
  1273. *ps = s;
  1274. return s;
  1275. }
  1276. /* pop labels until element last is reached. Look if any labels are
  1277. undefined. Define symbols if '&&label' was used. */
  1278. ST_FUNC void label_pop(Sym **ptop, Sym *slast, int keep)
  1279. {
  1280. Sym *s, *s1;
  1281. for(s = *ptop; s != slast; s = s1) {
  1282. s1 = s->prev;
  1283. if (s->r == LABEL_DECLARED) {
  1284. tcc_warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
  1285. } else if (s->r == LABEL_FORWARD) {
  1286. tcc_error("label '%s' used but not defined",
  1287. get_tok_str(s->v, NULL));
  1288. } else {
  1289. if (s->c) {
  1290. /* define corresponding symbol. A size of
  1291. 1 is put. */
  1292. put_extern_sym(s, cur_text_section, s->jnext, 1);
  1293. }
  1294. }
  1295. /* remove label */
  1296. table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
  1297. if (!keep)
  1298. sym_free(s);
  1299. }
  1300. if (!keep)
  1301. *ptop = slast;
  1302. }
  1303. /* fake the nth "#if defined test_..." for tcc -dt -run */
  1304. static void maybe_run_test(TCCState *s)
  1305. {
  1306. const char *p;
  1307. if (s->include_stack_ptr != s->include_stack)
  1308. return;
  1309. p = get_tok_str(tok, NULL);
  1310. if (0 != memcmp(p, "test_", 5))
  1311. return;
  1312. if (0 != --s->run_test)
  1313. return;
  1314. fprintf(s->ppfp, "\n[%s]\n" + !(s->dflag & 32), p), fflush(s->ppfp);
  1315. define_push(tok, MACRO_OBJ, NULL, NULL);
  1316. }
  1317. /* eval an expression for #if/#elif */
  1318. static int expr_preprocess(void)
  1319. {
  1320. int c, t;
  1321. TokenString *str;
  1322. str = tok_str_alloc();
  1323. pp_expr = 1;
  1324. while (tok != TOK_LINEFEED && tok != TOK_EOF) {
  1325. next(); /* do macro subst */
  1326. if (tok == TOK_DEFINED) {
  1327. next_nomacro();
  1328. t = tok;
  1329. if (t == '(')
  1330. next_nomacro();
  1331. if (tok < TOK_IDENT)
  1332. expect("identifier");
  1333. if (tcc_state->run_test)
  1334. maybe_run_test(tcc_state);
  1335. c = define_find(tok) != 0;
  1336. if (t == '(') {
  1337. next_nomacro();
  1338. if (tok != ')')
  1339. expect("')'");
  1340. }
  1341. tok = TOK_CINT;
  1342. tokc.i = c;
  1343. } else if (tok >= TOK_IDENT) {
  1344. /* if undefined macro */
  1345. tok = TOK_CINT;
  1346. tokc.i = 0;
  1347. }
  1348. tok_str_add_tok(str);
  1349. }
  1350. pp_expr = 0;
  1351. tok_str_add(str, -1); /* simulate end of file */
  1352. tok_str_add(str, 0);
  1353. /* now evaluate C constant expression */
  1354. begin_macro(str, 1);
  1355. next();
  1356. c = expr_const();
  1357. end_macro();
  1358. return c != 0;
  1359. }
  1360. /* parse after #define */
  1361. ST_FUNC void parse_define(void)
  1362. {
  1363. Sym *s, *first, **ps;
  1364. int v, t, varg, is_vaargs, spc;
  1365. int saved_parse_flags = parse_flags;
  1366. v = tok;
  1367. if (v < TOK_IDENT || v == TOK_DEFINED)
  1368. tcc_error("invalid macro name '%s'", get_tok_str(tok, &tokc));
  1369. /* XXX: should check if same macro (ANSI) */
  1370. first = NULL;
  1371. t = MACRO_OBJ;
  1372. /* We have to parse the whole define as if not in asm mode, in particular
  1373. no line comment with '#' must be ignored. Also for function
  1374. macros the argument list must be parsed without '.' being an ID
  1375. character. */
  1376. parse_flags = ((parse_flags & ~PARSE_FLAG_ASM_FILE) | PARSE_FLAG_SPACES);
  1377. /* '(' must be just after macro definition for MACRO_FUNC */
  1378. next_nomacro_spc();
  1379. if (tok == '(') {
  1380. int dotid = set_idnum('.', 0);
  1381. next_nomacro();
  1382. ps = &first;
  1383. if (tok != ')') for (;;) {
  1384. varg = tok;
  1385. next_nomacro();
  1386. is_vaargs = 0;
  1387. if (varg == TOK_DOTS) {
  1388. varg = TOK___VA_ARGS__;
  1389. is_vaargs = 1;
  1390. } else if (tok == TOK_DOTS && gnu_ext) {
  1391. is_vaargs = 1;
  1392. next_nomacro();
  1393. }
  1394. if (varg < TOK_IDENT)
  1395. bad_list:
  1396. tcc_error("bad macro parameter list");
  1397. s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
  1398. *ps = s;
  1399. ps = &s->next;
  1400. if (tok == ')')
  1401. break;
  1402. if (tok != ',' || is_vaargs)
  1403. goto bad_list;
  1404. next_nomacro();
  1405. }
  1406. next_nomacro_spc();
  1407. t = MACRO_FUNC;
  1408. set_idnum('.', dotid);
  1409. }
  1410. tokstr_buf.len = 0;
  1411. spc = 2;
  1412. parse_flags |= PARSE_FLAG_ACCEPT_STRAYS | PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED;
  1413. /* The body of a macro definition should be parsed such that identifiers
  1414. are parsed like the file mode determines (i.e. with '.' being an
  1415. ID character in asm mode). But '#' should be retained instead of
  1416. regarded as line comment leader, so still don't set ASM_FILE
  1417. in parse_flags. */
  1418. while (tok != TOK_LINEFEED && tok != TOK_EOF) {
  1419. /* remove spaces around ## and after '#' */
  1420. if (TOK_TWOSHARPS == tok) {
  1421. if (2 == spc)
  1422. goto bad_twosharp;
  1423. if (1 == spc)
  1424. --tokstr_buf.len;
  1425. spc = 3;
  1426. tok = TOK_PPJOIN;
  1427. } else if ('#' == tok) {
  1428. spc = 4;
  1429. } else if (check_space(tok, &spc)) {
  1430. goto skip;
  1431. }
  1432. tok_str_add2(&tokstr_buf, tok, &tokc);
  1433. skip:
  1434. next_nomacro_spc();
  1435. }
  1436. parse_flags = saved_parse_flags;
  1437. if (spc == 1)
  1438. --tokstr_buf.len; /* remove trailing space */
  1439. tok_str_add(&tokstr_buf, 0);
  1440. if (3 == spc)
  1441. bad_twosharp:
  1442. tcc_error("'##' cannot appear at either end of macro");
  1443. define_push(v, t, tok_str_dup(&tokstr_buf), first);
  1444. }
  1445. static CachedInclude *search_cached_include(TCCState *s1, const char *filename, int add)
  1446. {
  1447. const unsigned char *s;
  1448. unsigned int h;
  1449. CachedInclude *e;
  1450. int i;
  1451. h = TOK_HASH_INIT;
  1452. s = (unsigned char *) filename;
  1453. while (*s) {
  1454. #ifdef _WIN32
  1455. h = TOK_HASH_FUNC(h, toup(*s));
  1456. #else
  1457. h = TOK_HASH_FUNC(h, *s);
  1458. #endif
  1459. s++;
  1460. }
  1461. h &= (CACHED_INCLUDES_HASH_SIZE - 1);
  1462. i = s1->cached_includes_hash[h];
  1463. for(;;) {
  1464. if (i == 0)
  1465. break;
  1466. e = s1->cached_includes[i - 1];
  1467. if (0 == PATHCMP(e->filename, filename))
  1468. return e;
  1469. i = e->hash_next;
  1470. }
  1471. if (!add)
  1472. return NULL;
  1473. e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
  1474. strcpy(e->filename, filename);
  1475. e->ifndef_macro = e->once = 0;
  1476. dynarray_add(&s1->cached_includes, &s1->nb_cached_includes, e);
  1477. /* add in hash table */
  1478. e->hash_next = s1->cached_includes_hash[h];
  1479. s1->cached_includes_hash[h] = s1->nb_cached_includes;
  1480. #ifdef INC_DEBUG
  1481. printf("adding cached '%s'\n", filename);
  1482. #endif
  1483. return e;
  1484. }
  1485. static void pragma_parse(TCCState *s1)
  1486. {
  1487. next_nomacro();
  1488. if (tok == TOK_push_macro || tok == TOK_pop_macro) {
  1489. int t = tok, v;
  1490. Sym *s;
  1491. if (next(), tok != '(')
  1492. goto pragma_err;
  1493. if (next(), tok != TOK_STR)
  1494. goto pragma_err;
  1495. v = tok_alloc(tokc.str.data, tokc.str.size - 1)->tok;
  1496. if (next(), tok != ')')
  1497. goto pragma_err;
  1498. if (t == TOK_push_macro) {
  1499. while (NULL == (s = define_find(v)))
  1500. define_push(v, 0, NULL, NULL);
  1501. s->type.ref = s; /* set push boundary */
  1502. } else {
  1503. for (s = define_stack; s; s = s->prev)
  1504. if (s->v == v && s->type.ref == s) {
  1505. s->type.ref = NULL;
  1506. break;
  1507. }
  1508. }
  1509. if (s)
  1510. table_ident[v - TOK_IDENT]->sym_define = s->d ? s : NULL;
  1511. else
  1512. tcc_warning("unbalanced #pragma pop_macro");
  1513. pp_debug_tok = t, pp_debug_symv = v;
  1514. } else if (tok == TOK_once) {
  1515. search_cached_include(s1, file->filename, 1)->once = pp_once;
  1516. } else if (s1->output_type == TCC_OUTPUT_PREPROCESS) {
  1517. /* tcc -E: keep pragmas below unchanged */
  1518. unget_tok(' ');
  1519. unget_tok(TOK_PRAGMA);
  1520. unget_tok('#');
  1521. unget_tok(TOK_LINEFEED);
  1522. } else if (tok == TOK_pack) {
  1523. /* This may be:
  1524. #pragma pack(1) // set
  1525. #pragma pack() // reset to default
  1526. #pragma pack(push,1) // push & set
  1527. #pragma pack(pop) // restore previous */
  1528. next();
  1529. skip('(');
  1530. if (tok == TOK_ASM_pop) {
  1531. next();
  1532. if (s1->pack_stack_ptr <= s1->pack_stack) {
  1533. stk_error:
  1534. tcc_error("out of pack stack");
  1535. }
  1536. s1->pack_stack_ptr--;
  1537. } else {
  1538. int val = 0;
  1539. if (tok != ')') {
  1540. if (tok == TOK_ASM_push) {
  1541. next();
  1542. if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
  1543. goto stk_error;
  1544. s1->pack_stack_ptr++;
  1545. skip(',');
  1546. }
  1547. if (tok != TOK_CINT)
  1548. goto pragma_err;
  1549. val = tokc.i;
  1550. if (val < 1 || val > 16 || (val & (val - 1)) != 0)
  1551. goto pragma_err;
  1552. next();
  1553. }
  1554. *s1->pack_stack_ptr = val;
  1555. }
  1556. if (tok != ')')
  1557. goto pragma_err;
  1558. } else if (tok == TOK_comment) {
  1559. char *p; int t;
  1560. next();
  1561. skip('(');
  1562. t = tok;
  1563. next();
  1564. skip(',');
  1565. if (tok != TOK_STR)
  1566. goto pragma_err;
  1567. p = tcc_strdup((char *)tokc.str.data);
  1568. next();
  1569. if (tok != ')')
  1570. goto pragma_err;
  1571. if (t == TOK_lib) {
  1572. dynarray_add(&s1->pragma_libs, &s1->nb_pragma_libs, p);
  1573. } else {
  1574. if (t == TOK_option)
  1575. tcc_set_options(s1, p);
  1576. tcc_free(p);
  1577. }
  1578. } else if (s1->warn_unsupported) {
  1579. tcc_warning("#pragma %s is ignored", get_tok_str(tok, &tokc));
  1580. }
  1581. return;
  1582. pragma_err:
  1583. tcc_error("malformed #pragma directive");
  1584. return;
  1585. }
  1586. /* is_bof is true if first non space token at beginning of file */
  1587. ST_FUNC void preprocess(int is_bof)
  1588. {
  1589. TCCState *s1 = tcc_state;
  1590. int i, c, n, saved_parse_flags;
  1591. char buf[1024], *q;
  1592. Sym *s;
  1593. saved_parse_flags = parse_flags;
  1594. parse_flags = PARSE_FLAG_PREPROCESS
  1595. | PARSE_FLAG_TOK_NUM
  1596. | PARSE_FLAG_TOK_STR
  1597. | PARSE_FLAG_LINEFEED
  1598. | (parse_flags & PARSE_FLAG_ASM_FILE)
  1599. ;
  1600. next_nomacro();
  1601. redo:
  1602. switch(tok) {
  1603. case TOK_DEFINE:
  1604. pp_debug_tok = tok;
  1605. next_nomacro();
  1606. pp_debug_symv = tok;
  1607. parse_define();
  1608. break;
  1609. case TOK_UNDEF:
  1610. pp_debug_tok = tok;
  1611. next_nomacro();
  1612. pp_debug_symv = tok;
  1613. s = define_find(tok);
  1614. /* undefine symbol by putting an invalid name */
  1615. if (s)
  1616. define_undef(s);
  1617. break;
  1618. case TOK_INCLUDE:
  1619. case TOK_INCLUDE_NEXT:
  1620. ch = file->buf_ptr[0];
  1621. /* XXX: incorrect if comments : use next_nomacro with a special mode */
  1622. skip_spaces();
  1623. if (ch == '<') {
  1624. c = '>';
  1625. goto read_name;
  1626. } else if (ch == '\"') {
  1627. c = ch;
  1628. read_name:
  1629. inp();
  1630. q = buf;
  1631. while (ch != c && ch != '\n' && ch != CH_EOF) {
  1632. if ((q - buf) < sizeof(buf) - 1)
  1633. *q++ = ch;
  1634. if (ch == '\\') {
  1635. if (handle_stray_noerror() == 0)
  1636. --q;
  1637. } else
  1638. inp();
  1639. }
  1640. *q = '\0';
  1641. minp();
  1642. #if 0
  1643. /* eat all spaces and comments after include */
  1644. /* XXX: slightly incorrect */
  1645. while (ch1 != '\n' && ch1 != CH_EOF)
  1646. inp();
  1647. #endif
  1648. } else {
  1649. int len;
  1650. /* computed #include : concatenate everything up to linefeed,
  1651. the result must be one of the two accepted forms.
  1652. Don't convert pp-tokens to tokens here. */
  1653. parse_flags = (PARSE_FLAG_PREPROCESS
  1654. | PARSE_FLAG_LINEFEED
  1655. | (parse_flags & PARSE_FLAG_ASM_FILE));
  1656. next();
  1657. buf[0] = '\0';
  1658. while (tok != TOK_LINEFEED) {
  1659. pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
  1660. next();
  1661. }
  1662. len = strlen(buf);
  1663. /* check syntax and remove '<>|""' */
  1664. if ((len < 2 || ((buf[0] != '"' || buf[len-1] != '"') &&
  1665. (buf[0] != '<' || buf[len-1] != '>'))))
  1666. tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
  1667. c = buf[len-1];
  1668. memmove(buf, buf + 1, len - 2);
  1669. buf[len - 2] = '\0';
  1670. }
  1671. if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
  1672. tcc_error("#include recursion too deep");
  1673. /* store current file in stack, but increment stack later below */
  1674. *s1->include_stack_ptr = file;
  1675. i = tok == TOK_INCLUDE_NEXT ? file->include_next_index : 0;
  1676. n = 2 + s1->nb_include_paths + s1->nb_sysinclude_paths;
  1677. for (; i < n; ++i) {
  1678. char buf1[sizeof file->filename];
  1679. CachedInclude *e;
  1680. const char *path;
  1681. if (i == 0) {
  1682. /* check absolute include path */
  1683. if (!IS_ABSPATH(buf))
  1684. continue;
  1685. buf1[0] = 0;
  1686. } else if (i == 1) {
  1687. /* search in file's dir if "header.h" */
  1688. if (c != '\"')
  1689. continue;
  1690. /* https://savannah.nongnu.org/bugs/index.php?50847 */
  1691. path = file->true_filename;
  1692. pstrncpy(buf1, path, tcc_basename(path) - path);
  1693. } else {
  1694. /* search in all the include paths */
  1695. int j = i - 2, k = j - s1->nb_include_paths;
  1696. path = k < 0 ? s1->include_paths[j] : s1->sysinclude_paths[k];
  1697. pstrcpy(buf1, sizeof(buf1), path);
  1698. pstrcat(buf1, sizeof(buf1), "/");
  1699. }
  1700. pstrcat(buf1, sizeof(buf1), buf);
  1701. e = search_cached_include(s1, buf1, 0);
  1702. if (e && (define_find(e->ifndef_macro) || e->once == pp_once)) {
  1703. /* no need to parse the include because the 'ifndef macro'
  1704. is defined (or had #pragma once) */
  1705. #ifdef INC_DEBUG
  1706. printf("%s: skipping cached %s\n", file->filename, buf1);
  1707. #endif
  1708. goto include_done;
  1709. }
  1710. if (tcc_open(s1, buf1) < 0)
  1711. continue;
  1712. file->include_next_index = i + 1;
  1713. #ifdef INC_DEBUG
  1714. printf("%s: including %s\n", file->prev->filename, file->filename);
  1715. #endif
  1716. /* update target deps */
  1717. dynarray_add(&s1->target_deps, &s1->nb_target_deps,
  1718. tcc_strdup(buf1));
  1719. /* push current file in stack */
  1720. ++s1->include_stack_ptr;
  1721. /* add include file debug info */
  1722. if (s1->do_debug)
  1723. put_stabs(file->filename, N_BINCL, 0, 0, 0);
  1724. tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
  1725. ch = file->buf_ptr[0];
  1726. goto the_end;
  1727. }
  1728. tcc_error("include file '%s' not found", buf);
  1729. include_done:
  1730. break;
  1731. case TOK_IFNDEF:
  1732. c = 1;
  1733. goto do_ifdef;
  1734. case TOK_IF:
  1735. c = expr_preprocess();
  1736. goto do_if;
  1737. case TOK_IFDEF:
  1738. c = 0;
  1739. do_ifdef:
  1740. next_nomacro();
  1741. if (tok < TOK_IDENT)
  1742. tcc_error("invalid argument for '#if%sdef'", c ? "n" : "");
  1743. if (is_bof) {
  1744. if (c) {
  1745. #ifdef INC_DEBUG
  1746. printf("#ifndef %s\n", get_tok_str(tok, NULL));
  1747. #endif
  1748. file->ifndef_macro = tok;
  1749. }
  1750. }
  1751. c = (define_find(tok) != 0) ^ c;
  1752. do_if:
  1753. if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
  1754. tcc_error("memory full (ifdef)");
  1755. *s1->ifdef_stack_ptr++ = c;
  1756. goto test_skip;
  1757. case TOK_ELSE:
  1758. if (s1->ifdef_stack_ptr == s1->ifdef_stack)
  1759. tcc_error("#else without matching #if");
  1760. if (s1->ifdef_stack_ptr[-1] & 2)
  1761. tcc_error("#else after #else");
  1762. c = (s1->ifdef_stack_ptr[-1] ^= 3);
  1763. goto test_else;
  1764. case TOK_ELIF:
  1765. if (s1->ifdef_stack_ptr == s1->ifdef_stack)
  1766. tcc_error("#elif without matching #if");
  1767. c = s1->ifdef_stack_ptr[-1];
  1768. if (c > 1)
  1769. tcc_error("#elif after #else");
  1770. /* last #if/#elif expression was true: we skip */
  1771. if (c == 1) {
  1772. c = 0;
  1773. } else {
  1774. c = expr_preprocess();
  1775. s1->ifdef_stack_ptr[-1] = c;
  1776. }
  1777. test_else:
  1778. if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
  1779. file->ifndef_macro = 0;
  1780. test_skip:
  1781. if (!(c & 1)) {
  1782. preprocess_skip();
  1783. is_bof = 0;
  1784. goto redo;
  1785. }
  1786. break;
  1787. case TOK_ENDIF:
  1788. if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
  1789. tcc_error("#endif without matching #if");
  1790. s1->ifdef_stack_ptr--;
  1791. /* '#ifndef macro' was at the start of file. Now we check if
  1792. an '#endif' is exactly at the end of file */
  1793. if (file->ifndef_macro &&
  1794. s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
  1795. file->ifndef_macro_saved = file->ifndef_macro;
  1796. /* need to set to zero to avoid false matches if another
  1797. #ifndef at middle of file */
  1798. file->ifndef_macro = 0;
  1799. while (tok != TOK_LINEFEED)
  1800. next_nomacro();
  1801. tok_flags |= TOK_FLAG_ENDIF;
  1802. goto the_end;
  1803. }
  1804. break;
  1805. case TOK_PPNUM:
  1806. n = strtoul((char*)tokc.str.data, &q, 10);
  1807. goto _line_num;
  1808. case TOK_LINE:
  1809. next();
  1810. if (tok != TOK_CINT)
  1811. _line_err:
  1812. tcc_error("wrong #line format");
  1813. n = tokc.i;
  1814. _line_num:
  1815. next();
  1816. if (tok != TOK_LINEFEED) {
  1817. if (tok == TOK_STR) {
  1818. if (file->true_filename == file->filename)
  1819. file->true_filename = tcc_strdup(file->filename);
  1820. pstrcpy(file->filename, sizeof(file->filename), (char *)tokc.str.data);
  1821. } else if (parse_flags & PARSE_FLAG_ASM_FILE)
  1822. break;
  1823. else
  1824. goto _line_err;
  1825. --n;
  1826. }
  1827. if (file->fd > 0)
  1828. total_lines += file->line_num - n;
  1829. file->line_num = n;
  1830. if (s1->do_debug)
  1831. put_stabs(file->filename, N_BINCL, 0, 0, 0);
  1832. break;
  1833. case TOK_ERROR:
  1834. case TOK_WARNING:
  1835. c = tok;
  1836. ch = file->buf_ptr[0];
  1837. skip_spaces();
  1838. q = buf;
  1839. while (ch != '\n' && ch != CH_EOF) {
  1840. if ((q - buf) < sizeof(buf) - 1)
  1841. *q++ = ch;
  1842. if (ch == '\\') {
  1843. if (handle_stray_noerror() == 0)
  1844. --q;
  1845. } else
  1846. inp();
  1847. }
  1848. *q = '\0';
  1849. if (c == TOK_ERROR)
  1850. tcc_error("#error %s", buf);
  1851. else
  1852. tcc_warning("#warning %s", buf);
  1853. break;
  1854. case TOK_PRAGMA:
  1855. pragma_parse(s1);
  1856. break;
  1857. case TOK_LINEFEED:
  1858. goto the_end;
  1859. default:
  1860. /* ignore gas line comment in an 'S' file. */
  1861. if (saved_parse_flags & PARSE_FLAG_ASM_FILE)
  1862. goto ignore;
  1863. if (tok == '!' && is_bof)
  1864. /* '!' is ignored at beginning to allow C scripts. */
  1865. goto ignore;
  1866. tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
  1867. ignore:
  1868. file->buf_ptr = parse_line_comment(file->buf_ptr - 1);
  1869. goto the_end;
  1870. }
  1871. /* ignore other preprocess commands or #! for C scripts */
  1872. while (tok != TOK_LINEFEED)
  1873. next_nomacro();
  1874. the_end:
  1875. parse_flags = saved_parse_flags;
  1876. }
  1877. /* evaluate escape codes in a string. */
  1878. static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
  1879. {
  1880. int c, n;
  1881. const uint8_t *p;
  1882. p = buf;
  1883. for(;;) {
  1884. c = *p;
  1885. if (c == '\0')
  1886. break;
  1887. if (c == '\\') {
  1888. p++;
  1889. /* escape */
  1890. c = *p;
  1891. switch(c) {
  1892. case '0': case '1': case '2': case '3':
  1893. case '4': case '5': case '6': case '7':
  1894. /* at most three octal digits */
  1895. n = c - '0';
  1896. p++;
  1897. c = *p;
  1898. if (isoct(c)) {
  1899. n = n * 8 + c - '0';
  1900. p++;
  1901. c = *p;
  1902. if (isoct(c)) {
  1903. n = n * 8 + c - '0';
  1904. p++;
  1905. }
  1906. }
  1907. c = n;
  1908. goto add_char_nonext;
  1909. case 'x':
  1910. case 'u':
  1911. case 'U':
  1912. p++;
  1913. n = 0;
  1914. for(;;) {
  1915. c = *p;
  1916. if (c >= 'a' && c <= 'f')
  1917. c = c - 'a' + 10;
  1918. else if (c >= 'A' && c <= 'F')
  1919. c = c - 'A' + 10;
  1920. else if (isnum(c))
  1921. c = c - '0';
  1922. else
  1923. break;
  1924. n = n * 16 + c;
  1925. p++;
  1926. }
  1927. c = n;
  1928. goto add_char_nonext;
  1929. case 'a':
  1930. c = '\a';
  1931. break;
  1932. case 'b':
  1933. c = '\b';
  1934. break;
  1935. case 'f':
  1936. c = '\f';
  1937. break;
  1938. case 'n':
  1939. c = '\n';
  1940. break;
  1941. case 'r':
  1942. c = '\r';
  1943. break;
  1944. case 't':
  1945. c = '\t';
  1946. break;
  1947. case 'v':
  1948. c = '\v';
  1949. break;
  1950. case 'e':
  1951. if (!gnu_ext)
  1952. goto invalid_escape;
  1953. c = 27;
  1954. break;
  1955. case '\'':
  1956. case '\"':
  1957. case '\\':
  1958. case '?':
  1959. break;
  1960. default:
  1961. invalid_escape:
  1962. if (c >= '!' && c <= '~')
  1963. tcc_warning("unknown escape sequence: \'\\%c\'", c);
  1964. else
  1965. tcc_warning("unknown escape sequence: \'\\x%x\'", c);
  1966. break;
  1967. }
  1968. } else if (is_long && c >= 0x80) {
  1969. /* assume we are processing UTF-8 sequence */
  1970. /* reference: The Unicode Standard, Version 10.0, ch3.9 */
  1971. int cont; /* count of continuation bytes */
  1972. int skip; /* how many bytes should skip when error occurred */
  1973. int i;
  1974. /* decode leading byte */
  1975. if (c < 0xC2) {
  1976. skip = 1; goto invalid_utf8_sequence;
  1977. } else if (c <= 0xDF) {
  1978. cont = 1; n = c & 0x1f;
  1979. } else if (c <= 0xEF) {
  1980. cont = 2; n = c & 0xf;
  1981. } else if (c <= 0xF4) {
  1982. cont = 3; n = c & 0x7;
  1983. } else {
  1984. skip = 1; goto invalid_utf8_sequence;
  1985. }
  1986. /* decode continuation bytes */
  1987. for (i = 1; i <= cont; i++) {
  1988. int l = 0x80, h = 0xBF;
  1989. /* adjust limit for second byte */
  1990. if (i == 1) {
  1991. switch (c) {
  1992. case 0xE0: l = 0xA0; break;
  1993. case 0xED: h = 0x9F; break;
  1994. case 0xF0: l = 0x90; break;
  1995. case 0xF4: h = 0x8F; break;
  1996. }
  1997. }
  1998. if (p[i] < l || p[i] > h) {
  1999. skip = i; goto invalid_utf8_sequence;
  2000. }
  2001. n = (n << 6) | (p[i] & 0x3f);
  2002. }
  2003. /* advance pointer */
  2004. p += 1 + cont;
  2005. c = n;
  2006. goto add_char_nonext;
  2007. /* error handling */
  2008. invalid_utf8_sequence:
  2009. tcc_warning("ill-formed UTF-8 subsequence starting with: \'\\x%x\'", c);
  2010. c = 0xFFFD;
  2011. p += skip;
  2012. goto add_char_nonext;
  2013. }
  2014. p++;
  2015. add_char_nonext:
  2016. if (!is_long)
  2017. cstr_ccat(outstr, c);
  2018. else {
  2019. #ifdef TCC_TARGET_PE
  2020. /* store as UTF-16 */
  2021. if (c < 0x10000) {
  2022. cstr_wccat(outstr, c);
  2023. } else {
  2024. c -= 0x10000;
  2025. cstr_wccat(outstr, (c >> 10) + 0xD800);
  2026. cstr_wccat(outstr, (c & 0x3FF) + 0xDC00);
  2027. }
  2028. #else
  2029. cstr_wccat(outstr, c);
  2030. #endif
  2031. }
  2032. }
  2033. /* add a trailing '\0' */
  2034. if (!is_long)
  2035. cstr_ccat(outstr, '\0');
  2036. else
  2037. cstr_wccat(outstr, '\0');
  2038. }
  2039. static void parse_string(const char *s, int len)
  2040. {
  2041. uint8_t buf[1000], *p = buf;
  2042. int is_long, sep;
  2043. if ((is_long = *s == 'L'))
  2044. ++s, --len;
  2045. sep = *s++;
  2046. len -= 2;
  2047. if (len >= sizeof buf)
  2048. p = tcc_malloc(len + 1);
  2049. memcpy(p, s, len);
  2050. p[len] = 0;
  2051. cstr_reset(&tokcstr);
  2052. parse_escape_string(&tokcstr, p, is_long);
  2053. if (p != buf)
  2054. tcc_free(p);
  2055. if (sep == '\'') {
  2056. int char_size, i, n, c;
  2057. /* XXX: make it portable */
  2058. if (!is_long)
  2059. tok = TOK_CCHAR, char_size = 1;
  2060. else
  2061. tok = TOK_LCHAR, char_size = sizeof(nwchar_t);
  2062. n = tokcstr.size / char_size - 1;
  2063. if (n < 1)
  2064. tcc_error("empty character constant");
  2065. if (n > 1)
  2066. tcc_warning("multi-character character constant");
  2067. for (c = i = 0; i < n; ++i) {
  2068. if (is_long)
  2069. c = ((nwchar_t *)tokcstr.data)[i];
  2070. else
  2071. c = (c << 8) | ((char *)tokcstr.data)[i];
  2072. }
  2073. tokc.i = c;
  2074. } else {
  2075. tokc.str.size = tokcstr.size;
  2076. tokc.str.data = tokcstr.data;
  2077. if (!is_long)
  2078. tok = TOK_STR;
  2079. else
  2080. tok = TOK_LSTR;
  2081. }
  2082. }
  2083. /* we use 64 bit numbers */
  2084. #define BN_SIZE 2
  2085. /* bn = (bn << shift) | or_val */
  2086. static void bn_lshift(unsigned int *bn, int shift, int or_val)
  2087. {
  2088. int i;
  2089. unsigned int v;
  2090. for(i=0;i<BN_SIZE;i++) {
  2091. v = bn[i];
  2092. bn[i] = (v << shift) | or_val;
  2093. or_val = v >> (32 - shift);
  2094. }
  2095. }
  2096. static void bn_zero(unsigned int *bn)
  2097. {
  2098. int i;
  2099. for(i=0;i<BN_SIZE;i++) {
  2100. bn[i] = 0;
  2101. }
  2102. }
  2103. /* parse number in null terminated string 'p' and return it in the
  2104. current token */
  2105. static void parse_number(const char *p)
  2106. {
  2107. int b, t, shift, frac_bits, s, exp_val, ch;
  2108. char *q;
  2109. unsigned int bn[BN_SIZE];
  2110. double d;
  2111. /* number */
  2112. q = token_buf;
  2113. ch = *p++;
  2114. t = ch;
  2115. ch = *p++;
  2116. *q++ = t;
  2117. b = 10;
  2118. if (t == '.') {
  2119. goto float_frac_parse;
  2120. } else if (t == '0') {
  2121. if (ch == 'x' || ch == 'X') {
  2122. q--;
  2123. ch = *p++;
  2124. b = 16;
  2125. } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
  2126. q--;
  2127. ch = *p++;
  2128. b = 2;
  2129. }
  2130. }
  2131. /* parse all digits. cannot check octal numbers at this stage
  2132. because of floating point constants */
  2133. while (1) {
  2134. if (ch >= 'a' && ch <= 'f')
  2135. t = ch - 'a' + 10;
  2136. else if (ch >= 'A' && ch <= 'F')
  2137. t = ch - 'A' + 10;
  2138. else if (isnum(ch))
  2139. t = ch - '0';
  2140. else
  2141. break;
  2142. if (t >= b)
  2143. break;
  2144. if (q >= token_buf + STRING_MAX_SIZE) {
  2145. num_too_long:
  2146. tcc_error("number too long");
  2147. }
  2148. *q++ = ch;
  2149. ch = *p++;
  2150. }
  2151. if (ch == '.' ||
  2152. ((ch == 'e' || ch == 'E') && b == 10) ||
  2153. ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
  2154. if (b != 10) {
  2155. /* NOTE: strtox should support that for hexa numbers, but
  2156. non ISOC99 libcs do not support it, so we prefer to do
  2157. it by hand */
  2158. /* hexadecimal or binary floats */
  2159. /* XXX: handle overflows */
  2160. *q = '\0';
  2161. if (b == 16)
  2162. shift = 4;
  2163. else
  2164. shift = 1;
  2165. bn_zero(bn);
  2166. q = token_buf;
  2167. while (1) {
  2168. t = *q++;
  2169. if (t == '\0') {
  2170. break;
  2171. } else if (t >= 'a') {
  2172. t = t - 'a' + 10;
  2173. } else if (t >= 'A') {
  2174. t = t - 'A' + 10;
  2175. } else {
  2176. t = t - '0';
  2177. }
  2178. bn_lshift(bn, shift, t);
  2179. }
  2180. frac_bits = 0;
  2181. if (ch == '.') {
  2182. ch = *p++;
  2183. while (1) {
  2184. t = ch;
  2185. if (t >= 'a' && t <= 'f') {
  2186. t = t - 'a' + 10;
  2187. } else if (t >= 'A' && t <= 'F') {
  2188. t = t - 'A' + 10;
  2189. } else if (t >= '0' && t <= '9') {
  2190. t = t - '0';
  2191. } else {
  2192. break;
  2193. }
  2194. if (t >= b)
  2195. tcc_error("invalid digit");
  2196. bn_lshift(bn, shift, t);
  2197. frac_bits += shift;
  2198. ch = *p++;
  2199. }
  2200. }
  2201. if (ch != 'p' && ch != 'P')
  2202. expect("exponent");
  2203. ch = *p++;
  2204. s = 1;
  2205. exp_val = 0;
  2206. if (ch == '+') {
  2207. ch = *p++;
  2208. } else if (ch == '-') {
  2209. s = -1;
  2210. ch = *p++;
  2211. }
  2212. if (ch < '0' || ch > '9')
  2213. expect("exponent digits");
  2214. while (ch >= '0' && ch <= '9') {
  2215. exp_val = exp_val * 10 + ch - '0';
  2216. ch = *p++;
  2217. }
  2218. exp_val = exp_val * s;
  2219. /* now we can generate the number */
  2220. /* XXX: should patch directly float number */
  2221. d = (double)bn[1] * 4294967296.0 + (double)bn[0];
  2222. d = ldexp(d, exp_val - frac_bits);
  2223. t = toup(ch);
  2224. if (t == 'F') {
  2225. ch = *p++;
  2226. tok = TOK_CFLOAT;
  2227. /* float : should handle overflow */
  2228. tokc.f = (float)d;
  2229. } else if (t == 'L') {
  2230. ch = *p++;
  2231. #ifdef TCC_TARGET_PE
  2232. tok = TOK_CDOUBLE;
  2233. tokc.d = d;
  2234. #else
  2235. tok = TOK_CLDOUBLE;
  2236. /* XXX: not large enough */
  2237. tokc.ld = (long double)d;
  2238. #endif
  2239. } else {
  2240. tok = TOK_CDOUBLE;
  2241. tokc.d = d;
  2242. }
  2243. } else {
  2244. /* decimal floats */
  2245. if (ch == '.') {
  2246. if (q >= token_buf + STRING_MAX_SIZE)
  2247. goto num_too_long;
  2248. *q++ = ch;
  2249. ch = *p++;
  2250. float_frac_parse:
  2251. while (ch >= '0' && ch <= '9') {
  2252. if (q >= token_buf + STRING_MAX_SIZE)
  2253. goto num_too_long;
  2254. *q++ = ch;
  2255. ch = *p++;
  2256. }
  2257. }
  2258. if (ch == 'e' || ch == 'E') {
  2259. if (q >= token_buf + STRING_MAX_SIZE)
  2260. goto num_too_long;
  2261. *q++ = ch;
  2262. ch = *p++;
  2263. if (ch == '-' || ch == '+') {
  2264. if (q >= token_buf + STRING_MAX_SIZE)
  2265. goto num_too_long;
  2266. *q++ = ch;
  2267. ch = *p++;
  2268. }
  2269. if (ch < '0' || ch > '9')
  2270. expect("exponent digits");
  2271. while (ch >= '0' && ch <= '9') {
  2272. if (q >= token_buf + STRING_MAX_SIZE)
  2273. goto num_too_long;
  2274. *q++ = ch;
  2275. ch = *p++;
  2276. }
  2277. }
  2278. *q = '\0';
  2279. t = toup(ch);
  2280. errno = 0;
  2281. if (t == 'F') {
  2282. ch = *p++;
  2283. tok = TOK_CFLOAT;
  2284. tokc.f = strtof(token_buf, NULL);
  2285. } else if (t == 'L') {
  2286. ch = *p++;
  2287. #ifdef TCC_TARGET_PE
  2288. tok = TOK_CDOUBLE;
  2289. tokc.d = strtod(token_buf, NULL);
  2290. #else
  2291. tok = TOK_CLDOUBLE;
  2292. tokc.ld = strtold(token_buf, NULL);
  2293. #endif
  2294. } else {
  2295. tok = TOK_CDOUBLE;
  2296. tokc.d = strtod(token_buf, NULL);
  2297. }
  2298. }
  2299. } else {
  2300. unsigned long long n, n1;
  2301. int lcount, ucount, ov = 0;
  2302. const char *p1;
  2303. /* integer number */
  2304. *q = '\0';
  2305. q = token_buf;
  2306. if (b == 10 && *q == '0') {
  2307. b = 8;
  2308. q++;
  2309. }
  2310. n = 0;
  2311. while(1) {
  2312. t = *q++;
  2313. /* no need for checks except for base 10 / 8 errors */
  2314. if (t == '\0')
  2315. break;
  2316. else if (t >= 'a')
  2317. t = t - 'a' + 10;
  2318. else if (t >= 'A')
  2319. t = t - 'A' + 10;
  2320. else
  2321. t = t - '0';
  2322. if (t >= b)
  2323. tcc_error("invalid digit");
  2324. n1 = n;
  2325. n = n * b + t;
  2326. /* detect overflow */
  2327. if (n1 >= 0x1000000000000000ULL && n / b != n1)
  2328. ov = 1;
  2329. }
  2330. /* Determine the characteristics (unsigned and/or 64bit) the type of
  2331. the constant must have according to the constant suffix(es) */
  2332. lcount = ucount = 0;
  2333. p1 = p;
  2334. for(;;) {
  2335. t = toup(ch);
  2336. if (t == 'L') {
  2337. if (lcount >= 2)
  2338. tcc_error("three 'l's in integer constant");
  2339. if (lcount && *(p - 1) != ch)
  2340. tcc_error("incorrect integer suffix: %s", p1);
  2341. lcount++;
  2342. ch = *p++;
  2343. } else if (t == 'U') {
  2344. if (ucount >= 1)
  2345. tcc_error("two 'u's in integer constant");
  2346. ucount++;
  2347. ch = *p++;
  2348. } else {
  2349. break;
  2350. }
  2351. }
  2352. /* Determine if it needs 64 bits and/or unsigned in order to fit */
  2353. if (ucount == 0 && b == 10) {
  2354. if (lcount <= (LONG_SIZE == 4)) {
  2355. if (n >= 0x80000000U)
  2356. lcount = (LONG_SIZE == 4) + 1;
  2357. }
  2358. if (n >= 0x8000000000000000ULL)
  2359. ov = 1, ucount = 1;
  2360. } else {
  2361. if (lcount <= (LONG_SIZE == 4)) {
  2362. if (n >= 0x100000000ULL)
  2363. lcount = (LONG_SIZE == 4) + 1;
  2364. else if (n >= 0x80000000U)
  2365. ucount = 1;
  2366. }
  2367. if (n >= 0x8000000000000000ULL)
  2368. ucount = 1;
  2369. }
  2370. if (ov)
  2371. tcc_warning("integer constant overflow");
  2372. tok = TOK_CINT;
  2373. if (lcount) {
  2374. tok = TOK_CLONG;
  2375. if (lcount == 2)
  2376. tok = TOK_CLLONG;
  2377. }
  2378. if (ucount)
  2379. ++tok; /* TOK_CU... */
  2380. tokc.i = n;
  2381. }
  2382. if (ch)
  2383. tcc_error("invalid number\n");
  2384. }
  2385. #define PARSE2(c1, tok1, c2, tok2) \
  2386. case c1: \
  2387. PEEKC(c, p); \
  2388. if (c == c2) { \
  2389. p++; \
  2390. tok = tok2; \
  2391. } else { \
  2392. tok = tok1; \
  2393. } \
  2394. break;
  2395. /* return next token without macro substitution */
  2396. static inline void next_nomacro1(void)
  2397. {
  2398. int t, c, is_long, len;
  2399. TokenSym *ts;
  2400. uint8_t *p, *p1;
  2401. unsigned int h;
  2402. p = file->buf_ptr;
  2403. redo_no_start:
  2404. c = *p;
  2405. switch(c) {
  2406. case ' ':
  2407. case '\t':
  2408. tok = c;
  2409. p++;
  2410. if (parse_flags & PARSE_FLAG_SPACES)
  2411. goto keep_tok_flags;
  2412. while (isidnum_table[*p - CH_EOF] & IS_SPC)
  2413. ++p;
  2414. goto redo_no_start;
  2415. case '\f':
  2416. case '\v':
  2417. case '\r':
  2418. p++;
  2419. goto redo_no_start;
  2420. case '\\':
  2421. /* first look if it is in fact an end of buffer */
  2422. c = handle_stray1(p);
  2423. p = file->buf_ptr;
  2424. if (c == '\\')
  2425. goto parse_simple;
  2426. if (c != CH_EOF)
  2427. goto redo_no_start;
  2428. {
  2429. TCCState *s1 = tcc_state;
  2430. if ((parse_flags & PARSE_FLAG_LINEFEED)
  2431. && !(tok_flags & TOK_FLAG_EOF)) {
  2432. tok_flags |= TOK_FLAG_EOF;
  2433. tok = TOK_LINEFEED;
  2434. goto keep_tok_flags;
  2435. } else if (!(parse_flags & PARSE_FLAG_PREPROCESS)) {
  2436. tok = TOK_EOF;
  2437. } else if (s1->ifdef_stack_ptr != file->ifdef_stack_ptr) {
  2438. tcc_error("missing #endif");
  2439. } else if (s1->include_stack_ptr == s1->include_stack) {
  2440. /* no include left : end of file. */
  2441. tok = TOK_EOF;
  2442. } else {
  2443. tok_flags &= ~TOK_FLAG_EOF;
  2444. /* pop include file */
  2445. /* test if previous '#endif' was after a #ifdef at
  2446. start of file */
  2447. if (tok_flags & TOK_FLAG_ENDIF) {
  2448. #ifdef INC_DEBUG
  2449. printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
  2450. #endif
  2451. search_cached_include(s1, file->filename, 1)
  2452. ->ifndef_macro = file->ifndef_macro_saved;
  2453. tok_flags &= ~TOK_FLAG_ENDIF;
  2454. }
  2455. /* add end of include file debug info */
  2456. if (tcc_state->do_debug) {
  2457. put_stabd(N_EINCL, 0, 0);
  2458. }
  2459. /* pop include stack */
  2460. tcc_close();
  2461. s1->include_stack_ptr--;
  2462. p = file->buf_ptr;
  2463. if (p == file->buffer)
  2464. tok_flags = TOK_FLAG_BOF|TOK_FLAG_BOL;
  2465. goto redo_no_start;
  2466. }
  2467. }
  2468. break;
  2469. case '\n':
  2470. file->line_num++;
  2471. tok_flags |= TOK_FLAG_BOL;
  2472. p++;
  2473. maybe_newline:
  2474. if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
  2475. goto redo_no_start;
  2476. tok = TOK_LINEFEED;
  2477. goto keep_tok_flags;
  2478. case '#':
  2479. /* XXX: simplify */
  2480. PEEKC(c, p);
  2481. if ((tok_flags & TOK_FLAG_BOL) &&
  2482. (parse_flags & PARSE_FLAG_PREPROCESS)) {
  2483. file->buf_ptr = p;
  2484. preprocess(tok_flags & TOK_FLAG_BOF);
  2485. p = file->buf_ptr;
  2486. goto maybe_newline;
  2487. } else {
  2488. if (c == '#') {
  2489. p++;
  2490. tok = TOK_TWOSHARPS;
  2491. } else {
  2492. if (parse_flags & PARSE_FLAG_ASM_FILE) {
  2493. p = parse_line_comment(p - 1);
  2494. goto redo_no_start;
  2495. } else {
  2496. tok = '#';
  2497. }
  2498. }
  2499. }
  2500. break;
  2501. /* dollar is allowed to start identifiers when not parsing asm */
  2502. case '$':
  2503. if (!(isidnum_table[c - CH_EOF] & IS_ID)
  2504. || (parse_flags & PARSE_FLAG_ASM_FILE))
  2505. goto parse_simple;
  2506. case 'a': case 'b': case 'c': case 'd':
  2507. case 'e': case 'f': case 'g': case 'h':
  2508. case 'i': case 'j': case 'k': case 'l':
  2509. case 'm': case 'n': case 'o': case 'p':
  2510. case 'q': case 'r': case 's': case 't':
  2511. case 'u': case 'v': case 'w': case 'x':
  2512. case 'y': case 'z':
  2513. case 'A': case 'B': case 'C': case 'D':
  2514. case 'E': case 'F': case 'G': case 'H':
  2515. case 'I': case 'J': case 'K':
  2516. case 'M': case 'N': case 'O': case 'P':
  2517. case 'Q': case 'R': case 'S': case 'T':
  2518. case 'U': case 'V': case 'W': case 'X':
  2519. case 'Y': case 'Z':
  2520. case '_':
  2521. parse_ident_fast:
  2522. p1 = p;
  2523. h = TOK_HASH_INIT;
  2524. h = TOK_HASH_FUNC(h, c);
  2525. while (c = *++p, isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
  2526. h = TOK_HASH_FUNC(h, c);
  2527. len = p - p1;
  2528. if (c != '\\') {
  2529. TokenSym **pts;
  2530. /* fast case : no stray found, so we have the full token
  2531. and we have already hashed it */
  2532. h &= (TOK_HASH_SIZE - 1);
  2533. pts = &hash_ident[h];
  2534. for(;;) {
  2535. ts = *pts;
  2536. if (!ts)
  2537. break;
  2538. if (ts->len == len && !memcmp(ts->str, p1, len))
  2539. goto token_found;
  2540. pts = &(ts->hash_next);
  2541. }
  2542. ts = tok_alloc_new(pts, (char *) p1, len);
  2543. token_found: ;
  2544. } else {
  2545. /* slower case */
  2546. cstr_reset(&tokcstr);
  2547. cstr_cat(&tokcstr, (char *) p1, len);
  2548. p--;
  2549. PEEKC(c, p);
  2550. parse_ident_slow:
  2551. while (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
  2552. {
  2553. cstr_ccat(&tokcstr, c);
  2554. PEEKC(c, p);
  2555. }
  2556. ts = tok_alloc(tokcstr.data, tokcstr.size);
  2557. }
  2558. tok = ts->tok;
  2559. break;
  2560. case 'L':
  2561. t = p[1];
  2562. if (t != '\\' && t != '\'' && t != '\"') {
  2563. /* fast case */
  2564. goto parse_ident_fast;
  2565. } else {
  2566. PEEKC(c, p);
  2567. if (c == '\'' || c == '\"') {
  2568. is_long = 1;
  2569. goto str_const;
  2570. } else {
  2571. cstr_reset(&tokcstr);
  2572. cstr_ccat(&tokcstr, 'L');
  2573. goto parse_ident_slow;
  2574. }
  2575. }
  2576. break;
  2577. case '0': case '1': case '2': case '3':
  2578. case '4': case '5': case '6': case '7':
  2579. case '8': case '9':
  2580. t = c;
  2581. PEEKC(c, p);
  2582. /* after the first digit, accept digits, alpha, '.' or sign if
  2583. prefixed by 'eEpP' */
  2584. parse_num:
  2585. cstr_reset(&tokcstr);
  2586. for(;;) {
  2587. cstr_ccat(&tokcstr, t);
  2588. if (!((isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
  2589. || c == '.'
  2590. || ((c == '+' || c == '-')
  2591. && (((t == 'e' || t == 'E')
  2592. && !(parse_flags & PARSE_FLAG_ASM_FILE
  2593. /* 0xe+1 is 3 tokens in asm */
  2594. && ((char*)tokcstr.data)[0] == '0'
  2595. && toup(((char*)tokcstr.data)[1]) == 'X'))
  2596. || t == 'p' || t == 'P'))))
  2597. break;
  2598. t = c;
  2599. PEEKC(c, p);
  2600. }
  2601. /* We add a trailing '\0' to ease parsing */
  2602. cstr_ccat(&tokcstr, '\0');
  2603. tokc.str.size = tokcstr.size;
  2604. tokc.str.data = tokcstr.data;
  2605. tok = TOK_PPNUM;
  2606. break;
  2607. case '.':
  2608. /* special dot handling because it can also start a number */
  2609. PEEKC(c, p);
  2610. if (isnum(c)) {
  2611. t = '.';
  2612. goto parse_num;
  2613. } else if ((isidnum_table['.' - CH_EOF] & IS_ID)
  2614. && (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))) {
  2615. *--p = c = '.';
  2616. goto parse_ident_fast;
  2617. } else if (c == '.') {
  2618. PEEKC(c, p);
  2619. if (c == '.') {
  2620. p++;
  2621. tok = TOK_DOTS;
  2622. } else {
  2623. *--p = '.'; /* may underflow into file->unget[] */
  2624. tok = '.';
  2625. }
  2626. } else {
  2627. tok = '.';
  2628. }
  2629. break;
  2630. case '\'':
  2631. case '\"':
  2632. is_long = 0;
  2633. str_const:
  2634. cstr_reset(&tokcstr);
  2635. if (is_long)
  2636. cstr_ccat(&tokcstr, 'L');
  2637. cstr_ccat(&tokcstr, c);
  2638. p = parse_pp_string(p, c, &tokcstr);
  2639. cstr_ccat(&tokcstr, c);
  2640. cstr_ccat(&tokcstr, '\0');
  2641. tokc.str.size = tokcstr.size;
  2642. tokc.str.data = tokcstr.data;
  2643. tok = TOK_PPSTR;
  2644. break;
  2645. case '<':
  2646. PEEKC(c, p);
  2647. if (c == '=') {
  2648. p++;
  2649. tok = TOK_LE;
  2650. } else if (c == '<') {
  2651. PEEKC(c, p);
  2652. if (c == '=') {
  2653. p++;
  2654. tok = TOK_A_SHL;
  2655. } else {
  2656. tok = TOK_SHL;
  2657. }
  2658. } else {
  2659. tok = TOK_LT;
  2660. }
  2661. break;
  2662. case '>':
  2663. PEEKC(c, p);
  2664. if (c == '=') {
  2665. p++;
  2666. tok = TOK_GE;
  2667. } else if (c == '>') {
  2668. PEEKC(c, p);
  2669. if (c == '=') {
  2670. p++;
  2671. tok = TOK_A_SAR;
  2672. } else {
  2673. tok = TOK_SAR;
  2674. }
  2675. } else {
  2676. tok = TOK_GT;
  2677. }
  2678. break;
  2679. case '&':
  2680. PEEKC(c, p);
  2681. if (c == '&') {
  2682. p++;
  2683. tok = TOK_LAND;
  2684. } else if (c == '=') {
  2685. p++;
  2686. tok = TOK_A_AND;
  2687. } else {
  2688. tok = '&';
  2689. }
  2690. break;
  2691. case '|':
  2692. PEEKC(c, p);
  2693. if (c == '|') {
  2694. p++;
  2695. tok = TOK_LOR;
  2696. } else if (c == '=') {
  2697. p++;
  2698. tok = TOK_A_OR;
  2699. } else {
  2700. tok = '|';
  2701. }
  2702. break;
  2703. case '+':
  2704. PEEKC(c, p);
  2705. if (c == '+') {
  2706. p++;
  2707. tok = TOK_INC;
  2708. } else if (c == '=') {
  2709. p++;
  2710. tok = TOK_A_ADD;
  2711. } else {
  2712. tok = '+';
  2713. }
  2714. break;
  2715. case '-':
  2716. PEEKC(c, p);
  2717. if (c == '-') {
  2718. p++;
  2719. tok = TOK_DEC;
  2720. } else if (c == '=') {
  2721. p++;
  2722. tok = TOK_A_SUB;
  2723. } else if (c == '>') {
  2724. p++;
  2725. tok = TOK_ARROW;
  2726. } else {
  2727. tok = '-';
  2728. }
  2729. break;
  2730. PARSE2('!', '!', '=', TOK_NE)
  2731. PARSE2('=', '=', '=', TOK_EQ)
  2732. PARSE2('*', '*', '=', TOK_A_MUL)
  2733. PARSE2('%', '%', '=', TOK_A_MOD)
  2734. PARSE2('^', '^', '=', TOK_A_XOR)
  2735. /* comments or operator */
  2736. case '/':
  2737. PEEKC(c, p);
  2738. if (c == '*') {
  2739. p = parse_comment(p);
  2740. /* comments replaced by a blank */
  2741. tok = ' ';
  2742. goto keep_tok_flags;
  2743. } else if (c == '/') {
  2744. p = parse_line_comment(p);
  2745. tok = ' ';
  2746. goto keep_tok_flags;
  2747. } else if (c == '=') {
  2748. p++;
  2749. tok = TOK_A_DIV;
  2750. } else {
  2751. tok = '/';
  2752. }
  2753. break;
  2754. /* simple tokens */
  2755. case '(':
  2756. case ')':
  2757. case '[':
  2758. case ']':
  2759. case '{':
  2760. case '}':
  2761. case ',':
  2762. case ';':
  2763. case ':':
  2764. case '?':
  2765. case '~':
  2766. case '@': /* only used in assembler */
  2767. parse_simple:
  2768. tok = c;
  2769. p++;
  2770. break;
  2771. default:
  2772. if (c >= 0x80 && c <= 0xFF) /* utf8 identifiers */
  2773. goto parse_ident_fast;
  2774. if (parse_flags & PARSE_FLAG_ASM_FILE)
  2775. goto parse_simple;
  2776. tcc_error("unrecognized character \\x%02x", c);
  2777. break;
  2778. }
  2779. tok_flags = 0;
  2780. keep_tok_flags:
  2781. file->buf_ptr = p;
  2782. #if defined(PARSE_DEBUG)
  2783. printf("token = %d %s\n", tok, get_tok_str(tok, &tokc));
  2784. #endif
  2785. }
  2786. /* return next token without macro substitution. Can read input from
  2787. macro_ptr buffer */
  2788. static void next_nomacro_spc(void)
  2789. {
  2790. if (macro_ptr) {
  2791. redo:
  2792. tok = *macro_ptr;
  2793. if (tok) {
  2794. TOK_GET(&tok, &macro_ptr, &tokc);
  2795. if (tok == TOK_LINENUM) {
  2796. file->line_num = tokc.i;
  2797. goto redo;
  2798. }
  2799. }
  2800. } else {
  2801. next_nomacro1();
  2802. }
  2803. //printf("token = %s\n", get_tok_str(tok, &tokc));
  2804. }
  2805. ST_FUNC void next_nomacro(void)
  2806. {
  2807. do {
  2808. next_nomacro_spc();
  2809. } while (tok < 256 && (isidnum_table[tok - CH_EOF] & IS_SPC));
  2810. }
  2811. static void macro_subst(
  2812. TokenString *tok_str,
  2813. Sym **nested_list,
  2814. const int *macro_str
  2815. );
  2816. /* substitute arguments in replacement lists in macro_str by the values in
  2817. args (field d) and return allocated string */
  2818. static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args)
  2819. {
  2820. int t, t0, t1, spc;
  2821. const int *st;
  2822. Sym *s;
  2823. CValue cval;
  2824. TokenString str;
  2825. CString cstr;
  2826. tok_str_new(&str);
  2827. t0 = t1 = 0;
  2828. while(1) {
  2829. TOK_GET(&t, &macro_str, &cval);
  2830. if (!t)
  2831. break;
  2832. if (t == '#') {
  2833. /* stringize */
  2834. TOK_GET(&t, &macro_str, &cval);
  2835. if (!t)
  2836. goto bad_stringy;
  2837. s = sym_find2(args, t);
  2838. if (s) {
  2839. cstr_new(&cstr);
  2840. cstr_ccat(&cstr, '\"');
  2841. st = s->d;
  2842. spc = 0;
  2843. while (*st >= 0) {
  2844. TOK_GET(&t, &st, &cval);
  2845. if (t != TOK_PLCHLDR
  2846. && t != TOK_NOSUBST
  2847. && 0 == check_space(t, &spc)) {
  2848. const char *s = get_tok_str(t, &cval);
  2849. while (*s) {
  2850. if (t == TOK_PPSTR && *s != '\'')
  2851. add_char(&cstr, *s);
  2852. else
  2853. cstr_ccat(&cstr, *s);
  2854. ++s;
  2855. }
  2856. }
  2857. }
  2858. cstr.size -= spc;
  2859. cstr_ccat(&cstr, '\"');
  2860. cstr_ccat(&cstr, '\0');
  2861. #ifdef PP_DEBUG
  2862. printf("\nstringize: <%s>\n", (char *)cstr.data);
  2863. #endif
  2864. /* add string */
  2865. cval.str.size = cstr.size;
  2866. cval.str.data = cstr.data;
  2867. tok_str_add2(&str, TOK_PPSTR, &cval);
  2868. cstr_free(&cstr);
  2869. } else {
  2870. bad_stringy:
  2871. expect("macro parameter after '#'");
  2872. }
  2873. } else if (t >= TOK_IDENT) {
  2874. s = sym_find2(args, t);
  2875. if (s) {
  2876. int l0 = str.len;
  2877. st = s->d;
  2878. /* if '##' is present before or after, no arg substitution */
  2879. if (*macro_str == TOK_PPJOIN || t1 == TOK_PPJOIN) {
  2880. /* special case for var arg macros : ## eats the ','
  2881. if empty VA_ARGS variable. */
  2882. if (t1 == TOK_PPJOIN && t0 == ',' && gnu_ext && s->type.t) {
  2883. if (*st <= 0) {
  2884. /* suppress ',' '##' */
  2885. str.len -= 2;
  2886. } else {
  2887. /* suppress '##' and add variable */
  2888. str.len--;
  2889. goto add_var;
  2890. }
  2891. }
  2892. } else {
  2893. add_var:
  2894. if (!s->next) {
  2895. /* Expand arguments tokens and store them. In most
  2896. cases we could also re-expand each argument if
  2897. used multiple times, but not if the argument
  2898. contains the __COUNTER__ macro. */
  2899. TokenString str2;
  2900. sym_push2(&s->next, s->v, s->type.t, 0);
  2901. tok_str_new(&str2);
  2902. macro_subst(&str2, nested_list, st);
  2903. tok_str_add(&str2, 0);
  2904. s->next->d = str2.str;
  2905. }
  2906. st = s->next->d;
  2907. }
  2908. for(;;) {
  2909. int t2;
  2910. TOK_GET(&t2, &st, &cval);
  2911. if (t2 <= 0)
  2912. break;
  2913. tok_str_add2(&str, t2, &cval);
  2914. }
  2915. if (str.len == l0) /* expanded to empty string */
  2916. tok_str_add(&str, TOK_PLCHLDR);
  2917. } else {
  2918. tok_str_add(&str, t);
  2919. }
  2920. } else {
  2921. tok_str_add2(&str, t, &cval);
  2922. }
  2923. t0 = t1, t1 = t;
  2924. }
  2925. tok_str_add(&str, 0);
  2926. return str.str;
  2927. }
  2928. static char const ab_month_name[12][4] =
  2929. {
  2930. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  2931. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  2932. };
  2933. static int paste_tokens(int t1, CValue *v1, int t2, CValue *v2)
  2934. {
  2935. CString cstr;
  2936. int n, ret = 1;
  2937. cstr_new(&cstr);
  2938. if (t1 != TOK_PLCHLDR)
  2939. cstr_cat(&cstr, get_tok_str(t1, v1), -1);
  2940. n = cstr.size;
  2941. if (t2 != TOK_PLCHLDR)
  2942. cstr_cat(&cstr, get_tok_str(t2, v2), -1);
  2943. cstr_ccat(&cstr, '\0');
  2944. tcc_open_bf(tcc_state, ":paste:", cstr.size);
  2945. memcpy(file->buffer, cstr.data, cstr.size);
  2946. tok_flags = 0;
  2947. for (;;) {
  2948. next_nomacro1();
  2949. if (0 == *file->buf_ptr)
  2950. break;
  2951. if (is_space(tok))
  2952. continue;
  2953. tcc_warning("pasting \"%.*s\" and \"%s\" does not give a valid"
  2954. " preprocessing token", n, cstr.data, (char*)cstr.data + n);
  2955. ret = 0;
  2956. break;
  2957. }
  2958. tcc_close();
  2959. //printf("paste <%s>\n", (char*)cstr.data);
  2960. cstr_free(&cstr);
  2961. return ret;
  2962. }
  2963. /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
  2964. return the resulting string (which must be freed). */
  2965. static inline int *macro_twosharps(const int *ptr0)
  2966. {
  2967. int t;
  2968. CValue cval;
  2969. TokenString macro_str1;
  2970. int start_of_nosubsts = -1;
  2971. const int *ptr;
  2972. /* we search the first '##' */
  2973. for (ptr = ptr0;;) {
  2974. TOK_GET(&t, &ptr, &cval);
  2975. if (t == TOK_PPJOIN)
  2976. break;
  2977. if (t == 0)
  2978. return NULL;
  2979. }
  2980. tok_str_new(&macro_str1);
  2981. //tok_print(" $$$", ptr0);
  2982. for (ptr = ptr0;;) {
  2983. TOK_GET(&t, &ptr, &cval);
  2984. if (t == 0)
  2985. break;
  2986. if (t == TOK_PPJOIN)
  2987. continue;
  2988. while (*ptr == TOK_PPJOIN) {
  2989. int t1; CValue cv1;
  2990. /* given 'a##b', remove nosubsts preceding 'a' */
  2991. if (start_of_nosubsts >= 0)
  2992. macro_str1.len = start_of_nosubsts;
  2993. /* given 'a##b', remove nosubsts preceding 'b' */
  2994. while ((t1 = *++ptr) == TOK_NOSUBST)
  2995. ;
  2996. if (t1 && t1 != TOK_PPJOIN) {
  2997. TOK_GET(&t1, &ptr, &cv1);
  2998. if (t != TOK_PLCHLDR || t1 != TOK_PLCHLDR) {
  2999. if (paste_tokens(t, &cval, t1, &cv1)) {
  3000. t = tok, cval = tokc;
  3001. } else {
  3002. tok_str_add2(&macro_str1, t, &cval);
  3003. t = t1, cval = cv1;
  3004. }
  3005. }
  3006. }
  3007. }
  3008. if (t == TOK_NOSUBST) {
  3009. if (start_of_nosubsts < 0)
  3010. start_of_nosubsts = macro_str1.len;
  3011. } else {
  3012. start_of_nosubsts = -1;
  3013. }
  3014. tok_str_add2(&macro_str1, t, &cval);
  3015. }
  3016. tok_str_add(&macro_str1, 0);
  3017. //tok_print(" ###", macro_str1.str);
  3018. return macro_str1.str;
  3019. }
  3020. /* peek or read [ws_str == NULL] next token from function macro call,
  3021. walking up macro levels up to the file if necessary */
  3022. static int next_argstream(Sym **nested_list, TokenString *ws_str)
  3023. {
  3024. int t;
  3025. const int *p;
  3026. Sym *sa;
  3027. for (;;) {
  3028. if (macro_ptr) {
  3029. p = macro_ptr, t = *p;
  3030. if (ws_str) {
  3031. while (is_space(t) || TOK_LINEFEED == t || TOK_PLCHLDR == t)
  3032. tok_str_add(ws_str, t), t = *++p;
  3033. }
  3034. if (t == 0) {
  3035. end_macro();
  3036. /* also, end of scope for nested defined symbol */
  3037. sa = *nested_list;
  3038. while (sa && sa->v == 0)
  3039. sa = sa->prev;
  3040. if (sa)
  3041. sa->v = 0;
  3042. continue;
  3043. }
  3044. } else {
  3045. ch = handle_eob();
  3046. if (ws_str) {
  3047. while (is_space(ch) || ch == '\n' || ch == '/') {
  3048. if (ch == '/') {
  3049. int c;
  3050. uint8_t *p = file->buf_ptr;
  3051. PEEKC(c, p);
  3052. if (c == '*') {
  3053. p = parse_comment(p);
  3054. file->buf_ptr = p - 1;
  3055. } else if (c == '/') {
  3056. p = parse_line_comment(p);
  3057. file->buf_ptr = p - 1;
  3058. } else
  3059. break;
  3060. ch = ' ';
  3061. }
  3062. if (ch == '\n')
  3063. file->line_num++;
  3064. if (!(ch == '\f' || ch == '\v' || ch == '\r'))
  3065. tok_str_add(ws_str, ch);
  3066. cinp();
  3067. }
  3068. }
  3069. t = ch;
  3070. }
  3071. if (ws_str)
  3072. return t;
  3073. next_nomacro_spc();
  3074. return tok;
  3075. }
  3076. }
  3077. /* do macro substitution of current token with macro 's' and add
  3078. result to (tok_str,tok_len). 'nested_list' is the list of all
  3079. macros we got inside to avoid recursing. Return non zero if no
  3080. substitution needs to be done */
  3081. static int macro_subst_tok(
  3082. TokenString *tok_str,
  3083. Sym **nested_list,
  3084. Sym *s)
  3085. {
  3086. Sym *args, *sa, *sa1;
  3087. int parlevel, t, t1, spc;
  3088. TokenString str;
  3089. char *cstrval;
  3090. CValue cval;
  3091. CString cstr;
  3092. char buf[32];
  3093. /* if symbol is a macro, prepare substitution */
  3094. /* special macros */
  3095. if (tok == TOK___LINE__ || tok == TOK___COUNTER__) {
  3096. t = tok == TOK___LINE__ ? file->line_num : pp_counter++;
  3097. snprintf(buf, sizeof(buf), "%d", t);
  3098. cstrval = buf;
  3099. t1 = TOK_PPNUM;
  3100. goto add_cstr1;
  3101. } else if (tok == TOK___FILE__) {
  3102. cstrval = file->filename;
  3103. goto add_cstr;
  3104. } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
  3105. time_t ti;
  3106. struct tm *tm;
  3107. time(&ti);
  3108. tm = localtime(&ti);
  3109. if (tok == TOK___DATE__) {
  3110. snprintf(buf, sizeof(buf), "%s %2d %d",
  3111. ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
  3112. } else {
  3113. snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
  3114. tm->tm_hour, tm->tm_min, tm->tm_sec);
  3115. }
  3116. cstrval = buf;
  3117. add_cstr:
  3118. t1 = TOK_STR;
  3119. add_cstr1:
  3120. cstr_new(&cstr);
  3121. cstr_cat(&cstr, cstrval, 0);
  3122. cval.str.size = cstr.size;
  3123. cval.str.data = cstr.data;
  3124. tok_str_add2(tok_str, t1, &cval);
  3125. cstr_free(&cstr);
  3126. } else if (s->d) {
  3127. int saved_parse_flags = parse_flags;
  3128. int *joined_str = NULL;
  3129. int *mstr = s->d;
  3130. if (s->type.t == MACRO_FUNC) {
  3131. /* whitespace between macro name and argument list */
  3132. TokenString ws_str;
  3133. tok_str_new(&ws_str);
  3134. spc = 0;
  3135. parse_flags |= PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED
  3136. | PARSE_FLAG_ACCEPT_STRAYS;
  3137. /* get next token from argument stream */
  3138. t = next_argstream(nested_list, &ws_str);
  3139. if (t != '(') {
  3140. /* not a macro substitution after all, restore the
  3141. * macro token plus all whitespace we've read.
  3142. * whitespace is intentionally not merged to preserve
  3143. * newlines. */
  3144. parse_flags = saved_parse_flags;
  3145. tok_str_add(tok_str, tok);
  3146. if (parse_flags & PARSE_FLAG_SPACES) {
  3147. int i;
  3148. for (i = 0; i < ws_str.len; i++)
  3149. tok_str_add(tok_str, ws_str.str[i]);
  3150. }
  3151. tok_str_free_str(ws_str.str);
  3152. return 0;
  3153. } else {
  3154. tok_str_free_str(ws_str.str);
  3155. }
  3156. do {
  3157. next_nomacro(); /* eat '(' */
  3158. } while (tok == TOK_PLCHLDR);
  3159. /* argument macro */
  3160. args = NULL;
  3161. sa = s->next;
  3162. /* NOTE: empty args are allowed, except if no args */
  3163. for(;;) {
  3164. do {
  3165. next_argstream(nested_list, NULL);
  3166. } while (is_space(tok) || TOK_LINEFEED == tok);
  3167. empty_arg:
  3168. /* handle '()' case */
  3169. if (!args && !sa && tok == ')')
  3170. break;
  3171. if (!sa)
  3172. tcc_error("macro '%s' used with too many args",
  3173. get_tok_str(s->v, 0));
  3174. tok_str_new(&str);
  3175. parlevel = spc = 0;
  3176. /* NOTE: non zero sa->t indicates VA_ARGS */
  3177. while ((parlevel > 0 ||
  3178. (tok != ')' &&
  3179. (tok != ',' || sa->type.t)))) {
  3180. if (tok == TOK_EOF || tok == 0)
  3181. break;
  3182. if (tok == '(')
  3183. parlevel++;
  3184. else if (tok == ')')
  3185. parlevel--;
  3186. if (tok == TOK_LINEFEED)
  3187. tok = ' ';
  3188. if (!check_space(tok, &spc))
  3189. tok_str_add2(&str, tok, &tokc);
  3190. next_argstream(nested_list, NULL);
  3191. }
  3192. if (parlevel)
  3193. expect(")");
  3194. str.len -= spc;
  3195. tok_str_add(&str, -1);
  3196. tok_str_add(&str, 0);
  3197. sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
  3198. sa1->d = str.str;
  3199. sa = sa->next;
  3200. if (tok == ')') {
  3201. /* special case for gcc var args: add an empty
  3202. var arg argument if it is omitted */
  3203. if (sa && sa->type.t && gnu_ext)
  3204. goto empty_arg;
  3205. break;
  3206. }
  3207. if (tok != ',')
  3208. expect(",");
  3209. }
  3210. if (sa) {
  3211. tcc_error("macro '%s' used with too few args",
  3212. get_tok_str(s->v, 0));
  3213. }
  3214. parse_flags = saved_parse_flags;
  3215. /* now subst each arg */
  3216. mstr = macro_arg_subst(nested_list, mstr, args);
  3217. /* free memory */
  3218. sa = args;
  3219. while (sa) {
  3220. sa1 = sa->prev;
  3221. tok_str_free_str(sa->d);
  3222. if (sa->next) {
  3223. tok_str_free_str(sa->next->d);
  3224. sym_free(sa->next);
  3225. }
  3226. sym_free(sa);
  3227. sa = sa1;
  3228. }
  3229. }
  3230. sym_push2(nested_list, s->v, 0, 0);
  3231. parse_flags = saved_parse_flags;
  3232. joined_str = macro_twosharps(mstr);
  3233. macro_subst(tok_str, nested_list, joined_str ? joined_str : mstr);
  3234. /* pop nested defined symbol */
  3235. sa1 = *nested_list;
  3236. *nested_list = sa1->prev;
  3237. sym_free(sa1);
  3238. if (joined_str)
  3239. tok_str_free_str(joined_str);
  3240. if (mstr != s->d)
  3241. tok_str_free_str(mstr);
  3242. }
  3243. return 0;
  3244. }
  3245. /* do macro substitution of macro_str and add result to
  3246. (tok_str,tok_len). 'nested_list' is the list of all macros we got
  3247. inside to avoid recursing. */
  3248. static void macro_subst(
  3249. TokenString *tok_str,
  3250. Sym **nested_list,
  3251. const int *macro_str
  3252. )
  3253. {
  3254. Sym *s;
  3255. int t, spc, nosubst;
  3256. CValue cval;
  3257. spc = nosubst = 0;
  3258. while (1) {
  3259. TOK_GET(&t, &macro_str, &cval);
  3260. if (t <= 0)
  3261. break;
  3262. if (t >= TOK_IDENT && 0 == nosubst) {
  3263. s = define_find(t);
  3264. if (s == NULL)
  3265. goto no_subst;
  3266. /* if nested substitution, do nothing */
  3267. if (sym_find2(*nested_list, t)) {
  3268. /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
  3269. tok_str_add2(tok_str, TOK_NOSUBST, NULL);
  3270. goto no_subst;
  3271. }
  3272. {
  3273. TokenString str;
  3274. str.str = (int*)macro_str;
  3275. begin_macro(&str, 2);
  3276. tok = t;
  3277. macro_subst_tok(tok_str, nested_list, s);
  3278. if (str.alloc == 3) {
  3279. /* already finished by reading function macro arguments */
  3280. break;
  3281. }
  3282. macro_str = macro_ptr;
  3283. end_macro ();
  3284. }
  3285. if (tok_str->len)
  3286. spc = is_space(t = tok_str->str[tok_str->lastlen]);
  3287. } else {
  3288. if (t == '\\' && !(parse_flags & PARSE_FLAG_ACCEPT_STRAYS))
  3289. tcc_error("stray '\\' in program");
  3290. no_subst:
  3291. if (!check_space(t, &spc))
  3292. tok_str_add2(tok_str, t, &cval);
  3293. if (nosubst) {
  3294. if (nosubst > 1 && (spc || (++nosubst == 3 && t == '(')))
  3295. continue;
  3296. nosubst = 0;
  3297. }
  3298. if (t == TOK_NOSUBST)
  3299. nosubst = 1;
  3300. }
  3301. /* GCC supports 'defined' as result of a macro substitution */
  3302. if (t == TOK_DEFINED && pp_expr)
  3303. nosubst = 2;
  3304. }
  3305. }
  3306. /* return next token with macro substitution */
  3307. ST_FUNC void next(void)
  3308. {
  3309. redo:
  3310. if (parse_flags & PARSE_FLAG_SPACES)
  3311. next_nomacro_spc();
  3312. else
  3313. next_nomacro();
  3314. if (macro_ptr) {
  3315. if (tok == TOK_NOSUBST || tok == TOK_PLCHLDR) {
  3316. /* discard preprocessor markers */
  3317. goto redo;
  3318. } else if (tok == 0) {
  3319. /* end of macro or unget token string */
  3320. end_macro();
  3321. goto redo;
  3322. }
  3323. } else if (tok >= TOK_IDENT && (parse_flags & PARSE_FLAG_PREPROCESS)) {
  3324. Sym *s;
  3325. /* if reading from file, try to substitute macros */
  3326. s = define_find(tok);
  3327. if (s) {
  3328. Sym *nested_list = NULL;
  3329. tokstr_buf.len = 0;
  3330. macro_subst_tok(&tokstr_buf, &nested_list, s);
  3331. tok_str_add(&tokstr_buf, 0);
  3332. begin_macro(&tokstr_buf, 2);
  3333. goto redo;
  3334. }
  3335. }
  3336. /* convert preprocessor tokens into C tokens */
  3337. if (tok == TOK_PPNUM) {
  3338. if (parse_flags & PARSE_FLAG_TOK_NUM)
  3339. parse_number((char *)tokc.str.data);
  3340. } else if (tok == TOK_PPSTR) {
  3341. if (parse_flags & PARSE_FLAG_TOK_STR)
  3342. parse_string((char *)tokc.str.data, tokc.str.size - 1);
  3343. }
  3344. }
  3345. /* push back current token and set current token to 'last_tok'. Only
  3346. identifier case handled for labels. */
  3347. ST_INLN void unget_tok(int last_tok)
  3348. {
  3349. TokenString *str = tok_str_alloc();
  3350. tok_str_add2(str, tok, &tokc);
  3351. tok_str_add(str, 0);
  3352. begin_macro(str, 1);
  3353. tok = last_tok;
  3354. }
  3355. ST_FUNC void preprocess_start(TCCState *s1, int is_asm)
  3356. {
  3357. CString cstr;
  3358. int i;
  3359. s1->include_stack_ptr = s1->include_stack;
  3360. s1->ifdef_stack_ptr = s1->ifdef_stack;
  3361. file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
  3362. pp_expr = 0;
  3363. pp_counter = 0;
  3364. pp_debug_tok = pp_debug_symv = 0;
  3365. pp_once++;
  3366. pvtop = vtop = vstack - 1;
  3367. s1->pack_stack[0] = 0;
  3368. s1->pack_stack_ptr = s1->pack_stack;
  3369. set_idnum('$', s1->dollars_in_identifiers ? IS_ID : 0);
  3370. set_idnum('.', is_asm ? IS_ID : 0);
  3371. cstr_new(&cstr);
  3372. cstr_cat(&cstr, "\"", -1);
  3373. cstr_cat(&cstr, file->filename, -1);
  3374. cstr_cat(&cstr, "\"", 0);
  3375. tcc_define_symbol(s1, "__BASE_FILE__", cstr.data);
  3376. cstr_reset(&cstr);
  3377. for (i = 0; i < s1->nb_cmd_include_files; i++) {
  3378. cstr_cat(&cstr, "#include \"", -1);
  3379. cstr_cat(&cstr, s1->cmd_include_files[i], -1);
  3380. cstr_cat(&cstr, "\"\n", -1);
  3381. }
  3382. if (cstr.size) {
  3383. *s1->include_stack_ptr++ = file;
  3384. tcc_open_bf(s1, "<command line>", cstr.size);
  3385. memcpy(file->buffer, cstr.data, cstr.size);
  3386. }
  3387. cstr_free(&cstr);
  3388. if (is_asm)
  3389. tcc_define_symbol(s1, "__ASSEMBLER__", NULL);
  3390. parse_flags = is_asm ? PARSE_FLAG_ASM_FILE : 0;
  3391. tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
  3392. }
  3393. /* cleanup from error/setjmp */
  3394. ST_FUNC void preprocess_end(TCCState *s1)
  3395. {
  3396. while (macro_stack)
  3397. end_macro();
  3398. macro_ptr = NULL;
  3399. }
  3400. ST_FUNC void tccpp_new(TCCState *s)
  3401. {
  3402. int i, c;
  3403. const char *p, *r;
  3404. /* might be used in error() before preprocess_start() */
  3405. s->include_stack_ptr = s->include_stack;
  3406. s->ppfp = stdout;
  3407. /* init isid table */
  3408. for(i = CH_EOF; i<128; i++)
  3409. set_idnum(i,
  3410. is_space(i) ? IS_SPC
  3411. : isid(i) ? IS_ID
  3412. : isnum(i) ? IS_NUM
  3413. : 0);
  3414. for(i = 128; i<256; i++)
  3415. set_idnum(i, IS_ID);
  3416. /* init allocators */
  3417. tal_new(&toksym_alloc, TOKSYM_TAL_LIMIT, TOKSYM_TAL_SIZE);
  3418. tal_new(&tokstr_alloc, TOKSTR_TAL_LIMIT, TOKSTR_TAL_SIZE);
  3419. tal_new(&cstr_alloc, CSTR_TAL_LIMIT, CSTR_TAL_SIZE);
  3420. memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
  3421. cstr_new(&cstr_buf);
  3422. cstr_realloc(&cstr_buf, STRING_MAX_SIZE);
  3423. tok_str_new(&tokstr_buf);
  3424. tok_str_realloc(&tokstr_buf, TOKSTR_MAX_SIZE);
  3425. tok_ident = TOK_IDENT;
  3426. p = tcc_keywords;
  3427. while (*p) {
  3428. r = p;
  3429. for(;;) {
  3430. c = *r++;
  3431. if (c == '\0')
  3432. break;
  3433. }
  3434. tok_alloc(p, r - p - 1);
  3435. p = r;
  3436. }
  3437. }
  3438. ST_FUNC void tccpp_delete(TCCState *s)
  3439. {
  3440. int i, n;
  3441. /* free -D and compiler defines */
  3442. free_defines(NULL);
  3443. /* free tokens */
  3444. n = tok_ident - TOK_IDENT;
  3445. for(i = 0; i < n; i++)
  3446. tal_free(toksym_alloc, table_ident[i]);
  3447. tcc_free(table_ident);
  3448. table_ident = NULL;
  3449. /* free static buffers */
  3450. cstr_free(&tokcstr);
  3451. cstr_free(&cstr_buf);
  3452. cstr_free(&macro_equal_buf);
  3453. tok_str_free_str(tokstr_buf.str);
  3454. /* free allocators */
  3455. tal_delete(toksym_alloc);
  3456. toksym_alloc = NULL;
  3457. tal_delete(tokstr_alloc);
  3458. tokstr_alloc = NULL;
  3459. tal_delete(cstr_alloc);
  3460. cstr_alloc = NULL;
  3461. }
  3462. /* ------------------------------------------------------------------------- */
  3463. /* tcc -E [-P[1]] [-dD} support */
  3464. static void tok_print(const char *msg, const int *str)
  3465. {
  3466. FILE *fp;
  3467. int t, s = 0;
  3468. CValue cval;
  3469. fp = tcc_state->ppfp;
  3470. fprintf(fp, "%s", msg);
  3471. while (str) {
  3472. TOK_GET(&t, &str, &cval);
  3473. if (!t)
  3474. break;
  3475. fprintf(fp, " %s" + s, get_tok_str(t, &cval)), s = 1;
  3476. }
  3477. fprintf(fp, "\n");
  3478. }
  3479. static void pp_line(TCCState *s1, BufferedFile *f, int level)
  3480. {
  3481. int d = f->line_num - f->line_ref;
  3482. if (s1->dflag & 4)
  3483. return;
  3484. if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_NONE) {
  3485. ;
  3486. } else if (level == 0 && f->line_ref && d < 8) {
  3487. while (d > 0)
  3488. fputs("\n", s1->ppfp), --d;
  3489. } else if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_STD) {
  3490. fprintf(s1->ppfp, "#line %d \"%s\"\n", f->line_num, f->filename);
  3491. } else {
  3492. fprintf(s1->ppfp, "# %d \"%s\"%s\n", f->line_num, f->filename,
  3493. level > 0 ? " 1" : level < 0 ? " 2" : "");
  3494. }
  3495. f->line_ref = f->line_num;
  3496. }
  3497. static void define_print(TCCState *s1, int v)
  3498. {
  3499. FILE *fp;
  3500. Sym *s;
  3501. s = define_find(v);
  3502. if (NULL == s || NULL == s->d)
  3503. return;
  3504. fp = s1->ppfp;
  3505. fprintf(fp, "#define %s", get_tok_str(v, NULL));
  3506. if (s->type.t == MACRO_FUNC) {
  3507. Sym *a = s->next;
  3508. fprintf(fp,"(");
  3509. if (a)
  3510. for (;;) {
  3511. fprintf(fp,"%s", get_tok_str(a->v & ~SYM_FIELD, NULL));
  3512. if (!(a = a->next))
  3513. break;
  3514. fprintf(fp,",");
  3515. }
  3516. fprintf(fp,")");
  3517. }
  3518. tok_print("", s->d);
  3519. }
  3520. static void pp_debug_defines(TCCState *s1)
  3521. {
  3522. int v, t;
  3523. const char *vs;
  3524. FILE *fp;
  3525. t = pp_debug_tok;
  3526. if (t == 0)
  3527. return;
  3528. file->line_num--;
  3529. pp_line(s1, file, 0);
  3530. file->line_ref = ++file->line_num;
  3531. fp = s1->ppfp;
  3532. v = pp_debug_symv;
  3533. vs = get_tok_str(v, NULL);
  3534. if (t == TOK_DEFINE) {
  3535. define_print(s1, v);
  3536. } else if (t == TOK_UNDEF) {
  3537. fprintf(fp, "#undef %s\n", vs);
  3538. } else if (t == TOK_push_macro) {
  3539. fprintf(fp, "#pragma push_macro(\"%s\")\n", vs);
  3540. } else if (t == TOK_pop_macro) {
  3541. fprintf(fp, "#pragma pop_macro(\"%s\")\n", vs);
  3542. }
  3543. pp_debug_tok = 0;
  3544. }
  3545. static void pp_debug_builtins(TCCState *s1)
  3546. {
  3547. int v;
  3548. for (v = TOK_IDENT; v < tok_ident; ++v)
  3549. define_print(s1, v);
  3550. }
  3551. /* Add a space between tokens a and b to avoid unwanted textual pasting */
  3552. static int pp_need_space(int a, int b)
  3553. {
  3554. return 'E' == a ? '+' == b || '-' == b
  3555. : '+' == a ? TOK_INC == b || '+' == b
  3556. : '-' == a ? TOK_DEC == b || '-' == b
  3557. : a >= TOK_IDENT ? b >= TOK_IDENT
  3558. : a == TOK_PPNUM ? b >= TOK_IDENT
  3559. : 0;
  3560. }
  3561. /* maybe hex like 0x1e */
  3562. static int pp_check_he0xE(int t, const char *p)
  3563. {
  3564. if (t == TOK_PPNUM && toup(strchr(p, 0)[-1]) == 'E')
  3565. return 'E';
  3566. return t;
  3567. }
  3568. /* Preprocess the current file */
  3569. ST_FUNC int tcc_preprocess(TCCState *s1)
  3570. {
  3571. BufferedFile **iptr;
  3572. int token_seen, spcs, level;
  3573. const char *p;
  3574. char white[400];
  3575. parse_flags = PARSE_FLAG_PREPROCESS
  3576. | (parse_flags & PARSE_FLAG_ASM_FILE)
  3577. | PARSE_FLAG_LINEFEED
  3578. | PARSE_FLAG_SPACES
  3579. | PARSE_FLAG_ACCEPT_STRAYS
  3580. ;
  3581. /* Credits to Fabrice Bellard's initial revision to demonstrate its
  3582. capability to compile and run itself, provided all numbers are
  3583. given as decimals. tcc -E -P10 will do. */
  3584. if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_P10)
  3585. parse_flags |= PARSE_FLAG_TOK_NUM, s1->Pflag = 1;
  3586. #ifdef PP_BENCH
  3587. /* for PP benchmarks */
  3588. do next(); while (tok != TOK_EOF);
  3589. return 0;
  3590. #endif
  3591. if (s1->dflag & 1) {
  3592. pp_debug_builtins(s1);
  3593. s1->dflag &= ~1;
  3594. }
  3595. token_seen = TOK_LINEFEED, spcs = 0;
  3596. pp_line(s1, file, 0);
  3597. for (;;) {
  3598. iptr = s1->include_stack_ptr;
  3599. next();
  3600. if (tok == TOK_EOF)
  3601. break;
  3602. level = s1->include_stack_ptr - iptr;
  3603. if (level) {
  3604. if (level > 0)
  3605. pp_line(s1, *iptr, 0);
  3606. pp_line(s1, file, level);
  3607. }
  3608. if (s1->dflag & 7) {
  3609. pp_debug_defines(s1);
  3610. if (s1->dflag & 4)
  3611. continue;
  3612. }
  3613. if (is_space(tok)) {
  3614. if (spcs < sizeof white - 1)
  3615. white[spcs++] = tok;
  3616. continue;
  3617. } else if (tok == TOK_LINEFEED) {
  3618. spcs = 0;
  3619. if (token_seen == TOK_LINEFEED)
  3620. continue;
  3621. ++file->line_ref;
  3622. } else if (token_seen == TOK_LINEFEED) {
  3623. pp_line(s1, file, 0);
  3624. } else if (spcs == 0 && pp_need_space(token_seen, tok)) {
  3625. white[spcs++] = ' ';
  3626. }
  3627. white[spcs] = 0, fputs(white, s1->ppfp), spcs = 0;
  3628. fputs(p = get_tok_str(tok, &tokc), s1->ppfp);
  3629. token_seen = pp_check_he0xE(tok, p);
  3630. }
  3631. return 0;
  3632. }
  3633. /* ------------------------------------------------------------------------- */