tccpp.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936
  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. static const char tcc_keywords[] =
  21. #define DEF(id, str) str "\0"
  22. #include "tcctok.h"
  23. #undef DEF
  24. ;
  25. /* WARNING: the content of this string encodes token numbers */
  26. static char tok_two_chars[] = "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
  27. /* true if isid(c) || isnum(c) */
  28. static unsigned char isidnum_table[256-CH_EOF];
  29. struct macro_level {
  30. struct macro_level *prev;
  31. int *p;
  32. };
  33. static void next_nomacro(void);
  34. static void next_nomacro_spc(void);
  35. static void macro_subst(TokenString *tok_str, Sym **nested_list,
  36. const int *macro_str, struct macro_level **can_read_stream);
  37. /* allocate a new token */
  38. static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
  39. {
  40. TokenSym *ts, **ptable;
  41. int i;
  42. if (tok_ident >= SYM_FIRST_ANOM)
  43. error("memory full");
  44. /* expand token table if needed */
  45. i = tok_ident - TOK_IDENT;
  46. if ((i % TOK_ALLOC_INCR) == 0) {
  47. ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
  48. if (!ptable)
  49. error("memory full");
  50. table_ident = ptable;
  51. }
  52. ts = tcc_malloc(sizeof(TokenSym) + len);
  53. table_ident[i] = ts;
  54. ts->tok = tok_ident++;
  55. ts->sym_define = NULL;
  56. ts->sym_label = NULL;
  57. ts->sym_struct = NULL;
  58. ts->sym_identifier = NULL;
  59. ts->len = len;
  60. ts->hash_next = NULL;
  61. memcpy(ts->str, str, len);
  62. ts->str[len] = '\0';
  63. *pts = ts;
  64. return ts;
  65. }
  66. #define TOK_HASH_INIT 1
  67. #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
  68. /* find a token and add it if not found */
  69. static TokenSym *tok_alloc(const char *str, int len)
  70. {
  71. TokenSym *ts, **pts;
  72. int i;
  73. unsigned int h;
  74. h = TOK_HASH_INIT;
  75. for(i=0;i<len;i++)
  76. h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
  77. h &= (TOK_HASH_SIZE - 1);
  78. pts = &hash_ident[h];
  79. for(;;) {
  80. ts = *pts;
  81. if (!ts)
  82. break;
  83. if (ts->len == len && !memcmp(ts->str, str, len))
  84. return ts;
  85. pts = &(ts->hash_next);
  86. }
  87. return tok_alloc_new(pts, str, len);
  88. }
  89. /* XXX: buffer overflow */
  90. /* XXX: float tokens */
  91. char *get_tok_str(int v, CValue *cv)
  92. {
  93. static char buf[STRING_MAX_SIZE + 1];
  94. static CString cstr_buf;
  95. CString *cstr;
  96. unsigned char *q;
  97. char *p;
  98. int i, len;
  99. /* NOTE: to go faster, we give a fixed buffer for small strings */
  100. cstr_reset(&cstr_buf);
  101. cstr_buf.data = buf;
  102. cstr_buf.size_allocated = sizeof(buf);
  103. p = buf;
  104. switch(v) {
  105. case TOK_CINT:
  106. case TOK_CUINT:
  107. /* XXX: not quite exact, but only useful for testing */
  108. sprintf(p, "%u", cv->ui);
  109. break;
  110. case TOK_CLLONG:
  111. case TOK_CULLONG:
  112. /* XXX: not quite exact, but only useful for testing */
  113. sprintf(p, "%Lu", cv->ull);
  114. break;
  115. case TOK_LCHAR:
  116. cstr_ccat(&cstr_buf, 'L');
  117. case TOK_CCHAR:
  118. cstr_ccat(&cstr_buf, '\'');
  119. add_char(&cstr_buf, cv->i);
  120. cstr_ccat(&cstr_buf, '\'');
  121. cstr_ccat(&cstr_buf, '\0');
  122. break;
  123. case TOK_PPNUM:
  124. cstr = cv->cstr;
  125. len = cstr->size - 1;
  126. for(i=0;i<len;i++)
  127. add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
  128. cstr_ccat(&cstr_buf, '\0');
  129. break;
  130. case TOK_LSTR:
  131. cstr_ccat(&cstr_buf, 'L');
  132. case TOK_STR:
  133. cstr = cv->cstr;
  134. cstr_ccat(&cstr_buf, '\"');
  135. if (v == TOK_STR) {
  136. len = cstr->size - 1;
  137. for(i=0;i<len;i++)
  138. add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
  139. } else {
  140. len = (cstr->size / sizeof(nwchar_t)) - 1;
  141. for(i=0;i<len;i++)
  142. add_char(&cstr_buf, ((nwchar_t *)cstr->data)[i]);
  143. }
  144. cstr_ccat(&cstr_buf, '\"');
  145. cstr_ccat(&cstr_buf, '\0');
  146. break;
  147. case TOK_LT:
  148. v = '<';
  149. goto addv;
  150. case TOK_GT:
  151. v = '>';
  152. goto addv;
  153. case TOK_DOTS:
  154. return strcpy(p, "...");
  155. case TOK_A_SHL:
  156. return strcpy(p, "<<=");
  157. case TOK_A_SAR:
  158. return strcpy(p, ">>=");
  159. default:
  160. if (v < TOK_IDENT) {
  161. /* search in two bytes table */
  162. q = tok_two_chars;
  163. while (*q) {
  164. if (q[2] == v) {
  165. *p++ = q[0];
  166. *p++ = q[1];
  167. *p = '\0';
  168. return buf;
  169. }
  170. q += 3;
  171. }
  172. addv:
  173. *p++ = v;
  174. *p = '\0';
  175. } else if (v < tok_ident) {
  176. return table_ident[v - TOK_IDENT]->str;
  177. } else if (v >= SYM_FIRST_ANOM) {
  178. /* special name for anonymous symbol */
  179. sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
  180. } else {
  181. /* should never happen */
  182. return NULL;
  183. }
  184. break;
  185. }
  186. return cstr_buf.data;
  187. }
  188. /* fill input buffer and peek next char */
  189. static int tcc_peekc_slow(BufferedFile *bf)
  190. {
  191. int len;
  192. /* only tries to read if really end of buffer */
  193. if (bf->buf_ptr >= bf->buf_end) {
  194. if (bf->fd != -1) {
  195. #if defined(PARSE_DEBUG)
  196. len = 8;
  197. #else
  198. len = IO_BUF_SIZE;
  199. #endif
  200. len = read(bf->fd, bf->buffer, len);
  201. if (len < 0)
  202. len = 0;
  203. } else {
  204. len = 0;
  205. }
  206. total_bytes += len;
  207. bf->buf_ptr = bf->buffer;
  208. bf->buf_end = bf->buffer + len;
  209. *bf->buf_end = CH_EOB;
  210. }
  211. if (bf->buf_ptr < bf->buf_end) {
  212. return bf->buf_ptr[0];
  213. } else {
  214. bf->buf_ptr = bf->buf_end;
  215. return CH_EOF;
  216. }
  217. }
  218. /* return the current character, handling end of block if necessary
  219. (but not stray) */
  220. static int handle_eob(void)
  221. {
  222. return tcc_peekc_slow(file);
  223. }
  224. /* read next char from current input file and handle end of input buffer */
  225. static inline void inp(void)
  226. {
  227. ch = *(++(file->buf_ptr));
  228. /* end of buffer/file handling */
  229. if (ch == CH_EOB)
  230. ch = handle_eob();
  231. }
  232. /* handle '\[\r]\n' */
  233. static int handle_stray_noerror(void)
  234. {
  235. while (ch == '\\') {
  236. inp();
  237. if (ch == '\n') {
  238. file->line_num++;
  239. inp();
  240. } else if (ch == '\r') {
  241. inp();
  242. if (ch != '\n')
  243. goto fail;
  244. file->line_num++;
  245. inp();
  246. } else {
  247. fail:
  248. return 1;
  249. }
  250. }
  251. return 0;
  252. }
  253. static void handle_stray(void)
  254. {
  255. if (handle_stray_noerror())
  256. error("stray '\\' in program");
  257. }
  258. /* skip the stray and handle the \\n case. Output an error if
  259. incorrect char after the stray */
  260. static int handle_stray1(uint8_t *p)
  261. {
  262. int c;
  263. if (p >= file->buf_end) {
  264. file->buf_ptr = p;
  265. c = handle_eob();
  266. p = file->buf_ptr;
  267. if (c == '\\')
  268. goto parse_stray;
  269. } else {
  270. parse_stray:
  271. file->buf_ptr = p;
  272. ch = *p;
  273. handle_stray();
  274. p = file->buf_ptr;
  275. c = *p;
  276. }
  277. return c;
  278. }
  279. /* handle just the EOB case, but not stray */
  280. #define PEEKC_EOB(c, p)\
  281. {\
  282. p++;\
  283. c = *p;\
  284. if (c == '\\') {\
  285. file->buf_ptr = p;\
  286. c = handle_eob();\
  287. p = file->buf_ptr;\
  288. }\
  289. }
  290. /* handle the complicated stray case */
  291. #define PEEKC(c, p)\
  292. {\
  293. p++;\
  294. c = *p;\
  295. if (c == '\\') {\
  296. c = handle_stray1(p);\
  297. p = file->buf_ptr;\
  298. }\
  299. }
  300. /* input with '\[\r]\n' handling. Note that this function cannot
  301. handle other characters after '\', so you cannot call it inside
  302. strings or comments */
  303. static void minp(void)
  304. {
  305. inp();
  306. if (ch == '\\')
  307. handle_stray();
  308. }
  309. /* single line C++ comments */
  310. static uint8_t *parse_line_comment(uint8_t *p)
  311. {
  312. int c;
  313. p++;
  314. for(;;) {
  315. c = *p;
  316. redo:
  317. if (c == '\n' || c == CH_EOF) {
  318. break;
  319. } else if (c == '\\') {
  320. file->buf_ptr = p;
  321. c = handle_eob();
  322. p = file->buf_ptr;
  323. if (c == '\\') {
  324. PEEKC_EOB(c, p);
  325. if (c == '\n') {
  326. file->line_num++;
  327. PEEKC_EOB(c, p);
  328. } else if (c == '\r') {
  329. PEEKC_EOB(c, p);
  330. if (c == '\n') {
  331. file->line_num++;
  332. PEEKC_EOB(c, p);
  333. }
  334. }
  335. } else {
  336. goto redo;
  337. }
  338. } else {
  339. p++;
  340. }
  341. }
  342. return p;
  343. }
  344. /* C comments */
  345. static uint8_t *parse_comment(uint8_t *p)
  346. {
  347. int c;
  348. p++;
  349. for(;;) {
  350. /* fast skip loop */
  351. for(;;) {
  352. c = *p;
  353. if (c == '\n' || c == '*' || c == '\\')
  354. break;
  355. p++;
  356. c = *p;
  357. if (c == '\n' || c == '*' || c == '\\')
  358. break;
  359. p++;
  360. }
  361. /* now we can handle all the cases */
  362. if (c == '\n') {
  363. file->line_num++;
  364. p++;
  365. } else if (c == '*') {
  366. p++;
  367. for(;;) {
  368. c = *p;
  369. if (c == '*') {
  370. p++;
  371. } else if (c == '/') {
  372. goto end_of_comment;
  373. } else if (c == '\\') {
  374. file->buf_ptr = p;
  375. c = handle_eob();
  376. p = file->buf_ptr;
  377. if (c == '\\') {
  378. /* skip '\[\r]\n', otherwise just skip the stray */
  379. while (c == '\\') {
  380. PEEKC_EOB(c, p);
  381. if (c == '\n') {
  382. file->line_num++;
  383. PEEKC_EOB(c, p);
  384. } else if (c == '\r') {
  385. PEEKC_EOB(c, p);
  386. if (c == '\n') {
  387. file->line_num++;
  388. PEEKC_EOB(c, p);
  389. }
  390. } else {
  391. goto after_star;
  392. }
  393. }
  394. }
  395. } else {
  396. break;
  397. }
  398. }
  399. after_star: ;
  400. } else {
  401. /* stray, eob or eof */
  402. file->buf_ptr = p;
  403. c = handle_eob();
  404. p = file->buf_ptr;
  405. if (c == CH_EOF) {
  406. error("unexpected end of file in comment");
  407. } else if (c == '\\') {
  408. p++;
  409. }
  410. }
  411. }
  412. end_of_comment:
  413. p++;
  414. return p;
  415. }
  416. #define cinp minp
  417. static inline void skip_spaces(void)
  418. {
  419. while (is_space(ch))
  420. cinp();
  421. }
  422. static inline int check_space(int t, int *spc)
  423. {
  424. if (is_space(t)) {
  425. if (*spc)
  426. return 1;
  427. *spc = 1;
  428. } else
  429. *spc = 0;
  430. return 0;
  431. }
  432. /* parse a string without interpreting escapes */
  433. static uint8_t *parse_pp_string(uint8_t *p,
  434. int sep, CString *str)
  435. {
  436. int c;
  437. p++;
  438. for(;;) {
  439. c = *p;
  440. if (c == sep) {
  441. break;
  442. } else if (c == '\\') {
  443. file->buf_ptr = p;
  444. c = handle_eob();
  445. p = file->buf_ptr;
  446. if (c == CH_EOF) {
  447. unterminated_string:
  448. /* XXX: indicate line number of start of string */
  449. error("missing terminating %c character", sep);
  450. } else if (c == '\\') {
  451. /* escape : just skip \[\r]\n */
  452. PEEKC_EOB(c, p);
  453. if (c == '\n') {
  454. file->line_num++;
  455. p++;
  456. } else if (c == '\r') {
  457. PEEKC_EOB(c, p);
  458. if (c != '\n')
  459. expect("'\n' after '\r'");
  460. file->line_num++;
  461. p++;
  462. } else if (c == CH_EOF) {
  463. goto unterminated_string;
  464. } else {
  465. if (str) {
  466. cstr_ccat(str, '\\');
  467. cstr_ccat(str, c);
  468. }
  469. p++;
  470. }
  471. }
  472. } else if (c == '\n') {
  473. file->line_num++;
  474. goto add_char;
  475. } else if (c == '\r') {
  476. PEEKC_EOB(c, p);
  477. if (c != '\n') {
  478. if (str)
  479. cstr_ccat(str, '\r');
  480. } else {
  481. file->line_num++;
  482. goto add_char;
  483. }
  484. } else {
  485. add_char:
  486. if (str)
  487. cstr_ccat(str, c);
  488. p++;
  489. }
  490. }
  491. p++;
  492. return p;
  493. }
  494. /* skip block of text until #else, #elif or #endif. skip also pairs of
  495. #if/#endif */
  496. void preprocess_skip(void)
  497. {
  498. int a, start_of_line, c, in_warn_or_error;
  499. uint8_t *p;
  500. p = file->buf_ptr;
  501. a = 0;
  502. redo_start:
  503. start_of_line = 1;
  504. in_warn_or_error = 0;
  505. for(;;) {
  506. redo_no_start:
  507. c = *p;
  508. switch(c) {
  509. case ' ':
  510. case '\t':
  511. case '\f':
  512. case '\v':
  513. case '\r':
  514. p++;
  515. goto redo_no_start;
  516. case '\n':
  517. file->line_num++;
  518. p++;
  519. goto redo_start;
  520. case '\\':
  521. file->buf_ptr = p;
  522. c = handle_eob();
  523. if (c == CH_EOF) {
  524. expect("#endif");
  525. } else if (c == '\\') {
  526. ch = file->buf_ptr[0];
  527. handle_stray_noerror();
  528. }
  529. p = file->buf_ptr;
  530. goto redo_no_start;
  531. /* skip strings */
  532. case '\"':
  533. case '\'':
  534. if (in_warn_or_error)
  535. goto _default;
  536. p = parse_pp_string(p, c, NULL);
  537. break;
  538. /* skip comments */
  539. case '/':
  540. if (in_warn_or_error)
  541. goto _default;
  542. file->buf_ptr = p;
  543. ch = *p;
  544. minp();
  545. p = file->buf_ptr;
  546. if (ch == '*') {
  547. p = parse_comment(p);
  548. } else if (ch == '/') {
  549. p = parse_line_comment(p);
  550. }
  551. break;
  552. case '#':
  553. p++;
  554. if (start_of_line) {
  555. file->buf_ptr = p;
  556. next_nomacro();
  557. p = file->buf_ptr;
  558. if (a == 0 &&
  559. (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
  560. goto the_end;
  561. if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
  562. a++;
  563. else if (tok == TOK_ENDIF)
  564. a--;
  565. else if( tok == TOK_ERROR || tok == TOK_WARNING)
  566. in_warn_or_error = 1;
  567. }
  568. break;
  569. _default:
  570. default:
  571. p++;
  572. break;
  573. }
  574. start_of_line = 0;
  575. }
  576. the_end: ;
  577. file->buf_ptr = p;
  578. }
  579. /* ParseState handling */
  580. /* XXX: currently, no include file info is stored. Thus, we cannot display
  581. accurate messages if the function or data definition spans multiple
  582. files */
  583. /* save current parse state in 's' */
  584. void save_parse_state(ParseState *s)
  585. {
  586. s->line_num = file->line_num;
  587. s->macro_ptr = macro_ptr;
  588. s->tok = tok;
  589. s->tokc = tokc;
  590. }
  591. /* restore parse state from 's' */
  592. void restore_parse_state(ParseState *s)
  593. {
  594. file->line_num = s->line_num;
  595. macro_ptr = s->macro_ptr;
  596. tok = s->tok;
  597. tokc = s->tokc;
  598. }
  599. /* return the number of additional 'ints' necessary to store the
  600. token */
  601. static inline int tok_ext_size(int t)
  602. {
  603. switch(t) {
  604. /* 4 bytes */
  605. case TOK_CINT:
  606. case TOK_CUINT:
  607. case TOK_CCHAR:
  608. case TOK_LCHAR:
  609. case TOK_CFLOAT:
  610. case TOK_LINENUM:
  611. return 1;
  612. case TOK_STR:
  613. case TOK_LSTR:
  614. case TOK_PPNUM:
  615. error("unsupported token");
  616. return 1;
  617. case TOK_CDOUBLE:
  618. case TOK_CLLONG:
  619. case TOK_CULLONG:
  620. return 2;
  621. case TOK_CLDOUBLE:
  622. return LDOUBLE_SIZE / 4;
  623. default:
  624. return 0;
  625. }
  626. }
  627. /* token string handling */
  628. static inline void tok_str_new(TokenString *s)
  629. {
  630. s->str = NULL;
  631. s->len = 0;
  632. s->allocated_len = 0;
  633. s->last_line_num = -1;
  634. }
  635. static void tok_str_free(int *str)
  636. {
  637. tcc_free(str);
  638. }
  639. static int *tok_str_realloc(TokenString *s)
  640. {
  641. int *str, len;
  642. if (s->allocated_len == 0) {
  643. len = 8;
  644. } else {
  645. len = s->allocated_len * 2;
  646. }
  647. str = tcc_realloc(s->str, len * sizeof(int));
  648. if (!str)
  649. error("memory full");
  650. s->allocated_len = len;
  651. s->str = str;
  652. return str;
  653. }
  654. static void tok_str_add(TokenString *s, int t)
  655. {
  656. int len, *str;
  657. len = s->len;
  658. str = s->str;
  659. if (len >= s->allocated_len)
  660. str = tok_str_realloc(s);
  661. str[len++] = t;
  662. s->len = len;
  663. }
  664. static void tok_str_add2(TokenString *s, int t, CValue *cv)
  665. {
  666. int len, *str;
  667. len = s->len;
  668. str = s->str;
  669. /* allocate space for worst case */
  670. if (len + TOK_MAX_SIZE > s->allocated_len)
  671. str = tok_str_realloc(s);
  672. str[len++] = t;
  673. switch(t) {
  674. case TOK_CINT:
  675. case TOK_CUINT:
  676. case TOK_CCHAR:
  677. case TOK_LCHAR:
  678. case TOK_CFLOAT:
  679. case TOK_LINENUM:
  680. str[len++] = cv->tab[0];
  681. break;
  682. case TOK_PPNUM:
  683. case TOK_STR:
  684. case TOK_LSTR:
  685. {
  686. int nb_words;
  687. CString *cstr;
  688. nb_words = (sizeof(CString) + cv->cstr->size + 3) >> 2;
  689. while ((len + nb_words) > s->allocated_len)
  690. str = tok_str_realloc(s);
  691. cstr = (CString *)(str + len);
  692. cstr->data = NULL;
  693. cstr->size = cv->cstr->size;
  694. cstr->data_allocated = NULL;
  695. cstr->size_allocated = cstr->size;
  696. memcpy((char *)cstr + sizeof(CString),
  697. cv->cstr->data, cstr->size);
  698. len += nb_words;
  699. }
  700. break;
  701. case TOK_CDOUBLE:
  702. case TOK_CLLONG:
  703. case TOK_CULLONG:
  704. #if LDOUBLE_SIZE == 8
  705. case TOK_CLDOUBLE:
  706. #endif
  707. str[len++] = cv->tab[0];
  708. str[len++] = cv->tab[1];
  709. break;
  710. #if LDOUBLE_SIZE == 12
  711. case TOK_CLDOUBLE:
  712. str[len++] = cv->tab[0];
  713. str[len++] = cv->tab[1];
  714. str[len++] = cv->tab[2];
  715. #elif LDOUBLE_SIZE == 16
  716. case TOK_CLDOUBLE:
  717. str[len++] = cv->tab[0];
  718. str[len++] = cv->tab[1];
  719. str[len++] = cv->tab[2];
  720. str[len++] = cv->tab[3];
  721. #elif LDOUBLE_SIZE != 8
  722. #error add long double size support
  723. #endif
  724. break;
  725. default:
  726. break;
  727. }
  728. s->len = len;
  729. }
  730. /* add the current parse token in token string 's' */
  731. static void tok_str_add_tok(TokenString *s)
  732. {
  733. CValue cval;
  734. /* save line number info */
  735. if (file->line_num != s->last_line_num) {
  736. s->last_line_num = file->line_num;
  737. cval.i = s->last_line_num;
  738. tok_str_add2(s, TOK_LINENUM, &cval);
  739. }
  740. tok_str_add2(s, tok, &tokc);
  741. }
  742. #if LDOUBLE_SIZE == 16
  743. #define LDOUBLE_GET(p, cv) \
  744. cv.tab[0] = p[0]; \
  745. cv.tab[1] = p[1]; \
  746. cv.tab[2] = p[2]; \
  747. cv.tab[3] = p[3];
  748. #elif LDOUBLE_SIZE == 12
  749. #define LDOUBLE_GET(p, cv) \
  750. cv.tab[0] = p[0]; \
  751. cv.tab[1] = p[1]; \
  752. cv.tab[2] = p[2];
  753. #elif LDOUBLE_SIZE == 8
  754. #define LDOUBLE_GET(p, cv) \
  755. cv.tab[0] = p[0]; \
  756. cv.tab[1] = p[1];
  757. #else
  758. #error add long double size support
  759. #endif
  760. /* get a token from an integer array and increment pointer
  761. accordingly. we code it as a macro to avoid pointer aliasing. */
  762. #define TOK_GET(t, p, cv) \
  763. { \
  764. t = *p++; \
  765. switch(t) { \
  766. case TOK_CINT: \
  767. case TOK_CUINT: \
  768. case TOK_CCHAR: \
  769. case TOK_LCHAR: \
  770. case TOK_CFLOAT: \
  771. case TOK_LINENUM: \
  772. cv.tab[0] = *p++; \
  773. break; \
  774. case TOK_STR: \
  775. case TOK_LSTR: \
  776. case TOK_PPNUM: \
  777. cv.cstr = (CString *)p; \
  778. cv.cstr->data = (char *)p + sizeof(CString);\
  779. p += (sizeof(CString) + cv.cstr->size + 3) >> 2;\
  780. break; \
  781. case TOK_CDOUBLE: \
  782. case TOK_CLLONG: \
  783. case TOK_CULLONG: \
  784. cv.tab[0] = p[0]; \
  785. cv.tab[1] = p[1]; \
  786. p += 2; \
  787. break; \
  788. case TOK_CLDOUBLE: \
  789. LDOUBLE_GET(p, cv); \
  790. p += LDOUBLE_SIZE / 4; \
  791. break; \
  792. default: \
  793. break; \
  794. } \
  795. }
  796. /* defines handling */
  797. static inline void define_push(int v, int macro_type, int *str, Sym *first_arg)
  798. {
  799. Sym *s;
  800. s = sym_push2(&define_stack, v, macro_type, (long)str);
  801. s->next = first_arg;
  802. table_ident[v - TOK_IDENT]->sym_define = s;
  803. }
  804. /* undefined a define symbol. Its name is just set to zero */
  805. static void define_undef(Sym *s)
  806. {
  807. int v;
  808. v = s->v;
  809. if (v >= TOK_IDENT && v < tok_ident)
  810. table_ident[v - TOK_IDENT]->sym_define = NULL;
  811. s->v = 0;
  812. }
  813. static inline Sym *define_find(int v)
  814. {
  815. v -= TOK_IDENT;
  816. if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
  817. return NULL;
  818. return table_ident[v]->sym_define;
  819. }
  820. /* free define stack until top reaches 'b' */
  821. static void free_defines(Sym *b)
  822. {
  823. Sym *top, *top1;
  824. int v;
  825. top = define_stack;
  826. while (top != b) {
  827. top1 = top->prev;
  828. /* do not free args or predefined defines */
  829. if (top->c)
  830. tok_str_free((int *)top->c);
  831. v = top->v;
  832. if (v >= TOK_IDENT && v < tok_ident)
  833. table_ident[v - TOK_IDENT]->sym_define = NULL;
  834. sym_free(top);
  835. top = top1;
  836. }
  837. define_stack = b;
  838. }
  839. /* label lookup */
  840. static Sym *label_find(int v)
  841. {
  842. v -= TOK_IDENT;
  843. if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
  844. return NULL;
  845. return table_ident[v]->sym_label;
  846. }
  847. static Sym *label_push(Sym **ptop, int v, int flags)
  848. {
  849. Sym *s, **ps;
  850. s = sym_push2(ptop, v, 0, 0);
  851. s->r = flags;
  852. ps = &table_ident[v - TOK_IDENT]->sym_label;
  853. if (ptop == &global_label_stack) {
  854. /* modify the top most local identifier, so that
  855. sym_identifier will point to 's' when popped */
  856. while (*ps != NULL)
  857. ps = &(*ps)->prev_tok;
  858. }
  859. s->prev_tok = *ps;
  860. *ps = s;
  861. return s;
  862. }
  863. /* pop labels until element last is reached. Look if any labels are
  864. undefined. Define symbols if '&&label' was used. */
  865. static void label_pop(Sym **ptop, Sym *slast)
  866. {
  867. Sym *s, *s1;
  868. for(s = *ptop; s != slast; s = s1) {
  869. s1 = s->prev;
  870. if (s->r == LABEL_DECLARED) {
  871. warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
  872. } else if (s->r == LABEL_FORWARD) {
  873. error("label '%s' used but not defined",
  874. get_tok_str(s->v, NULL));
  875. } else {
  876. if (s->c) {
  877. /* define corresponding symbol. A size of
  878. 1 is put. */
  879. put_extern_sym(s, cur_text_section, (long)s->next, 1);
  880. }
  881. }
  882. /* remove label */
  883. table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
  884. sym_free(s);
  885. }
  886. *ptop = slast;
  887. }
  888. /* eval an expression for #if/#elif */
  889. static int expr_preprocess(void)
  890. {
  891. int c, t;
  892. TokenString str;
  893. tok_str_new(&str);
  894. while (tok != TOK_LINEFEED && tok != TOK_EOF) {
  895. next(); /* do macro subst */
  896. if (tok == TOK_DEFINED) {
  897. next_nomacro();
  898. t = tok;
  899. if (t == '(')
  900. next_nomacro();
  901. c = define_find(tok) != 0;
  902. if (t == '(')
  903. next_nomacro();
  904. tok = TOK_CINT;
  905. tokc.i = c;
  906. } else if (tok >= TOK_IDENT) {
  907. /* if undefined macro */
  908. tok = TOK_CINT;
  909. tokc.i = 0;
  910. }
  911. tok_str_add_tok(&str);
  912. }
  913. tok_str_add(&str, -1); /* simulate end of file */
  914. tok_str_add(&str, 0);
  915. /* now evaluate C constant expression */
  916. macro_ptr = str.str;
  917. next();
  918. c = expr_const();
  919. macro_ptr = NULL;
  920. tok_str_free(str.str);
  921. return c != 0;
  922. }
  923. #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
  924. static void tok_print(int *str)
  925. {
  926. int t;
  927. CValue cval;
  928. printf("<");
  929. while (1) {
  930. TOK_GET(t, str, cval);
  931. if (!t)
  932. break;
  933. printf("%s", get_tok_str(t, &cval));
  934. }
  935. printf(">\n");
  936. }
  937. #endif
  938. /* parse after #define */
  939. static void parse_define(void)
  940. {
  941. Sym *s, *first, **ps;
  942. int v, t, varg, is_vaargs, spc;
  943. TokenString str;
  944. v = tok;
  945. if (v < TOK_IDENT)
  946. error("invalid macro name '%s'", get_tok_str(tok, &tokc));
  947. /* XXX: should check if same macro (ANSI) */
  948. first = NULL;
  949. t = MACRO_OBJ;
  950. /* '(' must be just after macro definition for MACRO_FUNC */
  951. next_nomacro_spc();
  952. if (tok == '(') {
  953. next_nomacro();
  954. ps = &first;
  955. while (tok != ')') {
  956. varg = tok;
  957. next_nomacro();
  958. is_vaargs = 0;
  959. if (varg == TOK_DOTS) {
  960. varg = TOK___VA_ARGS__;
  961. is_vaargs = 1;
  962. } else if (tok == TOK_DOTS && gnu_ext) {
  963. is_vaargs = 1;
  964. next_nomacro();
  965. }
  966. if (varg < TOK_IDENT)
  967. error("badly punctuated parameter list");
  968. s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
  969. *ps = s;
  970. ps = &s->next;
  971. if (tok != ',')
  972. break;
  973. next_nomacro();
  974. }
  975. if (tok == ')')
  976. next_nomacro_spc();
  977. t = MACRO_FUNC;
  978. }
  979. tok_str_new(&str);
  980. spc = 2;
  981. /* EOF testing necessary for '-D' handling */
  982. while (tok != TOK_LINEFEED && tok != TOK_EOF) {
  983. /* remove spaces around ## and after '#' */
  984. if (TOK_TWOSHARPS == tok) {
  985. if (1 == spc)
  986. --str.len;
  987. spc = 2;
  988. } else if ('#' == tok) {
  989. spc = 2;
  990. } else if (check_space(tok, &spc)) {
  991. goto skip;
  992. }
  993. tok_str_add2(&str, tok, &tokc);
  994. skip:
  995. next_nomacro_spc();
  996. }
  997. if (spc == 1)
  998. --str.len; /* remove trailing space */
  999. tok_str_add(&str, 0);
  1000. #ifdef PP_DEBUG
  1001. printf("define %s %d: ", get_tok_str(v, NULL), t);
  1002. tok_print(str.str);
  1003. #endif
  1004. define_push(v, t, str.str, first);
  1005. }
  1006. static inline int hash_cached_include(int type, const char *filename)
  1007. {
  1008. const unsigned char *s;
  1009. unsigned int h;
  1010. h = TOK_HASH_INIT;
  1011. h = TOK_HASH_FUNC(h, type);
  1012. s = filename;
  1013. while (*s) {
  1014. h = TOK_HASH_FUNC(h, *s);
  1015. s++;
  1016. }
  1017. h &= (CACHED_INCLUDES_HASH_SIZE - 1);
  1018. return h;
  1019. }
  1020. /* XXX: use a token or a hash table to accelerate matching ? */
  1021. static CachedInclude *search_cached_include(TCCState *s1,
  1022. int type, const char *filename)
  1023. {
  1024. CachedInclude *e;
  1025. int i, h;
  1026. h = hash_cached_include(type, filename);
  1027. i = s1->cached_includes_hash[h];
  1028. for(;;) {
  1029. if (i == 0)
  1030. break;
  1031. e = s1->cached_includes[i - 1];
  1032. if (e->type == type && !PATHCMP(e->filename, filename))
  1033. return e;
  1034. i = e->hash_next;
  1035. }
  1036. return NULL;
  1037. }
  1038. static inline void add_cached_include(TCCState *s1, int type,
  1039. const char *filename, int ifndef_macro)
  1040. {
  1041. CachedInclude *e;
  1042. int h;
  1043. if (search_cached_include(s1, type, filename))
  1044. return;
  1045. #ifdef INC_DEBUG
  1046. printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
  1047. #endif
  1048. e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
  1049. if (!e)
  1050. return;
  1051. e->type = type;
  1052. strcpy(e->filename, filename);
  1053. e->ifndef_macro = ifndef_macro;
  1054. dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
  1055. /* add in hash table */
  1056. h = hash_cached_include(type, filename);
  1057. e->hash_next = s1->cached_includes_hash[h];
  1058. s1->cached_includes_hash[h] = s1->nb_cached_includes;
  1059. }
  1060. static void pragma_parse(TCCState *s1)
  1061. {
  1062. int val;
  1063. next();
  1064. if (tok == TOK_pack) {
  1065. /*
  1066. This may be:
  1067. #pragma pack(1) // set
  1068. #pragma pack() // reset to default
  1069. #pragma pack(push,1) // push & set
  1070. #pragma pack(pop) // restore previous
  1071. */
  1072. next();
  1073. skip('(');
  1074. if (tok == TOK_ASM_pop) {
  1075. next();
  1076. if (s1->pack_stack_ptr <= s1->pack_stack) {
  1077. stk_error:
  1078. error("out of pack stack");
  1079. }
  1080. s1->pack_stack_ptr--;
  1081. } else {
  1082. val = 0;
  1083. if (tok != ')') {
  1084. if (tok == TOK_ASM_push) {
  1085. next();
  1086. if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
  1087. goto stk_error;
  1088. s1->pack_stack_ptr++;
  1089. skip(',');
  1090. }
  1091. if (tok != TOK_CINT) {
  1092. pack_error:
  1093. error("invalid pack pragma");
  1094. }
  1095. val = tokc.i;
  1096. if (val < 1 || val > 16 || (val & (val - 1)) != 0)
  1097. goto pack_error;
  1098. next();
  1099. }
  1100. *s1->pack_stack_ptr = val;
  1101. skip(')');
  1102. }
  1103. }
  1104. }
  1105. /* is_bof is true if first non space token at beginning of file */
  1106. static void preprocess(int is_bof)
  1107. {
  1108. TCCState *s1 = tcc_state;
  1109. int i, c, n, saved_parse_flags;
  1110. char buf[1024], *q;
  1111. Sym *s;
  1112. saved_parse_flags = parse_flags;
  1113. parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
  1114. PARSE_FLAG_LINEFEED;
  1115. next_nomacro();
  1116. redo:
  1117. switch(tok) {
  1118. case TOK_DEFINE:
  1119. next_nomacro();
  1120. parse_define();
  1121. break;
  1122. case TOK_UNDEF:
  1123. next_nomacro();
  1124. s = define_find(tok);
  1125. /* undefine symbol by putting an invalid name */
  1126. if (s)
  1127. define_undef(s);
  1128. break;
  1129. case TOK_INCLUDE:
  1130. case TOK_INCLUDE_NEXT:
  1131. ch = file->buf_ptr[0];
  1132. /* XXX: incorrect if comments : use next_nomacro with a special mode */
  1133. skip_spaces();
  1134. if (ch == '<') {
  1135. c = '>';
  1136. goto read_name;
  1137. } else if (ch == '\"') {
  1138. c = ch;
  1139. read_name:
  1140. inp();
  1141. q = buf;
  1142. while (ch != c && ch != '\n' && ch != CH_EOF) {
  1143. if ((q - buf) < sizeof(buf) - 1)
  1144. *q++ = ch;
  1145. if (ch == '\\') {
  1146. if (handle_stray_noerror() == 0)
  1147. --q;
  1148. } else
  1149. inp();
  1150. }
  1151. *q = '\0';
  1152. minp();
  1153. #if 0
  1154. /* eat all spaces and comments after include */
  1155. /* XXX: slightly incorrect */
  1156. while (ch1 != '\n' && ch1 != CH_EOF)
  1157. inp();
  1158. #endif
  1159. } else {
  1160. /* computed #include : either we have only strings or
  1161. we have anything enclosed in '<>' */
  1162. next();
  1163. buf[0] = '\0';
  1164. if (tok == TOK_STR) {
  1165. while (tok != TOK_LINEFEED) {
  1166. if (tok != TOK_STR) {
  1167. include_syntax:
  1168. error("'#include' expects \"FILENAME\" or <FILENAME>");
  1169. }
  1170. pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
  1171. next();
  1172. }
  1173. c = '\"';
  1174. } else {
  1175. int len;
  1176. while (tok != TOK_LINEFEED) {
  1177. pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
  1178. next();
  1179. }
  1180. len = strlen(buf);
  1181. /* check syntax and remove '<>' */
  1182. if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
  1183. goto include_syntax;
  1184. memmove(buf, buf + 1, len - 2);
  1185. buf[len - 2] = '\0';
  1186. c = '>';
  1187. }
  1188. }
  1189. if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
  1190. error("#include recursion too deep");
  1191. n = s1->nb_include_paths + s1->nb_sysinclude_paths;
  1192. for (i = -2; i < n; ++i) {
  1193. char buf1[sizeof file->filename];
  1194. BufferedFile *f;
  1195. CachedInclude *e;
  1196. const char *path;
  1197. int size;
  1198. if (i == -2) {
  1199. /* check absolute include path */
  1200. if (!IS_ABSPATH(buf))
  1201. continue;
  1202. buf1[0] = 0;
  1203. } else if (i == -1) {
  1204. /* search in current dir if "header.h" */
  1205. if (c != '\"')
  1206. continue;
  1207. size = tcc_basename(file->filename) - file->filename;
  1208. memcpy(buf1, file->filename, size);
  1209. buf1[size] = '\0';
  1210. } else {
  1211. /* search in all the include paths */
  1212. if (i < s1->nb_include_paths)
  1213. path = s1->include_paths[i];
  1214. else
  1215. path = s1->sysinclude_paths[i - s1->nb_include_paths];
  1216. pstrcpy(buf1, sizeof(buf1), path);
  1217. pstrcat(buf1, sizeof(buf1), "/");
  1218. }
  1219. pstrcat(buf1, sizeof(buf1), buf);
  1220. e = search_cached_include(s1, c, buf1);
  1221. if (e && define_find(e->ifndef_macro)) {
  1222. /* no need to parse the include because the 'ifndef macro'
  1223. is defined */
  1224. #ifdef INC_DEBUG
  1225. printf("%s: skipping %s\n", file->filename, buf);
  1226. #endif
  1227. f = NULL;
  1228. } else {
  1229. f = tcc_open(s1, buf1);
  1230. if (!f)
  1231. continue;
  1232. }
  1233. if (tok == TOK_INCLUDE_NEXT) {
  1234. tok = TOK_INCLUDE;
  1235. if (f)
  1236. tcc_close(f);
  1237. continue;
  1238. }
  1239. if (!f)
  1240. goto include_done;
  1241. #ifdef INC_DEBUG
  1242. printf("%s: including %s\n", file->filename, buf1);
  1243. #endif
  1244. /* XXX: fix current line init */
  1245. /* push current file in stack */
  1246. *s1->include_stack_ptr++ = file;
  1247. f->inc_type = c;
  1248. pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf1);
  1249. file = f;
  1250. /* add include file debug info */
  1251. if (tcc_state->do_debug) {
  1252. put_stabs(file->filename, N_BINCL, 0, 0, 0);
  1253. }
  1254. tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
  1255. ch = file->buf_ptr[0];
  1256. goto the_end;
  1257. }
  1258. error("include file '%s' not found", buf);
  1259. include_done:
  1260. break;
  1261. case TOK_IFNDEF:
  1262. c = 1;
  1263. goto do_ifdef;
  1264. case TOK_IF:
  1265. c = expr_preprocess();
  1266. goto do_if;
  1267. case TOK_IFDEF:
  1268. c = 0;
  1269. do_ifdef:
  1270. next_nomacro();
  1271. if (tok < TOK_IDENT)
  1272. error("invalid argument for '#if%sdef'", c ? "n" : "");
  1273. if (is_bof) {
  1274. if (c) {
  1275. #ifdef INC_DEBUG
  1276. printf("#ifndef %s\n", get_tok_str(tok, NULL));
  1277. #endif
  1278. file->ifndef_macro = tok;
  1279. }
  1280. }
  1281. c = (define_find(tok) != 0) ^ c;
  1282. do_if:
  1283. if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
  1284. error("memory full");
  1285. *s1->ifdef_stack_ptr++ = c;
  1286. goto test_skip;
  1287. case TOK_ELSE:
  1288. if (s1->ifdef_stack_ptr == s1->ifdef_stack)
  1289. error("#else without matching #if");
  1290. if (s1->ifdef_stack_ptr[-1] & 2)
  1291. error("#else after #else");
  1292. c = (s1->ifdef_stack_ptr[-1] ^= 3);
  1293. goto test_skip;
  1294. case TOK_ELIF:
  1295. if (s1->ifdef_stack_ptr == s1->ifdef_stack)
  1296. error("#elif without matching #if");
  1297. c = s1->ifdef_stack_ptr[-1];
  1298. if (c > 1)
  1299. error("#elif after #else");
  1300. /* last #if/#elif expression was true: we skip */
  1301. if (c == 1)
  1302. goto skip;
  1303. c = expr_preprocess();
  1304. s1->ifdef_stack_ptr[-1] = c;
  1305. test_skip:
  1306. if (!(c & 1)) {
  1307. skip:
  1308. preprocess_skip();
  1309. is_bof = 0;
  1310. goto redo;
  1311. }
  1312. break;
  1313. case TOK_ENDIF:
  1314. if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
  1315. error("#endif without matching #if");
  1316. s1->ifdef_stack_ptr--;
  1317. /* '#ifndef macro' was at the start of file. Now we check if
  1318. an '#endif' is exactly at the end of file */
  1319. if (file->ifndef_macro &&
  1320. s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
  1321. file->ifndef_macro_saved = file->ifndef_macro;
  1322. /* need to set to zero to avoid false matches if another
  1323. #ifndef at middle of file */
  1324. file->ifndef_macro = 0;
  1325. while (tok != TOK_LINEFEED)
  1326. next_nomacro();
  1327. tok_flags |= TOK_FLAG_ENDIF;
  1328. goto the_end;
  1329. }
  1330. break;
  1331. case TOK_LINE:
  1332. next();
  1333. if (tok != TOK_CINT)
  1334. error("#line");
  1335. file->line_num = tokc.i - 1; /* the line number will be incremented after */
  1336. next();
  1337. if (tok != TOK_LINEFEED) {
  1338. if (tok != TOK_STR)
  1339. error("#line");
  1340. pstrcpy(file->filename, sizeof(file->filename),
  1341. (char *)tokc.cstr->data);
  1342. }
  1343. break;
  1344. case TOK_ERROR:
  1345. case TOK_WARNING:
  1346. c = tok;
  1347. ch = file->buf_ptr[0];
  1348. skip_spaces();
  1349. q = buf;
  1350. while (ch != '\n' && ch != CH_EOF) {
  1351. if ((q - buf) < sizeof(buf) - 1)
  1352. *q++ = ch;
  1353. if (ch == '\\') {
  1354. if (handle_stray_noerror() == 0)
  1355. --q;
  1356. } else
  1357. inp();
  1358. }
  1359. *q = '\0';
  1360. if (c == TOK_ERROR)
  1361. error("#error %s", buf);
  1362. else
  1363. warning("#warning %s", buf);
  1364. break;
  1365. case TOK_PRAGMA:
  1366. pragma_parse(s1);
  1367. break;
  1368. default:
  1369. if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_CINT) {
  1370. /* '!' is ignored to allow C scripts. numbers are ignored
  1371. to emulate cpp behaviour */
  1372. } else {
  1373. if (!(saved_parse_flags & PARSE_FLAG_ASM_COMMENTS))
  1374. warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
  1375. }
  1376. break;
  1377. }
  1378. /* ignore other preprocess commands or #! for C scripts */
  1379. while (tok != TOK_LINEFEED)
  1380. next_nomacro();
  1381. the_end:
  1382. parse_flags = saved_parse_flags;
  1383. }
  1384. /* evaluate escape codes in a string. */
  1385. static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
  1386. {
  1387. int c, n;
  1388. const uint8_t *p;
  1389. p = buf;
  1390. for(;;) {
  1391. c = *p;
  1392. if (c == '\0')
  1393. break;
  1394. if (c == '\\') {
  1395. p++;
  1396. /* escape */
  1397. c = *p;
  1398. switch(c) {
  1399. case '0': case '1': case '2': case '3':
  1400. case '4': case '5': case '6': case '7':
  1401. /* at most three octal digits */
  1402. n = c - '0';
  1403. p++;
  1404. c = *p;
  1405. if (isoct(c)) {
  1406. n = n * 8 + c - '0';
  1407. p++;
  1408. c = *p;
  1409. if (isoct(c)) {
  1410. n = n * 8 + c - '0';
  1411. p++;
  1412. }
  1413. }
  1414. c = n;
  1415. goto add_char_nonext;
  1416. case 'x':
  1417. case 'u':
  1418. case 'U':
  1419. p++;
  1420. n = 0;
  1421. for(;;) {
  1422. c = *p;
  1423. if (c >= 'a' && c <= 'f')
  1424. c = c - 'a' + 10;
  1425. else if (c >= 'A' && c <= 'F')
  1426. c = c - 'A' + 10;
  1427. else if (isnum(c))
  1428. c = c - '0';
  1429. else
  1430. break;
  1431. n = n * 16 + c;
  1432. p++;
  1433. }
  1434. c = n;
  1435. goto add_char_nonext;
  1436. case 'a':
  1437. c = '\a';
  1438. break;
  1439. case 'b':
  1440. c = '\b';
  1441. break;
  1442. case 'f':
  1443. c = '\f';
  1444. break;
  1445. case 'n':
  1446. c = '\n';
  1447. break;
  1448. case 'r':
  1449. c = '\r';
  1450. break;
  1451. case 't':
  1452. c = '\t';
  1453. break;
  1454. case 'v':
  1455. c = '\v';
  1456. break;
  1457. case 'e':
  1458. if (!gnu_ext)
  1459. goto invalid_escape;
  1460. c = 27;
  1461. break;
  1462. case '\'':
  1463. case '\"':
  1464. case '\\':
  1465. case '?':
  1466. break;
  1467. default:
  1468. invalid_escape:
  1469. if (c >= '!' && c <= '~')
  1470. warning("unknown escape sequence: \'\\%c\'", c);
  1471. else
  1472. warning("unknown escape sequence: \'\\x%x\'", c);
  1473. break;
  1474. }
  1475. }
  1476. p++;
  1477. add_char_nonext:
  1478. if (!is_long)
  1479. cstr_ccat(outstr, c);
  1480. else
  1481. cstr_wccat(outstr, c);
  1482. }
  1483. /* add a trailing '\0' */
  1484. if (!is_long)
  1485. cstr_ccat(outstr, '\0');
  1486. else
  1487. cstr_wccat(outstr, '\0');
  1488. }
  1489. /* we use 64 bit numbers */
  1490. #define BN_SIZE 2
  1491. /* bn = (bn << shift) | or_val */
  1492. void bn_lshift(unsigned int *bn, int shift, int or_val)
  1493. {
  1494. int i;
  1495. unsigned int v;
  1496. for(i=0;i<BN_SIZE;i++) {
  1497. v = bn[i];
  1498. bn[i] = (v << shift) | or_val;
  1499. or_val = v >> (32 - shift);
  1500. }
  1501. }
  1502. void bn_zero(unsigned int *bn)
  1503. {
  1504. int i;
  1505. for(i=0;i<BN_SIZE;i++) {
  1506. bn[i] = 0;
  1507. }
  1508. }
  1509. /* parse number in null terminated string 'p' and return it in the
  1510. current token */
  1511. void parse_number(const char *p)
  1512. {
  1513. int b, t, shift, frac_bits, s, exp_val, ch;
  1514. char *q;
  1515. unsigned int bn[BN_SIZE];
  1516. double d;
  1517. /* number */
  1518. q = token_buf;
  1519. ch = *p++;
  1520. t = ch;
  1521. ch = *p++;
  1522. *q++ = t;
  1523. b = 10;
  1524. if (t == '.') {
  1525. goto float_frac_parse;
  1526. } else if (t == '0') {
  1527. if (ch == 'x' || ch == 'X') {
  1528. q--;
  1529. ch = *p++;
  1530. b = 16;
  1531. } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
  1532. q--;
  1533. ch = *p++;
  1534. b = 2;
  1535. }
  1536. }
  1537. /* parse all digits. cannot check octal numbers at this stage
  1538. because of floating point constants */
  1539. while (1) {
  1540. if (ch >= 'a' && ch <= 'f')
  1541. t = ch - 'a' + 10;
  1542. else if (ch >= 'A' && ch <= 'F')
  1543. t = ch - 'A' + 10;
  1544. else if (isnum(ch))
  1545. t = ch - '0';
  1546. else
  1547. break;
  1548. if (t >= b)
  1549. break;
  1550. if (q >= token_buf + STRING_MAX_SIZE) {
  1551. num_too_long:
  1552. error("number too long");
  1553. }
  1554. *q++ = ch;
  1555. ch = *p++;
  1556. }
  1557. if (ch == '.' ||
  1558. ((ch == 'e' || ch == 'E') && b == 10) ||
  1559. ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
  1560. if (b != 10) {
  1561. /* NOTE: strtox should support that for hexa numbers, but
  1562. non ISOC99 libcs do not support it, so we prefer to do
  1563. it by hand */
  1564. /* hexadecimal or binary floats */
  1565. /* XXX: handle overflows */
  1566. *q = '\0';
  1567. if (b == 16)
  1568. shift = 4;
  1569. else
  1570. shift = 2;
  1571. bn_zero(bn);
  1572. q = token_buf;
  1573. while (1) {
  1574. t = *q++;
  1575. if (t == '\0') {
  1576. break;
  1577. } else if (t >= 'a') {
  1578. t = t - 'a' + 10;
  1579. } else if (t >= 'A') {
  1580. t = t - 'A' + 10;
  1581. } else {
  1582. t = t - '0';
  1583. }
  1584. bn_lshift(bn, shift, t);
  1585. }
  1586. frac_bits = 0;
  1587. if (ch == '.') {
  1588. ch = *p++;
  1589. while (1) {
  1590. t = ch;
  1591. if (t >= 'a' && t <= 'f') {
  1592. t = t - 'a' + 10;
  1593. } else if (t >= 'A' && t <= 'F') {
  1594. t = t - 'A' + 10;
  1595. } else if (t >= '0' && t <= '9') {
  1596. t = t - '0';
  1597. } else {
  1598. break;
  1599. }
  1600. if (t >= b)
  1601. error("invalid digit");
  1602. bn_lshift(bn, shift, t);
  1603. frac_bits += shift;
  1604. ch = *p++;
  1605. }
  1606. }
  1607. if (ch != 'p' && ch != 'P')
  1608. expect("exponent");
  1609. ch = *p++;
  1610. s = 1;
  1611. exp_val = 0;
  1612. if (ch == '+') {
  1613. ch = *p++;
  1614. } else if (ch == '-') {
  1615. s = -1;
  1616. ch = *p++;
  1617. }
  1618. if (ch < '0' || ch > '9')
  1619. expect("exponent digits");
  1620. while (ch >= '0' && ch <= '9') {
  1621. exp_val = exp_val * 10 + ch - '0';
  1622. ch = *p++;
  1623. }
  1624. exp_val = exp_val * s;
  1625. /* now we can generate the number */
  1626. /* XXX: should patch directly float number */
  1627. d = (double)bn[1] * 4294967296.0 + (double)bn[0];
  1628. d = ldexp(d, exp_val - frac_bits);
  1629. t = toup(ch);
  1630. if (t == 'F') {
  1631. ch = *p++;
  1632. tok = TOK_CFLOAT;
  1633. /* float : should handle overflow */
  1634. tokc.f = (float)d;
  1635. } else if (t == 'L') {
  1636. ch = *p++;
  1637. tok = TOK_CLDOUBLE;
  1638. /* XXX: not large enough */
  1639. tokc.ld = (long double)d;
  1640. } else {
  1641. tok = TOK_CDOUBLE;
  1642. tokc.d = d;
  1643. }
  1644. } else {
  1645. /* decimal floats */
  1646. if (ch == '.') {
  1647. if (q >= token_buf + STRING_MAX_SIZE)
  1648. goto num_too_long;
  1649. *q++ = ch;
  1650. ch = *p++;
  1651. float_frac_parse:
  1652. while (ch >= '0' && ch <= '9') {
  1653. if (q >= token_buf + STRING_MAX_SIZE)
  1654. goto num_too_long;
  1655. *q++ = ch;
  1656. ch = *p++;
  1657. }
  1658. }
  1659. if (ch == 'e' || ch == 'E') {
  1660. if (q >= token_buf + STRING_MAX_SIZE)
  1661. goto num_too_long;
  1662. *q++ = ch;
  1663. ch = *p++;
  1664. if (ch == '-' || ch == '+') {
  1665. if (q >= token_buf + STRING_MAX_SIZE)
  1666. goto num_too_long;
  1667. *q++ = ch;
  1668. ch = *p++;
  1669. }
  1670. if (ch < '0' || ch > '9')
  1671. expect("exponent digits");
  1672. while (ch >= '0' && ch <= '9') {
  1673. if (q >= token_buf + STRING_MAX_SIZE)
  1674. goto num_too_long;
  1675. *q++ = ch;
  1676. ch = *p++;
  1677. }
  1678. }
  1679. *q = '\0';
  1680. t = toup(ch);
  1681. errno = 0;
  1682. if (t == 'F') {
  1683. ch = *p++;
  1684. tok = TOK_CFLOAT;
  1685. tokc.f = strtof(token_buf, NULL);
  1686. } else if (t == 'L') {
  1687. ch = *p++;
  1688. tok = TOK_CLDOUBLE;
  1689. tokc.ld = strtold(token_buf, NULL);
  1690. } else {
  1691. tok = TOK_CDOUBLE;
  1692. tokc.d = strtod(token_buf, NULL);
  1693. }
  1694. }
  1695. } else {
  1696. unsigned long long n, n1;
  1697. int lcount, ucount;
  1698. /* integer number */
  1699. *q = '\0';
  1700. q = token_buf;
  1701. if (b == 10 && *q == '0') {
  1702. b = 8;
  1703. q++;
  1704. }
  1705. n = 0;
  1706. while(1) {
  1707. t = *q++;
  1708. /* no need for checks except for base 10 / 8 errors */
  1709. if (t == '\0') {
  1710. break;
  1711. } else if (t >= 'a') {
  1712. t = t - 'a' + 10;
  1713. } else if (t >= 'A') {
  1714. t = t - 'A' + 10;
  1715. } else {
  1716. t = t - '0';
  1717. if (t >= b)
  1718. error("invalid digit");
  1719. }
  1720. n1 = n;
  1721. n = n * b + t;
  1722. /* detect overflow */
  1723. /* XXX: this test is not reliable */
  1724. if (n < n1)
  1725. error("integer constant overflow");
  1726. }
  1727. /* XXX: not exactly ANSI compliant */
  1728. if ((n & 0xffffffff00000000LL) != 0) {
  1729. if ((n >> 63) != 0)
  1730. tok = TOK_CULLONG;
  1731. else
  1732. tok = TOK_CLLONG;
  1733. } else if (n > 0x7fffffff) {
  1734. tok = TOK_CUINT;
  1735. } else {
  1736. tok = TOK_CINT;
  1737. }
  1738. lcount = 0;
  1739. ucount = 0;
  1740. for(;;) {
  1741. t = toup(ch);
  1742. if (t == 'L') {
  1743. if (lcount >= 2)
  1744. error("three 'l's in integer constant");
  1745. lcount++;
  1746. if (lcount == 2) {
  1747. if (tok == TOK_CINT)
  1748. tok = TOK_CLLONG;
  1749. else if (tok == TOK_CUINT)
  1750. tok = TOK_CULLONG;
  1751. }
  1752. ch = *p++;
  1753. } else if (t == 'U') {
  1754. if (ucount >= 1)
  1755. error("two 'u's in integer constant");
  1756. ucount++;
  1757. if (tok == TOK_CINT)
  1758. tok = TOK_CUINT;
  1759. else if (tok == TOK_CLLONG)
  1760. tok = TOK_CULLONG;
  1761. ch = *p++;
  1762. } else {
  1763. break;
  1764. }
  1765. }
  1766. if (tok == TOK_CINT || tok == TOK_CUINT)
  1767. tokc.ui = n;
  1768. else
  1769. tokc.ull = n;
  1770. }
  1771. if (ch)
  1772. error("invalid number\n");
  1773. }
  1774. #define PARSE2(c1, tok1, c2, tok2) \
  1775. case c1: \
  1776. PEEKC(c, p); \
  1777. if (c == c2) { \
  1778. p++; \
  1779. tok = tok2; \
  1780. } else { \
  1781. tok = tok1; \
  1782. } \
  1783. break;
  1784. /* return next token without macro substitution */
  1785. static inline void next_nomacro1(void)
  1786. {
  1787. int t, c, is_long;
  1788. TokenSym *ts;
  1789. uint8_t *p, *p1;
  1790. unsigned int h;
  1791. p = file->buf_ptr;
  1792. redo_no_start:
  1793. c = *p;
  1794. switch(c) {
  1795. case ' ':
  1796. case '\t':
  1797. tok = c;
  1798. p++;
  1799. goto keep_tok_flags;
  1800. case '\f':
  1801. case '\v':
  1802. case '\r':
  1803. p++;
  1804. goto redo_no_start;
  1805. case '\\':
  1806. /* first look if it is in fact an end of buffer */
  1807. if (p >= file->buf_end) {
  1808. file->buf_ptr = p;
  1809. handle_eob();
  1810. p = file->buf_ptr;
  1811. if (p >= file->buf_end)
  1812. goto parse_eof;
  1813. else
  1814. goto redo_no_start;
  1815. } else {
  1816. file->buf_ptr = p;
  1817. ch = *p;
  1818. handle_stray();
  1819. p = file->buf_ptr;
  1820. goto redo_no_start;
  1821. }
  1822. parse_eof:
  1823. {
  1824. TCCState *s1 = tcc_state;
  1825. if ((parse_flags & PARSE_FLAG_LINEFEED)
  1826. && !(tok_flags & TOK_FLAG_EOF)) {
  1827. tok_flags |= TOK_FLAG_EOF;
  1828. tok = TOK_LINEFEED;
  1829. goto keep_tok_flags;
  1830. } else if (s1->include_stack_ptr == s1->include_stack ||
  1831. !(parse_flags & PARSE_FLAG_PREPROCESS)) {
  1832. /* no include left : end of file. */
  1833. tok = TOK_EOF;
  1834. } else {
  1835. tok_flags &= ~TOK_FLAG_EOF;
  1836. /* pop include file */
  1837. /* test if previous '#endif' was after a #ifdef at
  1838. start of file */
  1839. if (tok_flags & TOK_FLAG_ENDIF) {
  1840. #ifdef INC_DEBUG
  1841. printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
  1842. #endif
  1843. add_cached_include(s1, file->inc_type, file->inc_filename,
  1844. file->ifndef_macro_saved);
  1845. }
  1846. /* add end of include file debug info */
  1847. if (tcc_state->do_debug) {
  1848. put_stabd(N_EINCL, 0, 0);
  1849. }
  1850. /* pop include stack */
  1851. tcc_close(file);
  1852. s1->include_stack_ptr--;
  1853. file = *s1->include_stack_ptr;
  1854. p = file->buf_ptr;
  1855. goto redo_no_start;
  1856. }
  1857. }
  1858. break;
  1859. case '\n':
  1860. file->line_num++;
  1861. tok_flags |= TOK_FLAG_BOL;
  1862. p++;
  1863. if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
  1864. goto redo_no_start;
  1865. tok = TOK_LINEFEED;
  1866. goto keep_tok_flags;
  1867. case '#':
  1868. /* XXX: simplify */
  1869. PEEKC(c, p);
  1870. if ((tok_flags & TOK_FLAG_BOL) &&
  1871. (parse_flags & PARSE_FLAG_PREPROCESS)) {
  1872. file->buf_ptr = p;
  1873. preprocess(tok_flags & TOK_FLAG_BOF);
  1874. p = file->buf_ptr;
  1875. goto redo_no_start;
  1876. } else {
  1877. if (c == '#') {
  1878. p++;
  1879. tok = TOK_TWOSHARPS;
  1880. } else {
  1881. if (parse_flags & PARSE_FLAG_ASM_COMMENTS) {
  1882. p = parse_line_comment(p - 1);
  1883. goto redo_no_start;
  1884. } else {
  1885. tok = '#';
  1886. }
  1887. }
  1888. }
  1889. break;
  1890. case 'a': case 'b': case 'c': case 'd':
  1891. case 'e': case 'f': case 'g': case 'h':
  1892. case 'i': case 'j': case 'k': case 'l':
  1893. case 'm': case 'n': case 'o': case 'p':
  1894. case 'q': case 'r': case 's': case 't':
  1895. case 'u': case 'v': case 'w': case 'x':
  1896. case 'y': case 'z':
  1897. case 'A': case 'B': case 'C': case 'D':
  1898. case 'E': case 'F': case 'G': case 'H':
  1899. case 'I': case 'J': case 'K':
  1900. case 'M': case 'N': case 'O': case 'P':
  1901. case 'Q': case 'R': case 'S': case 'T':
  1902. case 'U': case 'V': case 'W': case 'X':
  1903. case 'Y': case 'Z':
  1904. case '_':
  1905. parse_ident_fast:
  1906. p1 = p;
  1907. h = TOK_HASH_INIT;
  1908. h = TOK_HASH_FUNC(h, c);
  1909. p++;
  1910. for(;;) {
  1911. c = *p;
  1912. if (!isidnum_table[c-CH_EOF])
  1913. break;
  1914. h = TOK_HASH_FUNC(h, c);
  1915. p++;
  1916. }
  1917. if (c != '\\') {
  1918. TokenSym **pts;
  1919. int len;
  1920. /* fast case : no stray found, so we have the full token
  1921. and we have already hashed it */
  1922. len = p - p1;
  1923. h &= (TOK_HASH_SIZE - 1);
  1924. pts = &hash_ident[h];
  1925. for(;;) {
  1926. ts = *pts;
  1927. if (!ts)
  1928. break;
  1929. if (ts->len == len && !memcmp(ts->str, p1, len))
  1930. goto token_found;
  1931. pts = &(ts->hash_next);
  1932. }
  1933. ts = tok_alloc_new(pts, p1, len);
  1934. token_found: ;
  1935. } else {
  1936. /* slower case */
  1937. cstr_reset(&tokcstr);
  1938. while (p1 < p) {
  1939. cstr_ccat(&tokcstr, *p1);
  1940. p1++;
  1941. }
  1942. p--;
  1943. PEEKC(c, p);
  1944. parse_ident_slow:
  1945. while (isidnum_table[c-CH_EOF]) {
  1946. cstr_ccat(&tokcstr, c);
  1947. PEEKC(c, p);
  1948. }
  1949. ts = tok_alloc(tokcstr.data, tokcstr.size);
  1950. }
  1951. tok = ts->tok;
  1952. break;
  1953. case 'L':
  1954. t = p[1];
  1955. if (t != '\\' && t != '\'' && t != '\"') {
  1956. /* fast case */
  1957. goto parse_ident_fast;
  1958. } else {
  1959. PEEKC(c, p);
  1960. if (c == '\'' || c == '\"') {
  1961. is_long = 1;
  1962. goto str_const;
  1963. } else {
  1964. cstr_reset(&tokcstr);
  1965. cstr_ccat(&tokcstr, 'L');
  1966. goto parse_ident_slow;
  1967. }
  1968. }
  1969. break;
  1970. case '0': case '1': case '2': case '3':
  1971. case '4': case '5': case '6': case '7':
  1972. case '8': case '9':
  1973. cstr_reset(&tokcstr);
  1974. /* after the first digit, accept digits, alpha, '.' or sign if
  1975. prefixed by 'eEpP' */
  1976. parse_num:
  1977. for(;;) {
  1978. t = c;
  1979. cstr_ccat(&tokcstr, c);
  1980. PEEKC(c, p);
  1981. if (!(isnum(c) || isid(c) || c == '.' ||
  1982. ((c == '+' || c == '-') &&
  1983. (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
  1984. break;
  1985. }
  1986. /* We add a trailing '\0' to ease parsing */
  1987. cstr_ccat(&tokcstr, '\0');
  1988. tokc.cstr = &tokcstr;
  1989. tok = TOK_PPNUM;
  1990. break;
  1991. case '.':
  1992. /* special dot handling because it can also start a number */
  1993. PEEKC(c, p);
  1994. if (isnum(c)) {
  1995. cstr_reset(&tokcstr);
  1996. cstr_ccat(&tokcstr, '.');
  1997. goto parse_num;
  1998. } else if (c == '.') {
  1999. PEEKC(c, p);
  2000. if (c != '.')
  2001. expect("'.'");
  2002. PEEKC(c, p);
  2003. tok = TOK_DOTS;
  2004. } else {
  2005. tok = '.';
  2006. }
  2007. break;
  2008. case '\'':
  2009. case '\"':
  2010. is_long = 0;
  2011. str_const:
  2012. {
  2013. CString str;
  2014. int sep;
  2015. sep = c;
  2016. /* parse the string */
  2017. cstr_new(&str);
  2018. p = parse_pp_string(p, sep, &str);
  2019. cstr_ccat(&str, '\0');
  2020. /* eval the escape (should be done as TOK_PPNUM) */
  2021. cstr_reset(&tokcstr);
  2022. parse_escape_string(&tokcstr, str.data, is_long);
  2023. cstr_free(&str);
  2024. if (sep == '\'') {
  2025. int char_size;
  2026. /* XXX: make it portable */
  2027. if (!is_long)
  2028. char_size = 1;
  2029. else
  2030. char_size = sizeof(nwchar_t);
  2031. if (tokcstr.size <= char_size)
  2032. error("empty character constant");
  2033. if (tokcstr.size > 2 * char_size)
  2034. warning("multi-character character constant");
  2035. if (!is_long) {
  2036. tokc.i = *(int8_t *)tokcstr.data;
  2037. tok = TOK_CCHAR;
  2038. } else {
  2039. tokc.i = *(nwchar_t *)tokcstr.data;
  2040. tok = TOK_LCHAR;
  2041. }
  2042. } else {
  2043. tokc.cstr = &tokcstr;
  2044. if (!is_long)
  2045. tok = TOK_STR;
  2046. else
  2047. tok = TOK_LSTR;
  2048. }
  2049. }
  2050. break;
  2051. case '<':
  2052. PEEKC(c, p);
  2053. if (c == '=') {
  2054. p++;
  2055. tok = TOK_LE;
  2056. } else if (c == '<') {
  2057. PEEKC(c, p);
  2058. if (c == '=') {
  2059. p++;
  2060. tok = TOK_A_SHL;
  2061. } else {
  2062. tok = TOK_SHL;
  2063. }
  2064. } else {
  2065. tok = TOK_LT;
  2066. }
  2067. break;
  2068. case '>':
  2069. PEEKC(c, p);
  2070. if (c == '=') {
  2071. p++;
  2072. tok = TOK_GE;
  2073. } else if (c == '>') {
  2074. PEEKC(c, p);
  2075. if (c == '=') {
  2076. p++;
  2077. tok = TOK_A_SAR;
  2078. } else {
  2079. tok = TOK_SAR;
  2080. }
  2081. } else {
  2082. tok = TOK_GT;
  2083. }
  2084. break;
  2085. case '&':
  2086. PEEKC(c, p);
  2087. if (c == '&') {
  2088. p++;
  2089. tok = TOK_LAND;
  2090. } else if (c == '=') {
  2091. p++;
  2092. tok = TOK_A_AND;
  2093. } else {
  2094. tok = '&';
  2095. }
  2096. break;
  2097. case '|':
  2098. PEEKC(c, p);
  2099. if (c == '|') {
  2100. p++;
  2101. tok = TOK_LOR;
  2102. } else if (c == '=') {
  2103. p++;
  2104. tok = TOK_A_OR;
  2105. } else {
  2106. tok = '|';
  2107. }
  2108. break;
  2109. case '+':
  2110. PEEKC(c, p);
  2111. if (c == '+') {
  2112. p++;
  2113. tok = TOK_INC;
  2114. } else if (c == '=') {
  2115. p++;
  2116. tok = TOK_A_ADD;
  2117. } else {
  2118. tok = '+';
  2119. }
  2120. break;
  2121. case '-':
  2122. PEEKC(c, p);
  2123. if (c == '-') {
  2124. p++;
  2125. tok = TOK_DEC;
  2126. } else if (c == '=') {
  2127. p++;
  2128. tok = TOK_A_SUB;
  2129. } else if (c == '>') {
  2130. p++;
  2131. tok = TOK_ARROW;
  2132. } else {
  2133. tok = '-';
  2134. }
  2135. break;
  2136. PARSE2('!', '!', '=', TOK_NE)
  2137. PARSE2('=', '=', '=', TOK_EQ)
  2138. PARSE2('*', '*', '=', TOK_A_MUL)
  2139. PARSE2('%', '%', '=', TOK_A_MOD)
  2140. PARSE2('^', '^', '=', TOK_A_XOR)
  2141. /* comments or operator */
  2142. case '/':
  2143. PEEKC(c, p);
  2144. if (c == '*') {
  2145. p = parse_comment(p);
  2146. goto redo_no_start;
  2147. } else if (c == '/') {
  2148. p = parse_line_comment(p);
  2149. goto redo_no_start;
  2150. } else if (c == '=') {
  2151. p++;
  2152. tok = TOK_A_DIV;
  2153. } else {
  2154. tok = '/';
  2155. }
  2156. break;
  2157. /* simple tokens */
  2158. case '(':
  2159. case ')':
  2160. case '[':
  2161. case ']':
  2162. case '{':
  2163. case '}':
  2164. case ',':
  2165. case ';':
  2166. case ':':
  2167. case '?':
  2168. case '~':
  2169. case '$': /* only used in assembler */
  2170. case '@': /* dito */
  2171. tok = c;
  2172. p++;
  2173. break;
  2174. default:
  2175. error("unrecognized character \\x%02x", c);
  2176. break;
  2177. }
  2178. tok_flags = 0;
  2179. keep_tok_flags:
  2180. file->buf_ptr = p;
  2181. #if defined(PARSE_DEBUG)
  2182. printf("token = %s\n", get_tok_str(tok, &tokc));
  2183. #endif
  2184. }
  2185. /* return next token without macro substitution. Can read input from
  2186. macro_ptr buffer */
  2187. static void next_nomacro_spc(void)
  2188. {
  2189. if (macro_ptr) {
  2190. redo:
  2191. tok = *macro_ptr;
  2192. if (tok) {
  2193. TOK_GET(tok, macro_ptr, tokc);
  2194. if (tok == TOK_LINENUM) {
  2195. file->line_num = tokc.i;
  2196. goto redo;
  2197. }
  2198. }
  2199. } else {
  2200. next_nomacro1();
  2201. }
  2202. }
  2203. static void next_nomacro(void)
  2204. {
  2205. do {
  2206. next_nomacro_spc();
  2207. } while (is_space(tok));
  2208. }
  2209. /* substitute args in macro_str and return allocated string */
  2210. static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args)
  2211. {
  2212. int *st, last_tok, t, spc;
  2213. Sym *s;
  2214. CValue cval;
  2215. TokenString str;
  2216. CString cstr;
  2217. tok_str_new(&str);
  2218. last_tok = 0;
  2219. while(1) {
  2220. TOK_GET(t, macro_str, cval);
  2221. if (!t)
  2222. break;
  2223. if (t == '#') {
  2224. /* stringize */
  2225. TOK_GET(t, macro_str, cval);
  2226. if (!t)
  2227. break;
  2228. s = sym_find2(args, t);
  2229. if (s) {
  2230. cstr_new(&cstr);
  2231. st = (int *)s->c;
  2232. spc = 0;
  2233. while (*st) {
  2234. TOK_GET(t, st, cval);
  2235. if (!check_space(t, &spc))
  2236. cstr_cat(&cstr, get_tok_str(t, &cval));
  2237. }
  2238. cstr.size -= spc;
  2239. cstr_ccat(&cstr, '\0');
  2240. #ifdef PP_DEBUG
  2241. printf("stringize: %s\n", (char *)cstr.data);
  2242. #endif
  2243. /* add string */
  2244. cval.cstr = &cstr;
  2245. tok_str_add2(&str, TOK_STR, &cval);
  2246. cstr_free(&cstr);
  2247. } else {
  2248. tok_str_add2(&str, t, &cval);
  2249. }
  2250. } else if (t >= TOK_IDENT) {
  2251. s = sym_find2(args, t);
  2252. if (s) {
  2253. st = (int *)s->c;
  2254. /* if '##' is present before or after, no arg substitution */
  2255. if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
  2256. /* special case for var arg macros : ## eats the
  2257. ',' if empty VA_ARGS variable. */
  2258. /* XXX: test of the ',' is not 100%
  2259. reliable. should fix it to avoid security
  2260. problems */
  2261. if (gnu_ext && s->type.t &&
  2262. last_tok == TOK_TWOSHARPS &&
  2263. str.len >= 2 && str.str[str.len - 2] == ',') {
  2264. if (*st == 0) {
  2265. /* suppress ',' '##' */
  2266. str.len -= 2;
  2267. } else {
  2268. /* suppress '##' and add variable */
  2269. str.len--;
  2270. goto add_var;
  2271. }
  2272. } else {
  2273. int t1;
  2274. add_var:
  2275. for(;;) {
  2276. TOK_GET(t1, st, cval);
  2277. if (!t1)
  2278. break;
  2279. tok_str_add2(&str, t1, &cval);
  2280. }
  2281. }
  2282. } else {
  2283. /* NOTE: the stream cannot be read when macro
  2284. substituing an argument */
  2285. macro_subst(&str, nested_list, st, NULL);
  2286. }
  2287. } else {
  2288. tok_str_add(&str, t);
  2289. }
  2290. } else {
  2291. tok_str_add2(&str, t, &cval);
  2292. }
  2293. last_tok = t;
  2294. }
  2295. tok_str_add(&str, 0);
  2296. return str.str;
  2297. }
  2298. static char const ab_month_name[12][4] =
  2299. {
  2300. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  2301. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  2302. };
  2303. /* do macro substitution of current token with macro 's' and add
  2304. result to (tok_str,tok_len). 'nested_list' is the list of all
  2305. macros we got inside to avoid recursing. Return non zero if no
  2306. substitution needs to be done */
  2307. static int macro_subst_tok(TokenString *tok_str,
  2308. Sym **nested_list, Sym *s, struct macro_level **can_read_stream)
  2309. {
  2310. Sym *args, *sa, *sa1;
  2311. int mstr_allocated, parlevel, *mstr, t, t1, *p, spc;
  2312. TokenString str;
  2313. char *cstrval;
  2314. CValue cval;
  2315. CString cstr;
  2316. char buf[32];
  2317. /* if symbol is a macro, prepare substitution */
  2318. /* special macros */
  2319. if (tok == TOK___LINE__) {
  2320. snprintf(buf, sizeof(buf), "%d", file->line_num);
  2321. cstrval = buf;
  2322. t1 = TOK_PPNUM;
  2323. goto add_cstr1;
  2324. } else if (tok == TOK___FILE__) {
  2325. cstrval = file->filename;
  2326. goto add_cstr;
  2327. } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
  2328. time_t ti;
  2329. struct tm *tm;
  2330. time(&ti);
  2331. tm = localtime(&ti);
  2332. if (tok == TOK___DATE__) {
  2333. snprintf(buf, sizeof(buf), "%s %2d %d",
  2334. ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
  2335. } else {
  2336. snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
  2337. tm->tm_hour, tm->tm_min, tm->tm_sec);
  2338. }
  2339. cstrval = buf;
  2340. add_cstr:
  2341. t1 = TOK_STR;
  2342. add_cstr1:
  2343. cstr_new(&cstr);
  2344. cstr_cat(&cstr, cstrval);
  2345. cstr_ccat(&cstr, '\0');
  2346. cval.cstr = &cstr;
  2347. tok_str_add2(tok_str, t1, &cval);
  2348. cstr_free(&cstr);
  2349. } else {
  2350. mstr = (int *)s->c;
  2351. mstr_allocated = 0;
  2352. if (s->type.t == MACRO_FUNC) {
  2353. /* NOTE: we do not use next_nomacro to avoid eating the
  2354. next token. XXX: find better solution */
  2355. redo:
  2356. if (macro_ptr) {
  2357. p = macro_ptr;
  2358. while (is_space(t = *p) || TOK_LINEFEED == t)
  2359. ++p;
  2360. if (t == 0 && can_read_stream) {
  2361. /* end of macro stream: we must look at the token
  2362. after in the file */
  2363. struct macro_level *ml = *can_read_stream;
  2364. macro_ptr = NULL;
  2365. if (ml)
  2366. {
  2367. macro_ptr = ml->p;
  2368. ml->p = NULL;
  2369. *can_read_stream = ml -> prev;
  2370. }
  2371. goto redo;
  2372. }
  2373. } else {
  2374. /* XXX: incorrect with comments */
  2375. ch = file->buf_ptr[0];
  2376. while (is_space(ch) || ch == '\n')
  2377. cinp();
  2378. t = ch;
  2379. }
  2380. if (t != '(') /* no macro subst */
  2381. return -1;
  2382. /* argument macro */
  2383. next_nomacro();
  2384. next_nomacro();
  2385. args = NULL;
  2386. sa = s->next;
  2387. /* NOTE: empty args are allowed, except if no args */
  2388. for(;;) {
  2389. /* handle '()' case */
  2390. if (!args && !sa && tok == ')')
  2391. break;
  2392. if (!sa)
  2393. error("macro '%s' used with too many args",
  2394. get_tok_str(s->v, 0));
  2395. tok_str_new(&str);
  2396. parlevel = spc = 0;
  2397. /* NOTE: non zero sa->t indicates VA_ARGS */
  2398. while ((parlevel > 0 ||
  2399. (tok != ')' &&
  2400. (tok != ',' || sa->type.t))) &&
  2401. tok != -1) {
  2402. if (tok == '(')
  2403. parlevel++;
  2404. else if (tok == ')')
  2405. parlevel--;
  2406. if (tok == TOK_LINEFEED)
  2407. tok = ' ';
  2408. if (!check_space(tok, &spc))
  2409. tok_str_add2(&str, tok, &tokc);
  2410. next_nomacro_spc();
  2411. }
  2412. str.len -= spc;
  2413. tok_str_add(&str, 0);
  2414. sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, (long)str.str);
  2415. sa = sa->next;
  2416. if (tok == ')') {
  2417. /* special case for gcc var args: add an empty
  2418. var arg argument if it is omitted */
  2419. if (sa && sa->type.t && gnu_ext)
  2420. continue;
  2421. else
  2422. break;
  2423. }
  2424. if (tok != ',')
  2425. expect(",");
  2426. next_nomacro();
  2427. }
  2428. if (sa) {
  2429. error("macro '%s' used with too few args",
  2430. get_tok_str(s->v, 0));
  2431. }
  2432. /* now subst each arg */
  2433. mstr = macro_arg_subst(nested_list, mstr, args);
  2434. /* free memory */
  2435. sa = args;
  2436. while (sa) {
  2437. sa1 = sa->prev;
  2438. tok_str_free((int *)sa->c);
  2439. sym_free(sa);
  2440. sa = sa1;
  2441. }
  2442. mstr_allocated = 1;
  2443. }
  2444. sym_push2(nested_list, s->v, 0, 0);
  2445. macro_subst(tok_str, nested_list, mstr, can_read_stream);
  2446. /* pop nested defined symbol */
  2447. sa1 = *nested_list;
  2448. *nested_list = sa1->prev;
  2449. sym_free(sa1);
  2450. if (mstr_allocated)
  2451. tok_str_free(mstr);
  2452. }
  2453. return 0;
  2454. }
  2455. /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
  2456. return the resulting string (which must be freed). */
  2457. static inline int *macro_twosharps(const int *macro_str)
  2458. {
  2459. TokenSym *ts;
  2460. const int *ptr, *saved_macro_ptr;
  2461. int t;
  2462. const char *p1, *p2;
  2463. CValue cval;
  2464. TokenString macro_str1;
  2465. CString cstr;
  2466. /* we search the first '##' */
  2467. for(ptr = macro_str;;) {
  2468. TOK_GET(t, ptr, cval);
  2469. if (t == TOK_TWOSHARPS)
  2470. break;
  2471. /* nothing more to do if end of string */
  2472. if (t == 0)
  2473. return NULL;
  2474. }
  2475. /* we saw '##', so we need more processing to handle it */
  2476. cstr_new(&cstr);
  2477. tok_str_new(&macro_str1);
  2478. saved_macro_ptr = macro_ptr;
  2479. /* XXX: get rid of the use of macro_ptr here */
  2480. macro_ptr = (int *)macro_str;
  2481. for(;;) {
  2482. next_nomacro_spc();
  2483. if (tok == 0)
  2484. break;
  2485. if (tok == TOK_TWOSHARPS)
  2486. continue;
  2487. while (*macro_ptr == TOK_TWOSHARPS) {
  2488. t = *++macro_ptr;
  2489. if (t && t != TOK_TWOSHARPS) {
  2490. TOK_GET(t, macro_ptr, cval);
  2491. /* We concatenate the two tokens if we have an
  2492. identifier or a preprocessing number */
  2493. cstr_reset(&cstr);
  2494. p1 = get_tok_str(tok, &tokc);
  2495. cstr_cat(&cstr, p1);
  2496. p2 = get_tok_str(t, &cval);
  2497. cstr_cat(&cstr, p2);
  2498. cstr_ccat(&cstr, '\0');
  2499. if ((tok >= TOK_IDENT || tok == TOK_PPNUM) &&
  2500. (t >= TOK_IDENT || t == TOK_PPNUM)) {
  2501. if (tok == TOK_PPNUM) {
  2502. /* if number, then create a number token */
  2503. /* NOTE: no need to allocate because
  2504. tok_str_add2() does it */
  2505. cstr_reset(&tokcstr);
  2506. tokcstr = cstr;
  2507. cstr_new(&cstr);
  2508. tokc.cstr = &tokcstr;
  2509. } else {
  2510. /* if identifier, we must do a test to
  2511. validate we have a correct identifier */
  2512. if (t == TOK_PPNUM) {
  2513. const char *p;
  2514. int c;
  2515. p = p2;
  2516. for(;;) {
  2517. c = *p;
  2518. if (c == '\0')
  2519. break;
  2520. p++;
  2521. if (!isnum(c) && !isid(c))
  2522. goto error_pasting;
  2523. }
  2524. }
  2525. ts = tok_alloc(cstr.data, strlen(cstr.data));
  2526. tok = ts->tok; /* modify current token */
  2527. }
  2528. } else {
  2529. const char *str = cstr.data;
  2530. const unsigned char *q;
  2531. /* we look for a valid token */
  2532. /* XXX: do more extensive checks */
  2533. if (!strcmp(str, ">>=")) {
  2534. tok = TOK_A_SAR;
  2535. } else if (!strcmp(str, "<<=")) {
  2536. tok = TOK_A_SHL;
  2537. } else if (strlen(str) == 2) {
  2538. /* search in two bytes table */
  2539. q = tok_two_chars;
  2540. for(;;) {
  2541. if (!*q)
  2542. goto error_pasting;
  2543. if (q[0] == str[0] && q[1] == str[1])
  2544. break;
  2545. q += 3;
  2546. }
  2547. tok = q[2];
  2548. } else {
  2549. error_pasting:
  2550. /* NOTE: because get_tok_str use a static buffer,
  2551. we must save it */
  2552. cstr_reset(&cstr);
  2553. p1 = get_tok_str(tok, &tokc);
  2554. cstr_cat(&cstr, p1);
  2555. cstr_ccat(&cstr, '\0');
  2556. p2 = get_tok_str(t, &cval);
  2557. warning("pasting \"%s\" and \"%s\" does not give a valid preprocessing token", cstr.data, p2);
  2558. /* cannot merge tokens: just add them separately */
  2559. tok_str_add2(&macro_str1, tok, &tokc);
  2560. /* XXX: free associated memory ? */
  2561. tok = t;
  2562. tokc = cval;
  2563. }
  2564. }
  2565. }
  2566. }
  2567. tok_str_add2(&macro_str1, tok, &tokc);
  2568. }
  2569. macro_ptr = (int *)saved_macro_ptr;
  2570. cstr_free(&cstr);
  2571. tok_str_add(&macro_str1, 0);
  2572. return macro_str1.str;
  2573. }
  2574. /* do macro substitution of macro_str and add result to
  2575. (tok_str,tok_len). 'nested_list' is the list of all macros we got
  2576. inside to avoid recursing. */
  2577. static void macro_subst(TokenString *tok_str, Sym **nested_list,
  2578. const int *macro_str, struct macro_level ** can_read_stream)
  2579. {
  2580. Sym *s;
  2581. int *macro_str1;
  2582. const int *ptr;
  2583. int t, ret, spc;
  2584. CValue cval;
  2585. struct macro_level ml;
  2586. /* first scan for '##' operator handling */
  2587. ptr = macro_str;
  2588. macro_str1 = macro_twosharps(ptr);
  2589. if (macro_str1)
  2590. ptr = macro_str1;
  2591. spc = 0;
  2592. while (1) {
  2593. /* NOTE: ptr == NULL can only happen if tokens are read from
  2594. file stream due to a macro function call */
  2595. if (ptr == NULL)
  2596. break;
  2597. TOK_GET(t, ptr, cval);
  2598. if (t == 0)
  2599. break;
  2600. s = define_find(t);
  2601. if (s != NULL) {
  2602. /* if nested substitution, do nothing */
  2603. if (sym_find2(*nested_list, t))
  2604. goto no_subst;
  2605. ml.p = macro_ptr;
  2606. if (can_read_stream)
  2607. ml.prev = *can_read_stream, *can_read_stream = &ml;
  2608. macro_ptr = (int *)ptr;
  2609. tok = t;
  2610. ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
  2611. ptr = (int *)macro_ptr;
  2612. macro_ptr = ml.p;
  2613. if (can_read_stream && *can_read_stream == &ml)
  2614. *can_read_stream = ml.prev;
  2615. if (ret != 0)
  2616. goto no_subst;
  2617. } else {
  2618. no_subst:
  2619. if (!check_space(t, &spc))
  2620. tok_str_add2(tok_str, t, &cval);
  2621. }
  2622. }
  2623. if (macro_str1)
  2624. tok_str_free(macro_str1);
  2625. }
  2626. /* return next token with macro substitution */
  2627. static void next(void)
  2628. {
  2629. Sym *nested_list, *s;
  2630. TokenString str;
  2631. struct macro_level *ml;
  2632. redo:
  2633. if (parse_flags & PARSE_FLAG_SPACES)
  2634. next_nomacro_spc();
  2635. else
  2636. next_nomacro();
  2637. if (!macro_ptr) {
  2638. /* if not reading from macro substituted string, then try
  2639. to substitute macros */
  2640. if (tok >= TOK_IDENT &&
  2641. (parse_flags & PARSE_FLAG_PREPROCESS)) {
  2642. s = define_find(tok);
  2643. if (s) {
  2644. /* we have a macro: we try to substitute */
  2645. tok_str_new(&str);
  2646. nested_list = NULL;
  2647. ml = NULL;
  2648. if (macro_subst_tok(&str, &nested_list, s, &ml) == 0) {
  2649. /* substitution done, NOTE: maybe empty */
  2650. tok_str_add(&str, 0);
  2651. macro_ptr = str.str;
  2652. macro_ptr_allocated = str.str;
  2653. goto redo;
  2654. }
  2655. }
  2656. }
  2657. } else {
  2658. if (tok == 0) {
  2659. /* end of macro or end of unget buffer */
  2660. if (unget_buffer_enabled) {
  2661. macro_ptr = unget_saved_macro_ptr;
  2662. unget_buffer_enabled = 0;
  2663. } else {
  2664. /* end of macro string: free it */
  2665. tok_str_free(macro_ptr_allocated);
  2666. macro_ptr = NULL;
  2667. }
  2668. goto redo;
  2669. }
  2670. }
  2671. /* convert preprocessor tokens into C tokens */
  2672. if (tok == TOK_PPNUM &&
  2673. (parse_flags & PARSE_FLAG_TOK_NUM)) {
  2674. parse_number((char *)tokc.cstr->data);
  2675. }
  2676. }
  2677. /* push back current token and set current token to 'last_tok'. Only
  2678. identifier case handled for labels. */
  2679. static inline void unget_tok(int last_tok)
  2680. {
  2681. int i, n;
  2682. int *q;
  2683. unget_saved_macro_ptr = macro_ptr;
  2684. unget_buffer_enabled = 1;
  2685. q = unget_saved_buffer;
  2686. macro_ptr = q;
  2687. *q++ = tok;
  2688. n = tok_ext_size(tok) - 1;
  2689. for(i=0;i<n;i++)
  2690. *q++ = tokc.tab[i];
  2691. *q = 0; /* end of token string */
  2692. tok = last_tok;
  2693. }
  2694. /* better than nothing, but needs extension to handle '-E' option
  2695. correctly too */
  2696. static void preprocess_init(TCCState *s1)
  2697. {
  2698. s1->include_stack_ptr = s1->include_stack;
  2699. /* XXX: move that before to avoid having to initialize
  2700. file->ifdef_stack_ptr ? */
  2701. s1->ifdef_stack_ptr = s1->ifdef_stack;
  2702. file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
  2703. /* XXX: not ANSI compliant: bound checking says error */
  2704. vtop = vstack - 1;
  2705. s1->pack_stack[0] = 0;
  2706. s1->pack_stack_ptr = s1->pack_stack;
  2707. }
  2708. void preprocess_new()
  2709. {
  2710. int i, c;
  2711. const char *p, *r;
  2712. TokenSym *ts;
  2713. /* init isid table */
  2714. for(i=CH_EOF;i<256;i++)
  2715. isidnum_table[i-CH_EOF] = isid(i) || isnum(i);
  2716. /* add all tokens */
  2717. table_ident = NULL;
  2718. memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
  2719. tok_ident = TOK_IDENT;
  2720. p = tcc_keywords;
  2721. while (*p) {
  2722. r = p;
  2723. for(;;) {
  2724. c = *r++;
  2725. if (c == '\0')
  2726. break;
  2727. }
  2728. ts = tok_alloc(p, r - p - 1);
  2729. p = r;
  2730. }
  2731. }
  2732. /* Preprocess the current file */
  2733. static int tcc_preprocess(TCCState *s1)
  2734. {
  2735. Sym *define_start;
  2736. BufferedFile *file_ref;
  2737. int token_seen, line_ref;
  2738. preprocess_init(s1);
  2739. define_start = define_stack;
  2740. ch = file->buf_ptr[0];
  2741. tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
  2742. parse_flags = PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_PREPROCESS |
  2743. PARSE_FLAG_LINEFEED | PARSE_FLAG_SPACES;
  2744. token_seen = 0;
  2745. line_ref = 0;
  2746. file_ref = NULL;
  2747. for (;;) {
  2748. next();
  2749. if (tok == TOK_EOF) {
  2750. break;
  2751. } else if (tok == TOK_LINEFEED) {
  2752. if (!token_seen)
  2753. continue;
  2754. ++line_ref;
  2755. token_seen = 0;
  2756. } else if (!token_seen) {
  2757. int d = file->line_num - line_ref;
  2758. if (file != file_ref || d < 0 || d >= 8)
  2759. fprintf(s1->outfile, "# %d \"%s\"\n", file->line_num, file->filename);
  2760. else
  2761. while (d)
  2762. fputs("\n", s1->outfile), --d;
  2763. line_ref = (file_ref = file)->line_num;
  2764. token_seen = 1;
  2765. }
  2766. fputs(get_tok_str(tok, &tokc), s1->outfile);
  2767. }
  2768. free_defines(define_start);
  2769. return 0;
  2770. }