Alien-TinyCCx
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;
ST_DATA int parse_flags;
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 TokenSym *hash_ident[TOK_HASH_SIZE];
static char token_buf[STRING_MAX_SIZE + 1];
static CString cstr_buf;
static TokenString tokstr_buf;
static unsigned char isidnum_table[256 - CH_EOF];
static int pp_debug_tok, pp_debug_symv;
static int pp_once;
static void tok_print(const char *msg, const int *str);
static struct TinyAlloc *toksym_alloc;
static struct TinyAlloc *tokstr_alloc;
static struct TinyAlloc *cstr_alloc;
/* isidnum_table flags: */
#define IS_SPC 1
#define IS_ID 2
#define IS_NUM 4
static TokenString *macro_stack;
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[] =
/* outdated -- gr
"<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253"
"-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
*/{
'<','=', TOK_LE,
'>','=', TOK_GE,
'!','=', TOK_NE,
'&','&', TOK_LAND,
'|','|', TOK_LOR,
'+','+', TOK_INC,
'-','-', TOK_DEC,
'=','=', TOK_EQ,
'<','<', TOK_SHL,
'>','>', TOK_SAR,
'+','=', TOK_A_ADD,
'-','=', TOK_A_SUB,
'*','=', TOK_A_MUL,
'/','=', TOK_A_DIV,
'%','=', TOK_A_MOD,
'&','=', TOK_A_AND,
'^','=', TOK_A_XOR,
'|','=', TOK_A_OR,
'-','>', TOK_ARROW,
'.','.', 0xa8, // C++ token ?
'#','#', TOK_TWOSHARPS,
0
};
static void next_nomacro_spc(void);
ST_FUNC void skip(int c)
{
if (tok != c)
tcc_error("'%c' expected (got \"%s\")", c, get_tok_str(tok, &tokc));
next();
}
ST_FUNC void expect(const char *msg)
{
tcc_error("%s expected", msg);
}
/* ------------------------------------------------------------------------- */
/* Custom allocator for tiny objects */
#define USE_TAL
#ifndef USE_TAL
#define tal_free(al, p) tcc_free(p)
#define tal_realloc(al, p, size) tcc_realloc(p, size)
#define tal_new(a,b,c)
#define tal_delete(a)
#else
#if !defined(MEM_DEBUG)
#define tal_free(al, p) tal_free_impl(al, p)
#define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size)
#define TAL_DEBUG_PARAMS
#else
#define TAL_DEBUG 1
//#define TAL_INFO 1 /* collect and dump allocators stats */
#define tal_free(al, p) tal_free_impl(al, p, __FILE__, __LINE__)
#define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size, __FILE__, __LINE__)
#define TAL_DEBUG_PARAMS , const char *file, int line
#define TAL_DEBUG_FILE_LEN 15
#endif
#define TOKSYM_TAL_SIZE (768 * 1024) /* allocator for tiny TokenSym in table_ident */
#define TOKSTR_TAL_SIZE (768 * 1024) /* allocator for tiny TokenString instances */
#define CSTR_TAL_SIZE (256 * 1024) /* allocator for tiny CString instances */
#define TOKSYM_TAL_LIMIT 256 /* prefer unique limits to distinguish allocators debug msgs */
#define TOKSTR_TAL_LIMIT 128 /* 32 * sizeof(int) */
#define CSTR_TAL_LIMIT 1024
typedef struct TinyAlloc {
size_t limit;
size_t size;
uint8_t *buffer;
uint8_t *p;
size_t nb_allocs;
struct TinyAlloc *next, *top;
#ifdef TAL_INFO
size_t nb_peak;
size_t nb_total;
size_t nb_missed;
uint8_t *peak_p;
#endif
} TinyAlloc;
typedef struct tal_header_t {
size_t size;
#ifdef TAL_DEBUG
int line_num; /* negative line_num used for double free check */
char file_name[TAL_DEBUG_FILE_LEN + 1];
#endif
} tal_header_t;
/* ------------------------------------------------------------------------- */
ST_FUNC TinyAlloc *tal_new(TinyAlloc **pal, size_t limit, size_t size)
{
TinyAlloc *al = tcc_mallocz(sizeof(TinyAlloc));
al->p = al->buffer = tcc_malloc(size);
al->limit = limit;
al->size = size;
if (pal) *pal = al;
return al;
}
ST_FUNC void tal_delete(TinyAlloc *al)
{
TinyAlloc *next;
tail_call:
if (!al)
return;
#ifdef TAL_INFO
fprintf(stderr, "limit=%5d, size=%5g MB, nb_peak=%6d, nb_total=%8d, nb_missed=%6d, usage=%5.1f%%\n",
al->limit, al->size / 1024.0 / 1024.0, al->nb_peak, al->nb_total, al->nb_missed,
(al->peak_p - al->buffer) * 100.0 / al->size);
#endif
#ifdef TAL_DEBUG
if (al->nb_allocs > 0) {
uint8_t *p;
fprintf(stderr, "TAL_DEBUG: mem leak %d chunks (limit= %d)\n",
al->nb_allocs, al->limit);
p = al->buffer;
while (p < al->p) {
tal_header_t *header = (tal_header_t *)p;
if (header->line_num > 0) {
fprintf(stderr, " file %s, line %u: %u bytes\n",
src/tccpp.c view on Meta::CPAN
h = TOK_HASH_FUNC(h, *s);
#endif
s++;
}
h &= (CACHED_INCLUDES_HASH_SIZE - 1);
i = s1->cached_includes_hash[h];
for(;;) {
if (i == 0)
break;
e = s1->cached_includes[i - 1];
if (0 == PATHCMP(e->filename, filename))
return e;
i = e->hash_next;
}
if (!add)
return NULL;
e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
strcpy(e->filename, filename);
e->ifndef_macro = e->once = 0;
dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
/* add in hash table */
e->hash_next = s1->cached_includes_hash[h];
s1->cached_includes_hash[h] = s1->nb_cached_includes;
#ifdef INC_DEBUG
printf("adding cached '%s'\n", filename);
#endif
return e;
}
static void pragma_parse(TCCState *s1)
{
next_nomacro();
if (tok == TOK_push_macro || tok == TOK_pop_macro) {
int t = tok, v;
Sym *s;
if (next(), tok != '(')
goto pragma_err;
if (next(), tok != TOK_STR)
goto pragma_err;
v = tok_alloc(tokc.str.data, tokc.str.size - 1)->tok;
if (next(), tok != ')')
goto pragma_err;
if (t == TOK_push_macro) {
while (NULL == (s = define_find(v)))
define_push(v, 0, NULL, NULL);
s->type.ref = s; /* set push boundary */
} else {
for (s = define_stack; s; s = s->prev)
if (s->v == v && s->type.ref == s) {
s->type.ref = NULL;
break;
}
}
if (s)
table_ident[v - TOK_IDENT]->sym_define = s->d ? s : NULL;
else
tcc_warning("unbalanced #pragma pop_macro");
pp_debug_tok = t, pp_debug_symv = v;
} else if (tok == TOK_once) {
search_cached_include(s1, file->filename, 1)->once = pp_once;
} else if (s1->ppfp) {
/* tcc -E: keep pragmas below unchanged */
unget_tok(' ');
unget_tok(TOK_PRAGMA);
unget_tok('#');
unget_tok(TOK_LINEFEED);
} else 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 {
int 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)
goto pragma_err;
val = tokc.i;
if (val < 1 || val > 16 || (val & (val - 1)) != 0)
goto pragma_err;
next();
}
*s1->pack_stack_ptr = val;
}
if (tok != ')')
goto pragma_err;
} else if (tok == TOK_comment) {
char *file;
next();
skip('(');
if (tok != TOK_lib)
goto pragma_warn;
next();
skip(',');
if (tok != TOK_STR)
goto pragma_err;
file = tcc_strdup((char *)tokc.str.data);
dynarray_add((void ***)&s1->pragma_libs, &s1->nb_pragma_libs, file);
next();
if (tok != ')')
goto pragma_err;
} else {
pragma_warn:
if (s1->warn_unsupported)
tcc_warning("#pragma %s is ignored", get_tok_str(tok, &tokc));
}
return;
pragma_err:
tcc_error("malformed #pragma directive");
return;
}
/* 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_TOK_STR
| PARSE_FLAG_LINEFEED
| (parse_flags & PARSE_FLAG_ASM_FILE)
;
next_nomacro();
redo:
switch(tok) {
case TOK_DEFINE:
pp_debug_tok = tok;
next_nomacro();
pp_debug_symv = tok;
parse_define();
break;
case TOK_UNDEF:
pp_debug_tok = tok;
next_nomacro();
pp_debug_symv = tok;
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.str.data);
next();
}
c = '\"';
} else {
int len;
while (tok != TOK_LINEFEED) {
pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
next();
}
len = strlen(buf);
/* check syntax and remove '<>' */
if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
goto include_syntax;
src/tccpp.c view on Meta::CPAN
buf[len - 2] = '\0';
c = '>';
}
}
if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
tcc_error("#include recursion too deep");
/* store current file in stack, but increment stack later below */
*s1->include_stack_ptr = file;
i = tok == TOK_INCLUDE_NEXT ? file->include_next_index : 0;
n = 2 + s1->nb_include_paths + s1->nb_sysinclude_paths;
for (; i < n; ++i) {
char buf1[sizeof file->filename];
CachedInclude *e;
const char *path;
if (i == 0) {
/* check absolute include path */
if (!IS_ABSPATH(buf))
continue;
buf1[0] = 0;
} else if (i == 1) {
/* search in file's dir if "header.h" */
if (c != '\"')
continue;
path = file->filename;
pstrncpy(buf1, path, tcc_basename(path) - path);
} else {
/* search in all the include paths */
int j = i - 2, k = j - s1->nb_include_paths;
path = k < 0 ? s1->include_paths[j] : s1->sysinclude_paths[k];
pstrcpy(buf1, sizeof(buf1), path);
pstrcat(buf1, sizeof(buf1), "/");
}
pstrcat(buf1, sizeof(buf1), buf);
e = search_cached_include(s1, buf1, 0);
if (e && (define_find(e->ifndef_macro) || e->once == pp_once)) {
/* no need to parse the include because the 'ifndef macro'
is defined (or had #pragma once) */
#ifdef INC_DEBUG
printf("%s: skipping cached %s\n", file->filename, buf1);
#endif
goto include_done;
}
if (tcc_open(s1, buf1) < 0)
continue;
file->include_next_index = i + 1;
#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 (ifdef)");
*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) {
c = 0;
} else {
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)) {
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_PPNUM:
n = strtoul((char*)tokc.str.data, &q, 10);
goto _line_num;
case TOK_LINE:
next();
if (tok != TOK_CINT)
_line_err:
tcc_error("wrong #line format");
n = tokc.i;
_line_num:
next();
if (tok != TOK_LINEFEED) {
if (tok == TOK_STR)
pstrcpy(file->filename, sizeof(file->filename), (char *)tokc.str.data);
else if (parse_flags & PARSE_FLAG_ASM_FILE)
break;
else
goto _line_err;
--n;
}
if (file->fd > 0)
total_lines += file->line_num - n;
file->line_num = n;
if (s1->do_debug)
put_stabs(file->filename, N_BINCL, 0, 0, 0);
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;
case TOK_LINEFEED:
goto the_end;
default:
/* ignore gas line comment in an 'S' file. */
if (saved_parse_flags & PARSE_FLAG_ASM_FILE)
goto ignore;
if (tok == '!' && is_bof)
/* '!' is ignored at beginning to allow C scripts. */
goto ignore;
tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
ignore:
file->buf_ptr = parse_line_comment(file->buf_ptr - 1);
goto the_end;
}
/* 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++;
src/tccpp.c view on Meta::CPAN
int t, c, is_long, len;
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++;
if (parse_flags & PARSE_FLAG_SPACES)
goto keep_tok_flags;
while (isidnum_table[*p - CH_EOF] & IS_SPC)
++p;
goto redo_no_start;
case '\f':
case '\v':
case '\r':
p++;
goto redo_no_start;
case '\\':
/* first look if it is in fact an end of buffer */
c = handle_stray1(p);
p = file->buf_ptr;
if (c == '\\')
goto parse_simple;
if (c != CH_EOF)
goto redo_no_start;
{
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
search_cached_include(s1, file->filename, 1)
->ifndef_macro = 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_FILE) {
p = parse_line_comment(p - 1);
goto redo_no_start;
} else {
tok = '#';
}
}
}
break;
/* dollar is allowed to start identifiers when not parsing asm */
case '$':
if (!(isidnum_table[c - CH_EOF] & IS_ID)
|| (parse_flags & PARSE_FLAG_ASM_FILE))
goto parse_simple;
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':
src/tccpp.c view on Meta::CPAN
if (!fp || !tcc_state->dflag)
fp = stdout;
fprintf(fp, "%s ", msg);
while (str) {
TOK_GET(&t, &str, &cval);
if (!t)
break;
fprintf(fp,"%s", get_tok_str(t, &cval));
}
fprintf(fp, "\n");
}
static void pp_line(TCCState *s1, BufferedFile *f, int level)
{
int d = f->line_num - f->line_ref;
if (s1->dflag & 4)
return;
if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_NONE) {
;
} else if (level == 0 && f->line_ref && d < 8) {
while (d > 0)
fputs("\n", s1->ppfp), --d;
} else if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_STD) {
fprintf(s1->ppfp, "#line %d \"%s\"\n", f->line_num, f->filename);
} else {
fprintf(s1->ppfp, "# %d \"%s\"%s\n", f->line_num, f->filename,
level > 0 ? " 1" : level < 0 ? " 2" : "");
}
f->line_ref = f->line_num;
}
static void define_print(TCCState *s1, int v)
{
FILE *fp;
Sym *s;
s = define_find(v);
if (NULL == s || NULL == s->d)
return;
fp = s1->ppfp;
fprintf(fp, "#define %s", get_tok_str(v, NULL));
if (s->type.t == MACRO_FUNC) {
Sym *a = s->next;
fprintf(fp,"(");
if (a)
for (;;) {
fprintf(fp,"%s", get_tok_str(a->v & ~SYM_FIELD, NULL));
if (!(a = a->next))
break;
fprintf(fp,",");
}
fprintf(fp,")");
}
tok_print("", s->d);
}
static void pp_debug_defines(TCCState *s1)
{
int v, t;
const char *vs;
FILE *fp;
t = pp_debug_tok;
if (t == 0)
return;
file->line_num--;
pp_line(s1, file, 0);
file->line_ref = ++file->line_num;
fp = s1->ppfp;
v = pp_debug_symv;
vs = get_tok_str(v, NULL);
if (t == TOK_DEFINE) {
define_print(s1, v);
} else if (t == TOK_UNDEF) {
fprintf(fp, "#undef %s\n", vs);
} else if (t == TOK_push_macro) {
fprintf(fp, "#pragma push_macro(\"%s\")\n", vs);
} else if (t == TOK_pop_macro) {
fprintf(fp, "#pragma pop_macro(\"%s\")\n", vs);
}
pp_debug_tok = 0;
}
static void pp_debug_builtins(TCCState *s1)
{
int v;
for (v = TOK_IDENT; v < tok_ident; ++v)
define_print(s1, v);
}
/* Add a space between tokens a and b to avoid unwanted textual pasting */
static int pp_need_space(int a, int b)
{
return 'E' == a ? '+' == b || '-' == b
: '+' == a ? TOK_INC == b || '+' == b
: '-' == a ? TOK_DEC == b || '-' == b
: a >= TOK_IDENT ? b >= TOK_IDENT
: 0;
}
/* maybe hex like 0x1e */
static int pp_check_he0xE(int t, const char *p)
{
if (t == TOK_PPNUM && toup(strchr(p, 0)[-1]) == 'E')
return 'E';
return t;
}
/* Preprocess the current file */
ST_FUNC int tcc_preprocess(TCCState *s1)
{
BufferedFile **iptr;
int token_seen, spcs, level;
const char *p;
Sym *define_start;
preprocess_start(s1);
ch = file->buf_ptr[0];
tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
parse_flags = PARSE_FLAG_PREPROCESS
| (parse_flags & PARSE_FLAG_ASM_FILE)
| PARSE_FLAG_LINEFEED
| PARSE_FLAG_SPACES
| PARSE_FLAG_ACCEPT_STRAYS
;
define_start = define_stack;
/* Credits to Fabrice Bellard's initial revision to demonstrate its
capability to compile and run itself, provided all numbers are
given as decimals. tcc -E -P10 will do. */
if (s1->Pflag == 1 + 10)
parse_flags |= PARSE_FLAG_TOK_NUM, s1->Pflag = 1;
#ifdef PP_BENCH
/* for PP benchmarks */
do next(); while (tok != TOK_EOF); return 0;
#endif
if (s1->dflag & 1) {
pp_debug_builtins(s1);
s1->dflag &= ~1;
}
token_seen = TOK_LINEFEED, spcs = 0;
pp_line(s1, file, 0);
for (;;) {
iptr = s1->include_stack_ptr;
next();
if (tok == TOK_EOF)
break;
level = s1->include_stack_ptr - iptr;
if (level) {
if (level > 0)
pp_line(s1, *iptr, 0);
pp_line(s1, file, level);
}
if (s1->dflag) {
pp_debug_defines(s1);
if (s1->dflag & 4)
continue;
}
if (token_seen == TOK_LINEFEED) {
if (tok == ' ') {
++spcs;
continue;
}
if (tok == TOK_LINEFEED) {
spcs = 0;
continue;
}
pp_line(s1, file, 0);
} else if (tok == TOK_LINEFEED) {
++file->line_ref;
} else {
spcs = pp_need_space(token_seen, tok);
}
while (spcs)
fputs(" ", s1->ppfp), --spcs;
fputs(p = get_tok_str(tok, &tokc), s1->ppfp);
token_seen = pp_check_he0xE(tok, p);;
}
/* reset define stack, but keep -D and built-ins */
free_defines(define_start);
return 0;
}
/* ------------------------------------------------------------------------- */
( run in 0.763 second using v1.01-cache-2.11-cpan-02777c243ea )