2001-11-01 22:48:10 +08:00
|
|
|
/*
|
|
|
|
* TCC - Tiny C Compiler
|
|
|
|
*
|
|
|
|
* Copyright (c) 2001 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
2001-12-07 07:39:24 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#ifndef CONFIG_TCC_STATIC
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
2001-10-28 07:48:39 +08:00
|
|
|
|
2001-11-11 10:53:01 +08:00
|
|
|
//#define DEBUG
|
|
|
|
/* preprocessor debug */
|
|
|
|
//#define PP_DEBUG
|
2001-11-06 09:20:20 +08:00
|
|
|
|
2001-11-12 02:00:58 +08:00
|
|
|
/* these sizes are dummy for unix, because malloc() does not use
|
|
|
|
memory when the pages are not used */
|
|
|
|
#define TEXT_SIZE (4*1024*1024)
|
|
|
|
#define DATA_SIZE (4*1024*1024)
|
|
|
|
|
2001-11-06 09:20:20 +08:00
|
|
|
#define INCLUDE_STACK_SIZE 32
|
2001-11-08 09:12:31 +08:00
|
|
|
#define IFDEF_STACK_SIZE 64
|
2001-11-08 09:30:44 +08:00
|
|
|
#define VSTACK_SIZE 64
|
2001-11-11 10:53:01 +08:00
|
|
|
#define STRING_MAX_SIZE 1024
|
|
|
|
#define INCLUDE_PATHS_MAX 32
|
2001-11-06 09:20:20 +08:00
|
|
|
|
2001-11-19 00:33:01 +08:00
|
|
|
#define TOK_HASH_SIZE 521
|
|
|
|
#define TOK_ALLOC_INCR 256 /* must be a power of two */
|
|
|
|
#define SYM_HASH_SIZE 263
|
|
|
|
|
2001-11-12 02:00:58 +08:00
|
|
|
/* number of available temporary registers */
|
2001-11-06 09:20:20 +08:00
|
|
|
#define NB_REGS 3
|
2001-12-03 05:24:43 +08:00
|
|
|
/* return register for functions */
|
|
|
|
#define FUNC_RET_REG 0
|
2001-11-19 00:33:01 +08:00
|
|
|
/* defined if function parameters must be evaluated in reverse order */
|
2001-11-12 02:00:58 +08:00
|
|
|
#define INVERT_FUNC_PARAMS
|
2001-12-03 05:24:43 +08:00
|
|
|
/* defined if structures are passed as pointers. Otherwise structures
|
|
|
|
are directly pushed on stack. */
|
|
|
|
//#define FUNC_STRUCT_PARAM_AS_PTR
|
2001-10-28 07:48:39 +08:00
|
|
|
|
2001-11-11 10:53:01 +08:00
|
|
|
/* token symbol management */
|
|
|
|
typedef struct TokenSym {
|
2001-11-19 00:33:01 +08:00
|
|
|
struct TokenSym *hash_next;
|
2001-11-11 10:53:01 +08:00
|
|
|
int tok; /* token number */
|
|
|
|
int len;
|
|
|
|
char str[1];
|
|
|
|
} TokenSym;
|
|
|
|
|
2001-11-01 22:48:10 +08:00
|
|
|
/* symbol management */
|
|
|
|
typedef struct Sym {
|
|
|
|
int v; /* symbol token */
|
|
|
|
int t; /* associated type */
|
|
|
|
int c; /* associated number */
|
|
|
|
struct Sym *next; /* next related symbol */
|
|
|
|
struct Sym *prev; /* prev symbol in stack */
|
2001-11-19 00:33:01 +08:00
|
|
|
struct Sym *hash_next; /* next symbol in hash table */
|
2001-11-01 22:48:10 +08:00
|
|
|
} Sym;
|
|
|
|
|
2001-11-19 00:33:01 +08:00
|
|
|
typedef struct SymStack {
|
|
|
|
struct Sym *top;
|
|
|
|
struct Sym *hash[SYM_HASH_SIZE];
|
|
|
|
} SymStack;
|
|
|
|
|
2001-12-05 08:45:08 +08:00
|
|
|
/* relocation entry (currently only used for functions or variables */
|
|
|
|
typedef struct Reloc {
|
|
|
|
int type; /* type of relocation */
|
|
|
|
int addr; /* address of relocation */
|
|
|
|
struct Reloc *next; /* next relocation */
|
|
|
|
} Reloc;
|
|
|
|
|
|
|
|
#define RELOC_ADDR32 1 /* 32 bits relocation */
|
|
|
|
#define RELOC_REL32 2 /* 32 bits relative relocation */
|
|
|
|
|
|
|
|
|
2001-12-08 23:04:01 +08:00
|
|
|
#define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
|
|
|
|
#define SYM_FIELD 0x20000000 /* struct/union field symbol space */
|
|
|
|
#define SYM_FIRST_ANOM (1 << (31 - VT_STRUCT_SHIFT)) /* first anonymous sym */
|
2001-11-01 22:48:10 +08:00
|
|
|
|
2001-11-08 07:13:29 +08:00
|
|
|
#define FUNC_NEW 1 /* ansi function prototype */
|
|
|
|
#define FUNC_OLD 2 /* old function prototype */
|
|
|
|
#define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
|
|
|
|
|
2001-11-12 00:07:43 +08:00
|
|
|
/* field 'Sym.t' for macros */
|
2001-11-11 10:53:01 +08:00
|
|
|
#define MACRO_OBJ 0 /* object like macro */
|
|
|
|
#define MACRO_FUNC 1 /* function like macro */
|
|
|
|
|
2001-11-12 00:07:43 +08:00
|
|
|
/* type_decl() types */
|
|
|
|
#define TYPE_ABSTRACT 1 /* type without variable */
|
|
|
|
#define TYPE_DIRECT 2 /* type with variable */
|
|
|
|
|
2001-11-06 09:20:20 +08:00
|
|
|
typedef struct {
|
2001-11-08 07:13:29 +08:00
|
|
|
FILE *file;
|
2001-11-06 09:20:20 +08:00
|
|
|
char *filename;
|
|
|
|
int line_num;
|
|
|
|
} IncludeFile;
|
|
|
|
|
2001-12-07 07:39:24 +08:00
|
|
|
/* parser */
|
|
|
|
FILE *file;
|
|
|
|
int ch, ch1, tok, tokc, tok1, tok1c;
|
|
|
|
|
2001-11-01 22:48:10 +08:00
|
|
|
/* loc : local variable index
|
2001-10-28 07:48:39 +08:00
|
|
|
glo : global variable index
|
|
|
|
ind : output code ptr
|
|
|
|
rsym: return symbol
|
|
|
|
prog: output code
|
2001-11-06 09:20:20 +08:00
|
|
|
anon_sym: anonymous symbol index
|
2001-10-28 07:48:39 +08:00
|
|
|
*/
|
2001-12-07 07:39:24 +08:00
|
|
|
int rsym, anon_sym,
|
2001-11-11 10:53:01 +08:00
|
|
|
prog, ind, loc, glo, vt, vc, const_wanted, line_num;
|
2001-12-02 22:47:48 +08:00
|
|
|
int global_expr; /* true if compound literals must be allocated
|
|
|
|
globally (used during initializers parsing */
|
2001-12-03 05:24:43 +08:00
|
|
|
int func_vt, func_vc; /* current function return type (used by
|
|
|
|
return instruction) */
|
2001-11-19 00:33:01 +08:00
|
|
|
int tok_ident;
|
|
|
|
TokenSym **table_ident;
|
2001-12-09 04:24:33 +08:00
|
|
|
TokenSym *hash_ident[TOK_HASH_SIZE];
|
2001-11-11 10:53:01 +08:00
|
|
|
char token_buf[STRING_MAX_SIZE + 1];
|
2001-11-14 06:54:35 +08:00
|
|
|
char *filename, *funcname;
|
2001-12-08 23:04:01 +08:00
|
|
|
/* contains global symbols which remain between each translation unit */
|
|
|
|
SymStack extern_stack;
|
2001-11-19 00:33:01 +08:00
|
|
|
SymStack define_stack, global_stack, local_stack, label_stack;
|
2001-10-28 07:48:39 +08:00
|
|
|
|
2001-11-08 09:30:44 +08:00
|
|
|
int vstack[VSTACK_SIZE], *vstack_ptr;
|
2001-11-11 10:53:01 +08:00
|
|
|
int *macro_ptr, *macro_ptr_allocated;
|
2001-11-06 09:20:20 +08:00
|
|
|
IncludeFile include_stack[INCLUDE_STACK_SIZE], *include_stack_ptr;
|
2001-11-08 09:12:31 +08:00
|
|
|
int ifdef_stack[IFDEF_STACK_SIZE], *ifdef_stack_ptr;
|
2001-11-11 10:53:01 +08:00
|
|
|
char *include_paths[INCLUDE_PATHS_MAX];
|
|
|
|
int nb_include_paths;
|
2001-10-28 07:48:39 +08:00
|
|
|
|
2001-12-05 10:02:25 +08:00
|
|
|
/* use GNU C extensions */
|
|
|
|
int gnu_ext = 1;
|
|
|
|
|
2001-11-06 09:20:20 +08:00
|
|
|
/* The current value can be: */
|
|
|
|
#define VT_VALMASK 0x000f
|
|
|
|
#define VT_CONST 0x000a /* constant in vc
|
|
|
|
(must be first non register value) */
|
|
|
|
#define VT_LLOCAL 0x000b /* lvalue, offset on stack */
|
|
|
|
#define VT_LOCAL 0x000c /* offset on stack */
|
|
|
|
#define VT_CMP 0x000d /* the value is stored in processor flags (in vc) */
|
|
|
|
#define VT_JMP 0x000e /* value is the consequence of jmp true */
|
|
|
|
#define VT_JMPI 0x000f /* value is the consequence of jmp false */
|
|
|
|
#define VT_LVAL 0x0010 /* var is an lvalue */
|
2001-10-28 07:48:39 +08:00
|
|
|
#define VT_LVALN -17 /* ~VT_LVAL */
|
2001-11-06 09:20:20 +08:00
|
|
|
#define VT_FORWARD 0x0020 /* value is forward reference
|
|
|
|
(only used for functions) */
|
|
|
|
|
2001-11-12 02:00:58 +08:00
|
|
|
/* types */
|
2001-11-29 06:48:43 +08:00
|
|
|
#define VT_INT 0
|
2001-11-06 09:20:20 +08:00
|
|
|
#define VT_VOID 0x00040
|
2001-11-12 02:00:58 +08:00
|
|
|
#define VT_BYTE 0x00080 /* signed byte type */
|
2001-11-06 09:20:20 +08:00
|
|
|
#define VT_PTR 0x00100 /* pointer increment */
|
|
|
|
#define VT_UNSIGNED 0x00200 /* unsigned type */
|
|
|
|
#define VT_ARRAY 0x00400 /* array type (only used in parsing) */
|
|
|
|
#define VT_ENUM 0x00800 /* enum definition */
|
2001-10-30 07:23:01 +08:00
|
|
|
#define VT_FUNC 0x01000 /* function type */
|
2001-11-06 09:20:20 +08:00
|
|
|
#define VT_STRUCT 0x002000 /* struct/union definition */
|
2001-12-05 08:45:08 +08:00
|
|
|
#define VT_SHORT 0x004000 /* short type */
|
2001-11-12 02:00:58 +08:00
|
|
|
#define VT_STRUCT_SHIFT 18 /* structure/enum name shift (14 bits left) */
|
2001-11-06 09:20:20 +08:00
|
|
|
|
2001-12-08 23:04:01 +08:00
|
|
|
#define VT_TYPE 0xfffc7fc0 /* type mask */
|
|
|
|
|
2001-12-05 08:45:08 +08:00
|
|
|
/* storage */
|
|
|
|
#define VT_EXTERN 0x00008000 /* extern definition */
|
|
|
|
#define VT_STATIC 0x00010000 /* static variable */
|
|
|
|
#define VT_TYPEDEF 0x00020000 /* typedef definition */
|
|
|
|
|
2001-11-06 09:20:20 +08:00
|
|
|
|
2001-10-28 07:48:39 +08:00
|
|
|
/* token values */
|
|
|
|
|
2001-11-08 07:13:29 +08:00
|
|
|
/* warning: the following compare tokens depend on i386 asm code */
|
|
|
|
#define TOK_ULT 0x92
|
|
|
|
#define TOK_UGE 0x93
|
|
|
|
#define TOK_EQ 0x94
|
|
|
|
#define TOK_NE 0x95
|
|
|
|
#define TOK_ULE 0x96
|
|
|
|
#define TOK_UGT 0x97
|
|
|
|
#define TOK_LT 0x9c
|
|
|
|
#define TOK_GE 0x9d
|
|
|
|
#define TOK_LE 0x9e
|
|
|
|
#define TOK_GT 0x9f
|
2001-10-28 07:48:39 +08:00
|
|
|
|
2001-11-06 09:20:20 +08:00
|
|
|
#define TOK_LAND 0xa0
|
|
|
|
#define TOK_LOR 0xa1
|
2001-10-28 07:48:39 +08:00
|
|
|
|
2001-11-01 22:48:10 +08:00
|
|
|
#define TOK_DEC 0xa2
|
|
|
|
#define TOK_MID 0xa3 /* inc/dec, to void constant */
|
|
|
|
#define TOK_INC 0xa4
|
|
|
|
#define TOK_ARROW 0xa7
|
2001-11-06 09:20:20 +08:00
|
|
|
#define TOK_DOTS 0xa8 /* three dots */
|
|
|
|
#define TOK_SHR 0xa9 /* unsigned shift right */
|
|
|
|
#define TOK_UDIV 0xb0 /* unsigned division */
|
|
|
|
#define TOK_UMOD 0xb1 /* unsigned modulo */
|
|
|
|
#define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
|
|
|
|
#define TOK_NUM 0xb3 /* number in tokc */
|
2001-11-11 10:53:01 +08:00
|
|
|
#define TOK_CCHAR 0xb4 /* char constant in tokc */
|
|
|
|
#define TOK_STR 0xb5 /* pointer to string in tokc */
|
|
|
|
#define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
|
2001-11-29 06:48:43 +08:00
|
|
|
#define TOK_LCHAR 0xb7
|
|
|
|
#define TOK_LSTR 0xb8
|
2001-11-11 10:53:01 +08:00
|
|
|
|
2001-11-06 09:20:20 +08:00
|
|
|
#define TOK_SHL 0x01 /* shift left */
|
|
|
|
#define TOK_SAR 0x02 /* signed shift right */
|
2001-10-28 07:48:39 +08:00
|
|
|
|
2001-10-29 06:24:16 +08:00
|
|
|
/* assignement operators : normal operator or 0x80 */
|
|
|
|
#define TOK_A_MOD 0xa5
|
|
|
|
#define TOK_A_AND 0xa6
|
|
|
|
#define TOK_A_MUL 0xaa
|
|
|
|
#define TOK_A_ADD 0xab
|
|
|
|
#define TOK_A_SUB 0xad
|
|
|
|
#define TOK_A_DIV 0xaf
|
|
|
|
#define TOK_A_XOR 0xde
|
|
|
|
#define TOK_A_OR 0xfc
|
|
|
|
#define TOK_A_SHL 0x81
|
2001-11-06 09:20:20 +08:00
|
|
|
#define TOK_A_SAR 0x82
|
2001-10-29 06:24:16 +08:00
|
|
|
|
2001-11-11 10:53:01 +08:00
|
|
|
/* all identificators and strings have token above that */
|
|
|
|
#define TOK_IDENT 256
|
|
|
|
|
2001-11-08 09:12:31 +08:00
|
|
|
enum {
|
2001-11-11 10:53:01 +08:00
|
|
|
TOK_INT = TOK_IDENT,
|
2001-11-08 09:12:31 +08:00
|
|
|
TOK_VOID,
|
|
|
|
TOK_CHAR,
|
|
|
|
TOK_IF,
|
|
|
|
TOK_ELSE,
|
|
|
|
TOK_WHILE,
|
|
|
|
TOK_BREAK,
|
|
|
|
TOK_RETURN,
|
|
|
|
TOK_FOR,
|
|
|
|
TOK_EXTERN,
|
|
|
|
TOK_STATIC,
|
|
|
|
TOK_UNSIGNED,
|
|
|
|
TOK_GOTO,
|
|
|
|
TOK_DO,
|
|
|
|
TOK_CONTINUE,
|
|
|
|
TOK_SWITCH,
|
|
|
|
TOK_CASE,
|
|
|
|
|
|
|
|
/* ignored types Must have contiguous values */
|
|
|
|
TOK_CONST,
|
|
|
|
TOK_VOLATILE,
|
|
|
|
TOK_LONG,
|
|
|
|
TOK_REGISTER,
|
|
|
|
TOK_SIGNED,
|
|
|
|
TOK_AUTO,
|
|
|
|
TOK_INLINE,
|
2001-11-14 06:54:35 +08:00
|
|
|
TOK_RESTRICT,
|
2001-11-08 09:12:31 +08:00
|
|
|
|
|
|
|
/* unsupported type */
|
|
|
|
TOK_FLOAT,
|
|
|
|
TOK_DOUBLE,
|
|
|
|
|
2001-11-11 10:53:01 +08:00
|
|
|
TOK_SHORT,
|
2001-11-08 09:12:31 +08:00
|
|
|
TOK_STRUCT,
|
|
|
|
TOK_UNION,
|
|
|
|
TOK_TYPEDEF,
|
|
|
|
TOK_DEFAULT,
|
|
|
|
TOK_ENUM,
|
|
|
|
TOK_SIZEOF,
|
|
|
|
|
|
|
|
/* preprocessor only */
|
2001-12-05 10:02:25 +08:00
|
|
|
TOK_UIDENT, /* first "user" ident (not keyword) */
|
|
|
|
TOK_DEFINE = TOK_UIDENT,
|
2001-11-08 09:12:31 +08:00
|
|
|
TOK_INCLUDE,
|
|
|
|
TOK_IFDEF,
|
|
|
|
TOK_IFNDEF,
|
|
|
|
TOK_ELIF,
|
|
|
|
TOK_ENDIF,
|
|
|
|
TOK_DEFINED,
|
2001-11-11 10:53:01 +08:00
|
|
|
TOK_UNDEF,
|
2001-11-14 06:54:35 +08:00
|
|
|
TOK_ERROR,
|
2001-11-18 01:22:38 +08:00
|
|
|
TOK_LINE,
|
2001-11-14 06:54:35 +08:00
|
|
|
TOK___LINE__,
|
|
|
|
TOK___FILE__,
|
|
|
|
TOK___DATE__,
|
|
|
|
TOK___TIME__,
|
2001-11-18 01:22:38 +08:00
|
|
|
TOK___VA_ARGS__,
|
2001-11-08 09:12:31 +08:00
|
|
|
|
|
|
|
/* special identifiers */
|
2001-11-14 06:54:35 +08:00
|
|
|
TOK___FUNC__,
|
2001-11-08 09:12:31 +08:00
|
|
|
TOK_MAIN,
|
|
|
|
};
|
|
|
|
|
2001-11-01 22:48:10 +08:00
|
|
|
void sum();
|
2001-12-05 08:45:08 +08:00
|
|
|
void next(void);
|
2001-11-11 10:53:01 +08:00
|
|
|
void next_nomacro();
|
2001-11-08 09:12:31 +08:00
|
|
|
int expr_const();
|
2001-11-01 22:48:10 +08:00
|
|
|
void expr_eq();
|
2001-12-05 08:45:08 +08:00
|
|
|
void expr(void);
|
|
|
|
void decl(int l);
|
2001-11-27 06:57:27 +08:00
|
|
|
void decl_initializer(int t, int c, int first, int size_only);
|
2001-12-02 22:47:48 +08:00
|
|
|
int decl_initializer_alloc(int t, int has_init);
|
2001-12-03 00:25:49 +08:00
|
|
|
int gv(void);
|
2001-11-06 09:20:20 +08:00
|
|
|
void move_reg();
|
|
|
|
void save_reg();
|
2001-12-03 05:24:43 +08:00
|
|
|
void vpush(void);
|
|
|
|
int get_reg(void);
|
2001-11-11 10:53:01 +08:00
|
|
|
void macro_subst(int **tok_str, int *tok_len,
|
|
|
|
Sym **nested_list, int *macro_str);
|
|
|
|
int save_reg_forced(int r);
|
2001-12-03 05:24:43 +08:00
|
|
|
void vstore(void);
|
2001-11-11 10:53:01 +08:00
|
|
|
int type_size(int t, int *a);
|
|
|
|
int pointed_type(int t);
|
|
|
|
int pointed_size(int t);
|
|
|
|
int ist(void);
|
2001-11-12 00:07:43 +08:00
|
|
|
int type_decl(int *v, int t, int td);
|
|
|
|
|
2001-12-07 07:39:24 +08:00
|
|
|
#ifdef CONFIG_TCC_STATIC
|
|
|
|
|
|
|
|
#define RTLD_LAZY 0x001
|
|
|
|
#define RTLD_NOW 0x002
|
|
|
|
#define RTLD_GLOBAL 0x100
|
|
|
|
|
2001-11-12 00:07:43 +08:00
|
|
|
/* dummy function for profiling */
|
|
|
|
void *dlopen(const char *filename, int flag)
|
|
|
|
{
|
2001-12-07 07:39:24 +08:00
|
|
|
return NULL;
|
2001-11-12 00:07:43 +08:00
|
|
|
}
|
2001-12-07 07:39:24 +08:00
|
|
|
|
2001-11-12 00:07:43 +08:00
|
|
|
const char *dlerror(void)
|
|
|
|
{
|
|
|
|
return "error";
|
|
|
|
}
|
|
|
|
|
2001-12-07 07:39:24 +08:00
|
|
|
typedef struct TCCSyms {
|
|
|
|
char *str;
|
|
|
|
void *ptr;
|
|
|
|
} TCCSyms;
|
|
|
|
|
|
|
|
#define TCCSYM(a) { #a, &a, },
|
|
|
|
|
|
|
|
/* add the symbol you want here if no dynamic linking is done */
|
|
|
|
static TCCSyms tcc_syms[] = {
|
|
|
|
TCCSYM(printf)
|
|
|
|
TCCSYM(fprintf)
|
|
|
|
TCCSYM(fopen)
|
|
|
|
TCCSYM(fclose)
|
|
|
|
{ NULL, NULL },
|
|
|
|
};
|
|
|
|
|
2001-11-12 00:07:43 +08:00
|
|
|
void *dlsym(void *handle, char *symbol)
|
|
|
|
{
|
2001-12-07 07:39:24 +08:00
|
|
|
TCCSyms *p;
|
|
|
|
p = tcc_syms;
|
|
|
|
while (p->str != NULL) {
|
|
|
|
if (!strcmp(p->str, symbol))
|
|
|
|
return p->ptr;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
return NULL;
|
2001-11-12 00:07:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2001-10-28 07:48:39 +08:00
|
|
|
|
2001-11-19 00:33:01 +08:00
|
|
|
inline int isid(c)
|
2001-10-28 07:48:39 +08:00
|
|
|
{
|
2001-11-19 00:33:01 +08:00
|
|
|
return (c >= 'a' && c <= 'z') ||
|
|
|
|
(c >= 'A' && c <= 'Z') ||
|
2001-10-28 07:48:39 +08:00
|
|
|
c == '_';
|
|
|
|
}
|
|
|
|
|
2001-11-19 00:33:01 +08:00
|
|
|
inline int isnum(c)
|
2001-10-28 07:48:39 +08:00
|
|
|
{
|
|
|
|
return c >= '0' & c <= '9';
|
|
|
|
}
|
|
|
|
|
2001-11-08 07:13:29 +08:00
|
|
|
void printline()
|
|
|
|
{
|
|
|
|
IncludeFile *f;
|
|
|
|
for(f = include_stack; f < include_stack_ptr; f++)
|
2001-11-12 00:07:43 +08:00
|
|
|
fprintf(stderr, "In file included from %s:%d:\n",
|
|
|
|
f->filename, f->line_num);
|
|
|
|
fprintf(stderr, "%s:%d: ", filename, line_num);
|
2001-11-08 07:13:29 +08:00
|
|
|
}
|
|
|
|
|
2001-11-12 00:07:43 +08:00
|
|
|
void error(const char *fmt, ...)
|
2001-10-28 23:20:12 +08:00
|
|
|
{
|
2001-11-12 00:07:43 +08:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2001-11-08 07:13:29 +08:00
|
|
|
printline();
|
2001-11-12 00:07:43 +08:00
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
fprintf(stderr, "\n");
|
2001-10-28 23:20:12 +08:00
|
|
|
exit(1);
|
2001-11-12 00:07:43 +08:00
|
|
|
va_end(ap);
|
2001-10-28 23:20:12 +08:00
|
|
|
}
|
|
|
|
|
2001-11-12 00:07:43 +08:00
|
|
|
void expect(const char *msg)
|
2001-11-01 22:48:10 +08:00
|
|
|
{
|
2001-11-12 00:07:43 +08:00
|
|
|
error("%s expected", msg);
|
2001-11-01 22:48:10 +08:00
|
|
|
}
|
|
|
|
|
2001-11-12 00:07:43 +08:00
|
|
|
void warning(const char *msg)
|
2001-10-28 23:20:12 +08:00
|
|
|
{
|
2001-11-08 07:13:29 +08:00
|
|
|
printline();
|
2001-11-12 00:07:43 +08:00
|
|
|
fprintf(stderr, "warning: %s\n", msg);
|
2001-10-28 23:20:12 +08:00
|
|
|
}
|
|
|
|
|
2001-10-28 07:48:39 +08:00
|
|
|
void skip(c)
|
|
|
|
{
|
2001-11-12 00:07:43 +08:00
|
|
|
if (tok != c)
|
|
|
|
error("'%c' expected", c);
|
2001-10-28 07:48:39 +08:00
|
|
|
next();
|
|
|
|
}
|
2001-10-28 23:20:12 +08:00
|
|
|
|
|
|
|
void test_lvalue()
|
|
|
|
{
|
|
|
|
if (!(vt & VT_LVAL))
|
2001-11-01 22:48:10 +08:00
|
|
|
expect("lvalue");
|
2001-10-28 23:20:12 +08:00
|
|
|
}
|
|
|
|
|
2001-11-11 10:53:01 +08:00
|
|
|
TokenSym *tok_alloc(char *str, int len)
|
|
|
|
{
|
2001-11-19 00:33:01 +08:00
|
|
|
TokenSym *ts, **pts, **ptable;
|
|
|
|
int h, i;
|
2001-11-11 10:53:01 +08:00
|
|
|
|
2001-11-14 06:54:35 +08:00
|
|
|
if (len <= 0)
|
|
|
|
len = strlen(str);
|
2001-11-19 00:33:01 +08:00
|
|
|
h = 1;
|
|
|
|
for(i=0;i<len;i++)
|
|
|
|
h = ((h << 8) | (str[i] & 0xff)) % TOK_HASH_SIZE;
|
2001-12-09 04:24:33 +08:00
|
|
|
|
2001-11-19 00:33:01 +08:00
|
|
|
pts = &hash_ident[h];
|
2001-11-11 10:53:01 +08:00
|
|
|
while (1) {
|
|
|
|
ts = *pts;
|
|
|
|
if (!ts)
|
|
|
|
break;
|
|
|
|
if (ts->len == len && !memcmp(ts->str, str, len))
|
|
|
|
return ts;
|
2001-11-19 00:33:01 +08:00
|
|
|
pts = &(ts->hash_next);
|
|
|
|
}
|
|
|
|
/* expand token table if needed */
|
|
|
|
i = tok_ident - TOK_IDENT;
|
|
|
|
if ((i % TOK_ALLOC_INCR) == 0) {
|
|
|
|
ptable = realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
|
|
|
|
if (!ptable)
|
|
|
|
error("memory full");
|
|
|
|
table_ident = ptable;
|
2001-11-11 10:53:01 +08:00
|
|
|
}
|
|
|
|
ts = malloc(sizeof(TokenSym) + len);
|
2001-12-09 04:24:33 +08:00
|
|
|
if (!ts || tok_ident >= SYM_FIRST_ANOM)
|
2001-11-11 10:53:01 +08:00
|
|
|
error("memory full");
|
2001-11-19 00:33:01 +08:00
|
|
|
table_ident[i] = ts;
|
|
|
|
ts->tok = tok_ident++;
|
2001-11-11 10:53:01 +08:00
|
|
|
ts->len = len;
|
2001-11-19 00:33:01 +08:00
|
|
|
ts->hash_next = NULL;
|
2001-11-11 10:53:01 +08:00
|
|
|
memcpy(ts->str, str, len + 1);
|
|
|
|
*pts = ts;
|
|
|
|
return ts;
|
|
|
|
}
|
2001-10-28 23:20:12 +08:00
|
|
|
|
2001-11-11 10:53:01 +08:00
|
|
|
void add_char(char **pp, int c)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
p = *pp;
|
|
|
|
if (c == '\'' || c == '\"' || c == '\\') {
|
|
|
|
/* XXX: could be more precise if char or string */
|
|
|
|
*p++ = '\\';
|
|
|
|
}
|
|
|
|
if (c >= 32 && c <= 126) {
|
|
|
|
*p++ = c;
|
|
|
|
} else {
|
|
|
|
*p++ = '\\';
|
|
|
|
if (c == '\n') {
|
|
|
|
*p++ = 'n';
|
|
|
|
} else {
|
|
|
|
*p++ = '0' + ((c >> 6) & 7);
|
|
|
|
*p++ = '0' + ((c >> 3) & 7);
|
|
|
|
*p++ = '0' + (c & 7);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*pp = p;
|
|
|
|
}
|
2001-10-28 07:48:39 +08:00
|
|
|
|
2001-11-11 10:53:01 +08:00
|
|
|
/* XXX: buffer overflow */
|
|
|
|
char *get_tok_str(int v, int c)
|
2001-10-30 07:23:01 +08:00
|
|
|
{
|
2001-11-11 10:53:01 +08:00
|
|
|
static char buf[STRING_MAX_SIZE + 1];
|
|
|
|
TokenSym *ts;
|
2001-10-30 07:23:01 +08:00
|
|
|
char *p;
|
2001-11-11 10:53:01 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (v == TOK_NUM) {
|
|
|
|
sprintf(buf, "%d", c);
|
|
|
|
return buf;
|
2001-11-29 06:48:43 +08:00
|
|
|
} else if (v == TOK_CCHAR || v == TOK_LCHAR) {
|
2001-11-11 10:53:01 +08:00
|
|
|
p = buf;
|
|
|
|
*p++ = '\'';
|
|
|
|
add_char(&p, c);
|
|
|
|
*p++ = '\'';
|
|
|
|
*p = '\0';
|
|
|
|
return buf;
|
2001-11-29 06:48:43 +08:00
|
|
|
} else if (v == TOK_STR || v == TOK_LSTR) {
|
2001-11-11 10:53:01 +08:00
|
|
|
ts = (TokenSym *)c;
|
|
|
|
p = buf;
|
|
|
|
*p++ = '\"';
|
|
|
|
for(i=0;i<ts->len;i++)
|
|
|
|
add_char(&p, ts->str[i]);
|
|
|
|
*p++ = '\"';
|
|
|
|
*p = '\0';
|
|
|
|
return buf;
|
|
|
|
} else if (v < TOK_IDENT) {
|
|
|
|
p = buf;
|
|
|
|
*p++ = v;
|
|
|
|
*p = '\0';
|
|
|
|
return buf;
|
2001-11-19 00:33:01 +08:00
|
|
|
} else if (v < tok_ident) {
|
|
|
|
return table_ident[v - TOK_IDENT]->str;
|
2001-11-11 10:53:01 +08:00
|
|
|
} else {
|
2001-11-19 00:33:01 +08:00
|
|
|
/* should never happen */
|
2001-11-11 10:53:01 +08:00
|
|
|
return NULL;
|
2001-10-30 07:23:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-19 00:33:01 +08:00
|
|
|
/* push, without hashing */
|
|
|
|
Sym *sym_push2(Sym **ps, int v, int t, int c)
|
|
|
|
{
|
|
|
|
Sym *s;
|
|
|
|
s = malloc(sizeof(Sym));
|
|
|
|
if (!s)
|
|
|
|
error("memory full");
|
|
|
|
s->v = v;
|
|
|
|
s->t = t;
|
|
|
|
s->c = c;
|
|
|
|
s->next = NULL;
|
|
|
|
/* add in stack */
|
|
|
|
s->prev = *ps;
|
|
|
|
*ps = s;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2001-11-01 22:48:10 +08:00
|
|
|
/* find a symbol and return its associated structure. 's' is the top
|
|
|
|
of the symbol stack */
|
2001-11-19 00:33:01 +08:00
|
|
|
Sym *sym_find2(Sym *s, int v)
|
2001-11-01 22:48:10 +08:00
|
|
|
{
|
|
|
|
while (s) {
|
|
|
|
if (s->v == v)
|
|
|
|
return s;
|
|
|
|
s = s->prev;
|
|
|
|
}
|
2001-11-19 00:33:01 +08:00
|
|
|
return NULL;
|
2001-11-01 22:48:10 +08:00
|
|
|
}
|
|
|
|
|
2001-11-19 00:33:01 +08:00
|
|
|
/* find a symbol and return its associated structure. 'st' is the
|
|
|
|
symbol stack */
|
|
|
|
Sym *sym_find1(SymStack *st, int v)
|
2001-11-01 22:48:10 +08:00
|
|
|
{
|
|
|
|
Sym *s;
|
2001-11-19 00:33:01 +08:00
|
|
|
|
|
|
|
s = st->hash[v % SYM_HASH_SIZE];
|
|
|
|
while (s) {
|
|
|
|
if (s->v == v)
|
|
|
|
return s;
|
|
|
|
s = s->hash_next;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sym *sym_push1(SymStack *st, int v, int t, int c)
|
|
|
|
{
|
|
|
|
Sym *s, **ps;
|
|
|
|
s = sym_push2(&st->top, v, t, c);
|
|
|
|
/* add in hash table */
|
|
|
|
ps = &st->hash[s->v % SYM_HASH_SIZE];
|
|
|
|
s->hash_next = *ps;
|
2001-11-01 22:48:10 +08:00
|
|
|
*ps = s;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find a symbol in the right symbol space */
|
|
|
|
Sym *sym_find(int v)
|
|
|
|
{
|
|
|
|
Sym *s;
|
2001-11-19 00:33:01 +08:00
|
|
|
s = sym_find1(&local_stack, v);
|
2001-11-01 22:48:10 +08:00
|
|
|
if (!s)
|
2001-11-19 00:33:01 +08:00
|
|
|
s = sym_find1(&global_stack, v);
|
2001-11-01 22:48:10 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* push a given symbol on the symbol stack */
|
|
|
|
Sym *sym_push(int v, int t, int c)
|
|
|
|
{
|
2001-11-19 00:33:01 +08:00
|
|
|
if (local_stack.top)
|
2001-11-01 22:48:10 +08:00
|
|
|
return sym_push1(&local_stack, v, t, c);
|
|
|
|
else
|
|
|
|
return sym_push1(&global_stack, v, t, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pop symbols until top reaches 'b' */
|
2001-11-19 00:33:01 +08:00
|
|
|
void sym_pop(SymStack *st, Sym *b)
|
2001-11-01 22:48:10 +08:00
|
|
|
{
|
|
|
|
Sym *s, *ss;
|
|
|
|
|
2001-11-19 00:33:01 +08:00
|
|
|
s = st->top;
|
2001-11-01 22:48:10 +08:00
|
|
|
while(s != b) {
|
|
|
|
ss = s->prev;
|
2001-11-19 00:33:01 +08:00
|
|
|
/* free hash table entry */
|
|
|
|
st->hash[s->v % SYM_HASH_SIZE] = s->hash_next;
|
2001-11-01 22:48:10 +08:00
|
|
|
free(s);
|
|
|
|
s = ss;
|
|
|
|
}
|
2001-11-19 00:33:01 +08:00
|
|
|
st->top = b;
|
2001-11-01 22:48:10 +08:00
|
|
|
}
|
|
|
|
|
2001-12-08 23:04:01 +08:00
|
|
|
/* undefined a hashed symbol (used for #undef). Its name is set to
|
|
|
|
zero */
|
|
|
|
void sym_undef(SymStack *st, Sym *s)
|
|
|
|
{
|
|
|
|
Sym **ss;
|
|
|
|
ss = &st->hash[s->v % SYM_HASH_SIZE];
|
|
|
|
while (*ss != NULL) {
|
|
|
|
if (*ss == s)
|
|
|
|
break;
|
|
|
|
ss = &(*ss)->hash_next;
|
|
|
|
}
|
|
|
|
*ss = s->hash_next;
|
|
|
|
s->v = 0;
|
|
|
|
}
|
|
|
|
|
2001-12-07 07:57:36 +08:00
|
|
|
/* no need to put that inline */
|
|
|
|
int handle_eof(void)
|
|
|
|
{
|
|
|
|
if (include_stack_ptr == include_stack)
|
|
|
|
return -1;
|
|
|
|
/* pop include stack */
|
|
|
|
fclose(file);
|
|
|
|
free(filename);
|
|
|
|
include_stack_ptr--;
|
|
|
|
file = include_stack_ptr->file;
|
|
|
|
filename = include_stack_ptr->filename;
|
|
|
|
line_num = include_stack_ptr->line_num;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-11-06 09:20:20 +08:00
|
|
|
/* read next char from current input file */
|
2001-12-07 07:57:36 +08:00
|
|
|
static inline void inp(void)
|
2001-11-06 09:20:20 +08:00
|
|
|
{
|
|
|
|
redo:
|
2001-12-07 07:39:24 +08:00
|
|
|
/* faster than fgetc */
|
|
|
|
ch1 = getc_unlocked(file);
|
2001-11-06 09:20:20 +08:00
|
|
|
if (ch1 == -1) {
|
2001-12-07 07:57:36 +08:00
|
|
|
if (handle_eof() < 0)
|
2001-11-06 09:20:20 +08:00
|
|
|
return;
|
2001-12-07 07:57:36 +08:00
|
|
|
else
|
|
|
|
goto redo;
|
2001-11-06 09:20:20 +08:00
|
|
|
}
|
|
|
|
if (ch1 == '\n')
|
|
|
|
line_num++;
|
2001-11-08 09:12:31 +08:00
|
|
|
// printf("ch1=%c 0x%x\n", ch1, ch1);
|
2001-11-06 09:20:20 +08:00
|
|
|
}
|
|
|
|
|
2001-11-11 10:53:01 +08:00
|
|
|
/* input with '\\n' handling */
|
2001-12-07 07:57:36 +08:00
|
|
|
static inline void minp(void)
|
2001-11-06 09:20:20 +08:00
|
|
|
{
|
2001-12-07 07:57:36 +08:00
|
|
|
redo:
|
2001-11-11 10:53:01 +08:00
|
|
|
ch = ch1;
|
|
|
|
inp();
|
|
|
|
if (ch == '\\' && ch1 == '\n') {
|
2001-11-06 09:20:20 +08:00
|
|
|
inp();
|
2001-11-11 10:53:01 +08:00
|
|
|
goto redo;
|
2001-11-06 09:20:20 +08:00
|
|
|
}
|
2001-11-11 10:53:01 +08:00
|
|
|
//printf("ch=%c 0x%x\n", ch, ch);
|
2001-11-06 09:20:20 +08:00
|
|
|
}
|
|
|
|
|
2001-12-07 07:57:36 +08:00
|
|
|
|
2001-11-06 09:20:20 +08:00
|
|
|
/* same as minp, but also skip comments */
|
2001-12-07 07:57:36 +08:00
|
|
|
void cinp(void)
|
2001-11-06 09:20:20 +08:00
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
if (ch1 == '/') {
|
|
|
|
inp();
|
|
|
|
if (ch1 == '/') {
|
|
|
|
/* single line C++ comments */
|
|
|
|
inp();
|
|
|
|
while (ch1 != '\n' && ch1 != -1)
|
|
|
|
inp();
|
|
|
|
inp();
|
|
|
|
ch = ' '; /* return space */
|
|
|
|
} else if (ch1 == '*') {
|
|
|
|
/* C comments */
|
|
|
|
inp();
|
|
|
|
while (ch1 != -1) {
|
|
|
|
c = ch1;
|
|
|
|
inp();
|
2001-12-07 07:57:36 +08:00
|
|
|
if (c == '*' && ch1 == '/') {
|
|
|
|
inp();
|
|
|
|
ch = ' '; /* return space */
|
|
|
|
break;
|
|
|
|
}
|
2001-11-06 09:20:20 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ch = '/';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
minp();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-07 07:57:36 +08:00
|
|
|
void skip_spaces(void)
|
2001-11-06 09:20:20 +08:00
|
|
|
{
|
|
|
|
while (ch == ' ' || ch == '\t')
|
|
|
|
cinp();
|
|
|
|
}
|
|
|
|
|
2001-11-08 09:12:31 +08:00
|
|
|
/* skip block of text until #else, #elif or #endif. skip also pairs of
|
|
|
|
#if/#endif */
|
|
|
|
void preprocess_skip()
|
|
|
|
{
|
|
|
|
int a;
|
|
|
|
a = 0;
|
|
|
|
while (1) {
|
|
|
|
while (ch != '\n') {
|
|
|
|
if (ch == -1)
|
|
|
|
expect("#endif");
|
|
|
|
cinp();
|
|
|
|
}
|
|
|
|
cinp();
|
|
|
|
skip_spaces();
|
|
|
|
if (ch == '#') {
|
|
|
|
cinp();
|
2001-11-11 10:53:01 +08:00
|
|
|
next_nomacro();
|
2001-11-08 09:12:31 +08:00
|
|
|
if (a == 0 &&
|
|
|
|
(tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
|
|
|
|
break;
|
|
|
|
if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
|
|
|
|
a++;
|
|
|
|
else if (tok == TOK_ENDIF)
|
|
|
|
a--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-29 06:48:43 +08:00
|
|
|
inline int is_long_tok(int t)
|
|
|
|
{
|
|
|
|
return (t == TOK_NUM ||
|
|
|
|
t == TOK_CCHAR || t == TOK_LCHAR ||
|
|
|
|
t == TOK_STR || t == TOK_LSTR);
|
|
|
|
}
|
|
|
|
|
2001-11-11 10:53:01 +08:00
|
|
|
void tok_add(int **tok_str, int *tok_len, int t)
|
2001-11-08 09:12:31 +08:00
|
|
|
{
|
2001-11-11 10:53:01 +08:00
|
|
|
int len, *str;
|
|
|
|
len = *tok_len;
|
|
|
|
str = *tok_str;
|
|
|
|
if ((len & 63) == 0) {
|
|
|
|
str = realloc(str, (len + 64) * sizeof(int));
|
|
|
|
if (!str)
|
|
|
|
return;
|
|
|
|
*tok_str = str;
|
|
|
|
}
|
|
|
|
str[len++] = t;
|
|
|
|
*tok_len = len;
|
2001-11-08 09:12:31 +08:00
|
|
|
}
|
|
|
|
|
2001-11-11 10:53:01 +08:00
|
|
|
void tok_add2(int **tok_str, int *tok_len, int t, int c)
|
2001-11-08 09:12:31 +08:00
|
|
|
{
|
2001-11-11 10:53:01 +08:00
|
|
|
tok_add(tok_str, tok_len, t);
|
2001-11-29 06:48:43 +08:00
|
|
|
if (is_long_tok(t))
|
2001-11-11 10:53:01 +08:00
|
|
|
tok_add(tok_str, tok_len, c);
|
|
|
|
}
|
2001-11-08 09:12:31 +08:00
|
|
|
|
2001-11-11 10:53:01 +08:00
|
|
|
/* eval an expression for #if/#elif */
|
|
|
|
int expr_preprocess()
|
|
|
|
{
|
|
|
|
int *str, len, c, t;
|
|
|
|
|
|
|
|
str = NULL;
|
|
|
|
len = 0;
|
|
|
|
while (1) {
|
|
|
|
skip_spaces();
|
|
|
|
if (ch == '\n')
|
|
|
|
break;
|
|
|
|
next(); /* do macro subst */
|
|
|
|
if (tok == TOK_DEFINED) {
|
|
|
|
next_nomacro();
|
|
|
|
t = tok;
|
|
|
|
if (t == '(')
|
|
|
|
next_nomacro();
|
2001-11-19 00:33:01 +08:00
|
|
|
c = sym_find1(&define_stack, tok) != 0;
|
2001-11-11 10:53:01 +08:00
|
|
|
if (t == '(')
|
|
|
|
next_nomacro();
|
|
|
|
tok = TOK_NUM;
|
|
|
|
tokc = c;
|
|
|
|
} else if (tok >= TOK_IDENT) {
|
|
|
|
/* if undefined macro */
|
|
|
|
tok = TOK_NUM;
|
|
|
|
tokc = 0;
|
|
|
|
}
|
|
|
|
tok_add2(&str, &len, tok, tokc);
|
|
|
|
}
|
|
|
|
tok_add(&str, &len, -1); /* simulate end of file */
|
|
|
|
tok_add(&str, &len, 0);
|
|
|
|
/* now evaluate C constant expression */
|
|
|
|
macro_ptr = str;
|
2001-11-08 09:12:31 +08:00
|
|
|
next();
|
|
|
|
c = expr_const();
|
2001-11-12 00:07:43 +08:00
|
|
|
macro_ptr = NULL;
|
2001-11-11 10:53:01 +08:00
|
|
|
free(str);
|
2001-11-08 09:12:31 +08:00
|
|
|
return c != 0;
|
|
|
|
}
|
|
|
|
|
2001-11-11 10:53:01 +08:00
|
|
|
#ifdef DEBUG
|
|
|
|
void tok_print(int *str)
|
|
|
|
{
|
|
|
|
int t, c;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
t = *str++;
|
|
|
|
if (!t)
|
|
|
|
break;
|
|
|
|
c = 0;
|
2001-11-29 06:48:43 +08:00
|
|
|
if (is_long_tok(t))
|
2001-11-11 10:53:01 +08:00
|
|
|
c = *str++;
|
|
|
|
printf(" %s", get_tok_str(t, c));
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-11-12 00:07:43 +08:00
|
|
|
/* XXX: should be more factorized */
|
|
|
|
void define_symbol(char *sym)
|
|
|
|
{
|
|
|
|
TokenSym *ts;
|
|
|
|
int *str, len;
|
|
|
|
|
2001-11-14 06:54:35 +08:00
|
|
|
ts = tok_alloc(sym, 0);
|
2001-11-12 00:07:43 +08:00
|
|
|
str = NULL;
|
|
|
|
len = 0;
|
|
|
|
tok_add2(&str, &len, TOK_NUM, 1);
|
|
|
|
tok_add(&str, &len, 0);
|
|
|
|
sym_push1(&define_stack, ts->tok, MACRO_OBJ, (int)str);
|
|
|
|
}
|
|
|
|
|
2001-11-06 09:20:20 +08:00
|
|
|
void preprocess()
|
|
|
|
{
|
2001-11-11 10:53:01 +08:00
|
|
|
int size, i, c, v, t, *str, len;
|
2001-11-06 09:20:20 +08:00
|
|
|
char buf[1024], *q, *p;
|
|
|
|
char buf1[1024];
|
|
|
|
FILE *f;
|
2001-11-11 10:53:01 +08:00
|
|
|
Sym **ps, *first, *s;
|
2001-11-06 09:20:20 +08:00
|
|
|
|
|
|
|
cinp();
|
2001-11-08 09:26:37 +08:00
|
|
|
next_nomacro();
|
2001-11-08 09:12:31 +08:00
|
|
|
redo:
|
2001-11-06 09:20:20 +08:00
|
|
|
if (tok == TOK_DEFINE) {
|
2001-11-11 10:53:01 +08:00
|
|
|
next_nomacro();
|
|
|
|
v = tok;
|
|
|
|
/* XXX: should check if same macro (ANSI) */
|
|
|
|
first = NULL;
|
|
|
|
t = MACRO_OBJ;
|
|
|
|
/* '(' must be just after macro definition for MACRO_FUNC */
|
|
|
|
if (ch == '(') {
|
|
|
|
next_nomacro();
|
|
|
|
next_nomacro();
|
|
|
|
ps = &first;
|
|
|
|
while (tok != ')') {
|
2001-11-18 01:22:38 +08:00
|
|
|
if (tok == TOK_DOTS)
|
|
|
|
tok = TOK___VA_ARGS__;
|
2001-11-11 10:53:01 +08:00
|
|
|
s = sym_push1(&define_stack, tok | SYM_FIELD, 0, 0);
|
|
|
|
*ps = s;
|
|
|
|
ps = &s->next;
|
|
|
|
next_nomacro();
|
|
|
|
if (tok != ',')
|
|
|
|
break;
|
|
|
|
next_nomacro();
|
|
|
|
}
|
|
|
|
t = MACRO_FUNC;
|
|
|
|
}
|
|
|
|
str = NULL;
|
|
|
|
len = 0;
|
|
|
|
while (1) {
|
|
|
|
skip_spaces();
|
|
|
|
if (ch == '\n' || ch == -1)
|
|
|
|
break;
|
|
|
|
next_nomacro();
|
|
|
|
tok_add2(&str, &len, tok, tokc);
|
|
|
|
}
|
|
|
|
tok_add(&str, &len, 0);
|
|
|
|
#ifdef PP_DEBUG
|
|
|
|
printf("define %s %d: ", get_tok_str(v, 0), t);
|
|
|
|
tok_print(str);
|
|
|
|
#endif
|
|
|
|
s = sym_push1(&define_stack, v, t, (int)str);
|
|
|
|
s->next = first;
|
|
|
|
} else if (tok == TOK_UNDEF) {
|
|
|
|
next_nomacro();
|
2001-11-19 00:33:01 +08:00
|
|
|
s = sym_find1(&define_stack, tok);
|
2001-11-11 10:53:01 +08:00
|
|
|
/* undefine symbol by putting an invalid name */
|
|
|
|
if (s)
|
2001-12-08 23:04:01 +08:00
|
|
|
sym_undef(&define_stack, s);
|
2001-11-06 09:20:20 +08:00
|
|
|
} else if (tok == TOK_INCLUDE) {
|
|
|
|
skip_spaces();
|
|
|
|
if (ch == '<') {
|
|
|
|
c = '>';
|
|
|
|
goto read_name;
|
|
|
|
} else if (ch == '\"') {
|
|
|
|
c = ch;
|
|
|
|
read_name:
|
|
|
|
minp();
|
|
|
|
q = buf;
|
|
|
|
while (ch != c && ch != '\n' && ch != -1) {
|
|
|
|
if ((q - buf) < sizeof(buf) - 1)
|
|
|
|
*q++ = ch;
|
|
|
|
minp();
|
|
|
|
}
|
|
|
|
*q = '\0';
|
2001-11-18 01:22:38 +08:00
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
if (tok != TOK_STR)
|
|
|
|
error("#include syntax error");
|
|
|
|
/* XXX: buffer overflow */
|
|
|
|
strcpy(buf, get_tok_str(tok, tokc));
|
|
|
|
c = '\"';
|
2001-11-06 09:20:20 +08:00
|
|
|
}
|
2001-11-18 01:22:38 +08:00
|
|
|
if (include_stack_ptr >= include_stack + INCLUDE_STACK_SIZE)
|
|
|
|
error("memory full");
|
|
|
|
if (c == '\"') {
|
|
|
|
/* first search in current dir if "header.h" */
|
|
|
|
/* XXX: buffer overflow */
|
|
|
|
size = 0;
|
|
|
|
p = strrchr(filename, '/');
|
|
|
|
if (p)
|
|
|
|
size = p + 1 - filename;
|
|
|
|
memcpy(buf1, filename, size);
|
|
|
|
buf1[size] = '\0';
|
|
|
|
strcat(buf1, buf);
|
|
|
|
f = fopen(buf1, "r");
|
|
|
|
if (f)
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
/* now search in standard include path */
|
|
|
|
for(i=nb_include_paths - 1;i>=0;i--) {
|
|
|
|
strcpy(buf1, include_paths[i]);
|
|
|
|
strcat(buf1, "/");
|
|
|
|
strcat(buf1, buf);
|
|
|
|
f = fopen(buf1, "r");
|
|
|
|
if (f)
|
|
|
|
goto found;
|
|
|
|
}
|
2001-12-08 23:04:01 +08:00
|
|
|
error("include file '%s' not found", buf1);
|
2001-11-18 01:22:38 +08:00
|
|
|
f = NULL;
|
|
|
|
found:
|
|
|
|
/* push current file in stack */
|
|
|
|
/* XXX: fix current line init */
|
|
|
|
include_stack_ptr->file = file;
|
|
|
|
include_stack_ptr->filename = filename;
|
|
|
|
include_stack_ptr->line_num = line_num;
|
|
|
|
include_stack_ptr++;
|
|
|
|
file = f;
|
|
|
|
filename = strdup(buf1);
|
|
|
|
line_num = 1;
|
2001-11-08 09:12:31 +08:00
|
|
|
} else if (tok == TOK_IFNDEF) {
|
2001-11-08 09:26:37 +08:00
|
|
|
c = 1;
|
2001-11-08 09:12:31 +08:00
|
|
|
goto do_ifdef;
|
|
|
|
} else if (tok == TOK_IF) {
|
|
|
|
c = expr_preprocess();
|
2001-11-08 09:26:37 +08:00
|
|
|
goto do_if;
|
2001-11-08 09:12:31 +08:00
|
|
|
} else if (tok == TOK_IFDEF) {
|
2001-11-08 09:26:37 +08:00
|
|
|
c = 0;
|
2001-11-08 09:12:31 +08:00
|
|
|
do_ifdef:
|
2001-11-08 09:26:37 +08:00
|
|
|
next_nomacro();
|
2001-11-19 00:33:01 +08:00
|
|
|
c = (sym_find1(&define_stack, tok) != 0) ^ c;
|
2001-11-08 09:26:37 +08:00
|
|
|
do_if:
|
2001-11-08 09:12:31 +08:00
|
|
|
if (ifdef_stack_ptr >= ifdef_stack + IFDEF_STACK_SIZE)
|
|
|
|
error("memory full");
|
|
|
|
*ifdef_stack_ptr++ = c;
|
|
|
|
goto test_skip;
|
|
|
|
} else if (tok == TOK_ELSE) {
|
|
|
|
if (ifdef_stack_ptr == ifdef_stack ||
|
|
|
|
(ifdef_stack_ptr[-1] & 2))
|
|
|
|
error("#else after #else");
|
|
|
|
c = (ifdef_stack_ptr[-1] ^= 3);
|
|
|
|
goto test_skip;
|
|
|
|
} else if (tok == TOK_ELIF) {
|
|
|
|
if (ifdef_stack_ptr == ifdef_stack ||
|
|
|
|
ifdef_stack_ptr[-1] > 1)
|
|
|
|
error("#elif after #else");
|
|
|
|
c = expr_preprocess();
|
|
|
|
ifdef_stack_ptr[-1] = c;
|
|
|
|
test_skip:
|
|
|
|
if (!(c & 1)) {
|
|
|
|
preprocess_skip();
|
|
|
|
goto redo;
|
|
|
|
}
|
|
|
|
} else if (tok == TOK_ENDIF) {
|
|
|
|
if (ifdef_stack_ptr == ifdef_stack)
|
|
|
|
expect("#if");
|
|
|
|
ifdef_stack_ptr--;
|
2001-11-18 01:22:38 +08:00
|
|
|
} else if (tok == TOK_LINE) {
|
|
|
|
next();
|
|
|
|
if (tok != TOK_NUM)
|
|
|
|
error("#line");
|
|
|
|
line_num = tokc;
|
|
|
|
skip_spaces();
|
|
|
|
if (ch != '\n') {
|
|
|
|
next();
|
|
|
|
if (tok != TOK_STR)
|
|
|
|
error("#line");
|
|
|
|
/* XXX: potential memory leak */
|
|
|
|
filename = strdup(get_tok_str(tok, tokc));
|
|
|
|
}
|
2001-11-14 06:54:35 +08:00
|
|
|
} else if (tok == TOK_ERROR) {
|
|
|
|
error("#error");
|
2001-11-06 09:20:20 +08:00
|
|
|
}
|
|
|
|
/* ignore other preprocess commands or #! for C scripts */
|
|
|
|
while (ch != '\n' && ch != -1)
|
|
|
|
cinp();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read a number in base b */
|
|
|
|
int getn(b)
|
|
|
|
{
|
|
|
|
int n, t;
|
|
|
|
n = 0;
|
|
|
|
while (1) {
|
|
|
|
if (ch >= 'a' & ch <= 'f')
|
|
|
|
t = ch - 'a' + 10;
|
|
|
|
else if (ch >= 'A' & ch <= 'F')
|
|
|
|
t = ch - 'A' + 10;
|
|
|
|
else if (isnum(ch))
|
|
|
|
t = ch - '0';
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
if (t < 0 | t >= b)
|
|
|
|
break;
|
|
|
|
n = n * b + t;
|
|
|
|
cinp();
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
2001-11-01 22:48:10 +08:00
|
|
|
|
2001-11-11 10:53:01 +08:00
|
|
|
/* read a character for string or char constant and eval escape codes */
|
|
|
|
int getq()
|
2001-10-28 07:48:39 +08:00
|
|
|
{
|
2001-11-11 10:53:01 +08:00
|
|
|
int c;
|
2001-10-28 07:48:39 +08:00
|
|
|
|
2001-11-11 10:53:01 +08:00
|
|
|
c = ch;
|
|
|
|
minp();
|
|
|
|
if (c == '\\') {
|
|
|
|
if (isnum(ch)) {
|
2001-11-18 01:22:38 +08:00
|
|
|
/* at most three octal digits */
|
|
|
|
c = ch - '0';
|
|
|
|
minp();
|
|
|
|
if (isnum(ch)) {
|
|
|
|
c = c * 8 + ch - '0';
|
|
|
|
minp();
|
|
|
|
if (isnum(ch)) {
|
|
|
|
c = c * 8 + ch - '0';
|
|
|
|
minp();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
} else if (ch == 'x') {
|
|
|
|
minp();
|
|
|
|
return getn(16);
|
2001-11-11 10:53:01 +08:00
|
|
|
} else {
|
|
|
|
if (ch == 'a')
|
|
|
|
c = '\a';
|
|
|
|
else if (ch == 'b')
|
|
|
|
c = '\b';
|
|
|
|
else if (ch == 'f')
|
|
|
|
c = '\f';
|
|
|
|
else if (ch == 'n')
|
|
|
|
c = '\n';
|
|
|
|
else if (ch == 'r')
|
|
|
|
c = '\r';
|
|
|
|
else if (ch == 't')
|
|
|
|
c = '\t';
|
|
|
|
else if (ch == 'v')
|
|
|
|
c = '\v';
|
|
|
|
else
|
|
|
|
c = ch;
|
|
|
|
minp();
|
|
|
|
}
|
2001-10-31 06:43:19 +08:00
|
|
|
}
|
2001-11-11 10:53:01 +08:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return next token without macro substitution */
|
|
|
|
void next_nomacro1()
|
|
|
|
{
|
|
|
|
int b;
|
|
|
|
char *q;
|
|
|
|
TokenSym *ts;
|
|
|
|
|
2001-11-06 09:20:20 +08:00
|
|
|
/* skip spaces */
|
2001-10-28 07:48:39 +08:00
|
|
|
while(1) {
|
2001-11-06 09:20:20 +08:00
|
|
|
while (ch == '\n') {
|
|
|
|
cinp();
|
|
|
|
while (ch == ' ' || ch == 9)
|
|
|
|
cinp();
|
|
|
|
if (ch == '#') {
|
|
|
|
/* preprocessor command if # at start of line after
|
|
|
|
spaces */
|
|
|
|
preprocess();
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
2001-10-28 23:20:12 +08:00
|
|
|
}
|
2001-11-11 10:53:01 +08:00
|
|
|
if (ch != ' ' && ch != '\t' && ch != '\f')
|
2001-10-28 07:48:39 +08:00
|
|
|
break;
|
2001-11-06 09:20:20 +08:00
|
|
|
cinp();
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
2001-11-06 09:20:20 +08:00
|
|
|
if (isid(ch)) {
|
2001-11-11 10:53:01 +08:00
|
|
|
q = token_buf;
|
2001-11-18 01:22:38 +08:00
|
|
|
*q++ = ch;
|
|
|
|
cinp();
|
|
|
|
if (q[-1] == 'L') {
|
2001-11-29 06:48:43 +08:00
|
|
|
if (ch == '\'') {
|
|
|
|
tok = TOK_LCHAR;
|
2001-11-18 01:22:38 +08:00
|
|
|
goto char_const;
|
2001-11-29 06:48:43 +08:00
|
|
|
}
|
|
|
|
if (ch == '\"') {
|
|
|
|
tok = TOK_LSTR;
|
2001-11-18 01:22:38 +08:00
|
|
|
goto str_const;
|
2001-11-29 06:48:43 +08:00
|
|
|
}
|
2001-11-18 01:22:38 +08:00
|
|
|
}
|
2001-11-11 10:53:01 +08:00
|
|
|
while (isid(ch) | isnum(ch)) {
|
|
|
|
if (q >= token_buf + STRING_MAX_SIZE)
|
|
|
|
error("ident too long");
|
2001-11-06 09:20:20 +08:00
|
|
|
*q++ = ch;
|
|
|
|
cinp();
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
2001-11-11 10:53:01 +08:00
|
|
|
*q = '\0';
|
|
|
|
ts = tok_alloc(token_buf, q - token_buf);
|
|
|
|
tok = ts->tok;
|
2001-11-06 09:20:20 +08:00
|
|
|
} else if (isnum(ch)) {
|
|
|
|
/* number */
|
|
|
|
b = 10;
|
|
|
|
if (ch == '0') {
|
|
|
|
cinp();
|
|
|
|
b = 8;
|
2001-11-11 10:53:01 +08:00
|
|
|
if (ch == 'x' || ch == 'X') {
|
2001-11-06 09:20:20 +08:00
|
|
|
cinp();
|
|
|
|
b = 16;
|
2001-11-18 01:22:38 +08:00
|
|
|
} else if (ch == 'b' || ch == 'B') {
|
|
|
|
cinp();
|
|
|
|
b = 2;
|
2001-11-06 09:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tokc = getn(b);
|
2001-11-11 10:53:01 +08:00
|
|
|
/* XXX: add unsigned constant support (ANSI) */
|
|
|
|
while (ch == 'L' || ch == 'l' || ch == 'U' || ch == 'u')
|
|
|
|
cinp();
|
2001-11-06 09:20:20 +08:00
|
|
|
tok = TOK_NUM;
|
2001-11-11 10:53:01 +08:00
|
|
|
} else if (ch == '\'') {
|
2001-11-29 06:48:43 +08:00
|
|
|
tok = TOK_CCHAR;
|
2001-11-18 01:22:38 +08:00
|
|
|
char_const:
|
2001-11-11 10:53:01 +08:00
|
|
|
minp();
|
|
|
|
tokc = getq();
|
|
|
|
if (ch != '\'')
|
|
|
|
expect("\'");
|
|
|
|
minp();
|
|
|
|
} else if (ch == '\"') {
|
2001-11-29 06:48:43 +08:00
|
|
|
tok = TOK_STR;
|
2001-11-18 01:22:38 +08:00
|
|
|
str_const:
|
2001-11-11 10:53:01 +08:00
|
|
|
minp();
|
|
|
|
q = token_buf;
|
|
|
|
while (ch != '\"') {
|
|
|
|
b = getq();
|
|
|
|
if (ch == -1)
|
|
|
|
error("unterminated string");
|
|
|
|
if (q >= token_buf + STRING_MAX_SIZE)
|
|
|
|
error("string too long");
|
|
|
|
*q++ = b;
|
|
|
|
}
|
|
|
|
*q = '\0';
|
|
|
|
tokc = (int)tok_alloc(token_buf, q - token_buf);
|
|
|
|
minp();
|
2001-10-28 07:48:39 +08:00
|
|
|
} else {
|
2001-11-11 10:53:01 +08:00
|
|
|
q = "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\247..\250##\266";
|
2001-10-28 07:48:39 +08:00
|
|
|
/* two chars */
|
2001-11-06 09:20:20 +08:00
|
|
|
tok = ch;
|
|
|
|
cinp();
|
2001-10-28 07:48:39 +08:00
|
|
|
while (*q) {
|
2001-11-06 09:20:20 +08:00
|
|
|
if (*q == tok & q[1] == ch) {
|
|
|
|
cinp();
|
2001-10-28 07:48:39 +08:00
|
|
|
tok = q[2] & 0xff;
|
2001-11-06 09:20:20 +08:00
|
|
|
/* three chars tests */
|
|
|
|
if (tok == TOK_SHL | tok == TOK_SAR) {
|
|
|
|
if (ch == '=') {
|
2001-10-29 06:24:16 +08:00
|
|
|
tok = tok | 0x80;
|
2001-11-06 09:20:20 +08:00
|
|
|
cinp();
|
|
|
|
}
|
|
|
|
} else if (tok == TOK_DOTS) {
|
|
|
|
if (ch != '.')
|
|
|
|
error("parse error");
|
|
|
|
cinp();
|
2001-10-29 06:24:16 +08:00
|
|
|
}
|
2001-10-28 07:48:39 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
q = q + 3;
|
|
|
|
}
|
|
|
|
/* single char substitutions */
|
2001-11-06 09:20:20 +08:00
|
|
|
if (tok == '<')
|
2001-10-28 07:48:39 +08:00
|
|
|
tok = TOK_LT;
|
2001-11-06 09:20:20 +08:00
|
|
|
else if (tok == '>')
|
2001-10-28 07:48:39 +08:00
|
|
|
tok = TOK_GT;
|
|
|
|
}
|
2001-11-11 10:53:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* return next token without macro substitution. Can read input from
|
|
|
|
macro_ptr buffer */
|
|
|
|
void next_nomacro()
|
|
|
|
{
|
|
|
|
if (macro_ptr) {
|
|
|
|
tok = *macro_ptr;
|
|
|
|
if (tok) {
|
|
|
|
macro_ptr++;
|
2001-11-29 06:48:43 +08:00
|
|
|
if (is_long_tok(tok))
|
2001-11-11 10:53:01 +08:00
|
|
|
tokc = *macro_ptr++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
next_nomacro1();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* substitute args in macro_str and return allocated string */
|
|
|
|
int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args)
|
|
|
|
{
|
|
|
|
int *st, last_tok, t, c, notfirst, *str, len;
|
|
|
|
Sym *s;
|
|
|
|
TokenSym *ts;
|
|
|
|
|
|
|
|
str = NULL;
|
|
|
|
len = 0;
|
|
|
|
last_tok = 0;
|
|
|
|
while(1) {
|
|
|
|
t = *macro_str++;
|
|
|
|
if (!t)
|
|
|
|
break;
|
|
|
|
if (t == '#') {
|
|
|
|
/* stringize */
|
|
|
|
t = *macro_str++;
|
|
|
|
if (!t)
|
|
|
|
break;
|
2001-11-19 00:33:01 +08:00
|
|
|
s = sym_find2(args, t);
|
2001-11-11 10:53:01 +08:00
|
|
|
if (s) {
|
|
|
|
token_buf[0] = '\0';
|
|
|
|
st = (int *)s->c;
|
|
|
|
/* XXX: buffer overflow */
|
|
|
|
notfirst = 0;
|
|
|
|
while (*st) {
|
|
|
|
if (notfirst)
|
|
|
|
strcat(token_buf, " ");
|
|
|
|
t = *st++;
|
|
|
|
c = 0;
|
2001-11-29 06:48:43 +08:00
|
|
|
if (is_long_tok(t))
|
2001-11-11 10:53:01 +08:00
|
|
|
c = *st++;
|
|
|
|
strcat(token_buf, get_tok_str(t, c));
|
|
|
|
notfirst = 1;
|
|
|
|
}
|
|
|
|
#ifdef PP_DEBUG
|
|
|
|
printf("stringize: %s\n", token_buf);
|
|
|
|
#endif
|
|
|
|
/* add string */
|
2001-11-14 06:54:35 +08:00
|
|
|
ts = tok_alloc(token_buf, 0);
|
2001-11-11 10:53:01 +08:00
|
|
|
tok_add2(&str, &len, TOK_STR, (int)ts);
|
|
|
|
} else {
|
|
|
|
tok_add(&str, &len, t);
|
|
|
|
}
|
2001-11-29 06:48:43 +08:00
|
|
|
} else if (is_long_tok(t)) {
|
2001-11-27 06:57:27 +08:00
|
|
|
tok_add2(&str, &len, t, *macro_str++);
|
2001-11-11 10:53:01 +08:00
|
|
|
} else {
|
2001-11-19 00:33:01 +08:00
|
|
|
s = sym_find2(args, t);
|
2001-11-11 10:53:01 +08:00
|
|
|
if (s) {
|
|
|
|
st = (int *)s->c;
|
|
|
|
/* if '##' is present before or after , no arg substitution */
|
|
|
|
if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
|
|
|
|
while (*st)
|
|
|
|
tok_add(&str, &len, *st++);
|
|
|
|
} else {
|
|
|
|
macro_subst(&str, &len, nested_list, st);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tok_add(&str, &len, t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
last_tok = t;
|
|
|
|
}
|
|
|
|
tok_add(&str, &len, 0);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handle the '##' operator */
|
|
|
|
int *macro_twosharps(int *macro_str)
|
|
|
|
{
|
|
|
|
TokenSym *ts;
|
|
|
|
int *macro_str1, macro_str1_len, *macro_ptr1;
|
|
|
|
int t, c;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
macro_str1 = NULL;
|
|
|
|
macro_str1_len = 0;
|
|
|
|
tok = 0;
|
|
|
|
while (1) {
|
|
|
|
next_nomacro();
|
|
|
|
if (tok == 0)
|
|
|
|
break;
|
|
|
|
if (*macro_ptr == TOK_TWOSHARPS) {
|
|
|
|
macro_ptr++;
|
|
|
|
macro_ptr1 = macro_ptr;
|
|
|
|
t = *macro_ptr;
|
|
|
|
if (t) {
|
|
|
|
macro_ptr++;
|
|
|
|
c = 0;
|
2001-11-29 06:48:43 +08:00
|
|
|
if (is_long_tok(t))
|
2001-11-11 10:53:01 +08:00
|
|
|
c = *macro_ptr++;
|
|
|
|
/* XXX: we handle only most common cases:
|
|
|
|
ident + ident or ident + number */
|
|
|
|
if (tok >= TOK_IDENT &&
|
|
|
|
(t >= TOK_IDENT || t == TOK_NUM)) {
|
|
|
|
/* XXX: buffer overflow */
|
|
|
|
p = get_tok_str(tok, tokc);
|
|
|
|
strcpy(token_buf, p);
|
|
|
|
p = get_tok_str(t, c);
|
|
|
|
strcat(token_buf, p);
|
2001-11-14 06:54:35 +08:00
|
|
|
ts = tok_alloc(token_buf, 0);
|
2001-11-11 10:53:01 +08:00
|
|
|
tok_add2(¯o_str1, ¯o_str1_len, ts->tok, 0);
|
|
|
|
} else {
|
|
|
|
/* cannot merge tokens: skip '##' */
|
|
|
|
macro_ptr = macro_ptr1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tok_add2(¯o_str1, ¯o_str1_len, tok, tokc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tok_add(¯o_str1, ¯o_str1_len, 0);
|
|
|
|
return macro_str1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* do macro substitution of macro_str and add result to
|
|
|
|
(tok_str,tok_len). If macro_str is NULL, then input stream token is
|
|
|
|
substituted. 'nested_list' is the list of all macros we got inside
|
|
|
|
to avoid recursing. */
|
|
|
|
void macro_subst(int **tok_str, int *tok_len,
|
|
|
|
Sym **nested_list, int *macro_str)
|
|
|
|
{
|
2001-11-19 00:33:01 +08:00
|
|
|
Sym *s, *args, *sa, *sa1;
|
2001-11-11 10:53:01 +08:00
|
|
|
int *str, parlevel, len, *mstr, t, *saved_macro_ptr;
|
|
|
|
int mstr_allocated, *macro_str1;
|
|
|
|
|
|
|
|
saved_macro_ptr = macro_ptr;
|
|
|
|
macro_ptr = macro_str;
|
|
|
|
macro_str1 = NULL;
|
|
|
|
if (macro_str) {
|
|
|
|
/* first scan for '##' operator handling */
|
|
|
|
macro_str1 = macro_twosharps(macro_str);
|
|
|
|
macro_ptr = macro_str1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
next_nomacro();
|
|
|
|
if (tok == 0)
|
|
|
|
break;
|
2001-11-14 06:54:35 +08:00
|
|
|
/* special macros */
|
|
|
|
if (tok == TOK___LINE__) {
|
|
|
|
tok_add2(tok_str, tok_len, TOK_NUM, line_num);
|
|
|
|
} else if (tok == TOK___FILE__) {
|
|
|
|
tok_add2(tok_str, tok_len, TOK_STR,
|
|
|
|
(int)tok_alloc(filename, 0));
|
|
|
|
} else if (tok == TOK___DATE__) {
|
|
|
|
tok_add2(tok_str, tok_len, TOK_STR,
|
|
|
|
(int)tok_alloc("Jan 1 1970", 0));
|
|
|
|
} else if (tok == TOK___TIME__) {
|
|
|
|
tok_add2(tok_str, tok_len, TOK_STR,
|
|
|
|
(int)tok_alloc("00:00:00", 0));
|
2001-11-19 00:33:01 +08:00
|
|
|
} else if ((s = sym_find1(&define_stack, tok)) != NULL) {
|
2001-11-14 06:54:35 +08:00
|
|
|
/* if symbol is a macro, prepare substitution */
|
2001-11-11 10:53:01 +08:00
|
|
|
/* if nested substitution, do nothing */
|
2001-11-19 00:33:01 +08:00
|
|
|
if (sym_find2(*nested_list, tok))
|
2001-11-11 10:53:01 +08:00
|
|
|
goto no_subst;
|
|
|
|
mstr = (int *)s->c;
|
|
|
|
mstr_allocated = 0;
|
|
|
|
if (s->t == MACRO_FUNC) {
|
|
|
|
/* NOTE: we do not use next_nomacro to avoid eating the
|
|
|
|
next token. XXX: find better solution */
|
|
|
|
if (macro_ptr) {
|
|
|
|
t = *macro_ptr;
|
|
|
|
} else {
|
|
|
|
while (ch == ' ' || ch == '\t' || ch == '\n')
|
|
|
|
cinp();
|
|
|
|
t = ch;
|
|
|
|
}
|
|
|
|
if (t != '(') /* no macro subst */
|
|
|
|
goto no_subst;
|
|
|
|
|
|
|
|
/* argument macro */
|
|
|
|
next_nomacro();
|
|
|
|
next_nomacro();
|
|
|
|
args = NULL;
|
|
|
|
sa = s->next;
|
|
|
|
while (tok != ')' && sa) {
|
|
|
|
len = 0;
|
|
|
|
str = NULL;
|
|
|
|
parlevel = 0;
|
2001-11-18 01:22:38 +08:00
|
|
|
while ((parlevel > 0 ||
|
|
|
|
(tok != ')' &&
|
|
|
|
(tok != ',' ||
|
|
|
|
sa->v == (TOK___VA_ARGS__ | SYM_FIELD)))) &&
|
2001-11-11 10:53:01 +08:00
|
|
|
tok != -1) {
|
|
|
|
if (tok == '(')
|
|
|
|
parlevel++;
|
|
|
|
else if (tok == ')')
|
|
|
|
parlevel--;
|
|
|
|
tok_add2(&str, &len, tok, tokc);
|
|
|
|
next_nomacro();
|
|
|
|
}
|
|
|
|
tok_add(&str, &len, 0);
|
2001-11-19 00:33:01 +08:00
|
|
|
sym_push2(&args, sa->v & ~SYM_FIELD, 0, (int)str);
|
2001-11-11 10:53:01 +08:00
|
|
|
if (tok != ',')
|
|
|
|
break;
|
|
|
|
next_nomacro();
|
|
|
|
sa = sa->next;
|
|
|
|
}
|
|
|
|
if (tok != ')')
|
|
|
|
expect(")");
|
|
|
|
/* now subst each arg */
|
|
|
|
mstr = macro_arg_subst(nested_list, mstr, args);
|
2001-11-12 02:00:58 +08:00
|
|
|
/* free memory */
|
|
|
|
sa = args;
|
|
|
|
while (sa) {
|
2001-11-19 00:33:01 +08:00
|
|
|
sa1 = sa->prev;
|
2001-11-12 02:00:58 +08:00
|
|
|
free((int *)sa->c);
|
2001-11-19 00:33:01 +08:00
|
|
|
free(sa);
|
|
|
|
sa = sa1;
|
2001-11-12 02:00:58 +08:00
|
|
|
}
|
2001-11-11 10:53:01 +08:00
|
|
|
mstr_allocated = 1;
|
|
|
|
}
|
2001-11-19 00:33:01 +08:00
|
|
|
sym_push2(nested_list, s->v, 0, 0);
|
2001-11-11 10:53:01 +08:00
|
|
|
macro_subst(tok_str, tok_len, nested_list, mstr);
|
2001-11-19 00:33:01 +08:00
|
|
|
/* pop nested defined symbol */
|
|
|
|
sa1 = *nested_list;
|
|
|
|
*nested_list = sa1->prev;
|
|
|
|
free(sa1);
|
2001-11-11 10:53:01 +08:00
|
|
|
if (mstr_allocated)
|
|
|
|
free(mstr);
|
|
|
|
} else {
|
|
|
|
no_subst:
|
|
|
|
/* no need to add if reading input stream */
|
|
|
|
if (!macro_str)
|
|
|
|
return;
|
|
|
|
tok_add2(tok_str, tok_len, tok, tokc);
|
|
|
|
}
|
2001-11-14 06:54:35 +08:00
|
|
|
/* only replace one macro while parsing input stream */
|
|
|
|
if (!macro_str)
|
|
|
|
return;
|
2001-11-11 10:53:01 +08:00
|
|
|
}
|
|
|
|
macro_ptr = saved_macro_ptr;
|
|
|
|
if (macro_str1)
|
|
|
|
free(macro_str1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return next token with macro substitution */
|
|
|
|
void next()
|
|
|
|
{
|
|
|
|
int len, *ptr;
|
|
|
|
Sym *nested_list;
|
|
|
|
|
|
|
|
/* special 'ungettok' case for label parsing */
|
|
|
|
if (tok1) {
|
|
|
|
tok = tok1;
|
2001-12-05 10:02:25 +08:00
|
|
|
tokc = tok1c;
|
2001-11-11 10:53:01 +08:00
|
|
|
tok1 = 0;
|
|
|
|
} else {
|
|
|
|
redo:
|
|
|
|
if (!macro_ptr) {
|
|
|
|
/* if not reading from macro substuted string, then try to substitute */
|
|
|
|
len = 0;
|
|
|
|
ptr = NULL;
|
|
|
|
nested_list = NULL;
|
|
|
|
macro_subst(&ptr, &len, &nested_list, NULL);
|
|
|
|
if (ptr) {
|
|
|
|
tok_add(&ptr, &len, 0);
|
|
|
|
macro_ptr = ptr;
|
|
|
|
macro_ptr_allocated = ptr;
|
|
|
|
goto redo;
|
|
|
|
}
|
|
|
|
if (tok == 0)
|
|
|
|
goto redo;
|
|
|
|
} else {
|
|
|
|
next_nomacro();
|
|
|
|
if (tok == 0) {
|
|
|
|
/* end of macro string: free it */
|
|
|
|
free(macro_ptr_allocated);
|
|
|
|
macro_ptr = NULL;
|
|
|
|
goto redo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-12-08 23:04:01 +08:00
|
|
|
#if defined(DEBUG)
|
2001-11-11 10:53:01 +08:00
|
|
|
printf("token = %s\n", get_tok_str(tok, tokc));
|
|
|
|
#endif
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
|
|
|
|
2001-11-06 09:20:20 +08:00
|
|
|
void swap(int *p, int *q)
|
|
|
|
{
|
|
|
|
int t;
|
|
|
|
t = *p;
|
|
|
|
*p = *q;
|
|
|
|
*q = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
void vset(t, v)
|
|
|
|
{
|
|
|
|
vt = t;
|
|
|
|
vc = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************/
|
|
|
|
/* X86 code generator */
|
|
|
|
|
2001-12-03 00:25:49 +08:00
|
|
|
typedef struct GFuncContext {
|
2001-12-03 05:24:43 +08:00
|
|
|
int args_size;
|
2001-12-03 00:25:49 +08:00
|
|
|
} GFuncContext;
|
|
|
|
|
2001-12-05 08:45:08 +08:00
|
|
|
void g(int c)
|
2001-10-28 07:48:39 +08:00
|
|
|
{
|
|
|
|
*(char *)ind++ = c;
|
|
|
|
}
|
|
|
|
|
2001-12-05 08:45:08 +08:00
|
|
|
void o(int c)
|
2001-10-28 07:48:39 +08:00
|
|
|
{
|
|
|
|
while (c) {
|
|
|
|
g(c);
|
|
|
|
c = c / 256;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-05 08:45:08 +08:00
|
|
|
void gen_le32(int c)
|
|
|
|
{
|
|
|
|
g(c);
|
|
|
|
g(c >> 8);
|
|
|
|
g(c >> 16);
|
|
|
|
g(c >> 24);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add a new relocation entry to symbol 's' */
|
|
|
|
void greloc(Sym *s, int addr, int type)
|
|
|
|
{
|
|
|
|
Reloc *p;
|
|
|
|
p = malloc(sizeof(Reloc));
|
|
|
|
if (!p)
|
|
|
|
error("memory full");
|
|
|
|
p->type = type;
|
|
|
|
p->addr = addr;
|
|
|
|
p->next = (Reloc *)s->c;
|
|
|
|
s->c = (int)p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* patch each relocation entry with value 'val' */
|
|
|
|
void greloc_patch(Sym *s, int val)
|
|
|
|
{
|
|
|
|
Reloc *p, *p1;
|
|
|
|
|
|
|
|
p = (Reloc *)s->c;
|
|
|
|
while (p != NULL) {
|
|
|
|
p1 = p->next;
|
|
|
|
switch(p->type) {
|
|
|
|
case RELOC_ADDR32:
|
|
|
|
*(int *)p->addr = val;
|
|
|
|
break;
|
|
|
|
case RELOC_REL32:
|
|
|
|
*(int *)p->addr = val - p->addr - 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(p);
|
|
|
|
p = p1;
|
|
|
|
}
|
|
|
|
s->c = val;
|
2001-12-05 10:02:25 +08:00
|
|
|
s->t &= ~VT_FORWARD;
|
2001-12-05 08:45:08 +08:00
|
|
|
}
|
|
|
|
|
2001-10-28 07:48:39 +08:00
|
|
|
/* output a symbol and patch all calls to it */
|
2001-10-31 04:27:54 +08:00
|
|
|
void gsym_addr(t, a)
|
2001-10-28 07:48:39 +08:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
while (t) {
|
|
|
|
n = *(int *)t; /* next value */
|
2001-10-31 04:27:54 +08:00
|
|
|
*(int *)t = a - t - 4;
|
2001-10-28 07:48:39 +08:00
|
|
|
t = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-31 04:27:54 +08:00
|
|
|
void gsym(t)
|
|
|
|
{
|
|
|
|
gsym_addr(t, ind);
|
|
|
|
}
|
|
|
|
|
2001-10-28 07:48:39 +08:00
|
|
|
/* psym is used to put an instruction with a data field which is a
|
|
|
|
reference to a symbol. It is in fact the same as oad ! */
|
|
|
|
#define psym oad
|
|
|
|
|
|
|
|
/* instruction + 4 bytes data. Return the address of the data */
|
2001-12-05 08:45:08 +08:00
|
|
|
int oad(int c, int s)
|
2001-10-28 07:48:39 +08:00
|
|
|
{
|
|
|
|
o(c);
|
|
|
|
*(int *)ind = s;
|
|
|
|
s = ind;
|
|
|
|
ind = ind + 4;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2001-12-05 08:45:08 +08:00
|
|
|
/* output constant with relocation if 't & VT_FORWARD' is true */
|
|
|
|
void gen_addr32(int c, int t)
|
|
|
|
{
|
|
|
|
if (!(t & VT_FORWARD)) {
|
|
|
|
gen_le32(c);
|
|
|
|
} else {
|
|
|
|
greloc((Sym *)c, ind, RELOC_ADDR32);
|
|
|
|
gen_le32(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-30 07:23:01 +08:00
|
|
|
/* XXX: generate correct pointer for forward references to functions */
|
2001-11-06 09:20:20 +08:00
|
|
|
/* r = (ft, fc) */
|
|
|
|
void load(r, ft, fc)
|
2001-10-28 07:48:39 +08:00
|
|
|
{
|
2001-11-06 09:20:20 +08:00
|
|
|
int v, t;
|
|
|
|
|
|
|
|
v = ft & VT_VALMASK;
|
|
|
|
if (ft & VT_LVAL) {
|
|
|
|
if (v == VT_LLOCAL) {
|
|
|
|
load(r, VT_LOCAL | VT_LVAL, fc);
|
|
|
|
v = r;
|
|
|
|
}
|
|
|
|
if ((ft & VT_TYPE) == VT_BYTE)
|
|
|
|
o(0xbe0f); /* movsbl */
|
2001-11-12 02:00:58 +08:00
|
|
|
else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED))
|
|
|
|
o(0xb60f); /* movzbl */
|
|
|
|
else if ((ft & VT_TYPE) == VT_SHORT)
|
|
|
|
o(0xbf0f); /* movswl */
|
|
|
|
else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED))
|
|
|
|
o(0xb70f); /* movzwl */
|
2001-10-28 07:48:39 +08:00
|
|
|
else
|
2001-11-06 09:20:20 +08:00
|
|
|
o(0x8b); /* movl */
|
|
|
|
if (v == VT_CONST) {
|
2001-12-05 08:45:08 +08:00
|
|
|
o(0x05 + r * 8); /* 0xXX, r */
|
|
|
|
gen_addr32(fc, ft);
|
2001-11-06 09:20:20 +08:00
|
|
|
} else if (v == VT_LOCAL) {
|
|
|
|
oad(0x85 + r * 8, fc); /* xx(%ebp), r */
|
|
|
|
} else {
|
|
|
|
g(0x00 + r * 8 + v); /* (v), r */
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
2001-11-06 09:20:20 +08:00
|
|
|
} else {
|
|
|
|
if (v == VT_CONST) {
|
2001-12-05 08:45:08 +08:00
|
|
|
o(0xb8 + r); /* mov $xx, r */
|
|
|
|
gen_addr32(fc, ft);
|
2001-11-06 09:20:20 +08:00
|
|
|
} else if (v == VT_LOCAL) {
|
|
|
|
o(0x8d);
|
|
|
|
oad(0x85 + r * 8, fc); /* lea xxx(%ebp), r */
|
|
|
|
} else if (v == VT_CMP) {
|
|
|
|
oad(0xb8 + r, 0); /* mov $0, r */
|
|
|
|
o(0x0f); /* setxx %br */
|
|
|
|
o(fc);
|
|
|
|
o(0xc0 + r);
|
|
|
|
} else if (v == VT_JMP || v == VT_JMPI) {
|
|
|
|
t = v & 1;
|
|
|
|
oad(0xb8 + r, t); /* mov $1, r */
|
2001-10-28 07:48:39 +08:00
|
|
|
oad(0xe9, 5); /* jmp after */
|
2001-11-06 09:20:20 +08:00
|
|
|
gsym(fc);
|
|
|
|
oad(0xb8 + r, t ^ 1); /* mov $0, r */
|
|
|
|
} else if (v != r) {
|
|
|
|
o(0x89);
|
|
|
|
o(0xc0 + r + v * 8); /* mov v, r */
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
|
|
|
}
|
2001-11-06 09:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (ft, fc) = r */
|
|
|
|
/* WARNING: r must not be allocated on the stack */
|
|
|
|
void store(r, ft, fc)
|
|
|
|
{
|
|
|
|
int fr, b;
|
|
|
|
|
|
|
|
fr = ft & VT_VALMASK;
|
|
|
|
b = (ft & VT_TYPE) == VT_BYTE;
|
2001-11-12 02:00:58 +08:00
|
|
|
/* XXX: incorrect if reg to reg */
|
|
|
|
if (ft & VT_SHORT)
|
|
|
|
o(0x66);
|
2001-11-06 09:20:20 +08:00
|
|
|
o(0x89 - b);
|
|
|
|
if (fr == VT_CONST) {
|
2001-12-05 08:45:08 +08:00
|
|
|
o(0x05 + r * 8); /* mov r,xxx */
|
|
|
|
gen_addr32(fc, ft);
|
2001-11-06 09:20:20 +08:00
|
|
|
} else if (fr == VT_LOCAL) {
|
|
|
|
oad(0x85 + r * 8, fc); /* mov r,xxx(%ebp) */
|
|
|
|
} else if (ft & VT_LVAL) {
|
|
|
|
g(fr + r * 8); /* mov r, (fr) */
|
|
|
|
} else if (fr != r) {
|
|
|
|
o(0xc0 + fr + r * 8); /* mov r, fr */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-03 00:25:49 +08:00
|
|
|
/* start function call and return function call context */
|
|
|
|
void gfunc_start(GFuncContext *c)
|
|
|
|
{
|
2001-12-03 05:24:43 +08:00
|
|
|
c->args_size = 0;
|
2001-12-03 00:25:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* push function parameter which is in (vt, vc) */
|
|
|
|
void gfunc_param(GFuncContext *c)
|
2001-11-12 02:00:58 +08:00
|
|
|
{
|
2001-12-03 05:24:43 +08:00
|
|
|
int size, align, ft, fc, r;
|
|
|
|
|
|
|
|
if ((vt & (VT_STRUCT | VT_LVAL)) == (VT_STRUCT | VT_LVAL)) {
|
|
|
|
size = type_size(vt, &align);
|
|
|
|
/* align to stack align size */
|
|
|
|
size = (size + 3) & ~3;
|
|
|
|
/* allocate the necessary size on stack */
|
|
|
|
oad(0xec81, size); /* sub $xxx, %esp */
|
|
|
|
/* generate structure store */
|
|
|
|
r = get_reg();
|
|
|
|
o(0x89); /* mov %esp, r */
|
|
|
|
o(0xe0 + r);
|
|
|
|
ft = vt;
|
|
|
|
fc = vc;
|
|
|
|
vset(VT_INT | r, 0);
|
|
|
|
vpush();
|
|
|
|
vt = ft;
|
|
|
|
vc = fc;
|
|
|
|
vstore();
|
|
|
|
c->args_size += size;
|
|
|
|
} else {
|
|
|
|
/* simple type (currently always same size) */
|
|
|
|
/* XXX: implicit cast ? */
|
|
|
|
r = gv();
|
|
|
|
o(0x50 + r); /* push r */
|
|
|
|
c->args_size += 4;
|
|
|
|
}
|
2001-12-03 00:25:49 +08:00
|
|
|
}
|
|
|
|
|
2001-12-09 04:24:33 +08:00
|
|
|
/* generate function call with address in (vt, vc) and free function
|
|
|
|
context */
|
|
|
|
void gfunc_call(GFuncContext *c)
|
2001-12-03 00:25:49 +08:00
|
|
|
{
|
|
|
|
int r;
|
2001-12-09 04:24:33 +08:00
|
|
|
if ((vt & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
|
|
|
|
/* constant case */
|
2001-12-03 00:25:49 +08:00
|
|
|
/* forward reference */
|
2001-12-09 04:24:33 +08:00
|
|
|
if (vt & VT_FORWARD) {
|
|
|
|
greloc((Sym *)vc, ind + 1, RELOC_REL32);
|
2001-12-05 08:45:08 +08:00
|
|
|
oad(0xe8, 0);
|
|
|
|
} else {
|
2001-12-09 04:24:33 +08:00
|
|
|
oad(0xe8, vc - ind - 5);
|
2001-12-05 08:45:08 +08:00
|
|
|
}
|
2001-12-03 00:25:49 +08:00
|
|
|
} else {
|
2001-12-09 04:24:33 +08:00
|
|
|
/* otherwise, indirect call */
|
|
|
|
r = gv();
|
2001-12-03 05:24:43 +08:00
|
|
|
o(0xff); /* call *r */
|
2001-12-03 00:25:49 +08:00
|
|
|
o(0xd0 + r);
|
|
|
|
}
|
2001-12-03 05:24:43 +08:00
|
|
|
if (c->args_size)
|
|
|
|
oad(0xc481, c->args_size); /* add $xxx, %esp */
|
2001-11-12 02:00:58 +08:00
|
|
|
}
|
|
|
|
|
2001-12-03 00:25:49 +08:00
|
|
|
int gjmp(int t)
|
2001-11-06 09:20:20 +08:00
|
|
|
{
|
|
|
|
return psym(0xe9, t);
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* generate a test. set 'inv' to invert test */
|
2001-12-09 04:24:33 +08:00
|
|
|
int gtst(int inv, int t)
|
2001-10-28 07:48:39 +08:00
|
|
|
{
|
2001-11-06 09:20:20 +08:00
|
|
|
int v, *p;
|
|
|
|
v = vt & VT_VALMASK;
|
|
|
|
if (v == VT_CMP) {
|
2001-10-28 07:48:39 +08:00
|
|
|
/* fast case : can jump directly since flags are set */
|
|
|
|
g(0x0f);
|
|
|
|
t = psym((vc - 16) ^ inv, t);
|
2001-11-06 09:20:20 +08:00
|
|
|
} else if (v == VT_JMP || v == VT_JMPI) {
|
2001-10-28 07:48:39 +08:00
|
|
|
/* && or || optimization */
|
2001-11-06 09:20:20 +08:00
|
|
|
if ((v & 1) == inv) {
|
|
|
|
/* insert vc jump list in t */
|
|
|
|
p = &vc;
|
|
|
|
while (*p != 0)
|
|
|
|
p = (int *)*p;
|
|
|
|
*p = t;
|
2001-10-28 07:48:39 +08:00
|
|
|
t = vc;
|
2001-11-06 09:20:20 +08:00
|
|
|
} else {
|
|
|
|
t = gjmp(t);
|
2001-10-28 07:48:39 +08:00
|
|
|
gsym(vc);
|
|
|
|
}
|
2001-11-06 09:20:20 +08:00
|
|
|
} else if ((vt & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
|
2001-10-28 07:48:39 +08:00
|
|
|
/* constant jmp optimization */
|
|
|
|
if ((vc != 0) != inv)
|
2001-11-06 09:20:20 +08:00
|
|
|
t = gjmp(t);
|
|
|
|
} else {
|
|
|
|
v = gv();
|
|
|
|
o(0x85);
|
|
|
|
o(0xc0 + v * 9);
|
2001-10-28 07:48:39 +08:00
|
|
|
g(0x0f);
|
|
|
|
t = psym(0x85 ^ inv, t);
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2001-11-06 09:20:20 +08:00
|
|
|
/* generate a binary operation 'v = r op fr' instruction and modifies
|
|
|
|
(vt,vc) if needed */
|
2001-12-09 04:24:33 +08:00
|
|
|
void gen_op1(int op, int r, int fr)
|
2001-11-06 09:20:20 +08:00
|
|
|
{
|
|
|
|
int t;
|
|
|
|
if (op == '+') {
|
|
|
|
o(0x01);
|
|
|
|
o(0xc0 + r + fr * 8);
|
|
|
|
} else if (op == '-') {
|
|
|
|
o(0x29);
|
|
|
|
o(0xc0 + r + fr * 8);
|
|
|
|
} else if (op == '&') {
|
|
|
|
o(0x21);
|
|
|
|
o(0xc0 + r + fr * 8);
|
|
|
|
} else if (op == '^') {
|
|
|
|
o(0x31);
|
|
|
|
o(0xc0 + r + fr * 8);
|
|
|
|
} else if (op == '|') {
|
|
|
|
o(0x09);
|
|
|
|
o(0xc0 + r + fr * 8);
|
|
|
|
} else if (op == '*') {
|
|
|
|
o(0xaf0f); /* imul fr, r */
|
|
|
|
o(0xc0 + fr + r * 8);
|
|
|
|
} else if (op == TOK_SHL | op == TOK_SHR | op == TOK_SAR) {
|
|
|
|
/* op2 is %ecx */
|
|
|
|
if (fr != 1) {
|
|
|
|
if (r == 1) {
|
|
|
|
r = fr;
|
|
|
|
fr = 1;
|
|
|
|
o(0x87); /* xchg r, %ecx */
|
|
|
|
o(0xc1 + r * 8);
|
|
|
|
} else
|
|
|
|
move_reg(1, fr);
|
|
|
|
}
|
|
|
|
o(0xd3); /* shl/shr/sar %cl, r */
|
|
|
|
if (op == TOK_SHL)
|
|
|
|
o(0xe0 + r);
|
|
|
|
else if (op == TOK_SHR)
|
|
|
|
o(0xe8 + r);
|
|
|
|
else
|
|
|
|
o(0xf8 + r);
|
|
|
|
vt = (vt & VT_TYPE) | r;
|
|
|
|
} else if (op == '/' | op == TOK_UDIV | op == TOK_PDIV |
|
|
|
|
op == '%' | op == TOK_UMOD) {
|
|
|
|
save_reg(2); /* save edx */
|
|
|
|
t = save_reg_forced(fr); /* save fr and get op2 location */
|
|
|
|
move_reg(0, r); /* op1 is %eax */
|
|
|
|
if (op == TOK_UDIV | op == TOK_UMOD) {
|
|
|
|
o(0xf7d231); /* xor %edx, %edx, div t(%ebp), %eax */
|
|
|
|
oad(0xb5, t);
|
|
|
|
} else {
|
|
|
|
o(0xf799); /* cltd, idiv t(%ebp), %eax */
|
|
|
|
oad(0xbd, t);
|
|
|
|
}
|
|
|
|
if (op == '%' | op == TOK_UMOD)
|
|
|
|
r = 2;
|
|
|
|
else
|
|
|
|
r = 0;
|
|
|
|
vt = (vt & VT_TYPE) | r;
|
|
|
|
} else {
|
|
|
|
o(0x39);
|
|
|
|
o(0xc0 + r + fr * 8); /* cmp fr, r */
|
|
|
|
vset(VT_CMP, op);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* end of X86 code generator */
|
|
|
|
/*************************************************************/
|
|
|
|
|
2001-11-11 10:53:01 +08:00
|
|
|
int save_reg_forced(int r)
|
2001-11-06 09:20:20 +08:00
|
|
|
{
|
|
|
|
int i, l, *p, t;
|
|
|
|
/* store register */
|
|
|
|
loc = (loc - 4) & -3;
|
|
|
|
store(r, VT_LOCAL, loc);
|
|
|
|
l = loc;
|
|
|
|
|
|
|
|
/* modify all stack values */
|
|
|
|
for(p=vstack;p<vstack_ptr;p+=2) {
|
|
|
|
i = p[0] & VT_VALMASK;
|
|
|
|
if (i == r) {
|
|
|
|
if (p[0] & VT_LVAL)
|
|
|
|
t = VT_LLOCAL;
|
|
|
|
else
|
|
|
|
t = VT_LOCAL;
|
|
|
|
p[0] = (p[0] & VT_TYPE) | VT_LVAL | t;
|
|
|
|
p[1] = l;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* save r to memory. and mark it as being free */
|
|
|
|
void save_reg(r)
|
|
|
|
{
|
|
|
|
int i, *p;
|
|
|
|
|
|
|
|
/* modify all stack values */
|
|
|
|
for(p=vstack;p<vstack_ptr;p+=2) {
|
|
|
|
i = p[0] & VT_VALMASK;
|
|
|
|
if (i == r) {
|
|
|
|
save_reg_forced(r);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find a free register. If none, save one register */
|
2001-12-03 05:24:43 +08:00
|
|
|
int get_reg(void)
|
2001-11-06 09:20:20 +08:00
|
|
|
{
|
|
|
|
int r, i, *p;
|
|
|
|
|
|
|
|
/* find a free register */
|
|
|
|
for(r=0;r<NB_REGS;r++) {
|
|
|
|
for(p=vstack;p<vstack_ptr;p+=2) {
|
|
|
|
i = p[0] & VT_VALMASK;
|
|
|
|
if (i == r)
|
|
|
|
goto notfound;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
notfound: ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* no register left : free the first one on the stack (very
|
|
|
|
important to start from the bottom to ensure that we don't
|
|
|
|
spill registers used in gen_op()) */
|
|
|
|
for(p=vstack;p<vstack_ptr;p+=2) {
|
|
|
|
r = p[0] & VT_VALMASK;
|
|
|
|
if (r < VT_CONST) {
|
|
|
|
save_reg(r);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
void save_regs()
|
|
|
|
{
|
|
|
|
int r, *p;
|
|
|
|
for(p=vstack;p<vstack_ptr;p+=2) {
|
|
|
|
r = p[0] & VT_VALMASK;
|
|
|
|
if (r < VT_CONST) {
|
|
|
|
save_reg(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* move register 's' to 'r', and flush previous value of r to memory
|
|
|
|
if needed */
|
|
|
|
void move_reg(r, s)
|
|
|
|
{
|
|
|
|
if (r != s) {
|
|
|
|
save_reg(r);
|
|
|
|
load(r, s, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-03 00:25:49 +08:00
|
|
|
/* convert a stack entry in register. lvalues are converted as
|
|
|
|
values. Cannot be used if cannot be converted to register value
|
|
|
|
(such as structures). */
|
2001-11-06 09:20:20 +08:00
|
|
|
int gvp(int *p)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
r = p[0] & VT_VALMASK;
|
|
|
|
if (r >= VT_CONST || (p[0] & VT_LVAL))
|
|
|
|
r = get_reg();
|
2001-12-03 00:25:49 +08:00
|
|
|
/* NOTE: get_reg can modify p[] */
|
2001-11-06 09:20:20 +08:00
|
|
|
load(r, p[0], p[1]);
|
|
|
|
p[0] = (p[0] & VT_TYPE) | r;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2001-12-03 05:24:43 +08:00
|
|
|
void vpush(void)
|
2001-11-06 09:20:20 +08:00
|
|
|
{
|
2001-11-08 09:30:44 +08:00
|
|
|
if (vstack_ptr >= vstack + VSTACK_SIZE)
|
|
|
|
error("memory full");
|
2001-11-06 09:20:20 +08:00
|
|
|
*vstack_ptr++ = vt;
|
|
|
|
*vstack_ptr++ = vc;
|
|
|
|
/* cannot let cpu flags if other instruction are generated */
|
2001-12-09 04:24:33 +08:00
|
|
|
/* XXX: VT_JMP test too ? */
|
2001-11-06 09:20:20 +08:00
|
|
|
if ((vt & VT_VALMASK) == VT_CMP)
|
|
|
|
gvp(vstack_ptr - 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void vpop(int *ft, int *fc)
|
|
|
|
{
|
|
|
|
*fc = *--vstack_ptr;
|
|
|
|
*ft = *--vstack_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generate a value in a register from vt and vc */
|
2001-12-03 00:25:49 +08:00
|
|
|
int gv(void)
|
2001-11-06 09:20:20 +08:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
vpush();
|
|
|
|
r = gvp(vstack_ptr - 2);
|
|
|
|
vpop(&vt, &vc);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handle constant optimizations and various machine independant opt */
|
|
|
|
void gen_opc(op)
|
|
|
|
{
|
|
|
|
int fr, ft, fc, r, c1, c2, n;
|
|
|
|
|
|
|
|
vpop(&ft, &fc);
|
|
|
|
vpop(&vt, &vc);
|
|
|
|
c1 = (vt & (VT_VALMASK | VT_LVAL)) == VT_CONST;
|
|
|
|
c2 = (ft & (VT_VALMASK | VT_LVAL)) == VT_CONST;
|
|
|
|
if (c1 && c2) {
|
|
|
|
switch(op) {
|
|
|
|
case '+': vc += fc; break;
|
|
|
|
case '-': vc -= fc; break;
|
|
|
|
case '&': vc &= fc; break;
|
|
|
|
case '^': vc ^= fc; break;
|
|
|
|
case '|': vc |= fc; break;
|
|
|
|
case '*': vc *= fc; break;
|
|
|
|
case TOK_PDIV:
|
|
|
|
case '/': vc /= fc; break; /* XXX: zero case ? */
|
|
|
|
case '%': vc %= fc; break; /* XXX: zero case ? */
|
|
|
|
case TOK_UDIV: vc = (unsigned)vc / fc; break; /* XXX: zero case ? */
|
|
|
|
case TOK_UMOD: vc = (unsigned)vc % fc; break; /* XXX: zero case ? */
|
|
|
|
case TOK_SHL: vc <<= fc; break;
|
|
|
|
case TOK_SHR: vc = (unsigned)vc >> fc; break;
|
|
|
|
case TOK_SAR: vc >>= fc; break;
|
2001-11-11 10:53:01 +08:00
|
|
|
/* tests */
|
|
|
|
case TOK_ULT: vc = (unsigned)vc < (unsigned)fc; break;
|
|
|
|
case TOK_UGE: vc = (unsigned)vc >= (unsigned)fc; break;
|
|
|
|
case TOK_EQ: vc = vc == fc; break;
|
|
|
|
case TOK_NE: vc = vc != fc; break;
|
|
|
|
case TOK_ULE: vc = (unsigned)vc <= (unsigned)fc; break;
|
|
|
|
case TOK_UGT: vc = (unsigned)vc > (unsigned)fc; break;
|
|
|
|
case TOK_LT: vc = vc < fc; break;
|
|
|
|
case TOK_GE: vc = vc >= fc; break;
|
|
|
|
case TOK_LE: vc = vc <= fc; break;
|
|
|
|
case TOK_GT: vc = vc > fc; break;
|
|
|
|
/* logical */
|
|
|
|
case TOK_LAND: vc = vc && fc; break;
|
|
|
|
case TOK_LOR: vc = vc || fc; break;
|
2001-11-06 09:20:20 +08:00
|
|
|
default:
|
|
|
|
goto general_case;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* if commutative ops, put c2 as constant */
|
|
|
|
if (c1 && (op == '+' || op == '&' || op == '^' ||
|
|
|
|
op == '|' || op == '*')) {
|
|
|
|
swap(&vt, &ft);
|
|
|
|
swap(&vc, &fc);
|
|
|
|
swap(&c1, &c2);
|
|
|
|
}
|
|
|
|
if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
|
|
|
|
op == TOK_PDIV) &&
|
|
|
|
fc == 1) ||
|
|
|
|
((op == '+' || op == '-' || op == '|' || op == '^' ||
|
|
|
|
op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
|
|
|
|
fc == 0) ||
|
|
|
|
(op == '&' &&
|
|
|
|
fc == -1))) {
|
|
|
|
} else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
|
|
|
|
/* try to use shifts instead of muls or divs */
|
|
|
|
if (fc > 0 && (fc & (fc - 1)) == 0) {
|
|
|
|
n = -1;
|
|
|
|
while (fc) {
|
|
|
|
fc >>= 1;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
fc = n;
|
|
|
|
if (op == '*')
|
|
|
|
op = TOK_SHL;
|
|
|
|
else if (op == TOK_PDIV)
|
|
|
|
op = TOK_SAR;
|
|
|
|
else
|
|
|
|
op = TOK_SHR;
|
|
|
|
}
|
|
|
|
goto general_case;
|
|
|
|
} else {
|
|
|
|
general_case:
|
|
|
|
vpush();
|
|
|
|
vt = ft;
|
|
|
|
vc = fc;
|
|
|
|
vpush();
|
|
|
|
r = gvp(vstack_ptr - 4);
|
|
|
|
fr = gvp(vstack_ptr - 2);
|
|
|
|
vpop(&ft, &fc);
|
|
|
|
vpop(&vt, &vc);
|
|
|
|
/* call low level op generator */
|
|
|
|
gen_op1(op, r, fr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-11 10:53:01 +08:00
|
|
|
int pointed_size(int t)
|
2001-11-06 09:20:20 +08:00
|
|
|
{
|
|
|
|
return type_size(pointed_type(t), &t);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generic gen_op: handles types problems */
|
2001-11-11 10:53:01 +08:00
|
|
|
void gen_op(int op)
|
2001-11-06 09:20:20 +08:00
|
|
|
{
|
|
|
|
int u, t1, t2;
|
|
|
|
|
|
|
|
vpush();
|
|
|
|
t1 = vstack_ptr[-4];
|
|
|
|
t2 = vstack_ptr[-2];
|
|
|
|
if (op == '+' | op == '-') {
|
|
|
|
if ((t1 & VT_PTR) && (t2 & VT_PTR)) {
|
|
|
|
if (op != '-')
|
|
|
|
error("invalid type");
|
|
|
|
/* XXX: check that types are compatible */
|
|
|
|
u = pointed_size(t1);
|
|
|
|
gen_opc(op);
|
|
|
|
vpush();
|
|
|
|
vstack_ptr[-2] &= ~VT_TYPE; /* set to integer */
|
|
|
|
vset(VT_CONST, u);
|
|
|
|
gen_op(TOK_PDIV);
|
|
|
|
} else if ((t1 | t2) & VT_PTR) {
|
|
|
|
if (t2 & VT_PTR) {
|
|
|
|
swap(vstack_ptr - 4, vstack_ptr - 2);
|
|
|
|
swap(vstack_ptr - 3, vstack_ptr - 1);
|
|
|
|
swap(&t1, &t2);
|
|
|
|
}
|
|
|
|
/* stack-4 contains pointer, stack-2 value to add */
|
|
|
|
vset(VT_CONST, pointed_size(vstack_ptr[-4]));
|
|
|
|
gen_op('*');
|
|
|
|
vpush();
|
|
|
|
gen_opc(op);
|
|
|
|
/* put again type if gen_opc() swaped operands */
|
2001-12-08 23:04:01 +08:00
|
|
|
vt = (vt & ~VT_TYPE) | (t1 & VT_TYPE);
|
2001-11-06 09:20:20 +08:00
|
|
|
} else {
|
|
|
|
gen_opc(op);
|
|
|
|
}
|
|
|
|
} else {
|
2001-11-08 09:12:31 +08:00
|
|
|
/* XXX: test types and compute returned value */
|
|
|
|
if ((t1 | t2) & (VT_UNSIGNED | VT_PTR)) {
|
2001-11-06 09:20:20 +08:00
|
|
|
if (op == TOK_SAR)
|
|
|
|
op = TOK_SHR;
|
|
|
|
else if (op == '/')
|
|
|
|
op = TOK_UDIV;
|
|
|
|
else if (op == '%')
|
|
|
|
op = TOK_UMOD;
|
2001-11-08 07:13:29 +08:00
|
|
|
else if (op == TOK_LT)
|
|
|
|
op = TOK_ULT;
|
|
|
|
else if (op == TOK_GT)
|
|
|
|
op = TOK_UGT;
|
|
|
|
else if (op == TOK_LE)
|
|
|
|
op = TOK_ULE;
|
|
|
|
else if (op == TOK_GE)
|
|
|
|
op = TOK_UGE;
|
2001-11-06 09:20:20 +08:00
|
|
|
}
|
|
|
|
gen_opc(op);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-03 05:24:43 +08:00
|
|
|
/* cast (vt, vc) to 't' type */
|
|
|
|
void gen_cast(int t)
|
|
|
|
{
|
|
|
|
int r, bits;
|
|
|
|
r = vt & VT_VALMASK;
|
|
|
|
if (!(t & VT_LVAL)) {
|
|
|
|
/* if not lvalue, then we convert now */
|
|
|
|
if ((t & VT_TYPE & ~VT_UNSIGNED) == VT_BYTE)
|
|
|
|
bits = 8;
|
|
|
|
else if ((t & VT_TYPE & ~VT_UNSIGNED) == VT_SHORT)
|
|
|
|
bits = 16;
|
|
|
|
else
|
|
|
|
goto the_end;
|
|
|
|
vpush();
|
|
|
|
if (t & VT_UNSIGNED) {
|
|
|
|
vset(VT_CONST, (1 << bits) - 1);
|
|
|
|
gen_op('&');
|
|
|
|
} else {
|
|
|
|
bits = 32 - bits;
|
|
|
|
vset(VT_CONST, bits);
|
|
|
|
gen_op(TOK_SHL);
|
|
|
|
vpush();
|
|
|
|
vset(VT_CONST, bits);
|
|
|
|
gen_op(TOK_SAR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
the_end:
|
2001-12-08 23:04:01 +08:00
|
|
|
vt = (vt & ~VT_TYPE) | t;
|
2001-12-03 05:24:43 +08:00
|
|
|
}
|
|
|
|
|
2001-11-01 22:48:10 +08:00
|
|
|
/* return type size. Put alignment at 'a' */
|
|
|
|
int type_size(int t, int *a)
|
2001-10-28 07:48:39 +08:00
|
|
|
{
|
2001-11-01 22:48:10 +08:00
|
|
|
Sym *s;
|
|
|
|
|
|
|
|
/* int, enum or pointer */
|
2001-11-06 09:20:20 +08:00
|
|
|
if (t & VT_STRUCT) {
|
2001-11-01 22:48:10 +08:00
|
|
|
/* struct/union */
|
|
|
|
s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT) | SYM_STRUCT);
|
|
|
|
*a = 4; /* XXX: cannot store it yet. Doing that is safe */
|
|
|
|
return s->c;
|
2001-11-06 09:20:20 +08:00
|
|
|
} else if (t & VT_ARRAY) {
|
|
|
|
s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT));
|
|
|
|
return type_size(s->t, a) * s->c;
|
2001-12-09 04:24:33 +08:00
|
|
|
} else if ((t & VT_PTR) ||
|
|
|
|
(t & VT_TYPE & ~VT_UNSIGNED) == VT_INT ||
|
2001-11-06 09:20:20 +08:00
|
|
|
(t & VT_ENUM)) {
|
|
|
|
*a = 4;
|
|
|
|
return 4;
|
2001-11-12 02:00:58 +08:00
|
|
|
} else if (t & VT_SHORT) {
|
|
|
|
*a = 2;
|
|
|
|
return 2;
|
2001-11-01 22:48:10 +08:00
|
|
|
} else {
|
|
|
|
*a = 1;
|
2001-10-28 07:48:39 +08:00
|
|
|
return 1;
|
2001-11-01 22:48:10 +08:00
|
|
|
}
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
|
|
|
|
2001-11-06 09:20:20 +08:00
|
|
|
/* return the pointed type of t */
|
|
|
|
int pointed_type(int t)
|
2001-10-30 07:23:01 +08:00
|
|
|
{
|
2001-11-06 09:20:20 +08:00
|
|
|
Sym *s;
|
|
|
|
s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT));
|
2001-12-08 23:04:01 +08:00
|
|
|
return s->t | (t & ~VT_TYPE);
|
2001-10-30 07:23:01 +08:00
|
|
|
}
|
|
|
|
|
2001-11-06 09:20:20 +08:00
|
|
|
int mk_pointer(int t)
|
2001-10-28 07:48:39 +08:00
|
|
|
{
|
2001-11-06 09:20:20 +08:00
|
|
|
int p;
|
|
|
|
p = anon_sym++;
|
|
|
|
sym_push(p, t, -1);
|
2001-12-08 23:04:01 +08:00
|
|
|
return VT_PTR | (p << VT_STRUCT_SHIFT) | (t & ~VT_TYPE);
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
|
|
|
|
2001-11-06 09:20:20 +08:00
|
|
|
/* store value in lvalue pushed on stack */
|
2001-12-03 05:24:43 +08:00
|
|
|
void vstore(void)
|
2001-10-28 07:48:39 +08:00
|
|
|
{
|
2001-12-03 00:25:49 +08:00
|
|
|
int ft, fc, r, t, size, align;
|
|
|
|
GFuncContext gf;
|
2001-11-06 09:20:20 +08:00
|
|
|
|
2001-12-03 00:25:49 +08:00
|
|
|
if (vt & VT_STRUCT) {
|
|
|
|
/* if structure, only generate pointer */
|
|
|
|
/* structure assignment : generate memcpy */
|
|
|
|
/* XXX: optimize if small size */
|
|
|
|
|
|
|
|
gfunc_start(&gf);
|
|
|
|
/* type size */
|
|
|
|
ft = vt;
|
|
|
|
fc = vc;
|
|
|
|
size = type_size(vt, &align);
|
|
|
|
vset(VT_CONST, size);
|
|
|
|
gfunc_param(&gf);
|
|
|
|
/* source */
|
|
|
|
vt = ft & ~VT_LVAL;
|
|
|
|
vc = fc;
|
|
|
|
gfunc_param(&gf);
|
|
|
|
/* destination */
|
|
|
|
vpop(&vt, &vc);
|
|
|
|
vt &= ~VT_LVAL;
|
|
|
|
gfunc_param(&gf);
|
|
|
|
|
|
|
|
save_regs();
|
2001-12-09 04:24:33 +08:00
|
|
|
vset(VT_CONST, (int)&memcpy);
|
|
|
|
gfunc_call(&gf);
|
2001-12-03 00:25:49 +08:00
|
|
|
|
|
|
|
/* generate again current type */
|
|
|
|
vt = ft;
|
|
|
|
vc = fc;
|
|
|
|
} else {
|
|
|
|
r = gv(); /* generate value */
|
|
|
|
vpush();
|
|
|
|
ft = vstack_ptr[-4];
|
|
|
|
fc = vstack_ptr[-3];
|
|
|
|
/* if lvalue was saved on stack, must read it */
|
|
|
|
if ((ft & VT_VALMASK) == VT_LLOCAL) {
|
|
|
|
t = get_reg();
|
|
|
|
load(t, VT_LOCAL | VT_LVAL, fc);
|
|
|
|
ft = (ft & ~VT_VALMASK) | t;
|
|
|
|
}
|
|
|
|
store(r, ft, fc);
|
|
|
|
vstack_ptr -= 4;
|
2001-10-28 09:15:41 +08:00
|
|
|
}
|
2001-11-06 09:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* post defines POST/PRE add. c is the token ++ or -- */
|
|
|
|
void inc(post, c)
|
|
|
|
{
|
|
|
|
int r, r1;
|
|
|
|
test_lvalue();
|
|
|
|
if (post)
|
|
|
|
vpush(); /* room for returned value */
|
|
|
|
vpush(); /* save lvalue */
|
|
|
|
r = gv();
|
|
|
|
vpush(); /* save value */
|
|
|
|
if (post) {
|
|
|
|
/* duplicate value */
|
|
|
|
r1 = get_reg();
|
|
|
|
load(r1, r, 0); /* move r to r1 */
|
|
|
|
vstack_ptr[-6] = (vt & VT_TYPE) | r1;
|
|
|
|
vstack_ptr[-5] = 0;
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
2001-11-06 09:20:20 +08:00
|
|
|
/* add constant */
|
|
|
|
vset(VT_CONST, c - TOK_MID);
|
|
|
|
gen_op('+');
|
|
|
|
vstore(); /* store value */
|
|
|
|
if (post)
|
|
|
|
vpop(&vt, &vc);
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
|
|
|
|
2001-11-01 22:48:10 +08:00
|
|
|
/* enum/struct/union declaration */
|
2001-12-05 08:45:08 +08:00
|
|
|
int struct_decl(int u)
|
2001-11-01 22:48:10 +08:00
|
|
|
{
|
|
|
|
int a, t, b, v, size, align, maxalign, c;
|
2001-11-18 01:22:38 +08:00
|
|
|
Sym *s, *ss, **ps;
|
2001-11-01 22:48:10 +08:00
|
|
|
|
|
|
|
a = tok; /* save decl type */
|
|
|
|
next();
|
|
|
|
if (tok != '{') {
|
|
|
|
v = tok;
|
|
|
|
next();
|
|
|
|
/* struct already defined ? return it */
|
|
|
|
/* XXX: check consistency */
|
|
|
|
if (s = sym_find(v | SYM_STRUCT)) {
|
|
|
|
if (s->t != a)
|
|
|
|
error("invalid type");
|
2001-11-12 00:07:43 +08:00
|
|
|
goto do_decl;
|
2001-11-01 22:48:10 +08:00
|
|
|
}
|
2001-11-06 09:20:20 +08:00
|
|
|
} else {
|
|
|
|
v = anon_sym++;
|
2001-11-01 22:48:10 +08:00
|
|
|
}
|
|
|
|
s = sym_push(v | SYM_STRUCT, a, 0);
|
|
|
|
/* put struct/union/enum name in type */
|
2001-11-12 00:07:43 +08:00
|
|
|
do_decl:
|
2001-11-01 22:48:10 +08:00
|
|
|
u = u | (v << VT_STRUCT_SHIFT);
|
|
|
|
|
|
|
|
if (tok == '{') {
|
|
|
|
next();
|
2001-11-12 00:07:43 +08:00
|
|
|
if (s->c)
|
|
|
|
error("struct/union/enum already defined");
|
2001-11-01 22:48:10 +08:00
|
|
|
/* cannot be empty */
|
|
|
|
c = 0;
|
|
|
|
maxalign = 0;
|
2001-11-18 01:22:38 +08:00
|
|
|
ps = &s->next;
|
2001-11-01 22:48:10 +08:00
|
|
|
while (1) {
|
|
|
|
if (a == TOK_ENUM) {
|
|
|
|
v = tok;
|
|
|
|
next();
|
|
|
|
if (tok == '=') {
|
|
|
|
next();
|
|
|
|
c = expr_const();
|
|
|
|
}
|
2001-12-08 23:04:01 +08:00
|
|
|
/* enum symbols have static storage */
|
|
|
|
sym_push(v, VT_CONST | VT_STATIC, c);
|
2001-11-01 22:48:10 +08:00
|
|
|
if (tok == ',')
|
|
|
|
next();
|
|
|
|
c++;
|
|
|
|
} else {
|
|
|
|
b = ist();
|
|
|
|
while (1) {
|
2001-11-12 00:07:43 +08:00
|
|
|
t = type_decl(&v, b, TYPE_DIRECT);
|
2001-11-01 22:48:10 +08:00
|
|
|
if (t & (VT_FUNC | VT_TYPEDEF))
|
|
|
|
error("invalid type");
|
|
|
|
/* XXX: align & correct type size */
|
|
|
|
v |= SYM_FIELD;
|
|
|
|
size = type_size(t, &align);
|
|
|
|
if (a == TOK_STRUCT) {
|
|
|
|
c = (c + align - 1) & -align;
|
|
|
|
ss = sym_push(v, t, c);
|
|
|
|
c += size;
|
|
|
|
} else {
|
|
|
|
ss = sym_push(v, t, 0);
|
|
|
|
if (size > c)
|
|
|
|
c = size;
|
|
|
|
}
|
|
|
|
if (align > maxalign)
|
|
|
|
maxalign = align;
|
2001-11-18 01:22:38 +08:00
|
|
|
*ps = ss;
|
|
|
|
ps = &ss->next;
|
2001-11-01 22:48:10 +08:00
|
|
|
if (tok == ';' || tok == -1)
|
|
|
|
break;
|
|
|
|
skip(',');
|
|
|
|
}
|
|
|
|
skip(';');
|
|
|
|
}
|
|
|
|
if (tok == '}')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
skip('}');
|
|
|
|
/* size for struct/union, dummy for enum */
|
|
|
|
s->c = (c + maxalign - 1) & -maxalign;
|
|
|
|
}
|
|
|
|
return u;
|
|
|
|
}
|
|
|
|
|
2001-10-28 07:48:39 +08:00
|
|
|
/* return 0 if no type declaration. otherwise, return the basic type
|
|
|
|
and skip it.
|
|
|
|
XXX: A '2' is ored to ensure non zero return if int type.
|
|
|
|
*/
|
2001-11-11 10:53:01 +08:00
|
|
|
int ist(void)
|
2001-10-28 07:48:39 +08:00
|
|
|
{
|
2001-11-11 10:53:01 +08:00
|
|
|
int t;
|
2001-11-01 22:48:10 +08:00
|
|
|
Sym *s;
|
2001-10-31 06:43:19 +08:00
|
|
|
|
2001-10-30 07:23:01 +08:00
|
|
|
t = 0;
|
|
|
|
while(1) {
|
2001-10-31 06:43:19 +08:00
|
|
|
if (tok == TOK_ENUM) {
|
2001-11-06 09:20:20 +08:00
|
|
|
t |= struct_decl(VT_ENUM);
|
2001-11-01 22:48:10 +08:00
|
|
|
} else if (tok == TOK_STRUCT || tok == TOK_UNION) {
|
2001-11-06 09:20:20 +08:00
|
|
|
t |= struct_decl(VT_STRUCT);
|
2001-11-11 10:53:01 +08:00
|
|
|
} else {
|
2001-11-06 09:20:20 +08:00
|
|
|
if (tok == TOK_CHAR) {
|
2001-10-31 06:43:19 +08:00
|
|
|
t |= VT_BYTE;
|
2001-11-06 09:20:20 +08:00
|
|
|
} else if (tok == TOK_VOID) {
|
|
|
|
t |= VT_VOID;
|
2001-11-12 02:00:58 +08:00
|
|
|
} else if (tok == TOK_SHORT) {
|
|
|
|
t |= VT_SHORT;
|
|
|
|
} else if (tok == TOK_INT |
|
2001-11-06 09:20:20 +08:00
|
|
|
(tok >= TOK_CONST & tok <= TOK_INLINE)) {
|
2001-10-31 06:43:19 +08:00
|
|
|
/* ignored types */
|
2001-11-11 10:53:01 +08:00
|
|
|
} else if (tok == TOK_FLOAT || tok == TOK_DOUBLE) {
|
2001-11-12 00:07:43 +08:00
|
|
|
/* We allow that to compile standard headers */
|
|
|
|
// warning("floats not supported");
|
2001-10-31 06:43:19 +08:00
|
|
|
} else if (tok == TOK_EXTERN) {
|
|
|
|
t |= VT_EXTERN;
|
|
|
|
} else if (tok == TOK_STATIC) {
|
|
|
|
t |= VT_STATIC;
|
|
|
|
} else if (tok == TOK_UNSIGNED) {
|
|
|
|
t |= VT_UNSIGNED;
|
|
|
|
} else if (tok == TOK_TYPEDEF) {
|
|
|
|
t |= VT_TYPEDEF;
|
|
|
|
} else {
|
2001-11-01 22:48:10 +08:00
|
|
|
s = sym_find(tok);
|
|
|
|
if (!s || !(s->t & VT_TYPEDEF))
|
2001-10-31 06:43:19 +08:00
|
|
|
break;
|
2001-11-12 00:07:43 +08:00
|
|
|
t |= (s->t & ~VT_TYPEDEF);
|
2001-10-31 06:43:19 +08:00
|
|
|
}
|
|
|
|
next();
|
2001-10-30 07:23:01 +08:00
|
|
|
}
|
|
|
|
t |= 2;
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
2001-10-30 07:23:01 +08:00
|
|
|
return t;
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
|
|
|
|
2001-12-08 23:04:01 +08:00
|
|
|
int post_type(int t)
|
2001-11-06 09:20:20 +08:00
|
|
|
{
|
2001-12-08 23:04:01 +08:00
|
|
|
int p, n, pt, l, t1;
|
2001-12-03 05:24:43 +08:00
|
|
|
Sym **plast, *s, *first;
|
2001-11-06 09:20:20 +08:00
|
|
|
|
|
|
|
if (tok == '(') {
|
|
|
|
/* function declaration */
|
|
|
|
next();
|
|
|
|
l = 0;
|
2001-12-03 05:24:43 +08:00
|
|
|
first = NULL;
|
|
|
|
plast = &first;
|
2001-11-06 09:20:20 +08:00
|
|
|
while (tok != ')') {
|
|
|
|
/* read param name and compute offset */
|
2001-11-08 07:13:29 +08:00
|
|
|
if (l != FUNC_OLD) {
|
2001-11-06 09:20:20 +08:00
|
|
|
if (!(pt = ist())) {
|
|
|
|
if (l) {
|
|
|
|
error("invalid type");
|
|
|
|
} else {
|
2001-11-08 07:13:29 +08:00
|
|
|
l = FUNC_OLD;
|
2001-11-06 09:20:20 +08:00
|
|
|
goto old_proto;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pt & VT_VOID && tok == ')')
|
|
|
|
break;
|
2001-11-08 07:13:29 +08:00
|
|
|
l = FUNC_NEW;
|
2001-11-12 00:07:43 +08:00
|
|
|
pt = type_decl(&n, pt, TYPE_DIRECT | TYPE_ABSTRACT);
|
2001-11-06 09:20:20 +08:00
|
|
|
} else {
|
|
|
|
old_proto:
|
|
|
|
n = tok;
|
2001-12-03 05:24:43 +08:00
|
|
|
pt = VT_INT;
|
2001-11-06 09:20:20 +08:00
|
|
|
next();
|
|
|
|
}
|
|
|
|
/* array must be transformed to pointer according to ANSI C */
|
|
|
|
pt &= ~VT_ARRAY;
|
2001-12-03 05:24:43 +08:00
|
|
|
s = sym_push(n | SYM_FIELD, pt, 0);
|
|
|
|
*plast = s;
|
|
|
|
plast = &s->next;
|
2001-11-06 09:20:20 +08:00
|
|
|
if (tok == ',') {
|
|
|
|
next();
|
2001-11-08 07:13:29 +08:00
|
|
|
if (l == FUNC_NEW && tok == TOK_DOTS) {
|
|
|
|
l = FUNC_ELLIPSIS;
|
2001-11-06 09:20:20 +08:00
|
|
|
next();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
skip(')');
|
2001-12-08 23:04:01 +08:00
|
|
|
t1 = t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN);
|
|
|
|
t = post_type(t & ~(VT_TYPEDEF | VT_STATIC | VT_EXTERN));
|
2001-11-08 07:13:29 +08:00
|
|
|
/* we push a anonymous symbol which will contain the function prototype */
|
|
|
|
p = anon_sym++;
|
|
|
|
s = sym_push(p, t, l);
|
2001-12-03 05:24:43 +08:00
|
|
|
s->next = first;
|
2001-12-08 23:04:01 +08:00
|
|
|
t = t1 | VT_FUNC | (p << VT_STRUCT_SHIFT);
|
2001-11-06 09:20:20 +08:00
|
|
|
} else if (tok == '[') {
|
|
|
|
/* array definition */
|
|
|
|
next();
|
|
|
|
n = -1;
|
|
|
|
if (tok != ']') {
|
|
|
|
n = expr_const();
|
|
|
|
if (n < 0)
|
|
|
|
error("invalid array size");
|
|
|
|
}
|
|
|
|
skip(']');
|
|
|
|
/* parse next post type */
|
2001-12-09 04:24:33 +08:00
|
|
|
t1 = t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN);
|
|
|
|
t = post_type(t & ~(VT_TYPEDEF | VT_STATIC | VT_EXTERN));
|
2001-11-06 09:20:20 +08:00
|
|
|
|
|
|
|
/* we push a anonymous symbol which will contain the array
|
|
|
|
element type */
|
|
|
|
p = anon_sym++;
|
|
|
|
sym_push(p, t, n);
|
2001-12-09 04:24:33 +08:00
|
|
|
t = t1 | VT_ARRAY | VT_PTR | (p << VT_STRUCT_SHIFT);
|
2001-11-06 09:20:20 +08:00
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2001-10-28 07:48:39 +08:00
|
|
|
/* Read a type declaration (except basic type), and return the
|
|
|
|
type. If v is true, then also put variable name in 'vc' */
|
2001-11-12 00:07:43 +08:00
|
|
|
int type_decl(int *v, int t, int td)
|
2001-10-28 07:48:39 +08:00
|
|
|
{
|
2001-11-08 07:13:29 +08:00
|
|
|
int u, p;
|
|
|
|
Sym *s;
|
2001-10-28 07:48:39 +08:00
|
|
|
|
|
|
|
t = t & -3; /* suppress the ored '2' */
|
|
|
|
while (tok == '*') {
|
|
|
|
next();
|
2001-11-14 06:54:35 +08:00
|
|
|
while (tok == TOK_CONST || tok == TOK_VOLATILE || tok == TOK_RESTRICT)
|
2001-11-12 00:07:43 +08:00
|
|
|
next();
|
2001-11-06 09:20:20 +08:00
|
|
|
t = mk_pointer(t);
|
|
|
|
}
|
2001-10-28 07:48:39 +08:00
|
|
|
|
|
|
|
/* recursive type */
|
|
|
|
/* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
|
|
|
|
if (tok == '(') {
|
|
|
|
next();
|
2001-11-12 00:07:43 +08:00
|
|
|
u = type_decl(v, 0, td);
|
2001-10-28 07:48:39 +08:00
|
|
|
skip(')');
|
|
|
|
} else {
|
|
|
|
u = 0;
|
|
|
|
/* type identifier */
|
2001-11-12 00:07:43 +08:00
|
|
|
if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
|
2001-10-30 07:23:01 +08:00
|
|
|
*v = tok;
|
2001-10-28 07:48:39 +08:00
|
|
|
next();
|
2001-11-12 00:07:43 +08:00
|
|
|
} else {
|
|
|
|
if (!(td & TYPE_ABSTRACT))
|
|
|
|
expect("identifier");
|
|
|
|
*v = 0;
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
|
|
|
}
|
2001-11-08 07:13:29 +08:00
|
|
|
/* append t at the end of u */
|
|
|
|
t = post_type(t);
|
|
|
|
if (!u)
|
|
|
|
return t;
|
|
|
|
p = u;
|
|
|
|
while(1) {
|
|
|
|
s = sym_find((unsigned)p >> VT_STRUCT_SHIFT);
|
|
|
|
p = s->t;
|
|
|
|
if (!p) {
|
|
|
|
s->t = t;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return u;
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
|
|
|
|
2001-10-30 07:23:01 +08:00
|
|
|
/* define a new external reference to a function 'v' of type 'u' */
|
2001-12-05 08:45:08 +08:00
|
|
|
Sym *external_sym(int v, int u)
|
2001-10-30 07:23:01 +08:00
|
|
|
{
|
2001-11-01 22:48:10 +08:00
|
|
|
Sym *s;
|
|
|
|
s = sym_find(v);
|
|
|
|
if (!s) {
|
2001-12-05 08:45:08 +08:00
|
|
|
/* push forward reference */
|
|
|
|
s = sym_push1(&global_stack,
|
|
|
|
v, u | VT_CONST | VT_FORWARD, 0);
|
2001-10-30 07:23:01 +08:00
|
|
|
}
|
2001-11-01 22:48:10 +08:00
|
|
|
return s;
|
2001-10-30 07:23:01 +08:00
|
|
|
}
|
|
|
|
|
2001-12-05 08:45:08 +08:00
|
|
|
void indir(void)
|
2001-10-28 07:48:39 +08:00
|
|
|
{
|
2001-11-06 09:20:20 +08:00
|
|
|
if (vt & VT_LVAL)
|
|
|
|
gv();
|
|
|
|
if (!(vt & VT_PTR))
|
|
|
|
expect("pointer");
|
|
|
|
vt = pointed_type(vt);
|
|
|
|
if (!(vt & VT_ARRAY)) /* an array is never an lvalue */
|
|
|
|
vt |= VT_LVAL;
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
|
|
|
|
2001-12-05 08:45:08 +08:00
|
|
|
void unary(void)
|
2001-10-28 07:48:39 +08:00
|
|
|
{
|
2001-12-03 05:24:43 +08:00
|
|
|
int n, t, ft, fc, p, align, size;
|
2001-11-01 22:48:10 +08:00
|
|
|
Sym *s;
|
2001-12-03 00:25:49 +08:00
|
|
|
GFuncContext gf;
|
2001-10-28 07:48:39 +08:00
|
|
|
|
2001-11-29 06:48:43 +08:00
|
|
|
if (tok == TOK_NUM || tok == TOK_CCHAR || tok == TOK_LCHAR) {
|
2001-11-06 09:20:20 +08:00
|
|
|
vset(VT_CONST, tokc);
|
2001-10-28 09:15:41 +08:00
|
|
|
next();
|
2001-11-14 06:54:35 +08:00
|
|
|
} else if (tok == TOK___FUNC__) {
|
|
|
|
/* special function name identifier */
|
|
|
|
/* generate (char *) type */
|
2001-11-18 01:22:38 +08:00
|
|
|
vset(VT_CONST | mk_pointer(VT_BYTE), glo);
|
2001-11-14 06:54:35 +08:00
|
|
|
strcpy((void *)glo, funcname);
|
|
|
|
glo += strlen(funcname) + 1;
|
2001-12-08 23:04:01 +08:00
|
|
|
next();
|
2001-11-29 06:48:43 +08:00
|
|
|
} else if (tok == TOK_LSTR) {
|
|
|
|
t = VT_INT;
|
|
|
|
goto str_init;
|
2001-11-11 10:53:01 +08:00
|
|
|
} else if (tok == TOK_STR) {
|
2001-11-29 06:48:43 +08:00
|
|
|
/* string parsing */
|
|
|
|
t = VT_BYTE;
|
|
|
|
str_init:
|
|
|
|
type_size(t, &align);
|
|
|
|
glo = (glo + align - 1) & -align;
|
|
|
|
fc = glo;
|
|
|
|
/* we must declare it as an array first to use initializer parser */
|
|
|
|
t = VT_CONST | VT_ARRAY | mk_pointer(t);
|
|
|
|
decl_initializer(t, glo, 1, 0);
|
|
|
|
glo += type_size(t, &align);
|
|
|
|
/* put it as pointer */
|
|
|
|
vset(t & ~VT_ARRAY, fc);
|
2001-10-28 07:48:39 +08:00
|
|
|
} else {
|
|
|
|
t = tok;
|
|
|
|
next();
|
|
|
|
if (t == '(') {
|
|
|
|
/* cast ? */
|
|
|
|
if (t = ist()) {
|
2001-11-12 00:07:43 +08:00
|
|
|
ft = type_decl(&n, t, TYPE_ABSTRACT);
|
2001-10-28 07:48:39 +08:00
|
|
|
skip(')');
|
2001-12-02 22:47:48 +08:00
|
|
|
/* check ISOC99 compound literal */
|
|
|
|
if (tok == '{') {
|
|
|
|
/* data is allocated locally by default */
|
|
|
|
if (global_expr)
|
|
|
|
ft |= VT_CONST;
|
|
|
|
else
|
|
|
|
ft |= VT_LOCAL;
|
|
|
|
/* all except arrays are lvalues */
|
|
|
|
if (!(ft & VT_ARRAY))
|
|
|
|
ft |= VT_LVAL;
|
|
|
|
fc = decl_initializer_alloc(ft, 1);
|
|
|
|
vset(ft, fc);
|
|
|
|
} else {
|
|
|
|
unary();
|
2001-12-03 05:24:43 +08:00
|
|
|
gen_cast(ft);
|
2001-12-02 22:47:48 +08:00
|
|
|
}
|
2001-10-28 07:48:39 +08:00
|
|
|
} else {
|
|
|
|
expr();
|
|
|
|
skip(')');
|
|
|
|
}
|
|
|
|
} else if (t == '*') {
|
|
|
|
unary();
|
2001-11-06 09:20:20 +08:00
|
|
|
indir();
|
2001-10-28 07:48:39 +08:00
|
|
|
} else if (t == '&') {
|
|
|
|
unary();
|
2001-12-05 08:45:08 +08:00
|
|
|
/* functions names must be treated as function pointers,
|
|
|
|
except for unary '&' and sizeof. Since we consider that
|
|
|
|
functions are not lvalues, we only have to handle it
|
|
|
|
there and in function calls. */
|
|
|
|
if (!(vt & VT_FUNC))
|
|
|
|
test_lvalue();
|
2001-11-06 09:20:20 +08:00
|
|
|
vt = mk_pointer(vt & VT_LVALN);
|
2001-10-28 07:48:39 +08:00
|
|
|
} else
|
|
|
|
if (t == '!') {
|
|
|
|
unary();
|
2001-12-09 04:24:33 +08:00
|
|
|
if ((vt & (VT_VALMASK | VT_LVAL)) == VT_CONST)
|
2001-11-11 10:53:01 +08:00
|
|
|
vc = !vc;
|
|
|
|
else if ((vt & VT_VALMASK) == VT_CMP)
|
2001-10-28 07:48:39 +08:00
|
|
|
vc = vc ^ 1;
|
|
|
|
else
|
|
|
|
vset(VT_JMP, gtst(1, 0));
|
2001-11-11 10:53:01 +08:00
|
|
|
} else
|
2001-10-28 07:48:39 +08:00
|
|
|
if (t == '~') {
|
|
|
|
unary();
|
2001-11-06 09:20:20 +08:00
|
|
|
vpush();
|
|
|
|
vset(VT_CONST, -1);
|
|
|
|
gen_op('^');
|
2001-10-28 07:48:39 +08:00
|
|
|
} else
|
2001-10-28 12:08:38 +08:00
|
|
|
if (t == '+') {
|
|
|
|
unary();
|
|
|
|
} else
|
2001-11-06 09:20:20 +08:00
|
|
|
if (t == TOK_SIZEOF) {
|
|
|
|
/* XXX: some code can be generated */
|
|
|
|
if (tok == '(') {
|
|
|
|
next();
|
|
|
|
if (t = ist())
|
2001-11-12 00:07:43 +08:00
|
|
|
vt = type_decl(&n, t, TYPE_ABSTRACT);
|
2001-11-06 09:20:20 +08:00
|
|
|
else
|
|
|
|
expr();
|
|
|
|
skip(')');
|
|
|
|
} else {
|
|
|
|
unary();
|
|
|
|
}
|
|
|
|
vset(VT_CONST, type_size(vt, &t));
|
|
|
|
} else
|
2001-10-28 07:48:39 +08:00
|
|
|
if (t == TOK_INC | t == TOK_DEC) {
|
|
|
|
unary();
|
2001-11-06 09:20:20 +08:00
|
|
|
inc(0, t);
|
2001-10-28 07:48:39 +08:00
|
|
|
} else if (t == '-') {
|
2001-11-06 09:20:20 +08:00
|
|
|
vset(VT_CONST, 0);
|
|
|
|
vpush();
|
2001-10-28 07:48:39 +08:00
|
|
|
unary();
|
2001-11-06 09:20:20 +08:00
|
|
|
gen_op('-');
|
2001-10-28 12:08:38 +08:00
|
|
|
} else
|
|
|
|
{
|
2001-11-01 22:48:10 +08:00
|
|
|
s = sym_find(t);
|
|
|
|
if (!s) {
|
2001-10-30 07:23:01 +08:00
|
|
|
if (tok != '(')
|
2001-11-18 01:22:38 +08:00
|
|
|
error("'%s' undeclared", get_tok_str(t, 0));
|
2001-10-30 07:23:01 +08:00
|
|
|
/* for simple function calls, we tolerate undeclared
|
|
|
|
external reference */
|
2001-11-08 07:13:29 +08:00
|
|
|
p = anon_sym++;
|
|
|
|
sym_push1(&global_stack, p, 0, FUNC_OLD);
|
|
|
|
/* int() function */
|
2001-12-05 08:45:08 +08:00
|
|
|
s = external_sym(t, VT_FUNC | (p << VT_STRUCT_SHIFT));
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
2001-11-01 22:48:10 +08:00
|
|
|
vset(s->t, s->c);
|
2001-12-05 08:45:08 +08:00
|
|
|
/* if forward reference, we must point to s */
|
2001-10-30 07:23:01 +08:00
|
|
|
if (vt & VT_FORWARD)
|
2001-12-05 08:45:08 +08:00
|
|
|
vc = (int)s;
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* post operations */
|
2001-11-01 22:48:10 +08:00
|
|
|
while (1) {
|
|
|
|
if (tok == TOK_INC | tok == TOK_DEC) {
|
2001-11-06 09:20:20 +08:00
|
|
|
inc(1, tok);
|
2001-11-01 22:48:10 +08:00
|
|
|
next();
|
|
|
|
} else if (tok == '.' | tok == TOK_ARROW) {
|
|
|
|
/* field */
|
2001-11-06 09:20:20 +08:00
|
|
|
if (tok == TOK_ARROW)
|
|
|
|
indir();
|
|
|
|
test_lvalue();
|
|
|
|
vt &= VT_LVALN;
|
2001-11-01 22:48:10 +08:00
|
|
|
next();
|
|
|
|
/* expect pointer on structure */
|
2001-11-06 09:20:20 +08:00
|
|
|
if (!(vt & VT_STRUCT))
|
2001-11-01 22:48:10 +08:00
|
|
|
expect("struct or union");
|
|
|
|
s = sym_find(((unsigned)vt >> VT_STRUCT_SHIFT) | SYM_STRUCT);
|
|
|
|
/* find field */
|
|
|
|
tok |= SYM_FIELD;
|
|
|
|
while (s = s->next) {
|
|
|
|
if (s->v == tok)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!s)
|
|
|
|
error("field not found");
|
|
|
|
/* add field offset to pointer */
|
2001-12-08 23:04:01 +08:00
|
|
|
vt = (vt & ~VT_TYPE) | VT_INT; /* change type to int */
|
2001-11-06 09:20:20 +08:00
|
|
|
vpush();
|
|
|
|
vset(VT_CONST, s->c);
|
|
|
|
gen_op('+');
|
2001-11-01 22:48:10 +08:00
|
|
|
/* change type to field type, and set to lvalue */
|
2001-12-08 23:04:01 +08:00
|
|
|
vt = (vt & ~VT_TYPE) | s->t;
|
2001-11-11 10:53:01 +08:00
|
|
|
/* an array is never an lvalue */
|
|
|
|
if (!(vt & VT_ARRAY))
|
|
|
|
vt |= VT_LVAL;
|
2001-11-01 22:48:10 +08:00
|
|
|
next();
|
|
|
|
} else if (tok == '[') {
|
2001-11-06 09:20:20 +08:00
|
|
|
next();
|
|
|
|
vpush();
|
|
|
|
expr();
|
|
|
|
gen_op('+');
|
|
|
|
indir();
|
2001-11-01 22:48:10 +08:00
|
|
|
skip(']');
|
|
|
|
} else if (tok == '(') {
|
2001-12-03 05:24:43 +08:00
|
|
|
int rett, retc;
|
|
|
|
|
2001-11-01 22:48:10 +08:00
|
|
|
/* function call */
|
2001-12-05 08:45:08 +08:00
|
|
|
if (!(vt & VT_FUNC)) {
|
|
|
|
if ((vt & (VT_PTR | VT_ARRAY)) == VT_PTR) {
|
|
|
|
vt = pointed_type(vt);
|
|
|
|
if (!(vt & VT_FUNC))
|
|
|
|
goto error_func;
|
|
|
|
} else {
|
|
|
|
error_func:
|
2001-12-09 04:24:33 +08:00
|
|
|
expect("function pointer");
|
2001-12-05 08:45:08 +08:00
|
|
|
}
|
2001-12-09 04:24:33 +08:00
|
|
|
} else {
|
|
|
|
vt &= ~VT_LVAL; /* no lvalue */
|
2001-12-05 08:45:08 +08:00
|
|
|
}
|
2001-12-09 04:24:33 +08:00
|
|
|
|
2001-12-03 05:24:43 +08:00
|
|
|
/* get return type */
|
|
|
|
s = sym_find((unsigned)vt >> VT_STRUCT_SHIFT);
|
2001-12-03 00:25:49 +08:00
|
|
|
vpush(); /* push function address */
|
2001-11-06 09:20:20 +08:00
|
|
|
save_regs(); /* save used temporary registers */
|
2001-12-03 00:25:49 +08:00
|
|
|
gfunc_start(&gf);
|
2001-11-01 22:48:10 +08:00
|
|
|
next();
|
2001-11-12 02:00:58 +08:00
|
|
|
#ifdef INVERT_FUNC_PARAMS
|
|
|
|
{
|
|
|
|
int *str, len, parlevel, *saved_macro_ptr;
|
2001-11-19 00:33:01 +08:00
|
|
|
Sym *args, *s1;
|
2001-11-12 02:00:58 +08:00
|
|
|
|
|
|
|
/* read each argument and store it on a stack */
|
|
|
|
/* XXX: merge it with macro args ? */
|
|
|
|
args = NULL;
|
|
|
|
while (tok != ')') {
|
|
|
|
len = 0;
|
|
|
|
str = NULL;
|
|
|
|
parlevel = 0;
|
|
|
|
while ((parlevel > 0 || (tok != ')' && tok != ',')) &&
|
|
|
|
tok != -1) {
|
|
|
|
if (tok == '(')
|
|
|
|
parlevel++;
|
|
|
|
else if (tok == ')')
|
|
|
|
parlevel--;
|
|
|
|
tok_add2(&str, &len, tok, tokc);
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
tok_add(&str, &len, -1); /* end of file added */
|
|
|
|
tok_add(&str, &len, 0);
|
2001-11-19 00:33:01 +08:00
|
|
|
sym_push2(&args, 0, 0, (int)str);
|
2001-11-12 02:00:58 +08:00
|
|
|
if (tok != ',')
|
|
|
|
break;
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
if (tok != ')')
|
|
|
|
expect(")");
|
|
|
|
|
|
|
|
/* now generate code in reverse order by reading the stack */
|
|
|
|
saved_macro_ptr = macro_ptr;
|
|
|
|
while (args) {
|
|
|
|
macro_ptr = (int *)args->c;
|
|
|
|
next();
|
|
|
|
expr_eq();
|
2001-12-08 23:04:01 +08:00
|
|
|
if (tok != -1)
|
|
|
|
expect("',' or ')'");
|
2001-12-03 00:25:49 +08:00
|
|
|
gfunc_param(&gf);
|
2001-11-19 00:33:01 +08:00
|
|
|
s1 = args->prev;
|
2001-11-12 02:00:58 +08:00
|
|
|
free((int *)args->c);
|
2001-11-19 00:33:01 +08:00
|
|
|
free(args);
|
|
|
|
args = s1;
|
2001-11-12 02:00:58 +08:00
|
|
|
}
|
|
|
|
macro_ptr = saved_macro_ptr;
|
|
|
|
/* restore token */
|
|
|
|
tok = ')';
|
|
|
|
}
|
2001-12-03 05:24:43 +08:00
|
|
|
#endif
|
|
|
|
/* compute first implicit argument if a structure is returned */
|
|
|
|
if (s->t & VT_STRUCT) {
|
|
|
|
/* get some space for the returned structure */
|
|
|
|
size = type_size(s->t, &align);
|
|
|
|
loc = (loc - size) & -align;
|
|
|
|
rett = s->t | VT_LOCAL | VT_LVAL;
|
|
|
|
/* pass it as 'int' to avoid structure arg passing
|
|
|
|
problems */
|
|
|
|
vset(VT_INT | VT_LOCAL, loc);
|
|
|
|
retc = vc;
|
|
|
|
gfunc_param(&gf);
|
|
|
|
} else {
|
|
|
|
rett = s->t | FUNC_RET_REG; /* return in register */
|
|
|
|
retc = 0;
|
|
|
|
}
|
|
|
|
#ifndef INVERT_FUNC_PARAMS
|
2001-11-01 22:48:10 +08:00
|
|
|
while (tok != ')') {
|
|
|
|
expr_eq();
|
2001-12-03 00:25:49 +08:00
|
|
|
gfunc_param(&gf);
|
2001-11-01 22:48:10 +08:00
|
|
|
if (tok == ',')
|
|
|
|
next();
|
|
|
|
}
|
2001-11-12 02:00:58 +08:00
|
|
|
#endif
|
2001-11-01 22:48:10 +08:00
|
|
|
skip(')');
|
2001-12-09 04:24:33 +08:00
|
|
|
vpop(&vt, &vc);
|
|
|
|
gfunc_call(&gf);
|
2001-12-03 05:24:43 +08:00
|
|
|
/* return value */
|
|
|
|
vt = rett;
|
|
|
|
vc = retc;
|
2001-10-28 07:48:39 +08:00
|
|
|
} else {
|
2001-11-01 22:48:10 +08:00
|
|
|
break;
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-05 10:02:25 +08:00
|
|
|
int is_compatible_types(int t1, int t2)
|
2001-12-05 08:45:08 +08:00
|
|
|
{
|
2001-12-05 10:02:25 +08:00
|
|
|
Sym *s1, *s2;
|
|
|
|
|
2001-12-05 08:45:08 +08:00
|
|
|
t1 &= VT_TYPE;
|
|
|
|
t2 &= VT_TYPE;
|
|
|
|
if (t1 & VT_PTR) {
|
|
|
|
t1 = pointed_type(t1);
|
2001-12-05 10:02:25 +08:00
|
|
|
/* if function, then convert implictely to function pointer */
|
|
|
|
if (!(t2 & VT_FUNC)) {
|
|
|
|
if (!(t2 & VT_PTR))
|
|
|
|
return 0;
|
|
|
|
t2 = pointed_type(t2);
|
|
|
|
}
|
2001-12-05 08:45:08 +08:00
|
|
|
/* void matches everything */
|
2001-12-05 10:02:25 +08:00
|
|
|
t1 &= VT_TYPE;
|
|
|
|
t2 &= VT_TYPE;
|
2001-12-05 08:45:08 +08:00
|
|
|
if (t1 == VT_VOID || t2 == VT_VOID)
|
|
|
|
return 1;
|
2001-12-05 10:02:25 +08:00
|
|
|
return is_compatible_types(t1, t2);
|
2001-12-05 08:45:08 +08:00
|
|
|
} else if (t1 & VT_STRUCT) {
|
|
|
|
return (t2 == t1);
|
2001-12-05 10:02:25 +08:00
|
|
|
} else if (t1 & VT_FUNC) {
|
|
|
|
if (!(t2 & VT_FUNC))
|
|
|
|
return 0;
|
|
|
|
s1 = sym_find(((unsigned)t1 >> VT_STRUCT_SHIFT));
|
|
|
|
s2 = sym_find(((unsigned)t2 >> VT_STRUCT_SHIFT));
|
|
|
|
if (!is_compatible_types(s1->t, s2->t))
|
|
|
|
return 0;
|
|
|
|
/* XXX: not complete */
|
|
|
|
if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
|
|
|
|
return 1;
|
|
|
|
if (s1->c != s2->c)
|
|
|
|
return 0;
|
|
|
|
while (s1 != NULL) {
|
|
|
|
if (s2 == NULL)
|
|
|
|
return 0;
|
|
|
|
if (!is_compatible_types(s1->t, s2->t))
|
|
|
|
return 0;
|
|
|
|
s1 = s1->next;
|
|
|
|
s2 = s2->next;
|
|
|
|
}
|
|
|
|
if (s2)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
2001-12-05 08:45:08 +08:00
|
|
|
} else {
|
|
|
|
/* XXX: not complete */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-05 10:02:25 +08:00
|
|
|
int check_assign_types(int t1, int t2)
|
|
|
|
{
|
|
|
|
t1 &= VT_TYPE;
|
|
|
|
t2 &= VT_TYPE;
|
|
|
|
if ((t1 & VT_PTR) && (t2 & VT_FUNC)) {
|
|
|
|
return is_compatible_types(pointed_type(t1), t2);
|
|
|
|
} else {
|
|
|
|
return is_compatible_types(t1, t2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-05 08:45:08 +08:00
|
|
|
|
2001-10-28 07:48:39 +08:00
|
|
|
void uneq()
|
|
|
|
{
|
2001-11-06 09:20:20 +08:00
|
|
|
int t;
|
2001-10-28 07:48:39 +08:00
|
|
|
|
|
|
|
unary();
|
2001-10-29 06:24:16 +08:00
|
|
|
if (tok == '=' |
|
2001-11-06 09:20:20 +08:00
|
|
|
(tok >= TOK_A_MOD & tok <= TOK_A_DIV) |
|
2001-10-29 06:24:16 +08:00
|
|
|
tok == TOK_A_XOR | tok == TOK_A_OR |
|
2001-11-06 09:20:20 +08:00
|
|
|
tok == TOK_A_SHL | tok == TOK_A_SAR) {
|
2001-10-28 23:20:12 +08:00
|
|
|
test_lvalue();
|
2001-11-06 09:20:20 +08:00
|
|
|
vpush();
|
|
|
|
t = tok;
|
|
|
|
next();
|
|
|
|
if (t == '=') {
|
2001-10-29 06:24:16 +08:00
|
|
|
expr_eq();
|
2001-12-05 10:02:25 +08:00
|
|
|
if (!check_assign_types(vstack_ptr[-2], vt))
|
|
|
|
warning("incompatible types");
|
2001-11-06 09:20:20 +08:00
|
|
|
} else {
|
|
|
|
vpush();
|
|
|
|
expr_eq();
|
|
|
|
gen_op(t & 0x7f);
|
|
|
|
}
|
|
|
|
vstore();
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void sum(l)
|
|
|
|
{
|
2001-11-11 10:53:01 +08:00
|
|
|
int t;
|
2001-11-06 09:20:20 +08:00
|
|
|
|
2001-10-28 07:48:39 +08:00
|
|
|
if (l == 0)
|
|
|
|
uneq();
|
|
|
|
else {
|
2001-10-28 09:15:41 +08:00
|
|
|
sum(--l);
|
|
|
|
while ((l == 0 & (tok == '*' | tok == '/' | tok == '%')) |
|
|
|
|
(l == 1 & (tok == '+' | tok == '-')) |
|
2001-11-06 09:20:20 +08:00
|
|
|
(l == 2 & (tok == TOK_SHL | tok == TOK_SAR)) |
|
2001-11-08 07:13:29 +08:00
|
|
|
(l == 3 & ((tok >= TOK_ULE & tok <= TOK_GT) |
|
|
|
|
tok == TOK_ULT | tok == TOK_UGE)) |
|
2001-10-28 09:15:41 +08:00
|
|
|
(l == 4 & (tok == TOK_EQ | tok == TOK_NE)) |
|
|
|
|
(l == 5 & tok == '&') |
|
|
|
|
(l == 6 & tok == '^') |
|
2001-11-11 10:53:01 +08:00
|
|
|
(l == 7 & tok == '|') |
|
|
|
|
(l == 8 & tok == TOK_LAND) |
|
|
|
|
(l == 9 & tok == TOK_LOR)) {
|
2001-11-06 09:20:20 +08:00
|
|
|
vpush();
|
|
|
|
t = tok;
|
|
|
|
next();
|
|
|
|
sum(l);
|
|
|
|
gen_op(t);
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-11 10:53:01 +08:00
|
|
|
/* only used if non constant */
|
2001-12-09 04:24:33 +08:00
|
|
|
void eand(void)
|
2001-10-28 07:48:39 +08:00
|
|
|
{
|
|
|
|
int t;
|
|
|
|
|
|
|
|
sum(8);
|
|
|
|
t = 0;
|
|
|
|
while (1) {
|
|
|
|
if (tok != TOK_LAND) {
|
|
|
|
if (t) {
|
|
|
|
t = gtst(1, t);
|
2001-11-06 09:20:20 +08:00
|
|
|
vset(VT_JMPI, t);
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
t = gtst(1, t);
|
|
|
|
next();
|
|
|
|
sum(8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-09 04:24:33 +08:00
|
|
|
void eor(void)
|
2001-10-28 07:48:39 +08:00
|
|
|
{
|
2001-11-11 10:53:01 +08:00
|
|
|
int t;
|
2001-10-28 07:48:39 +08:00
|
|
|
|
|
|
|
eand();
|
|
|
|
t = 0;
|
|
|
|
while (1) {
|
|
|
|
if (tok != TOK_LOR) {
|
|
|
|
if (t) {
|
|
|
|
t = gtst(0, t);
|
|
|
|
vset(VT_JMP, t);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
t = gtst(0, t);
|
|
|
|
next();
|
|
|
|
eand();
|
|
|
|
}
|
|
|
|
}
|
2001-10-28 10:51:52 +08:00
|
|
|
|
2001-11-11 10:53:01 +08:00
|
|
|
/* XXX: better constant handling */
|
2001-10-28 10:51:52 +08:00
|
|
|
void expr_eq()
|
|
|
|
{
|
2001-11-12 02:00:58 +08:00
|
|
|
int t, u, c, r1, r2;
|
2001-11-11 10:53:01 +08:00
|
|
|
|
|
|
|
if (const_wanted) {
|
|
|
|
sum(10);
|
|
|
|
if (tok == '?') {
|
|
|
|
c = vc;
|
|
|
|
next();
|
|
|
|
expr();
|
|
|
|
t = vc;
|
|
|
|
skip(':');
|
|
|
|
expr_eq();
|
|
|
|
if (c)
|
|
|
|
vc = t;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
eor();
|
|
|
|
if (tok == '?') {
|
|
|
|
next();
|
|
|
|
t = gtst(1, 0);
|
|
|
|
expr();
|
2001-11-12 02:00:58 +08:00
|
|
|
r1 = gv();
|
2001-11-11 10:53:01 +08:00
|
|
|
skip(':');
|
|
|
|
u = gjmp(0);
|
|
|
|
gsym(t);
|
|
|
|
expr_eq();
|
2001-11-12 02:00:58 +08:00
|
|
|
r2 = gv();
|
|
|
|
move_reg(r1, r2);
|
|
|
|
vt = (vt & VT_TYPE) | r1;
|
2001-11-11 10:53:01 +08:00
|
|
|
gsym(u);
|
|
|
|
}
|
2001-10-28 10:51:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void expr()
|
|
|
|
{
|
|
|
|
while (1) {
|
|
|
|
expr_eq();
|
|
|
|
if (tok != ',')
|
|
|
|
break;
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-11 10:53:01 +08:00
|
|
|
int expr_const()
|
|
|
|
{
|
|
|
|
int a;
|
|
|
|
a = const_wanted;
|
|
|
|
const_wanted = 1;
|
|
|
|
expr_eq();
|
|
|
|
if ((vt & (VT_CONST | VT_LVAL)) != VT_CONST)
|
|
|
|
expect("constant");
|
|
|
|
const_wanted = a;
|
|
|
|
return vc;
|
|
|
|
}
|
2001-10-28 07:48:39 +08:00
|
|
|
|
2001-12-05 10:02:25 +08:00
|
|
|
/* return the label token if current token is a label, otherwise
|
|
|
|
return zero */
|
|
|
|
int is_label(void)
|
|
|
|
{
|
|
|
|
int t, c;
|
|
|
|
|
|
|
|
/* fast test first */
|
|
|
|
if (tok < TOK_UIDENT)
|
|
|
|
return 0;
|
|
|
|
t = tok;
|
|
|
|
c = tokc;
|
|
|
|
next();
|
|
|
|
if (tok == ':') {
|
|
|
|
next();
|
|
|
|
return t;
|
|
|
|
} else {
|
|
|
|
/* XXX: may not work in all cases (macros ?) */
|
|
|
|
tok1 = tok;
|
|
|
|
tok1c = tokc;
|
|
|
|
tok = t;
|
|
|
|
tokc = c;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-06 09:20:20 +08:00
|
|
|
void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg)
|
2001-10-28 07:48:39 +08:00
|
|
|
{
|
2001-10-31 04:27:54 +08:00
|
|
|
int a, b, c, d;
|
2001-11-01 22:48:10 +08:00
|
|
|
Sym *s;
|
2001-10-28 07:48:39 +08:00
|
|
|
|
|
|
|
if (tok == TOK_IF) {
|
|
|
|
/* if test */
|
|
|
|
next();
|
|
|
|
skip('(');
|
|
|
|
expr();
|
|
|
|
skip(')');
|
|
|
|
a = gtst(1, 0);
|
2001-11-06 09:20:20 +08:00
|
|
|
block(bsym, csym, case_sym, def_sym, case_reg);
|
2001-10-28 07:48:39 +08:00
|
|
|
c = tok;
|
|
|
|
if (c == TOK_ELSE) {
|
|
|
|
next();
|
2001-11-06 09:20:20 +08:00
|
|
|
d = gjmp(0);
|
2001-10-28 07:48:39 +08:00
|
|
|
gsym(a);
|
2001-11-06 09:20:20 +08:00
|
|
|
block(bsym, csym, case_sym, def_sym, case_reg);
|
2001-10-28 07:48:39 +08:00
|
|
|
gsym(d); /* patch else jmp */
|
|
|
|
} else
|
|
|
|
gsym(a);
|
|
|
|
} else if (tok == TOK_WHILE) {
|
|
|
|
next();
|
|
|
|
d = ind;
|
|
|
|
skip('(');
|
|
|
|
expr();
|
|
|
|
skip(')');
|
2001-10-31 04:27:54 +08:00
|
|
|
a = gtst(1, 0);
|
|
|
|
b = 0;
|
2001-11-06 09:20:20 +08:00
|
|
|
block(&a, &b, case_sym, def_sym, case_reg);
|
2001-10-28 07:48:39 +08:00
|
|
|
oad(0xe9, d - ind - 5); /* jmp */
|
2001-10-31 04:27:54 +08:00
|
|
|
gsym(a);
|
|
|
|
gsym_addr(b, d);
|
2001-10-28 07:48:39 +08:00
|
|
|
} else if (tok == '{') {
|
|
|
|
next();
|
|
|
|
/* declarations */
|
2001-11-19 00:33:01 +08:00
|
|
|
s = local_stack.top;
|
2001-11-18 01:22:38 +08:00
|
|
|
while (tok != '}') {
|
|
|
|
decl(VT_LOCAL);
|
2001-12-05 08:45:08 +08:00
|
|
|
if (tok != '}')
|
|
|
|
block(bsym, csym, case_sym, def_sym, case_reg);
|
2001-11-18 01:22:38 +08:00
|
|
|
}
|
2001-11-01 22:48:10 +08:00
|
|
|
/* pop locally defined symbols */
|
|
|
|
sym_pop(&local_stack, s);
|
2001-10-28 07:48:39 +08:00
|
|
|
next();
|
|
|
|
} else if (tok == TOK_RETURN) {
|
|
|
|
next();
|
|
|
|
if (tok != ';') {
|
2001-12-03 05:24:43 +08:00
|
|
|
if (func_vt & VT_STRUCT) {
|
|
|
|
/* if returning structure, must copy it to implicit
|
|
|
|
first pointer arg location */
|
|
|
|
vset(mk_pointer(func_vt) | VT_LOCAL | VT_LVAL, func_vc);
|
|
|
|
indir();
|
|
|
|
vpush();
|
|
|
|
}
|
2001-10-28 07:48:39 +08:00
|
|
|
expr();
|
2001-12-03 05:24:43 +08:00
|
|
|
if (func_vt & VT_STRUCT) {
|
|
|
|
/* copy structure value to pointer */
|
|
|
|
vstore();
|
|
|
|
} else {
|
|
|
|
/* move return value to standard return register */
|
|
|
|
move_reg(FUNC_RET_REG, gv());
|
|
|
|
}
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
|
|
|
skip(';');
|
2001-11-06 09:20:20 +08:00
|
|
|
rsym = gjmp(rsym); /* jmp */
|
2001-10-28 07:48:39 +08:00
|
|
|
} else if (tok == TOK_BREAK) {
|
|
|
|
/* compute jump */
|
2001-10-31 04:27:54 +08:00
|
|
|
if (!bsym)
|
|
|
|
error("cannot break");
|
2001-11-06 09:20:20 +08:00
|
|
|
*bsym = gjmp(*bsym);
|
2001-10-31 04:27:54 +08:00
|
|
|
next();
|
|
|
|
skip(';');
|
|
|
|
} else if (tok == TOK_CONTINUE) {
|
|
|
|
/* compute jump */
|
|
|
|
if (!csym)
|
|
|
|
error("cannot continue");
|
2001-11-06 09:20:20 +08:00
|
|
|
*csym = gjmp(*csym);
|
2001-10-28 07:48:39 +08:00
|
|
|
next();
|
|
|
|
skip(';');
|
2001-11-11 10:53:01 +08:00
|
|
|
} else if (tok == TOK_FOR) {
|
2001-10-28 10:51:52 +08:00
|
|
|
int e;
|
|
|
|
next();
|
|
|
|
skip('(');
|
|
|
|
if (tok != ';')
|
|
|
|
expr();
|
|
|
|
skip(';');
|
|
|
|
d = ind;
|
|
|
|
c = ind;
|
|
|
|
a = 0;
|
2001-10-31 04:27:54 +08:00
|
|
|
b = 0;
|
2001-10-28 10:51:52 +08:00
|
|
|
if (tok != ';') {
|
|
|
|
expr();
|
|
|
|
a = gtst(1, 0);
|
|
|
|
}
|
|
|
|
skip(';');
|
|
|
|
if (tok != ')') {
|
2001-11-06 09:20:20 +08:00
|
|
|
e = gjmp(0);
|
2001-10-28 10:51:52 +08:00
|
|
|
c = ind;
|
|
|
|
expr();
|
|
|
|
oad(0xe9, d - ind - 5); /* jmp */
|
|
|
|
gsym(e);
|
|
|
|
}
|
|
|
|
skip(')');
|
2001-11-06 09:20:20 +08:00
|
|
|
block(&a, &b, case_sym, def_sym, case_reg);
|
2001-10-28 10:51:52 +08:00
|
|
|
oad(0xe9, c - ind - 5); /* jmp */
|
2001-10-31 04:27:54 +08:00
|
|
|
gsym(a);
|
|
|
|
gsym_addr(b, c);
|
2001-10-28 10:51:52 +08:00
|
|
|
} else
|
2001-10-31 04:27:54 +08:00
|
|
|
if (tok == TOK_DO) {
|
|
|
|
next();
|
|
|
|
a = 0;
|
|
|
|
b = 0;
|
|
|
|
d = ind;
|
2001-11-06 09:20:20 +08:00
|
|
|
block(&a, &b, case_sym, def_sym, case_reg);
|
2001-10-31 04:27:54 +08:00
|
|
|
skip(TOK_WHILE);
|
|
|
|
skip('(');
|
|
|
|
gsym(b);
|
|
|
|
expr();
|
|
|
|
c = gtst(0, 0);
|
|
|
|
gsym_addr(c, d);
|
|
|
|
skip(')');
|
|
|
|
gsym(a);
|
2001-12-05 08:45:08 +08:00
|
|
|
skip(';');
|
2001-10-31 04:27:54 +08:00
|
|
|
} else
|
2001-10-31 06:43:19 +08:00
|
|
|
if (tok == TOK_SWITCH) {
|
|
|
|
next();
|
|
|
|
skip('(');
|
|
|
|
expr();
|
2001-11-06 09:20:20 +08:00
|
|
|
case_reg = gv();
|
2001-10-31 06:43:19 +08:00
|
|
|
skip(')');
|
|
|
|
a = 0;
|
|
|
|
b = 0;
|
|
|
|
c = 0;
|
2001-11-06 09:20:20 +08:00
|
|
|
block(&a, csym, &b, &c, case_reg);
|
2001-10-31 06:43:19 +08:00
|
|
|
/* if no default, jmp after switch */
|
|
|
|
if (c == 0)
|
|
|
|
c = ind;
|
|
|
|
/* default label */
|
|
|
|
gsym_addr(b, c);
|
|
|
|
/* break label */
|
|
|
|
gsym(a);
|
|
|
|
} else
|
|
|
|
if (tok == TOK_CASE) {
|
|
|
|
next();
|
|
|
|
a = expr_const();
|
|
|
|
if (!case_sym)
|
2001-11-01 22:48:10 +08:00
|
|
|
expect("switch");
|
2001-10-31 06:43:19 +08:00
|
|
|
gsym(*case_sym);
|
2001-11-06 09:20:20 +08:00
|
|
|
vset(case_reg, 0);
|
|
|
|
vpush();
|
|
|
|
vset(VT_CONST, a);
|
|
|
|
gen_op(TOK_EQ);
|
|
|
|
*case_sym = gtst(1, 0);
|
2001-10-31 06:43:19 +08:00
|
|
|
skip(':');
|
2001-11-06 09:20:20 +08:00
|
|
|
block(bsym, csym, case_sym, def_sym, case_reg);
|
2001-10-31 06:43:19 +08:00
|
|
|
} else
|
|
|
|
if (tok == TOK_DEFAULT) {
|
|
|
|
next();
|
|
|
|
skip(':');
|
|
|
|
if (!def_sym)
|
2001-11-01 22:48:10 +08:00
|
|
|
expect("switch");
|
2001-10-31 06:43:19 +08:00
|
|
|
if (*def_sym)
|
|
|
|
error("too many 'default'");
|
|
|
|
*def_sym = ind;
|
2001-11-06 09:20:20 +08:00
|
|
|
block(bsym, csym, case_sym, def_sym, case_reg);
|
2001-10-31 06:43:19 +08:00
|
|
|
} else
|
|
|
|
if (tok == TOK_GOTO) {
|
|
|
|
next();
|
2001-11-19 00:33:01 +08:00
|
|
|
s = sym_find1(&label_stack, tok);
|
2001-11-01 22:48:10 +08:00
|
|
|
/* put forward definition if needed */
|
|
|
|
if (!s)
|
|
|
|
s = sym_push1(&label_stack, tok, VT_FORWARD, 0);
|
2001-10-31 06:43:19 +08:00
|
|
|
/* label already defined */
|
2001-11-01 22:48:10 +08:00
|
|
|
if (s->t & VT_FORWARD)
|
2001-11-06 09:20:20 +08:00
|
|
|
s->c = gjmp(s->c); /* jmp xxx */
|
2001-10-31 06:43:19 +08:00
|
|
|
else
|
2001-11-01 22:48:10 +08:00
|
|
|
oad(0xe9, s->c - ind - 5); /* jmp xxx */
|
2001-10-31 06:43:19 +08:00
|
|
|
next();
|
|
|
|
skip(';');
|
2001-11-11 10:53:01 +08:00
|
|
|
} else {
|
2001-12-05 10:02:25 +08:00
|
|
|
b = is_label();
|
|
|
|
if (b) {
|
2001-10-31 06:43:19 +08:00
|
|
|
/* label case */
|
2001-11-19 00:33:01 +08:00
|
|
|
s = sym_find1(&label_stack, b);
|
2001-11-01 22:48:10 +08:00
|
|
|
if (s) {
|
|
|
|
if (!(s->t & VT_FORWARD))
|
2001-10-31 06:43:19 +08:00
|
|
|
error("multiple defined label");
|
2001-11-01 22:48:10 +08:00
|
|
|
gsym(s->c);
|
|
|
|
s->c = ind;
|
|
|
|
s->t = 0;
|
|
|
|
} else {
|
|
|
|
sym_push1(&label_stack, b, 0, ind);
|
2001-10-31 06:43:19 +08:00
|
|
|
}
|
2001-12-05 10:02:25 +08:00
|
|
|
/* we accept this, but it is a mistake */
|
|
|
|
if (tok == '}')
|
|
|
|
warning("deprecated use of label at end of compound statement");
|
|
|
|
else
|
|
|
|
block(bsym, csym, case_sym, def_sym, case_reg);
|
2001-10-31 06:43:19 +08:00
|
|
|
} else {
|
2001-12-05 10:02:25 +08:00
|
|
|
/* expression case */
|
2001-10-31 06:43:19 +08:00
|
|
|
if (tok != ';') {
|
|
|
|
expr();
|
|
|
|
}
|
|
|
|
skip(';');
|
|
|
|
}
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-18 01:22:38 +08:00
|
|
|
/* t is the array or struct type. c is the array or struct
|
2001-11-27 06:57:27 +08:00
|
|
|
address. cur_index/cur_field is the pointer to the current
|
|
|
|
value. 'size_only' is true if only size info is needed (only used
|
|
|
|
in arrays) */
|
2001-11-29 06:48:43 +08:00
|
|
|
void decl_designator(int t, int c,
|
|
|
|
int *cur_index, Sym **cur_field,
|
|
|
|
int size_only)
|
2001-11-18 01:22:38 +08:00
|
|
|
{
|
|
|
|
Sym *s, *f;
|
2001-12-05 10:02:25 +08:00
|
|
|
int notfirst, index, align, l;
|
2001-11-18 01:22:38 +08:00
|
|
|
|
|
|
|
notfirst = 0;
|
2001-12-05 10:02:25 +08:00
|
|
|
if (gnu_ext && (l = is_label()) != 0)
|
|
|
|
goto struct_field;
|
|
|
|
|
2001-11-18 01:22:38 +08:00
|
|
|
while (tok == '[' || tok == '.') {
|
|
|
|
if (tok == '[') {
|
|
|
|
if (!(t & VT_ARRAY))
|
|
|
|
expect("array type");
|
|
|
|
s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT));
|
|
|
|
next();
|
|
|
|
index = expr_const();
|
|
|
|
if (index < 0 || (s->c >= 0 && index >= s->c))
|
|
|
|
expect("invalid index");
|
|
|
|
skip(']');
|
|
|
|
if (!notfirst)
|
|
|
|
*cur_index = index;
|
|
|
|
t = pointed_type(t);
|
|
|
|
c += index * type_size(t, &align);
|
|
|
|
} else {
|
2001-12-05 10:02:25 +08:00
|
|
|
next();
|
|
|
|
l = tok;
|
|
|
|
next();
|
|
|
|
struct_field:
|
2001-11-18 01:22:38 +08:00
|
|
|
if (!(t & VT_STRUCT))
|
|
|
|
expect("struct/union type");
|
|
|
|
s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT) | SYM_STRUCT);
|
2001-12-05 10:02:25 +08:00
|
|
|
l |= SYM_FIELD;
|
2001-11-18 01:22:38 +08:00
|
|
|
f = s->next;
|
|
|
|
while (f) {
|
2001-12-05 10:02:25 +08:00
|
|
|
if (f->v == l)
|
2001-11-18 01:22:38 +08:00
|
|
|
break;
|
|
|
|
f = f->next;
|
|
|
|
}
|
|
|
|
if (!f)
|
|
|
|
expect("field");
|
|
|
|
if (!notfirst)
|
|
|
|
*cur_field = f;
|
2001-12-08 23:04:01 +08:00
|
|
|
t = f->t | (t & ~VT_TYPE);
|
2001-11-18 01:22:38 +08:00
|
|
|
c += f->c;
|
|
|
|
}
|
|
|
|
notfirst = 1;
|
|
|
|
}
|
|
|
|
if (notfirst) {
|
2001-12-05 10:02:25 +08:00
|
|
|
if (tok == '=') {
|
|
|
|
next();
|
|
|
|
} else {
|
|
|
|
if (!gnu_ext)
|
|
|
|
expect("=");
|
|
|
|
}
|
2001-11-18 01:22:38 +08:00
|
|
|
} else {
|
|
|
|
if (t & VT_ARRAY) {
|
|
|
|
index = *cur_index;
|
|
|
|
t = pointed_type(t);
|
|
|
|
c += index * type_size(t, &align);
|
|
|
|
} else {
|
|
|
|
f = *cur_field;
|
|
|
|
if (!f)
|
|
|
|
error("too many field init");
|
2001-12-08 23:04:01 +08:00
|
|
|
t = f->t | (t & ~VT_TYPE);
|
2001-11-18 01:22:38 +08:00
|
|
|
c += f->c;
|
|
|
|
}
|
|
|
|
}
|
2001-11-27 06:57:27 +08:00
|
|
|
decl_initializer(t, c, 0, size_only);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* store a value or an expression directly in global data or in local array */
|
|
|
|
|
|
|
|
void init_putv(int t, int c, int v, int is_expr)
|
|
|
|
{
|
2001-12-02 22:47:48 +08:00
|
|
|
int saved_global_expr;
|
|
|
|
|
2001-11-27 06:57:27 +08:00
|
|
|
if ((t & VT_VALMASK) == VT_CONST) {
|
2001-12-02 22:47:48 +08:00
|
|
|
if (is_expr) {
|
|
|
|
/* compound literals must be allocated globally in this case */
|
|
|
|
saved_global_expr = global_expr;
|
|
|
|
global_expr = 1;
|
2001-11-27 06:57:27 +08:00
|
|
|
v = expr_const();
|
2001-12-02 22:47:48 +08:00
|
|
|
global_expr = saved_global_expr;
|
|
|
|
}
|
2001-11-27 06:57:27 +08:00
|
|
|
if (t & VT_BYTE)
|
|
|
|
*(char *)c = v;
|
|
|
|
else if (t & VT_SHORT)
|
|
|
|
*(short *)c = v;
|
|
|
|
else
|
|
|
|
*(int *)c = v;
|
|
|
|
} else {
|
|
|
|
vt = t;
|
|
|
|
vc = c;
|
|
|
|
vpush();
|
|
|
|
if (is_expr)
|
|
|
|
expr_eq();
|
|
|
|
else
|
|
|
|
vset(VT_CONST, v);
|
|
|
|
vstore();
|
|
|
|
}
|
2001-11-18 01:22:38 +08:00
|
|
|
}
|
|
|
|
|
2001-11-29 06:48:43 +08:00
|
|
|
/* put zeros for variable based init */
|
|
|
|
void init_putz(int t, int c, int size)
|
|
|
|
{
|
2001-12-03 00:25:49 +08:00
|
|
|
GFuncContext gf;
|
2001-11-29 06:48:43 +08:00
|
|
|
|
|
|
|
if ((t & VT_VALMASK) == VT_CONST) {
|
|
|
|
/* nothing to do because global are already set to zero */
|
|
|
|
} else {
|
2001-12-03 00:25:49 +08:00
|
|
|
gfunc_start(&gf);
|
2001-11-29 06:48:43 +08:00
|
|
|
vset(VT_CONST, size);
|
2001-12-03 00:25:49 +08:00
|
|
|
gfunc_param(&gf);
|
2001-11-29 06:48:43 +08:00
|
|
|
vset(VT_CONST, 0);
|
2001-12-03 00:25:49 +08:00
|
|
|
gfunc_param(&gf);
|
2001-11-29 06:48:43 +08:00
|
|
|
vset(VT_LOCAL, c);
|
2001-12-03 00:25:49 +08:00
|
|
|
gfunc_param(&gf);
|
2001-12-09 04:24:33 +08:00
|
|
|
vset(VT_CONST, (int)&memset);
|
|
|
|
gfunc_call(&gf);
|
2001-11-29 06:48:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-18 01:22:38 +08:00
|
|
|
/* 't' contains the type and storage info. c is the address of the
|
|
|
|
object. 'first' is true if array '{' must be read (multi dimension
|
2001-11-27 06:57:27 +08:00
|
|
|
implicit array init handling). 'size_only' is true if size only
|
|
|
|
evaluation is wanted (only for arrays). */
|
|
|
|
void decl_initializer(int t, int c, int first, int size_only)
|
2001-11-18 01:22:38 +08:00
|
|
|
{
|
2001-11-29 06:48:43 +08:00
|
|
|
int index, array_length, n, no_oblock, nb, parlevel, i;
|
|
|
|
int t1, size1, align1;
|
2001-11-18 01:22:38 +08:00
|
|
|
Sym *s, *f;
|
|
|
|
TokenSym *ts;
|
|
|
|
|
|
|
|
if (t & VT_ARRAY) {
|
|
|
|
s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT));
|
|
|
|
n = s->c;
|
2001-11-27 06:57:27 +08:00
|
|
|
array_length = 0;
|
2001-11-29 06:48:43 +08:00
|
|
|
t1 = pointed_type(t);
|
|
|
|
size1 = type_size(t1, &align1);
|
2001-12-02 22:47:48 +08:00
|
|
|
|
|
|
|
no_oblock = 1;
|
|
|
|
if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
|
|
|
|
tok == '{') {
|
|
|
|
skip('{');
|
|
|
|
no_oblock = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* only parse strings here if correct type (otherwise: handle
|
|
|
|
them as ((w)char *) expressions */
|
|
|
|
if ((tok == TOK_LSTR &&
|
|
|
|
(t1 & VT_TYPE & ~VT_UNSIGNED) == VT_INT) ||
|
|
|
|
(tok == TOK_STR &&
|
|
|
|
(t1 & VT_TYPE & ~VT_UNSIGNED) == VT_BYTE)) {
|
2001-11-27 06:57:27 +08:00
|
|
|
/* XXX: move multiple string parsing in parser ? */
|
2001-11-29 06:48:43 +08:00
|
|
|
while (tok == TOK_STR || tok == TOK_LSTR) {
|
2001-11-27 06:57:27 +08:00
|
|
|
ts = (TokenSym *)tokc;
|
|
|
|
/* compute maximum number of chars wanted */
|
|
|
|
nb = ts->len;
|
|
|
|
if (n >= 0 && nb > (n - array_length))
|
|
|
|
nb = n - array_length;
|
|
|
|
if (!size_only) {
|
|
|
|
if (ts->len > nb)
|
|
|
|
warning("initializer-string for array is too long");
|
|
|
|
for(i=0;i<nb;i++) {
|
2001-11-29 06:48:43 +08:00
|
|
|
init_putv(t1, c + (array_length + i) * size1,
|
|
|
|
ts->str[i], 0);
|
2001-11-27 06:57:27 +08:00
|
|
|
}
|
2001-11-18 01:22:38 +08:00
|
|
|
}
|
2001-11-27 06:57:27 +08:00
|
|
|
array_length += nb;
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
/* only add trailing zero if enough storage (no
|
|
|
|
warning in this case since it is standard) */
|
|
|
|
if (n < 0 || array_length < n) {
|
|
|
|
if (!size_only) {
|
2001-11-29 06:48:43 +08:00
|
|
|
init_putv(t1, c + (array_length * size1), 0, 0);
|
2001-11-27 06:57:27 +08:00
|
|
|
}
|
|
|
|
array_length++;
|
2001-11-18 01:22:38 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
index = 0;
|
|
|
|
while (tok != '}') {
|
2001-11-27 06:57:27 +08:00
|
|
|
decl_designator(t, c, &index, NULL, size_only);
|
2001-11-18 01:22:38 +08:00
|
|
|
if (n >= 0 && index >= n)
|
|
|
|
error("index too large");
|
2001-11-29 06:48:43 +08:00
|
|
|
/* must put zero in holes (note that doing it that way
|
|
|
|
ensures that it even works with designators) */
|
|
|
|
if (!size_only && array_length < index) {
|
|
|
|
init_putz(t1, c + array_length * size1,
|
|
|
|
(index - array_length) * size1);
|
|
|
|
}
|
2001-11-18 01:22:38 +08:00
|
|
|
index++;
|
2001-11-27 06:57:27 +08:00
|
|
|
if (index > array_length)
|
|
|
|
array_length = index;
|
2001-11-18 01:22:38 +08:00
|
|
|
/* special test for multi dimensional arrays (may not
|
|
|
|
be strictly correct if designators are used at the
|
|
|
|
same time) */
|
|
|
|
if (index >= n && no_oblock)
|
|
|
|
break;
|
|
|
|
if (tok == '}')
|
|
|
|
break;
|
|
|
|
skip(',');
|
|
|
|
}
|
|
|
|
}
|
2001-12-02 22:47:48 +08:00
|
|
|
if (!no_oblock)
|
|
|
|
skip('}');
|
2001-11-29 06:48:43 +08:00
|
|
|
/* put zeros at the end */
|
|
|
|
if (!size_only && n >= 0 && array_length < n) {
|
|
|
|
init_putz(t1, c + array_length * size1,
|
|
|
|
(n - array_length) * size1);
|
|
|
|
}
|
2001-11-18 01:22:38 +08:00
|
|
|
/* patch type size if needed */
|
|
|
|
if (n < 0)
|
2001-11-27 06:57:27 +08:00
|
|
|
s->c = array_length;
|
2001-12-05 08:45:08 +08:00
|
|
|
} else if ((t & VT_STRUCT) && tok == '{') {
|
2001-11-18 01:22:38 +08:00
|
|
|
/* XXX: union needs only one init */
|
2001-12-05 08:45:08 +08:00
|
|
|
next();
|
2001-11-18 01:22:38 +08:00
|
|
|
s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT) | SYM_STRUCT);
|
|
|
|
f = s->next;
|
2001-11-29 06:48:43 +08:00
|
|
|
array_length = 0;
|
|
|
|
index = 0;
|
|
|
|
n = s->c;
|
2001-11-18 01:22:38 +08:00
|
|
|
while (tok != '}') {
|
2001-11-27 06:57:27 +08:00
|
|
|
decl_designator(t, c, NULL, &f, size_only);
|
2001-11-29 06:48:43 +08:00
|
|
|
/* fill with zero between fields */
|
|
|
|
index = f->c;
|
|
|
|
if (!size_only && array_length < index) {
|
|
|
|
init_putz(t, c + array_length,
|
|
|
|
index - array_length);
|
|
|
|
}
|
|
|
|
index = index + type_size(f->t, &align1);
|
|
|
|
if (index > array_length)
|
|
|
|
array_length = index;
|
2001-11-18 01:22:38 +08:00
|
|
|
if (tok == '}')
|
|
|
|
break;
|
|
|
|
skip(',');
|
|
|
|
f = f->next;
|
|
|
|
}
|
2001-11-29 06:48:43 +08:00
|
|
|
/* put zeros at the end */
|
|
|
|
if (!size_only && array_length < n) {
|
|
|
|
init_putz(t, c + array_length,
|
|
|
|
n - array_length);
|
|
|
|
}
|
|
|
|
skip('}');
|
|
|
|
} else if (tok == '{') {
|
|
|
|
next();
|
|
|
|
decl_initializer(t, c, first, size_only);
|
2001-11-18 01:22:38 +08:00
|
|
|
skip('}');
|
2001-11-27 06:57:27 +08:00
|
|
|
} else if (size_only) {
|
|
|
|
/* just skip expression */
|
|
|
|
parlevel = 0;
|
|
|
|
while ((parlevel > 0 || (tok != '}' && tok != ',')) &&
|
|
|
|
tok != -1) {
|
|
|
|
if (tok == '(')
|
|
|
|
parlevel++;
|
|
|
|
else if (tok == ')')
|
|
|
|
parlevel--;
|
|
|
|
next();
|
2001-11-29 06:48:43 +08:00
|
|
|
}
|
2001-11-18 01:22:38 +08:00
|
|
|
} else {
|
2001-11-27 06:57:27 +08:00
|
|
|
init_putv(t, c, 0, 1);
|
2001-11-18 01:22:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-02 22:47:48 +08:00
|
|
|
/* parse an initializer for type 't' if 'has_init' is true, and
|
|
|
|
allocate space in local or global data space. The allocated address
|
|
|
|
in returned */
|
|
|
|
int decl_initializer_alloc(int t, int has_init)
|
|
|
|
{
|
|
|
|
int size, align, addr, tok1;
|
|
|
|
int *init_str, init_len, level, *saved_macro_ptr;
|
|
|
|
|
|
|
|
size = type_size(t, &align);
|
|
|
|
/* If unknown size, we must evaluate it before
|
|
|
|
evaluating initializers because
|
|
|
|
initializers can generate global data too
|
|
|
|
(e.g. string pointers or ISOC99 compound
|
|
|
|
literals). It also simplifies local
|
|
|
|
initializers handling */
|
|
|
|
init_len = 0;
|
|
|
|
init_str = NULL;
|
|
|
|
saved_macro_ptr = NULL; /* avoid warning */
|
|
|
|
tok1 = 0;
|
|
|
|
if (size < 0) {
|
|
|
|
if (!has_init)
|
|
|
|
error("unknown type size");
|
|
|
|
/* get all init string */
|
|
|
|
level = 0;
|
|
|
|
while (level > 0 || (tok != ',' && tok != ';')) {
|
|
|
|
if (tok < 0)
|
|
|
|
error("unexpect end of file in initializer");
|
|
|
|
tok_add2(&init_str, &init_len, tok, tokc);
|
|
|
|
if (tok == '{')
|
|
|
|
level++;
|
|
|
|
else if (tok == '}') {
|
|
|
|
if (level == 0)
|
|
|
|
break;
|
|
|
|
level--;
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
tok1 = tok;
|
|
|
|
tok_add(&init_str, &init_len, -1);
|
|
|
|
tok_add(&init_str, &init_len, 0);
|
|
|
|
|
|
|
|
/* compute size */
|
|
|
|
saved_macro_ptr = macro_ptr;
|
|
|
|
macro_ptr = init_str;
|
|
|
|
next();
|
|
|
|
decl_initializer(t, 0, 1, 1);
|
|
|
|
/* prepare second initializer parsing */
|
|
|
|
macro_ptr = init_str;
|
|
|
|
next();
|
|
|
|
|
|
|
|
/* if still unknown size, error */
|
|
|
|
size = type_size(t, &align);
|
|
|
|
if (size < 0)
|
|
|
|
error("unknown type size");
|
|
|
|
}
|
|
|
|
if ((t & VT_VALMASK) == VT_LOCAL) {
|
|
|
|
loc = (loc - size) & -align;
|
|
|
|
addr = loc;
|
|
|
|
} else {
|
|
|
|
glo = (glo + align - 1) & -align;
|
|
|
|
addr = glo;
|
|
|
|
/* very important to increment global
|
|
|
|
pointer at this time because
|
|
|
|
initializers themselves can create new
|
|
|
|
initializers */
|
|
|
|
glo += size;
|
|
|
|
}
|
|
|
|
if (has_init) {
|
|
|
|
decl_initializer(t, addr, 1, 0);
|
|
|
|
/* restore parse state if needed */
|
|
|
|
if (init_str) {
|
|
|
|
free(init_str);
|
|
|
|
macro_ptr = saved_macro_ptr;
|
|
|
|
tok = tok1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-30 07:23:01 +08:00
|
|
|
/* 'l' is VT_LOCAL or VT_CONST to define default storage type */
|
2001-12-05 08:45:08 +08:00
|
|
|
void decl(int l)
|
2001-10-28 07:48:39 +08:00
|
|
|
{
|
2001-12-05 08:45:08 +08:00
|
|
|
int *a, t, b, v, u, addr, has_init, size, align;
|
2001-11-06 09:20:20 +08:00
|
|
|
Sym *sym;
|
2001-11-27 06:57:27 +08:00
|
|
|
|
2001-12-02 22:59:18 +08:00
|
|
|
while (1) {
|
|
|
|
b = ist();
|
|
|
|
if (!b) {
|
2001-12-05 08:45:08 +08:00
|
|
|
/* skip redundant ';' */
|
|
|
|
/* XXX: find more elegant solution */
|
|
|
|
if (tok == ';') {
|
|
|
|
next();
|
|
|
|
continue;
|
|
|
|
}
|
2001-12-02 22:59:18 +08:00
|
|
|
/* special test for old K&R protos without explicit int
|
|
|
|
type. Only accepted when defining global data */
|
|
|
|
if (l == VT_LOCAL || tok < TOK_DEFINE)
|
|
|
|
break;
|
|
|
|
b = VT_INT;
|
|
|
|
}
|
2001-11-01 22:48:10 +08:00
|
|
|
if ((b & (VT_ENUM | VT_STRUCT)) && tok == ';') {
|
2001-10-31 06:43:19 +08:00
|
|
|
/* we accept no variable after */
|
|
|
|
next();
|
|
|
|
continue;
|
|
|
|
}
|
2001-10-28 07:48:39 +08:00
|
|
|
while (1) { /* iterate thru each declaration */
|
2001-11-12 00:07:43 +08:00
|
|
|
t = type_decl(&v, b, TYPE_DIRECT);
|
2001-10-28 07:48:39 +08:00
|
|
|
if (tok == '{') {
|
2001-12-05 08:45:08 +08:00
|
|
|
if (l == VT_LOCAL)
|
|
|
|
error("cannot use local functions");
|
2001-11-08 07:13:29 +08:00
|
|
|
if (!(t & VT_FUNC))
|
2001-11-29 06:48:43 +08:00
|
|
|
expect("function definition");
|
2001-10-30 07:23:01 +08:00
|
|
|
/* patch forward references */
|
2001-11-01 22:48:10 +08:00
|
|
|
if ((sym = sym_find(v)) && (sym->t & VT_FORWARD)) {
|
2001-12-05 08:45:08 +08:00
|
|
|
greloc_patch(sym, ind);
|
|
|
|
sym->t = VT_CONST | t;
|
2001-11-01 22:48:10 +08:00
|
|
|
} else {
|
|
|
|
/* put function address */
|
2001-12-05 08:45:08 +08:00
|
|
|
sym_push1(&global_stack, v, VT_CONST | t, ind);
|
2001-11-01 22:48:10 +08:00
|
|
|
}
|
2001-11-14 06:54:35 +08:00
|
|
|
funcname = get_tok_str(v, 0);
|
2001-11-08 07:13:29 +08:00
|
|
|
/* push a dummy symbol to enable local sym storage */
|
|
|
|
sym_push1(&local_stack, 0, 0, 0);
|
|
|
|
/* define parameters */
|
|
|
|
sym = sym_find((unsigned)t >> VT_STRUCT_SHIFT);
|
2001-12-03 05:24:43 +08:00
|
|
|
/* XXX: the following is x86 dependant -> move it to
|
|
|
|
x86 code gen */
|
|
|
|
addr = 8;
|
|
|
|
/* if the function returns a structure, then add an
|
|
|
|
implicit pointer parameter */
|
|
|
|
func_vt = sym->t;
|
|
|
|
if (func_vt & VT_STRUCT) {
|
|
|
|
func_vc = addr;
|
|
|
|
addr += 4;
|
|
|
|
}
|
|
|
|
while (sym = sym->next) {
|
|
|
|
u = sym->t;
|
|
|
|
sym_push(sym->v & ~SYM_FIELD,
|
|
|
|
u | VT_LOCAL | VT_LVAL,
|
|
|
|
addr);
|
|
|
|
if (u & VT_STRUCT) {
|
|
|
|
#ifdef FUNC_STRUCT_PARAM_AS_PTR
|
|
|
|
/* structs are passed as pointer */
|
|
|
|
size = 4;
|
|
|
|
#else
|
|
|
|
/* structs are directly put on stack (x86
|
|
|
|
like) */
|
|
|
|
size = type_size(u, &align);
|
|
|
|
size = (size + 3) & ~3;
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
/* XXX: size will be different someday */
|
|
|
|
size = 4;
|
|
|
|
}
|
|
|
|
addr += size;
|
|
|
|
}
|
2001-10-28 07:48:39 +08:00
|
|
|
loc = 0;
|
|
|
|
o(0xe58955); /* push %ebp, mov %esp, %ebp */
|
2001-11-01 22:48:10 +08:00
|
|
|
a = (int *)oad(0xec81, 0); /* sub $xxx, %esp */
|
2001-10-28 07:48:39 +08:00
|
|
|
rsym = 0;
|
2001-11-06 09:20:20 +08:00
|
|
|
block(0, 0, 0, 0, 0);
|
2001-10-28 07:48:39 +08:00
|
|
|
gsym(rsym);
|
|
|
|
o(0xc3c9); /* leave, ret */
|
2001-10-30 07:23:01 +08:00
|
|
|
*a = (-loc + 3) & -4; /* align local size to word &
|
|
|
|
save local variables */
|
2001-11-01 22:48:10 +08:00
|
|
|
sym_pop(&label_stack, 0); /* reset label stack */
|
|
|
|
sym_pop(&local_stack, 0); /* reset local stack */
|
2001-12-03 05:24:43 +08:00
|
|
|
funcname = ""; /* for safety */
|
|
|
|
func_vt = VT_VOID; /* for safety */
|
2001-10-28 07:48:39 +08:00
|
|
|
break;
|
|
|
|
} else {
|
2001-11-06 09:20:20 +08:00
|
|
|
if (b & VT_TYPEDEF) {
|
2001-12-08 23:04:01 +08:00
|
|
|
/* save typedefed type */
|
|
|
|
/* XXX: test storage specifiers ? */
|
2001-11-06 09:20:20 +08:00
|
|
|
sym_push(v, t | VT_TYPEDEF, 0);
|
2001-10-31 06:43:19 +08:00
|
|
|
} else if (t & VT_FUNC) {
|
2001-10-30 07:23:01 +08:00
|
|
|
/* external function definition */
|
2001-12-05 08:45:08 +08:00
|
|
|
external_sym(v, t);
|
2001-10-28 07:48:39 +08:00
|
|
|
} else {
|
2001-10-30 07:23:01 +08:00
|
|
|
/* not lvalue if array */
|
|
|
|
if (!(t & VT_ARRAY))
|
|
|
|
t |= VT_LVAL;
|
2001-11-12 00:07:43 +08:00
|
|
|
if (b & VT_EXTERN) {
|
2001-10-30 07:23:01 +08:00
|
|
|
/* external variable */
|
2001-12-05 08:45:08 +08:00
|
|
|
external_sym(v, t);
|
2001-10-30 07:23:01 +08:00
|
|
|
} else {
|
|
|
|
u = l;
|
|
|
|
if (t & VT_STATIC)
|
|
|
|
u = VT_CONST;
|
2001-11-01 22:48:10 +08:00
|
|
|
u |= t;
|
2001-12-02 22:47:48 +08:00
|
|
|
has_init = (tok == '=');
|
|
|
|
if (has_init)
|
2001-11-18 01:22:38 +08:00
|
|
|
next();
|
2001-12-02 22:47:48 +08:00
|
|
|
addr = decl_initializer_alloc(u, has_init);
|
2001-12-05 10:02:25 +08:00
|
|
|
if (l == VT_CONST) {
|
|
|
|
/* global scope: see if already defined */
|
|
|
|
sym = sym_find(v);
|
|
|
|
if (!sym)
|
|
|
|
goto do_def;
|
|
|
|
if (!is_compatible_types(sym->t, u))
|
|
|
|
error("incompatible types for redefinition of '%s'",
|
|
|
|
get_tok_str(v, 0));
|
|
|
|
if (!(sym->t & VT_FORWARD))
|
|
|
|
error("redefinition of '%s'", get_tok_str(v, 0));
|
|
|
|
greloc_patch(sym, addr);
|
|
|
|
} else {
|
|
|
|
do_def:
|
|
|
|
sym_push(v, u, addr);
|
|
|
|
}
|
2001-10-30 07:23:01 +08:00
|
|
|
}
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
|
|
|
if (tok != ',') {
|
|
|
|
skip(';');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-08 23:04:01 +08:00
|
|
|
/* put all global symbols in the extern stack and do all the
|
|
|
|
resolving which can be done without using external symbols from DLLs */
|
|
|
|
/* XXX: could try to verify types, but would not to save them in
|
|
|
|
extern_stack too */
|
|
|
|
void resolve_global_syms(void)
|
|
|
|
{
|
|
|
|
Sym *s, *s1, *ext_sym;
|
|
|
|
Reloc **p;
|
|
|
|
|
|
|
|
s = global_stack.top;
|
|
|
|
while (s != NULL) {
|
|
|
|
s1 = s->prev;
|
|
|
|
/* do not save static or typedefed symbols or types */
|
|
|
|
if (!(s->t & (VT_STATIC | VT_TYPEDEF)) &&
|
|
|
|
!(s->v & (SYM_FIELD | SYM_STRUCT)) &&
|
|
|
|
(s->v < SYM_FIRST_ANOM)) {
|
|
|
|
ext_sym = sym_find1(&extern_stack, s->v);
|
|
|
|
if (!ext_sym) {
|
|
|
|
/* if the symbol do not exist, we simply save it */
|
|
|
|
sym_push1(&extern_stack, s->v, s->t, s->c);
|
|
|
|
} else if (ext_sym->t & VT_FORWARD) {
|
|
|
|
/* external symbol already exists, but only as forward
|
|
|
|
definition */
|
|
|
|
if (!(s->t & VT_FORWARD)) {
|
|
|
|
/* s is not forward, so we can relocate all symbols */
|
|
|
|
greloc_patch(ext_sym, s->c);
|
|
|
|
} else {
|
|
|
|
/* the two symbols are forward: merge them */
|
|
|
|
p = (Reloc **)&ext_sym->c;
|
|
|
|
while (*p != NULL)
|
|
|
|
p = &(*p)->next;
|
|
|
|
*p = (Reloc *)s->c;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* external symbol already exists and is defined :
|
|
|
|
patch all references to it */
|
|
|
|
if (!(s->t & VT_FORWARD))
|
|
|
|
error("'%s' defined twice", get_tok_str(s->v, 0));
|
|
|
|
greloc_patch(s, ext_sym->c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s = s1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* compile a C file. Return non zero if errors. */
|
|
|
|
int tcc_compile_file(const char *filename1)
|
|
|
|
{
|
|
|
|
Sym *define_start;
|
|
|
|
|
|
|
|
filename = (char *)filename1;
|
|
|
|
|
|
|
|
line_num = 1;
|
|
|
|
funcname = "";
|
|
|
|
file = fopen(filename, "r");
|
|
|
|
if (!file)
|
|
|
|
error("file '%s' not found", filename);
|
|
|
|
include_stack_ptr = include_stack;
|
|
|
|
ifdef_stack_ptr = ifdef_stack;
|
|
|
|
|
|
|
|
vstack_ptr = vstack;
|
|
|
|
anon_sym = SYM_FIRST_ANOM;
|
|
|
|
|
|
|
|
define_start = define_stack.top;
|
|
|
|
inp();
|
|
|
|
ch = '\n'; /* needed to parse correctly first preprocessor command */
|
|
|
|
next();
|
|
|
|
decl(VT_CONST);
|
|
|
|
if (tok != -1)
|
|
|
|
expect("declaration");
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
/* reset define stack, but leave -Dsymbols (may be incorrect if
|
|
|
|
they are undefined) */
|
|
|
|
sym_pop(&define_stack, define_start);
|
|
|
|
|
|
|
|
resolve_global_syms();
|
|
|
|
|
|
|
|
sym_pop(&global_stack, NULL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-11-12 00:07:43 +08:00
|
|
|
/* open a dynamic library so that its symbol are available for
|
|
|
|
compiled programs */
|
|
|
|
void open_dll(char *libname)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
void *h;
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "lib%s.so", libname);
|
|
|
|
h = dlopen(buf, RTLD_GLOBAL | RTLD_LAZY);
|
|
|
|
if (!h)
|
|
|
|
error((char *)dlerror());
|
|
|
|
}
|
|
|
|
|
2001-12-08 23:04:01 +08:00
|
|
|
void resolve_extern_syms(void)
|
2001-12-05 08:45:08 +08:00
|
|
|
{
|
|
|
|
Sym *s, *s1;
|
|
|
|
char *str;
|
|
|
|
int addr;
|
|
|
|
|
2001-12-08 23:04:01 +08:00
|
|
|
s = extern_stack.top;
|
2001-12-05 08:45:08 +08:00
|
|
|
while (s != NULL) {
|
|
|
|
s1 = s->prev;
|
|
|
|
if (s->t & VT_FORWARD) {
|
|
|
|
/* if there is at least one relocation to do, then find it
|
|
|
|
and patch it */
|
|
|
|
if (s->c) {
|
|
|
|
str = get_tok_str(s->v, 0);
|
|
|
|
addr = (int)dlsym(NULL, str);
|
|
|
|
if (!addr)
|
|
|
|
error("unresolved external reference '%s'", str);
|
|
|
|
greloc_patch(s, addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s = s1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-19 00:33:01 +08:00
|
|
|
/* output a binary file (for testing) */
|
|
|
|
void build_exe(char *filename)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
f = fopen(filename, "w");
|
|
|
|
fwrite((void *)prog, 1, ind - prog, f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
2001-11-11 10:53:01 +08:00
|
|
|
int main(int argc, char **argv)
|
2001-10-28 07:48:39 +08:00
|
|
|
{
|
2001-11-01 22:48:10 +08:00
|
|
|
Sym *s;
|
2001-10-28 07:48:39 +08:00
|
|
|
int (*t)();
|
2001-11-19 00:33:01 +08:00
|
|
|
char *p, *r, *outfile;
|
2001-11-11 10:53:01 +08:00
|
|
|
int optind;
|
|
|
|
|
|
|
|
include_paths[0] = "/usr/include";
|
|
|
|
include_paths[1] = "/usr/lib/tcc";
|
|
|
|
include_paths[2] = "/usr/local/lib/tcc";
|
|
|
|
nb_include_paths = 3;
|
|
|
|
|
|
|
|
/* add all tokens */
|
2001-11-19 00:33:01 +08:00
|
|
|
tok_ident = TOK_IDENT;
|
2001-11-18 01:22:38 +08:00
|
|
|
p = "int\0void\0char\0if\0else\0while\0break\0return\0for\0extern\0static\0unsigned\0goto\0do\0continue\0switch\0case\0const\0volatile\0long\0register\0signed\0auto\0inline\0restrict\0float\0double\0short\0struct\0union\0typedef\0default\0enum\0sizeof\0define\0include\0ifdef\0ifndef\0elif\0endif\0defined\0undef\0error\0line\0__LINE__\0__FILE__\0__DATE__\0__TIME__\0__VA_ARGS__\0__func__\0main\0";
|
2001-11-11 10:53:01 +08:00
|
|
|
while (*p) {
|
|
|
|
r = p;
|
|
|
|
while (*r++);
|
|
|
|
tok_alloc(p, r - p - 1);
|
|
|
|
p = r;
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
2001-11-11 10:53:01 +08:00
|
|
|
|
2001-11-12 00:07:43 +08:00
|
|
|
/* standard defines */
|
|
|
|
define_symbol("__STDC__");
|
|
|
|
#ifdef __i386__
|
|
|
|
define_symbol("__i386__");
|
|
|
|
#endif
|
2001-12-08 23:04:01 +08:00
|
|
|
/* tiny C specific defines */
|
|
|
|
define_symbol("__TINYC__");
|
2001-11-12 00:07:43 +08:00
|
|
|
|
2001-12-08 23:04:01 +08:00
|
|
|
glo = (int)malloc(DATA_SIZE);
|
|
|
|
memset((void *)glo, 0, DATA_SIZE);
|
|
|
|
prog = (int)malloc(TEXT_SIZE);
|
|
|
|
ind = prog;
|
|
|
|
|
2001-11-11 10:53:01 +08:00
|
|
|
optind = 1;
|
2001-11-19 00:33:01 +08:00
|
|
|
outfile = NULL;
|
2001-11-11 10:53:01 +08:00
|
|
|
while (1) {
|
|
|
|
if (optind >= argc) {
|
2001-11-19 00:33:01 +08:00
|
|
|
show_help:
|
2001-12-03 05:44:49 +08:00
|
|
|
printf("tcc version 0.9.1 - Tiny C Compiler - Copyright (C) 2001 Fabrice Bellard\n"
|
2001-12-08 23:04:01 +08:00
|
|
|
"usage: tcc [-Idir] [-Dsym] [-llib] [-i infile]... infile [infile_args...]\n");
|
2001-11-11 10:53:01 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
r = argv[optind];
|
|
|
|
if (r[0] != '-')
|
|
|
|
break;
|
2001-11-19 00:33:01 +08:00
|
|
|
optind++;
|
2001-11-11 10:53:01 +08:00
|
|
|
if (r[1] == 'I') {
|
|
|
|
if (nb_include_paths >= INCLUDE_PATHS_MAX)
|
|
|
|
error("too many include paths");
|
|
|
|
include_paths[nb_include_paths++] = r + 2;
|
2001-11-12 00:07:43 +08:00
|
|
|
} else if (r[1] == 'D') {
|
|
|
|
define_symbol(r + 2);
|
|
|
|
} else if (r[1] == 'l') {
|
|
|
|
open_dll(r + 2);
|
2001-12-08 23:04:01 +08:00
|
|
|
} else if (r[1] == 'i') {
|
|
|
|
if (optind >= argc)
|
|
|
|
goto show_help;
|
|
|
|
tcc_compile_file(argv[optind++]);
|
2001-11-19 00:33:01 +08:00
|
|
|
} else if (r[1] == 'o') {
|
|
|
|
/* currently, only for testing, so not documented */
|
|
|
|
if (optind >= argc)
|
|
|
|
goto show_help;
|
|
|
|
outfile = argv[optind++];
|
2001-11-11 10:53:01 +08:00
|
|
|
} else {
|
2001-11-12 00:07:43 +08:00
|
|
|
fprintf(stderr, "invalid option -- '%s'\n", r);
|
|
|
|
exit(1);
|
2001-11-11 10:53:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-08 23:04:01 +08:00
|
|
|
tcc_compile_file(argv[optind]);
|
|
|
|
|
|
|
|
resolve_extern_syms();
|
2001-11-08 09:12:31 +08:00
|
|
|
|
2001-11-19 00:33:01 +08:00
|
|
|
if (outfile) {
|
|
|
|
build_exe(outfile);
|
2001-10-28 07:48:39 +08:00
|
|
|
return 0;
|
2001-11-19 00:33:01 +08:00
|
|
|
} else {
|
2001-12-08 23:04:01 +08:00
|
|
|
s = sym_find1(&extern_stack, TOK_MAIN);
|
|
|
|
if (!s || (s->t & VT_FORWARD))
|
2001-11-19 00:33:01 +08:00
|
|
|
error("main() not defined");
|
|
|
|
t = (int (*)())s->c;
|
|
|
|
return (*t)(argc - optind, argv + optind);
|
|
|
|
}
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|