Alien-TinyCC
view release on metacpan or search on metacpan
src/tccpp.c view on Meta::CPAN
/*
* TCC - Tiny C Compiler
*
* Copyright (c) 2001-2004 Fabrice Bellard
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "tcc.h"
/********************************************************/
/* global variables */
ST_DATA int tok_flags;
/* additional informations about token */
#define TOK_FLAG_BOL 0x0001 /* beginning of line before */
#define TOK_FLAG_BOF 0x0002 /* beginning of file before */
#define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
#define TOK_FLAG_EOF 0x0008 /* end of file */
ST_DATA int parse_flags;
#define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
#define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
#define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
token. line feed is also
returned at eof */
#define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
#define PARSE_FLAG_SPACES 0x0010 /* next() returns space tokens (for -E) */
ST_DATA struct BufferedFile *file;
ST_DATA int ch, tok;
ST_DATA CValue tokc;
ST_DATA const int *macro_ptr;
ST_DATA CString tokcstr; /* current parsed string, if any */
/* display benchmark infos */
ST_DATA int total_lines;
ST_DATA int total_bytes;
ST_DATA int tok_ident;
ST_DATA TokenSym **table_ident;
/* ------------------------------------------------------------------------- */
static int *macro_ptr_allocated;
static const int *unget_saved_macro_ptr;
static int unget_saved_buffer[TOK_MAX_SIZE + 1];
static int unget_buffer_enabled;
static TokenSym *hash_ident[TOK_HASH_SIZE];
static char token_buf[STRING_MAX_SIZE + 1];
/* true if isid(c) || isnum(c) */
static unsigned char isidnum_table[256-CH_EOF];
static const char tcc_keywords[] =
#define DEF(id, str) str "\0"
#include "tcctok.h"
#undef DEF
;
/* WARNING: the content of this string encodes token numbers */
static const unsigned 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";
struct macro_level {
struct macro_level *prev;
const int *p;
};
static void next_nomacro_spc(void);
static void macro_subst(
TokenString *tok_str,
Sym **nested_list,
const int *macro_str,
struct macro_level **can_read_stream
);
ST_FUNC void skip(int c)
{
if (tok != c)
tcc_error("'%c' expected (got \"%s\")", c, get_tok_str(tok, &tokc));
next();
src/tccpp.c view on Meta::CPAN
Sym *s;
s = define_find(v);
if (s && !macro_is_equal(s->d, str))
tcc_warning("%s redefined", get_tok_str(v, NULL));
s = sym_push2(&define_stack, v, macro_type, 0);
s->d = str;
s->next = first_arg;
table_ident[v - TOK_IDENT]->sym_define = s;
}
/* undefined a define symbol. Its name is just set to zero */
ST_FUNC void define_undef(Sym *s)
{
int v;
v = s->v;
if (v >= TOK_IDENT && v < tok_ident)
table_ident[v - TOK_IDENT]->sym_define = NULL;
s->v = 0;
}
ST_INLN Sym *define_find(int v)
{
v -= TOK_IDENT;
if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
return NULL;
return table_ident[v]->sym_define;
}
/* free define stack until top reaches 'b' */
ST_FUNC void free_defines(Sym *b)
{
Sym *top, *top1;
int v;
top = define_stack;
while (top != b) {
top1 = top->prev;
/* do not free args or predefined defines */
if (top->d)
tok_str_free(top->d);
v = top->v;
if (v >= TOK_IDENT && v < tok_ident)
table_ident[v - TOK_IDENT]->sym_define = NULL;
sym_free(top);
top = top1;
}
define_stack = b;
}
/* label lookup */
ST_FUNC Sym *label_find(int v)
{
v -= TOK_IDENT;
if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
return NULL;
return table_ident[v]->sym_label;
}
ST_FUNC Sym *label_push(Sym **ptop, int v, int flags)
{
Sym *s, **ps;
s = sym_push2(ptop, v, 0, 0);
s->r = flags;
ps = &table_ident[v - TOK_IDENT]->sym_label;
if (ptop == &global_label_stack) {
/* modify the top most local identifier, so that
sym_identifier will point to 's' when popped */
while (*ps != NULL)
ps = &(*ps)->prev_tok;
}
s->prev_tok = *ps;
*ps = s;
return s;
}
/* pop labels until element last is reached. Look if any labels are
undefined. Define symbols if '&&label' was used. */
ST_FUNC void label_pop(Sym **ptop, Sym *slast)
{
Sym *s, *s1;
for(s = *ptop; s != slast; s = s1) {
s1 = s->prev;
if (s->r == LABEL_DECLARED) {
tcc_warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
} else if (s->r == LABEL_FORWARD) {
tcc_error("label '%s' used but not defined",
get_tok_str(s->v, NULL));
} else {
if (s->c) {
/* define corresponding symbol. A size of
1 is put. */
put_extern_sym(s, cur_text_section, s->jnext, 1);
}
}
/* remove label */
table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
sym_free(s);
}
*ptop = slast;
}
/* eval an expression for #if/#elif */
static int expr_preprocess(void)
{
int c, t;
TokenString str;
tok_str_new(&str);
while (tok != TOK_LINEFEED && tok != TOK_EOF) {
next(); /* do macro subst */
if (tok == TOK_DEFINED) {
next_nomacro();
t = tok;
if (t == '(')
next_nomacro();
c = define_find(tok) != 0;
if (t == '(')
next_nomacro();
tok = TOK_CINT;
tokc.i = c;
} else if (tok >= TOK_IDENT) {
/* if undefined macro */
tok = TOK_CINT;
src/tccpp.c view on Meta::CPAN
strcpy(e->filename, filename);
e->ifndef_macro = ifndef_macro;
dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
/* add in hash table */
h = hash_cached_include(filename);
e->hash_next = s1->cached_includes_hash[h];
s1->cached_includes_hash[h] = s1->nb_cached_includes;
}
static void pragma_parse(TCCState *s1)
{
int val;
next();
if (tok == TOK_pack) {
/*
This may be:
#pragma pack(1) // set
#pragma pack() // reset to default
#pragma pack(push,1) // push & set
#pragma pack(pop) // restore previous
*/
next();
skip('(');
if (tok == TOK_ASM_pop) {
next();
if (s1->pack_stack_ptr <= s1->pack_stack) {
stk_error:
tcc_error("out of pack stack");
}
s1->pack_stack_ptr--;
} else {
val = 0;
if (tok != ')') {
if (tok == TOK_ASM_push) {
next();
if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
goto stk_error;
s1->pack_stack_ptr++;
skip(',');
}
if (tok != TOK_CINT) {
pack_error:
tcc_error("invalid pack pragma");
}
val = tokc.i;
if (val < 1 || val > 16 || (val & (val - 1)) != 0)
goto pack_error;
next();
}
*s1->pack_stack_ptr = val;
skip(')');
}
}
}
/* is_bof is true if first non space token at beginning of file */
ST_FUNC void preprocess(int is_bof)
{
TCCState *s1 = tcc_state;
int i, c, n, saved_parse_flags;
char buf[1024], *q;
Sym *s;
saved_parse_flags = parse_flags;
parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
PARSE_FLAG_LINEFEED;
next_nomacro();
redo:
switch(tok) {
case TOK_DEFINE:
next_nomacro();
parse_define();
break;
case TOK_UNDEF:
next_nomacro();
s = define_find(tok);
/* undefine symbol by putting an invalid name */
if (s)
define_undef(s);
break;
case TOK_INCLUDE:
case TOK_INCLUDE_NEXT:
ch = file->buf_ptr[0];
/* XXX: incorrect if comments : use next_nomacro with a special mode */
skip_spaces();
if (ch == '<') {
c = '>';
goto read_name;
} else if (ch == '\"') {
c = ch;
read_name:
inp();
q = buf;
while (ch != c && ch != '\n' && ch != CH_EOF) {
if ((q - buf) < sizeof(buf) - 1)
*q++ = ch;
if (ch == '\\') {
if (handle_stray_noerror() == 0)
--q;
} else
inp();
}
*q = '\0';
minp();
#if 0
/* eat all spaces and comments after include */
/* XXX: slightly incorrect */
while (ch1 != '\n' && ch1 != CH_EOF)
inp();
#endif
} else {
/* computed #include : either we have only strings or
we have anything enclosed in '<>' */
next();
buf[0] = '\0';
if (tok == TOK_STR) {
while (tok != TOK_LINEFEED) {
if (tok != TOK_STR) {
include_syntax:
tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
}
pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
next();
}
c = '\"';
src/tccpp.c view on Meta::CPAN
if (i == -2) {
/* check absolute include path */
if (!IS_ABSPATH(buf))
continue;
buf1[0] = 0;
i = n; /* force end loop */
} else if (i == -1) {
/* search in current dir if "header.h" */
if (c != '\"')
continue;
path = file->filename;
pstrncpy(buf1, path, tcc_basename(path) - path);
} else {
/* search in all the include paths */
if (i < s1->nb_include_paths)
path = s1->include_paths[i];
else
path = s1->sysinclude_paths[i - s1->nb_include_paths];
pstrcpy(buf1, sizeof(buf1), path);
pstrcat(buf1, sizeof(buf1), "/");
}
pstrcat(buf1, sizeof(buf1), buf);
if (tok == TOK_INCLUDE_NEXT)
for (f = s1->include_stack_ptr; f >= s1->include_stack; --f)
if (0 == PATHCMP((*f)->filename, buf1)) {
#ifdef INC_DEBUG
printf("%s: #include_next skipping %s\n", file->filename, buf1);
#endif
goto include_trynext;
}
e = search_cached_include(s1, buf1);
if (e && define_find(e->ifndef_macro)) {
/* no need to parse the include because the 'ifndef macro'
is defined */
#ifdef INC_DEBUG
printf("%s: skipping cached %s\n", file->filename, buf1);
#endif
goto include_done;
}
if (tcc_open(s1, buf1) < 0)
include_trynext:
continue;
#ifdef INC_DEBUG
printf("%s: including %s\n", file->prev->filename, file->filename);
#endif
/* update target deps */
dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
tcc_strdup(buf1));
/* push current file in stack */
++s1->include_stack_ptr;
/* add include file debug info */
if (s1->do_debug)
put_stabs(file->filename, N_BINCL, 0, 0, 0);
tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
ch = file->buf_ptr[0];
goto the_end;
}
tcc_error("include file '%s' not found", buf);
include_done:
break;
case TOK_IFNDEF:
c = 1;
goto do_ifdef;
case TOK_IF:
c = expr_preprocess();
goto do_if;
case TOK_IFDEF:
c = 0;
do_ifdef:
next_nomacro();
if (tok < TOK_IDENT)
tcc_error("invalid argument for '#if%sdef'", c ? "n" : "");
if (is_bof) {
if (c) {
#ifdef INC_DEBUG
printf("#ifndef %s\n", get_tok_str(tok, NULL));
#endif
file->ifndef_macro = tok;
}
}
c = (define_find(tok) != 0) ^ c;
do_if:
if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
tcc_error("memory full");
*s1->ifdef_stack_ptr++ = c;
goto test_skip;
case TOK_ELSE:
if (s1->ifdef_stack_ptr == s1->ifdef_stack)
tcc_error("#else without matching #if");
if (s1->ifdef_stack_ptr[-1] & 2)
tcc_error("#else after #else");
c = (s1->ifdef_stack_ptr[-1] ^= 3);
goto test_else;
case TOK_ELIF:
if (s1->ifdef_stack_ptr == s1->ifdef_stack)
tcc_error("#elif without matching #if");
c = s1->ifdef_stack_ptr[-1];
if (c > 1)
tcc_error("#elif after #else");
/* last #if/#elif expression was true: we skip */
if (c == 1)
goto skip;
c = expr_preprocess();
s1->ifdef_stack_ptr[-1] = c;
test_else:
if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
file->ifndef_macro = 0;
test_skip:
if (!(c & 1)) {
skip:
preprocess_skip();
is_bof = 0;
goto redo;
}
break;
case TOK_ENDIF:
if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
tcc_error("#endif without matching #if");
s1->ifdef_stack_ptr--;
/* '#ifndef macro' was at the start of file. Now we check if
an '#endif' is exactly at the end of file */
if (file->ifndef_macro &&
s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
file->ifndef_macro_saved = file->ifndef_macro;
/* need to set to zero to avoid false matches if another
#ifndef at middle of file */
file->ifndef_macro = 0;
while (tok != TOK_LINEFEED)
next_nomacro();
tok_flags |= TOK_FLAG_ENDIF;
goto the_end;
}
break;
case TOK_LINE:
next();
if (tok != TOK_CINT)
tcc_error("#line");
file->line_num = tokc.i - 1; /* the line number will be incremented after */
next();
if (tok != TOK_LINEFEED) {
if (tok != TOK_STR)
tcc_error("#line");
pstrcpy(file->filename, sizeof(file->filename),
(char *)tokc.cstr->data);
}
break;
case TOK_ERROR:
case TOK_WARNING:
c = tok;
ch = file->buf_ptr[0];
skip_spaces();
q = buf;
while (ch != '\n' && ch != CH_EOF) {
if ((q - buf) < sizeof(buf) - 1)
*q++ = ch;
if (ch == '\\') {
if (handle_stray_noerror() == 0)
--q;
} else
inp();
}
*q = '\0';
if (c == TOK_ERROR)
tcc_error("#error %s", buf);
else
tcc_warning("#warning %s", buf);
break;
case TOK_PRAGMA:
pragma_parse(s1);
break;
default:
if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_PPNUM) {
/* '!' is ignored to allow C scripts. numbers are ignored
to emulate cpp behaviour */
} else {
if (!(saved_parse_flags & PARSE_FLAG_ASM_COMMENTS))
tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
else {
/* this is a gas line comment in an 'S' file. */
file->buf_ptr = parse_line_comment(file->buf_ptr);
goto the_end;
}
}
break;
}
/* ignore other preprocess commands or #! for C scripts */
while (tok != TOK_LINEFEED)
next_nomacro();
the_end:
parse_flags = saved_parse_flags;
}
/* evaluate escape codes in a string. */
static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
{
int c, n;
const uint8_t *p;
p = buf;
for(;;) {
c = *p;
if (c == '\0')
break;
if (c == '\\') {
p++;
/* escape */
c = *p;
switch(c) {
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
/* at most three octal digits */
n = c - '0';
p++;
c = *p;
if (isoct(c)) {
n = n * 8 + c - '0';
p++;
c = *p;
if (isoct(c)) {
n = n * 8 + c - '0';
p++;
}
}
c = n;
goto add_char_nonext;
case 'x':
case 'u':
case 'U':
p++;
n = 0;
for(;;) {
c = *p;
if (c >= 'a' && c <= 'f')
c = c - 'a' + 10;
else if (c >= 'A' && c <= 'F')
c = c - 'A' + 10;
else if (isnum(c))
c = c - '0';
else
break;
n = n * 16 + c;
p++;
}
c = n;
goto add_char_nonext;
case 'a':
c = '\a';
break;
case 'b':
c = '\b';
src/tccpp.c view on Meta::CPAN
if (lcount == 2) {
#endif
if (tok == TOK_CINT)
tok = TOK_CLLONG;
else if (tok == TOK_CUINT)
tok = TOK_CULLONG;
#if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
}
#endif
ch = *p++;
} else if (t == 'U') {
if (ucount >= 1)
tcc_error("two 'u's in integer constant");
ucount++;
if (tok == TOK_CINT)
tok = TOK_CUINT;
else if (tok == TOK_CLLONG)
tok = TOK_CULLONG;
ch = *p++;
} else {
break;
}
}
if (tok == TOK_CINT || tok == TOK_CUINT)
tokc.ui = n;
else
tokc.ull = n;
}
if (ch)
tcc_error("invalid number\n");
}
#define PARSE2(c1, tok1, c2, tok2) \
case c1: \
PEEKC(c, p); \
if (c == c2) { \
p++; \
tok = tok2; \
} else { \
tok = tok1; \
} \
break;
/* return next token without macro substitution */
static inline void next_nomacro1(void)
{
int t, c, is_long;
TokenSym *ts;
uint8_t *p, *p1;
unsigned int h;
p = file->buf_ptr;
redo_no_start:
c = *p;
switch(c) {
case ' ':
case '\t':
tok = c;
p++;
goto keep_tok_flags;
case '\f':
case '\v':
case '\r':
p++;
goto redo_no_start;
case '\\':
/* first look if it is in fact an end of buffer */
if (p >= file->buf_end) {
file->buf_ptr = p;
handle_eob();
p = file->buf_ptr;
if (p >= file->buf_end)
goto parse_eof;
else
goto redo_no_start;
} else {
file->buf_ptr = p;
ch = *p;
handle_stray();
p = file->buf_ptr;
goto redo_no_start;
}
parse_eof:
{
TCCState *s1 = tcc_state;
if ((parse_flags & PARSE_FLAG_LINEFEED)
&& !(tok_flags & TOK_FLAG_EOF)) {
tok_flags |= TOK_FLAG_EOF;
tok = TOK_LINEFEED;
goto keep_tok_flags;
} else if (!(parse_flags & PARSE_FLAG_PREPROCESS)) {
tok = TOK_EOF;
} else if (s1->ifdef_stack_ptr != file->ifdef_stack_ptr) {
tcc_error("missing #endif");
} else if (s1->include_stack_ptr == s1->include_stack) {
/* no include left : end of file. */
tok = TOK_EOF;
} else {
tok_flags &= ~TOK_FLAG_EOF;
/* pop include file */
/* test if previous '#endif' was after a #ifdef at
start of file */
if (tok_flags & TOK_FLAG_ENDIF) {
#ifdef INC_DEBUG
printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
#endif
add_cached_include(s1, file->filename, file->ifndef_macro_saved);
tok_flags &= ~TOK_FLAG_ENDIF;
}
/* add end of include file debug info */
if (tcc_state->do_debug) {
put_stabd(N_EINCL, 0, 0);
}
/* pop include stack */
tcc_close();
s1->include_stack_ptr--;
p = file->buf_ptr;
goto redo_no_start;
}
}
break;
case '\n':
file->line_num++;
tok_flags |= TOK_FLAG_BOL;
p++;
maybe_newline:
if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
goto redo_no_start;
tok = TOK_LINEFEED;
goto keep_tok_flags;
case '#':
/* XXX: simplify */
PEEKC(c, p);
if ((tok_flags & TOK_FLAG_BOL) &&
(parse_flags & PARSE_FLAG_PREPROCESS)) {
file->buf_ptr = p;
preprocess(tok_flags & TOK_FLAG_BOF);
p = file->buf_ptr;
goto maybe_newline;
} else {
if (c == '#') {
p++;
tok = TOK_TWOSHARPS;
} else {
if (parse_flags & PARSE_FLAG_ASM_COMMENTS) {
p = parse_line_comment(p - 1);
goto redo_no_start;
} else {
tok = '#';
}
}
}
break;
case 'a': case 'b': case 'c': case 'd':
case 'e': case 'f': case 'g': case 'h':
case 'i': case 'j': case 'k': case 'l':
case 'm': case 'n': case 'o': case 'p':
case 'q': case 'r': case 's': case 't':
case 'u': case 'v': case 'w': case 'x':
case 'y': case 'z':
case 'A': case 'B': case 'C': case 'D':
case 'E': case 'F': case 'G': case 'H':
case 'I': case 'J': case 'K':
case 'M': case 'N': case 'O': case 'P':
case 'Q': case 'R': case 'S': case 'T':
case 'U': case 'V': case 'W': case 'X':
case 'Y': case 'Z':
case '_':
parse_ident_fast:
p1 = p;
h = TOK_HASH_INIT;
h = TOK_HASH_FUNC(h, c);
p++;
for(;;) {
c = *p;
if (!isidnum_table[c-CH_EOF])
break;
h = TOK_HASH_FUNC(h, c);
p++;
}
if (c != '\\') {
TokenSym **pts;
int len;
/* fast case : no stray found, so we have the full token
and we have already hashed it */
len = p - p1;
h &= (TOK_HASH_SIZE - 1);
pts = &hash_ident[h];
for(;;) {
ts = *pts;
if (!ts)
break;
if (ts->len == len && !memcmp(ts->str, p1, len))
goto token_found;
pts = &(ts->hash_next);
}
ts = tok_alloc_new(pts, p1, len);
token_found: ;
} else {
/* slower case */
cstr_reset(&tokcstr);
while (p1 < p) {
src/tccpp.c view on Meta::CPAN
} else {
tok = '&';
}
break;
case '|':
PEEKC(c, p);
if (c == '|') {
p++;
tok = TOK_LOR;
} else if (c == '=') {
p++;
tok = TOK_A_OR;
} else {
tok = '|';
}
break;
case '+':
PEEKC(c, p);
if (c == '+') {
p++;
tok = TOK_INC;
} else if (c == '=') {
p++;
tok = TOK_A_ADD;
} else {
tok = '+';
}
break;
case '-':
PEEKC(c, p);
if (c == '-') {
p++;
tok = TOK_DEC;
} else if (c == '=') {
p++;
tok = TOK_A_SUB;
} else if (c == '>') {
p++;
tok = TOK_ARROW;
} else {
tok = '-';
}
break;
PARSE2('!', '!', '=', TOK_NE)
PARSE2('=', '=', '=', TOK_EQ)
PARSE2('*', '*', '=', TOK_A_MUL)
PARSE2('%', '%', '=', TOK_A_MOD)
PARSE2('^', '^', '=', TOK_A_XOR)
/* comments or operator */
case '/':
PEEKC(c, p);
if (c == '*') {
p = parse_comment(p);
/* comments replaced by a blank */
tok = ' ';
goto keep_tok_flags;
} else if (c == '/') {
p = parse_line_comment(p);
tok = ' ';
goto keep_tok_flags;
} else if (c == '=') {
p++;
tok = TOK_A_DIV;
} else {
tok = '/';
}
break;
/* simple tokens */
case '(':
case ')':
case '[':
case ']':
case '{':
case '}':
case ',':
case ';':
case ':':
case '?':
case '~':
case '$': /* only used in assembler */
case '@': /* dito */
tok = c;
p++;
break;
default:
tcc_error("unrecognized character \\x%02x", c);
break;
}
tok_flags = 0;
keep_tok_flags:
file->buf_ptr = p;
#if defined(PARSE_DEBUG)
printf("token = %s\n", get_tok_str(tok, &tokc));
#endif
}
/* return next token without macro substitution. Can read input from
macro_ptr buffer */
static void next_nomacro_spc(void)
{
if (macro_ptr) {
redo:
tok = *macro_ptr;
if (tok) {
TOK_GET(&tok, ¯o_ptr, &tokc);
if (tok == TOK_LINENUM) {
file->line_num = tokc.i;
goto redo;
}
}
} else {
next_nomacro1();
}
}
ST_FUNC void next_nomacro(void)
{
do {
next_nomacro_spc();
} while (is_space(tok));
}
/* substitute args in macro_str and return allocated string */
static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args)
{
int last_tok, t, spc;
const int *st;
Sym *s;
CValue cval;
TokenString str;
CString cstr;
tok_str_new(&str);
last_tok = 0;
while(1) {
TOK_GET(&t, ¯o_str, &cval);
if (!t)
break;
if (t == '#') {
/* stringize */
TOK_GET(&t, ¯o_str, &cval);
if (!t)
break;
s = sym_find2(args, t);
if (s) {
cstr_new(&cstr);
st = s->d;
spc = 0;
while (*st) {
TOK_GET(&t, &st, &cval);
src/tccpp.c view on Meta::CPAN
}
/* do macro substitution of macro_str and add result to
(tok_str,tok_len). 'nested_list' is the list of all macros we got
inside to avoid recursing. */
static void macro_subst(TokenString *tok_str, Sym **nested_list,
const int *macro_str, struct macro_level ** can_read_stream)
{
Sym *s;
int *macro_str1;
const int *ptr;
int t, ret, spc;
CValue cval;
struct macro_level ml;
int force_blank;
/* first scan for '##' operator handling */
ptr = macro_str;
macro_str1 = macro_twosharps(ptr);
if (macro_str1)
ptr = macro_str1;
spc = 0;
force_blank = 0;
while (1) {
/* NOTE: ptr == NULL can only happen if tokens are read from
file stream due to a macro function call */
if (ptr == NULL)
break;
TOK_GET(&t, &ptr, &cval);
if (t == 0)
break;
if (t == TOK_NOSUBST) {
/* following token has already been subst'd. just copy it on */
tok_str_add2(tok_str, TOK_NOSUBST, NULL);
TOK_GET(&t, &ptr, &cval);
goto no_subst;
}
s = define_find(t);
if (s != NULL) {
/* if nested substitution, do nothing */
if (sym_find2(*nested_list, t)) {
/* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
tok_str_add2(tok_str, TOK_NOSUBST, NULL);
goto no_subst;
}
ml.p = macro_ptr;
if (can_read_stream)
ml.prev = *can_read_stream, *can_read_stream = &ml;
macro_ptr = (int *)ptr;
tok = t;
ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
ptr = (int *)macro_ptr;
macro_ptr = ml.p;
if (can_read_stream && *can_read_stream == &ml)
*can_read_stream = ml.prev;
if (ret != 0)
goto no_subst;
if (parse_flags & PARSE_FLAG_SPACES)
force_blank = 1;
} else {
no_subst:
if (force_blank) {
tok_str_add(tok_str, ' ');
spc = 1;
force_blank = 0;
}
if (!check_space(t, &spc))
tok_str_add2(tok_str, t, &cval);
}
}
if (macro_str1)
tok_str_free(macro_str1);
}
/* return next token with macro substitution */
ST_FUNC void next(void)
{
Sym *nested_list, *s;
TokenString str;
struct macro_level *ml;
redo:
if (parse_flags & PARSE_FLAG_SPACES)
next_nomacro_spc();
else
next_nomacro();
if (!macro_ptr) {
/* if not reading from macro substituted string, then try
to substitute macros */
if (tok >= TOK_IDENT &&
(parse_flags & PARSE_FLAG_PREPROCESS)) {
s = define_find(tok);
if (s) {
/* we have a macro: we try to substitute */
tok_str_new(&str);
nested_list = NULL;
ml = NULL;
if (macro_subst_tok(&str, &nested_list, s, &ml) == 0) {
/* substitution done, NOTE: maybe empty */
tok_str_add(&str, 0);
macro_ptr = str.str;
macro_ptr_allocated = str.str;
goto redo;
}
}
}
} else {
if (tok == 0) {
/* end of macro or end of unget buffer */
if (unget_buffer_enabled) {
macro_ptr = unget_saved_macro_ptr;
unget_buffer_enabled = 0;
} else {
/* end of macro string: free it */
tok_str_free(macro_ptr_allocated);
macro_ptr_allocated = NULL;
macro_ptr = NULL;
}
goto redo;
} else if (tok == TOK_NOSUBST) {
/* discard preprocessor's nosubst markers */
goto redo;
}
}
/* convert preprocessor tokens into C tokens */
if (tok == TOK_PPNUM &&
(parse_flags & PARSE_FLAG_TOK_NUM)) {
parse_number((char *)tokc.cstr->data);
}
}
/* push back current token and set current token to 'last_tok'. Only
identifier case handled for labels. */
ST_INLN void unget_tok(int last_tok)
{
int i, n;
int *q;
if (unget_buffer_enabled)
{
/* assert(macro_ptr == unget_saved_buffer + 1);
assert(*macro_ptr == 0); */
}
else
{
unget_saved_macro_ptr = macro_ptr;
unget_buffer_enabled = 1;
}
q = unget_saved_buffer;
macro_ptr = q;
*q++ = tok;
n = tok_ext_size(tok) - 1;
for(i=0;i<n;i++)
*q++ = tokc.tab[i];
*q = 0; /* end of token string */
tok = last_tok;
}
/* better than nothing, but needs extension to handle '-E' option
correctly too */
ST_FUNC void preprocess_init(TCCState *s1)
{
s1->include_stack_ptr = s1->include_stack;
/* XXX: move that before to avoid having to initialize
file->ifdef_stack_ptr ? */
s1->ifdef_stack_ptr = s1->ifdef_stack;
file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
vtop = vstack - 1;
s1->pack_stack[0] = 0;
s1->pack_stack_ptr = s1->pack_stack;
}
ST_FUNC void preprocess_new(void)
{
int i, c;
const char *p, *r;
/* init isid table */
for(i=CH_EOF;i<256;i++)
isidnum_table[i-CH_EOF] = isid(i) || isnum(i);
/* add all tokens */
table_ident = NULL;
memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
tok_ident = TOK_IDENT;
p = tcc_keywords;
while (*p) {
r = p;
for(;;) {
c = *r++;
if (c == '\0')
break;
}
tok_alloc(p, r - p - 1);
p = r;
}
}
/* Preprocess the current file */
ST_FUNC int tcc_preprocess(TCCState *s1)
{
Sym *define_start;
BufferedFile *file_ref, **iptr, **iptr_new;
int token_seen, line_ref, d;
const char *s;
preprocess_init(s1);
define_start = define_stack;
ch = file->buf_ptr[0];
tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
parse_flags = PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_PREPROCESS |
PARSE_FLAG_LINEFEED | PARSE_FLAG_SPACES;
token_seen = 0;
line_ref = 0;
file_ref = NULL;
iptr = s1->include_stack_ptr;
for (;;) {
next();
if (tok == TOK_EOF) {
break;
} else if (file != file_ref) {
goto print_line;
} else if (tok == TOK_LINEFEED) {
if (!token_seen)
continue;
++line_ref;
token_seen = 0;
} else if (!token_seen) {
d = file->line_num - line_ref;
if (file != file_ref || d < 0 || d >= 8) {
print_line:
iptr_new = s1->include_stack_ptr;
s = iptr_new > iptr ? " 1"
: iptr_new < iptr ? " 2"
: iptr_new > s1->include_stack ? " 3"
: ""
;
iptr = iptr_new;
fprintf(s1->ppfp, "# %d \"%s\"%s\n", file->line_num, file->filename, s);
} else {
while (d)
fputs("\n", s1->ppfp), --d;
}
line_ref = (file_ref = file)->line_num;
token_seen = tok != TOK_LINEFEED;
if (!token_seen)
continue;
}
fputs(get_tok_str(tok, &tokc), s1->ppfp);
}
free_defines(define_start);
return 0;
}
( run in 1.202 second using v1.01-cache-2.11-cpan-9bca49b1385 )