2001-11-01 22:48:10 +08:00
|
|
|
/*
|
|
|
|
* TCC - Tiny C Compiler
|
|
|
|
*
|
2002-01-05 04:17:24 +08:00
|
|
|
* Copyright (c) 2001, 2002 Fabrice Bellard
|
2001-11-01 22:48:10 +08:00
|
|
|
*
|
|
|
|
* 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>
|
2002-01-04 05:14:59 +08:00
|
|
|
#include <stdarg.h>
|
2001-12-07 07:39:24 +08:00
|
|
|
#include <string.h>
|
2001-12-14 06:28:53 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <math.h>
|
2002-01-05 04:17:24 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
2002-01-13 00:39:35 +08:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#ifndef WIN32
|
2002-01-05 04:17:24 +08:00
|
|
|
#include <sys/ucontext.h>
|
2002-01-04 05:14:59 +08:00
|
|
|
#include <sys/mman.h>
|
2002-01-13 00:39:35 +08:00
|
|
|
#endif
|
|
|
|
#include "elf.h"
|
|
|
|
#include "stab.h"
|
2001-12-07 07:39:24 +08:00
|
|
|
#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
|
|
|
|
2002-02-11 00:14:03 +08:00
|
|
|
/* target selection */
|
|
|
|
//#define TCC_TARGET_I386 /* i386 code generator */
|
|
|
|
//#define TCC_TARGET_IL /* .NET CLI generator */
|
|
|
|
|
|
|
|
/* default target is I386 */
|
|
|
|
#if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_IL)
|
|
|
|
#define TCC_TARGET_I386
|
|
|
|
#endif
|
|
|
|
|
2002-03-04 06:45:55 +08:00
|
|
|
#if !defined(WIN32) && !defined(TCC_UCLIBC) && !defined(TCC_TARGET_IL)
|
|
|
|
#define CONFIG_TCC_BCHECK /* enable bound checking code */
|
|
|
|
#endif
|
|
|
|
|
2002-01-13 19:07:19 +08:00
|
|
|
/* amount of virtual memory associated to a section (currently, we do
|
2002-01-04 05:14:59 +08:00
|
|
|
not realloc them) */
|
|
|
|
#define SECTION_VSIZE (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
|
|
|
|
2002-01-27 02:05:29 +08:00
|
|
|
#define TOK_HASH_SIZE 2048 /* must be a power of two */
|
|
|
|
#define TOK_ALLOC_INCR 512 /* must be a power of two */
|
|
|
|
#define SYM_HASH_SIZE 1031
|
2001-11-19 00:33:01 +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-12-14 06:28:53 +08:00
|
|
|
/* constant value */
|
|
|
|
typedef union CValue {
|
|
|
|
long double ld;
|
|
|
|
double d;
|
|
|
|
float f;
|
|
|
|
int i;
|
|
|
|
unsigned int ui;
|
2001-12-18 05:56:48 +08:00
|
|
|
unsigned int ul; /* address (should be unsigned long on 64 bit cpu) */
|
|
|
|
long long ll;
|
|
|
|
unsigned long long ull;
|
2001-12-14 06:28:53 +08:00
|
|
|
struct TokenSym *ts;
|
2001-12-18 05:56:48 +08:00
|
|
|
struct Sym *sym;
|
2001-12-20 09:05:21 +08:00
|
|
|
int tab[1];
|
2001-12-14 06:28:53 +08:00
|
|
|
} CValue;
|
|
|
|
|
2001-12-17 01:58:32 +08:00
|
|
|
/* value on stack */
|
|
|
|
typedef struct SValue {
|
2001-12-20 09:05:21 +08:00
|
|
|
int t; /* type */
|
2002-01-04 05:14:59 +08:00
|
|
|
unsigned short r; /* register + flags */
|
|
|
|
unsigned short r2; /* second register, used for 'long long'
|
|
|
|
type. If not used, set to VT_CONST */
|
2001-12-20 09:05:21 +08:00
|
|
|
CValue c; /* constant */
|
2001-12-17 01:58:32 +08:00
|
|
|
} SValue;
|
|
|
|
|
2001-11-01 22:48:10 +08:00
|
|
|
/* symbol management */
|
|
|
|
typedef struct Sym {
|
|
|
|
int v; /* symbol token */
|
|
|
|
int t; /* associated type */
|
2001-12-23 01:05:23 +08:00
|
|
|
int r; /* associated register */
|
2001-11-01 22:48:10 +08:00
|
|
|
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 */
|
|
|
|
|
2002-01-04 05:14:59 +08:00
|
|
|
/* section definition */
|
|
|
|
typedef struct Section {
|
|
|
|
char name[64]; /* section name */
|
|
|
|
unsigned char *data; /* section data */
|
|
|
|
unsigned char *data_ptr; /* current data pointer */
|
2002-01-05 04:17:24 +08:00
|
|
|
int sh_num; /* elf section number */
|
|
|
|
int sh_type; /* elf section type */
|
|
|
|
int sh_flags; /* elf section flags */
|
|
|
|
int sh_entsize; /* elf entry size */
|
|
|
|
struct Section *link; /* link to another section */
|
2002-01-04 05:14:59 +08:00
|
|
|
struct Section *next;
|
|
|
|
} Section;
|
|
|
|
|
|
|
|
/* GNUC attribute definition */
|
|
|
|
typedef struct AttributeDef {
|
|
|
|
int aligned;
|
|
|
|
Section *section;
|
2002-01-27 02:05:29 +08:00
|
|
|
unsigned char func_call; /* FUNC_CDECL or FUNC_STDCALL */
|
2002-01-04 05:14:59 +08:00
|
|
|
} AttributeDef;
|
2001-12-05 08:45:08 +08:00
|
|
|
|
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
|
|
|
|
2002-01-27 02:05:29 +08:00
|
|
|
/* stored in 'Sym.c' field */
|
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 ... */
|
|
|
|
|
2002-01-27 02:05:29 +08:00
|
|
|
/* stored in 'Sym.r' field */
|
|
|
|
#define FUNC_CDECL 0 /* standard c call */
|
|
|
|
#define FUNC_STDCALL 1 /* pascal c call */
|
|
|
|
|
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 */
|
|
|
|
|
2002-01-13 00:39:35 +08:00
|
|
|
#define IO_BUF_SIZE 8192
|
|
|
|
|
|
|
|
typedef struct BufferedFile {
|
|
|
|
unsigned char *buf_ptr;
|
|
|
|
unsigned char *buf_end;
|
|
|
|
int fd;
|
|
|
|
int line_num; /* current line number - here to simply code */
|
|
|
|
char filename[1024]; /* current filename - here to simply code */
|
|
|
|
unsigned char buffer[IO_BUF_SIZE + 1]; /* extra size for CH_EOB char */
|
|
|
|
} BufferedFile;
|
|
|
|
|
|
|
|
#define CH_EOB 0 /* end of buffer or '\0' char in file */
|
|
|
|
#define CH_EOF (-1) /* end of file */
|
2001-11-06 09:20:20 +08:00
|
|
|
|
2001-12-07 07:39:24 +08:00
|
|
|
/* parser */
|
2002-01-13 00:39:35 +08:00
|
|
|
struct BufferedFile *file;
|
2001-12-14 06:28:53 +08:00
|
|
|
int ch, ch1, tok, tok1;
|
|
|
|
CValue tokc, tok1c;
|
2001-12-07 07:39:24 +08:00
|
|
|
|
2002-01-04 05:14:59 +08:00
|
|
|
/* sections */
|
|
|
|
Section *first_section;
|
2002-01-05 04:17:24 +08:00
|
|
|
int section_num;
|
2002-01-04 05:14:59 +08:00
|
|
|
Section *text_section, *data_section, *bss_section; /* predefined sections */
|
|
|
|
Section *cur_text_section; /* current section where function code is
|
|
|
|
generated */
|
2002-01-06 00:15:57 +08:00
|
|
|
/* bound check related sections */
|
2002-01-05 04:17:24 +08:00
|
|
|
Section *bounds_section; /* contains global data bound description */
|
2002-01-06 00:15:57 +08:00
|
|
|
Section *lbounds_section; /* contains local data bound description */
|
2002-01-05 04:17:24 +08:00
|
|
|
/* debug sections */
|
|
|
|
Section *stab_section, *stabstr_section, *symtab_section, *strtab_section;
|
2002-01-04 05:14:59 +08:00
|
|
|
|
2001-11-01 22:48:10 +08:00
|
|
|
/* loc : local variable index
|
2002-01-04 05:14:59 +08:00
|
|
|
ind : output code index
|
2001-10-28 07:48:39 +08:00
|
|
|
rsym: return symbol
|
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,
|
2002-01-04 05:14:59 +08:00
|
|
|
prog, ind, loc, const_wanted;
|
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) */
|
2002-01-05 04:17:24 +08:00
|
|
|
int last_line_num, last_ind, func_ind; /* debug last line number and pc */
|
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];
|
2002-01-13 00:39:35 +08:00
|
|
|
char *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-12-17 01:58:32 +08:00
|
|
|
SValue vstack[VSTACK_SIZE], *vtop;
|
2001-11-11 10:53:01 +08:00
|
|
|
int *macro_ptr, *macro_ptr_allocated;
|
2002-01-13 00:39:35 +08:00
|
|
|
BufferedFile *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;
|
2002-01-05 04:17:24 +08:00
|
|
|
int char_pointer_type;
|
|
|
|
|
|
|
|
/* compile with debug symbol (and use them if error during execution) */
|
|
|
|
int do_debug = 0;
|
|
|
|
|
|
|
|
/* compile with built-in memory and bounds checker */
|
|
|
|
int do_bounds_check = 0;
|
2001-10-28 07:48:39 +08:00
|
|
|
|
2002-01-27 02:05:29 +08:00
|
|
|
/* display benchmark infos */
|
|
|
|
int do_bench = 0;
|
|
|
|
int total_lines;
|
|
|
|
int total_bytes;
|
|
|
|
|
2001-12-05 10:02:25 +08:00
|
|
|
/* use GNU C extensions */
|
|
|
|
int gnu_ext = 1;
|
|
|
|
|
2001-12-14 06:28:53 +08:00
|
|
|
/* use Tiny C extensions */
|
|
|
|
int tcc_ext = 1;
|
|
|
|
|
2001-11-06 09:20:20 +08:00
|
|
|
/* The current value can be: */
|
2002-01-06 00:15:57 +08:00
|
|
|
#define VT_VALMASK 0x00ff
|
|
|
|
#define VT_CONST 0x00f0 /* constant in vc
|
2001-11-06 09:20:20 +08:00
|
|
|
(must be first non register value) */
|
2002-01-06 00:15:57 +08:00
|
|
|
#define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
|
|
|
|
#define VT_LOCAL 0x00f2 /* offset on stack */
|
|
|
|
#define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
|
|
|
|
#define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
|
|
|
|
#define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
|
|
|
|
#define VT_LVAL 0x0100 /* var is an lvalue */
|
|
|
|
#define VT_FORWARD 0x0200 /* value is forward reference */
|
|
|
|
#define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
|
2002-01-05 08:40:47 +08:00
|
|
|
char/short stored in integer registers) */
|
2002-01-06 00:15:57 +08:00
|
|
|
#define VT_MUSTBOUND 0x0800 /* bound checking must be done before
|
|
|
|
dereferencing value */
|
2001-12-05 08:45:08 +08:00
|
|
|
|
2001-12-09 06:51:04 +08:00
|
|
|
/* types */
|
2001-12-10 06:04:15 +08:00
|
|
|
#define VT_STRUCT_SHIFT 16 /* structure/enum name shift (16 bits left) */
|
2001-12-09 06:51:04 +08:00
|
|
|
|
2001-12-23 01:05:23 +08:00
|
|
|
#define VT_INT 0 /* integer type */
|
|
|
|
#define VT_BYTE 1 /* signed byte type */
|
|
|
|
#define VT_SHORT 2 /* short type */
|
|
|
|
#define VT_VOID 3 /* void type */
|
|
|
|
#define VT_PTR 4 /* pointer increment */
|
|
|
|
#define VT_ENUM 5 /* enum definition */
|
|
|
|
#define VT_FUNC 6 /* function type */
|
|
|
|
#define VT_STRUCT 7 /* struct/union definition */
|
|
|
|
#define VT_FLOAT 8 /* IEEE float */
|
|
|
|
#define VT_DOUBLE 9 /* IEEE double */
|
|
|
|
#define VT_LDOUBLE 10 /* IEEE long double */
|
|
|
|
#define VT_BOOL 11 /* ISOC99 boolean type */
|
|
|
|
#define VT_LLONG 12 /* 64 bit integer */
|
|
|
|
#define VT_LONG 13 /* long integer (NEVER USED as type, only
|
|
|
|
during parsing) */
|
|
|
|
#define VT_BTYPE 0x000f /* mask for basic type */
|
|
|
|
#define VT_UNSIGNED 0x0010 /* unsigned type */
|
|
|
|
#define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
|
|
|
|
#define VT_BITFIELD 0x0040 /* bitfield modifier */
|
|
|
|
|
|
|
|
/* storage */
|
|
|
|
#define VT_EXTERN 0x00000080 /* extern definition */
|
|
|
|
#define VT_STATIC 0x00000100 /* static variable */
|
|
|
|
#define VT_TYPEDEF 0x00000200 /* typedef definition */
|
|
|
|
|
|
|
|
/* type mask (except storage) */
|
|
|
|
#define VT_TYPE (~(VT_EXTERN | VT_STATIC | VT_TYPEDEF))
|
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
|
2001-11-06 09:20:20 +08:00
|
|
|
#define TOK_UDIV 0xb0 /* unsigned division */
|
|
|
|
#define TOK_UMOD 0xb1 /* unsigned modulo */
|
|
|
|
#define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
|
2002-01-04 05:14:59 +08:00
|
|
|
#define TOK_CINT 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-12-14 06:28:53 +08:00
|
|
|
#define TOK_CFLOAT 0xb9 /* float constant */
|
|
|
|
#define TOK_CDOUBLE 0xc0 /* double constant */
|
|
|
|
#define TOK_CLDOUBLE 0xc1 /* long double constant */
|
2002-01-04 05:14:59 +08:00
|
|
|
#define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
|
|
|
|
#define TOK_ADDC1 0xc3 /* add with carry generation */
|
|
|
|
#define TOK_ADDC2 0xc4 /* add with carry use */
|
|
|
|
#define TOK_SUBC1 0xc5 /* add with carry generation */
|
|
|
|
#define TOK_SUBC2 0xc6 /* add with carry use */
|
|
|
|
#define TOK_CUINT 0xc8 /* unsigned int constant */
|
|
|
|
#define TOK_CLLONG 0xc9 /* long long constant */
|
|
|
|
#define TOK_CULLONG 0xca /* unsigned long long constant */
|
|
|
|
#define TOK_ARROW 0xcb
|
|
|
|
#define TOK_DOTS 0xcc /* three dots */
|
|
|
|
#define TOK_SHR 0xcd /* unsigned shift right */
|
|
|
|
|
|
|
|
#define TOK_SHL 0x01 /* shift left */
|
2001-11-06 09:20:20 +08:00
|
|
|
#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
|
|
|
|
2002-01-13 00:39:35 +08:00
|
|
|
#define TOK_EOF (-1) /* end of file */
|
|
|
|
|
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,
|
2002-01-13 00:39:35 +08:00
|
|
|
TOK___SIGNED__, /* gcc keyword */
|
2001-11-08 09:12:31 +08:00
|
|
|
TOK_AUTO,
|
|
|
|
TOK_INLINE,
|
2002-01-13 00:39:35 +08:00
|
|
|
TOK___INLINE__, /* gcc keyword */
|
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-12-13 05:16:17 +08:00
|
|
|
TOK_BOOL,
|
2001-11-08 09:12:31 +08:00
|
|
|
|
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,
|
2002-01-04 05:14:59 +08:00
|
|
|
TOK___ATTRIBUTE__,
|
|
|
|
|
2001-11-08 09:12:31 +08:00
|
|
|
/* 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,
|
2002-01-04 05:14:59 +08:00
|
|
|
/* attribute identifiers */
|
|
|
|
TOK_SECTION,
|
|
|
|
TOK___SECTION__,
|
|
|
|
TOK_ALIGNED,
|
|
|
|
TOK___ALIGNED__,
|
|
|
|
TOK_UNUSED,
|
|
|
|
TOK___UNUSED__,
|
2002-01-27 02:05:29 +08:00
|
|
|
TOK_CDECL,
|
|
|
|
TOK___CDECL,
|
|
|
|
TOK___CDECL__,
|
|
|
|
TOK_STDCALL,
|
|
|
|
TOK___STDCALL,
|
|
|
|
TOK___STDCALL__,
|
|
|
|
TOK_NORETURN,
|
|
|
|
TOK___NORETURN__,
|
2001-11-08 09:12:31 +08:00
|
|
|
};
|
|
|
|
|
2002-01-27 02:05:29 +08:00
|
|
|
char *tcc_keywords =
|
|
|
|
"int\0void\0char\0if\0else\0while\0break\0return\0for\0extern\0static\0"
|
|
|
|
"unsigned\0goto\0do\0continue\0switch\0case\0const\0volatile\0long\0"
|
|
|
|
"register\0signed\0__signed__\0auto\0inline\0__inline__\0restrict\0"
|
|
|
|
"float\0double\0_Bool\0short\0struct\0union\0typedef\0default\0enum\0"
|
|
|
|
"sizeof\0__attribute__\0"
|
|
|
|
/* the following are not keywords. They are included to ease parsing */
|
|
|
|
"define\0include\0ifdef\0ifndef\0elif\0endif\0"
|
|
|
|
"defined\0undef\0error\0line\0"
|
|
|
|
"__LINE__\0__FILE__\0__DATE__\0__TIME__\0__VA_ARGS__\0"
|
|
|
|
"__func__\0main\0"
|
|
|
|
/* attributes */
|
|
|
|
"section\0__section__\0aligned\0__aligned__\0unused\0__unused__\0"
|
|
|
|
"cdecl\0__cdecl\0__cdecl__\0stdcall\0__stdcall\0__stdcall__\0"
|
|
|
|
"noreturn\0__noreturn__\0"
|
|
|
|
;
|
|
|
|
|
2002-01-13 00:39:35 +08:00
|
|
|
#ifdef WIN32
|
|
|
|
#define snprintf _snprintf
|
2002-03-04 06:45:55 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(WIN32) || defined(TCC_UCLIBC)
|
2002-01-13 00:39:35 +08:00
|
|
|
/* currently incorrect */
|
|
|
|
long double strtold(const char *nptr, char **endptr)
|
|
|
|
{
|
|
|
|
return (long double)strtod(nptr, endptr);
|
|
|
|
}
|
|
|
|
float strtof(const char *nptr, char **endptr)
|
|
|
|
{
|
|
|
|
return (float)strtod(nptr, endptr);
|
|
|
|
}
|
|
|
|
#else
|
2001-12-14 07:20:55 +08:00
|
|
|
/* XXX: need to define this to use them in non ISOC99 context */
|
|
|
|
extern float strtof (const char *__nptr, char **__endptr);
|
|
|
|
extern long double strtold (const char *__nptr, char **__endptr);
|
2002-01-13 00:39:35 +08:00
|
|
|
#endif
|
2001-12-14 07:20:55 +08:00
|
|
|
|
2002-02-11 00:14:03 +08:00
|
|
|
char *pstrcpy(char *buf, int buf_size, const char *s);
|
|
|
|
char *pstrcat(char *buf, int buf_size, const char *s);
|
|
|
|
|
2001-12-14 06:28:53 +08:00
|
|
|
void sum(int l);
|
2001-12-05 08:45:08 +08:00
|
|
|
void next(void);
|
2001-12-14 06:28:53 +08:00
|
|
|
void next_nomacro(void);
|
|
|
|
int expr_const(void);
|
|
|
|
void expr_eq(void);
|
2001-12-18 05:56:48 +08:00
|
|
|
void gexpr(void);
|
2001-12-05 08:45:08 +08:00
|
|
|
void decl(int l);
|
2001-12-23 01:05:23 +08:00
|
|
|
void decl_initializer(int t, int r, int c, int first, int size_only);
|
2002-01-04 05:14:59 +08:00
|
|
|
int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init);
|
2001-12-23 08:34:26 +08:00
|
|
|
int gv(int rc);
|
2002-01-06 00:15:57 +08:00
|
|
|
void gv2(int rc1, int rc2);
|
2001-12-13 05:16:17 +08:00
|
|
|
void move_reg(int r, int s);
|
2002-01-05 04:17:24 +08:00
|
|
|
void save_regs(void);
|
2001-12-13 05:16:17 +08:00
|
|
|
void save_reg(int r);
|
2001-12-17 01:58:32 +08:00
|
|
|
void vpop(void);
|
2001-12-13 05:16:17 +08:00
|
|
|
void vswap(void);
|
2001-12-17 01:58:32 +08:00
|
|
|
void vdup(void);
|
2001-12-13 05:16:17 +08:00
|
|
|
int get_reg(int rc);
|
|
|
|
|
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-10 06:04:15 +08:00
|
|
|
void gen_op(int op);
|
2002-01-04 05:14:59 +08:00
|
|
|
void force_charshort_cast(int t);
|
2001-12-13 05:16:17 +08:00
|
|
|
void gen_cast(int t);
|
2001-12-03 05:24:43 +08:00
|
|
|
void vstore(void);
|
2002-01-04 06:43:10 +08:00
|
|
|
Sym *sym_find(int v);
|
|
|
|
Sym *sym_push(int v, int t, int r, int c);
|
2002-01-04 05:14:59 +08:00
|
|
|
|
|
|
|
/* type handling */
|
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);
|
2002-01-04 05:14:59 +08:00
|
|
|
int is_compatible_types(int t1, int t2);
|
|
|
|
int parse_btype(int *type_ptr, AttributeDef *ad);
|
2002-01-27 02:05:29 +08:00
|
|
|
int type_decl(AttributeDef *ad, int *v, int t, int td);
|
2002-01-04 05:14:59 +08:00
|
|
|
|
2001-12-14 06:28:53 +08:00
|
|
|
void error(const char *fmt, ...);
|
2002-01-06 01:02:38 +08:00
|
|
|
void rt_error(unsigned long pc, const char *fmt, ...);
|
2001-12-23 08:34:26 +08:00
|
|
|
void vpushi(int v);
|
2001-12-20 09:05:21 +08:00
|
|
|
void vset(int t, int r, int v);
|
2002-01-04 05:14:59 +08:00
|
|
|
void type_to_str(char *buf, int buf_size,
|
|
|
|
int t, const char *varstr);
|
2001-12-14 06:28:53 +08:00
|
|
|
|
2002-01-05 04:17:24 +08:00
|
|
|
/* section generation */
|
|
|
|
void greloc(Sym *s, int addr, int type);
|
|
|
|
static int put_elf_str(Section *s, const char *sym);
|
|
|
|
static void put_elf_sym(Section *s,
|
|
|
|
unsigned long value, unsigned long size,
|
|
|
|
int info, int other, int shndx, const char *name);
|
|
|
|
static void put_stabs(const char *str, int type, int other, int desc, int value);
|
|
|
|
static void put_stabn(int type, int other, int desc, int value);
|
|
|
|
static void put_stabd(int type, int other, int desc);
|
|
|
|
|
2001-12-18 05:56:48 +08:00
|
|
|
/* true if float/double/long double type */
|
|
|
|
static inline int is_float(int t)
|
|
|
|
{
|
|
|
|
int bt;
|
|
|
|
bt = t & VT_BTYPE;
|
|
|
|
return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT;
|
|
|
|
}
|
|
|
|
|
2002-03-04 06:45:55 +08:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2002-01-05 04:17:24 +08:00
|
|
|
#include "bcheck.c"
|
2002-03-04 06:45:55 +08:00
|
|
|
#endif
|
2002-01-05 04:17:24 +08:00
|
|
|
|
2002-02-11 00:14:03 +08:00
|
|
|
#ifdef TCC_TARGET_I386
|
2001-12-14 06:28:53 +08:00
|
|
|
#include "i386-gen.c"
|
2002-02-11 00:14:03 +08:00
|
|
|
#endif
|
|
|
|
#ifdef TCC_TARGET_IL
|
|
|
|
#include "il-gen.c"
|
|
|
|
#endif
|
2001-11-12 00:07:43 +08:00
|
|
|
|
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 },
|
|
|
|
};
|
|
|
|
|
2002-01-13 00:39:35 +08:00
|
|
|
void *dlsym(void *handle, const char *symbol)
|
2001-11-12 00:07:43 +08:00
|
|
|
{
|
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
|
|
|
|
2002-01-04 05:14:59 +08:00
|
|
|
/********************************************************/
|
|
|
|
/* runtime library is there */
|
|
|
|
/* XXX: we suppose that the host compiler handles 'long long'. It
|
|
|
|
would not be difficult to suppress this assumption */
|
|
|
|
|
|
|
|
long long __divll(long long a, long long b)
|
|
|
|
{
|
|
|
|
return a / b;
|
|
|
|
}
|
|
|
|
|
|
|
|
long long __modll(long long a, long long b)
|
|
|
|
{
|
|
|
|
return a % b;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long long __divull(unsigned long long a, unsigned long long b)
|
|
|
|
{
|
|
|
|
return a / b;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long long __modull(unsigned long long a, unsigned long long b)
|
|
|
|
{
|
|
|
|
return a % b;
|
|
|
|
}
|
|
|
|
|
|
|
|
long long __sardi3(long long a, int b)
|
|
|
|
{
|
|
|
|
return a >> b;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long long __shrdi3(unsigned long long a, int b)
|
|
|
|
{
|
|
|
|
return a >> b;
|
|
|
|
}
|
|
|
|
|
|
|
|
long long __shldi3(long long a, int b)
|
|
|
|
{
|
|
|
|
return a << b;
|
|
|
|
}
|
|
|
|
|
|
|
|
float __ulltof(unsigned long long a)
|
|
|
|
{
|
|
|
|
return (float)a;
|
|
|
|
}
|
|
|
|
|
|
|
|
double __ulltod(unsigned long long a)
|
|
|
|
{
|
|
|
|
return (double)a;
|
|
|
|
}
|
|
|
|
|
|
|
|
long double __ulltold(unsigned long long a)
|
|
|
|
{
|
|
|
|
return (long double)a;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long long __ftoull(float a)
|
|
|
|
{
|
|
|
|
return (unsigned long long)a;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long long __dtoull(double a)
|
|
|
|
{
|
|
|
|
return (unsigned long long)a;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long long __ldtoull(long double a)
|
|
|
|
{
|
|
|
|
return (unsigned long long)a;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/********************************************************/
|
|
|
|
|
2002-03-17 19:32:48 +08:00
|
|
|
/* we use our own 'finite' function to avoid potential problems with
|
|
|
|
non standard math libs */
|
|
|
|
/* XXX: endianness dependant */
|
|
|
|
int ieee_finite(double d)
|
|
|
|
{
|
|
|
|
int *p = (int *)&d;
|
|
|
|
return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
|
|
|
|
}
|
|
|
|
|
2002-01-13 00:39:35 +08:00
|
|
|
/* copy a string and truncate it. */
|
2002-01-04 05:14:59 +08:00
|
|
|
char *pstrcpy(char *buf, int buf_size, const char *s)
|
|
|
|
{
|
|
|
|
char *q, *q_end;
|
|
|
|
int c;
|
|
|
|
|
2002-01-13 00:39:35 +08:00
|
|
|
if (buf_size > 0) {
|
|
|
|
q = buf;
|
|
|
|
q_end = buf + buf_size - 1;
|
|
|
|
while (q < q_end) {
|
|
|
|
c = *s++;
|
|
|
|
if (c == '\0')
|
|
|
|
break;
|
|
|
|
*q++ = c;
|
|
|
|
}
|
|
|
|
*q = '\0';
|
2002-01-04 05:14:59 +08:00
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2002-01-13 00:39:35 +08:00
|
|
|
/* strcat and truncate. */
|
2002-01-04 06:43:10 +08:00
|
|
|
char *pstrcat(char *buf, int buf_size, const char *s)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
len = strlen(buf);
|
|
|
|
if (len < buf_size)
|
|
|
|
pstrcpy(buf + len, buf_size - len, s);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2002-01-05 04:17:24 +08:00
|
|
|
Section *new_section(const char *name, int sh_type, int sh_flags)
|
2002-01-04 05:14:59 +08:00
|
|
|
{
|
|
|
|
Section *sec, **psec;
|
|
|
|
void *data;
|
|
|
|
|
|
|
|
sec = malloc(sizeof(Section));
|
|
|
|
if (!sec)
|
|
|
|
error("memory full");
|
2002-01-05 04:17:24 +08:00
|
|
|
memset(sec, 0, sizeof(Section));
|
2002-01-04 05:14:59 +08:00
|
|
|
pstrcpy(sec->name, sizeof(sec->name), name);
|
2002-01-05 04:17:24 +08:00
|
|
|
sec->link = NULL;
|
|
|
|
sec->sh_num = ++section_num;
|
|
|
|
sec->sh_type = sh_type;
|
|
|
|
sec->sh_flags = sh_flags;
|
2002-01-13 00:39:35 +08:00
|
|
|
#ifdef WIN32
|
|
|
|
/* XXX: currently, a single malloc */
|
|
|
|
data = malloc(SECTION_VSIZE);
|
|
|
|
if (data == NULL)
|
|
|
|
error("could not alloc section '%s'", name);
|
|
|
|
#else
|
2002-01-04 05:14:59 +08:00
|
|
|
data = mmap(NULL, SECTION_VSIZE,
|
|
|
|
PROT_EXEC | PROT_READ | PROT_WRITE,
|
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS,
|
|
|
|
-1, 0);
|
|
|
|
if (data == (void *)(-1))
|
|
|
|
error("could not mmap section '%s'", name);
|
2002-01-13 00:39:35 +08:00
|
|
|
#endif
|
2002-01-04 05:14:59 +08:00
|
|
|
sec->data = data;
|
|
|
|
sec->data_ptr = data;
|
|
|
|
psec = &first_section;
|
|
|
|
while (*psec != NULL)
|
|
|
|
psec = &(*psec)->next;
|
|
|
|
sec->next = NULL;
|
|
|
|
*psec = sec;
|
|
|
|
return sec;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return a reference to a section, and create it if it does not
|
|
|
|
exists */
|
|
|
|
Section *find_section(const char *name)
|
|
|
|
{
|
|
|
|
Section *sec;
|
|
|
|
|
|
|
|
for(sec = first_section; sec != NULL; sec = sec->next) {
|
|
|
|
if (!strcmp(name, sec->name))
|
|
|
|
return sec;
|
|
|
|
}
|
2002-01-05 04:17:24 +08:00
|
|
|
/* sections are created as PROGBITS */
|
|
|
|
return new_section(name, SHT_PROGBITS, SHF_ALLOC);
|
2002-01-04 05:14:59 +08:00
|
|
|
}
|
|
|
|
|
2001-12-23 08:34:26 +08:00
|
|
|
/* 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;
|
|
|
|
greloc_patch1(p, val);
|
|
|
|
free(p);
|
|
|
|
p = p1;
|
|
|
|
}
|
|
|
|
s->c = val;
|
|
|
|
s->r &= ~VT_FORWARD;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-14 06:28:53 +08:00
|
|
|
static inline int isid(int 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-12-14 06:28:53 +08:00
|
|
|
static inline int isnum(int c)
|
2001-10-28 07:48:39 +08:00
|
|
|
{
|
2002-01-04 05:14:59 +08:00
|
|
|
return c >= '0' && c <= '9';
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
|
|
|
|
2001-12-14 06:28:53 +08:00
|
|
|
static inline int toup(int c)
|
|
|
|
{
|
|
|
|
if (ch >= 'a' && ch <= 'z')
|
|
|
|
return ch - 'a' + 'A';
|
|
|
|
else
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
2001-12-09 06:51:04 +08:00
|
|
|
void printline(void)
|
2001-11-08 07:13:29 +08:00
|
|
|
{
|
2002-01-13 00:39:35 +08:00
|
|
|
BufferedFile **f;
|
2002-03-04 06:45:55 +08:00
|
|
|
if (file) {
|
|
|
|
for(f = include_stack; f < include_stack_ptr; f++)
|
|
|
|
fprintf(stderr, "In file included from %s:%d:\n",
|
|
|
|
(*f)->filename, (*f)->line_num);
|
|
|
|
fprintf(stderr, "%s:%d: ", file->filename, file->line_num);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "tcc: ");
|
|
|
|
}
|
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-12-20 07:01:51 +08:00
|
|
|
void warning(const char *fmt, ...)
|
2001-10-28 23:20:12 +08:00
|
|
|
{
|
2001-12-20 07:01:51 +08:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2001-11-08 07:13:29 +08:00
|
|
|
printline();
|
2001-12-20 07:01:51 +08:00
|
|
|
fprintf(stderr, "warning: ");
|
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
va_end(ap);
|
2001-10-28 23:20:12 +08:00
|
|
|
}
|
|
|
|
|
2001-12-09 06:51:04 +08:00
|
|
|
void skip(int c)
|
2001-10-28 07:48:39 +08:00
|
|
|
{
|
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
|
|
|
|
2001-12-09 06:51:04 +08:00
|
|
|
void test_lvalue(void)
|
2001-10-28 23:20:12 +08:00
|
|
|
{
|
2001-12-20 09:05:21 +08:00
|
|
|
if (!(vtop->r & VT_LVAL))
|
2001-11-01 22:48:10 +08:00
|
|
|
expect("lvalue");
|
2001-10-28 23:20:12 +08:00
|
|
|
}
|
|
|
|
|
2002-01-04 05:14:59 +08:00
|
|
|
TokenSym *tok_alloc(const char *str, int len)
|
2001-11-11 10:53:01 +08:00
|
|
|
{
|
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++)
|
2002-01-27 02:05:29 +08:00
|
|
|
h = (h * 263 + ((unsigned char *)str)[i]) & (TOK_HASH_SIZE - 1);
|
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);
|
|
|
|
}
|
2001-12-09 08:27:44 +08:00
|
|
|
|
|
|
|
if (tok_ident >= SYM_FIRST_ANOM)
|
|
|
|
error("memory full");
|
|
|
|
|
2001-11-19 00:33:01 +08:00
|
|
|
/* 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
|
|
|
}
|
2001-12-09 08:27:44 +08:00
|
|
|
|
2001-11-11 10:53:01 +08:00
|
|
|
ts = malloc(sizeof(TokenSym) + len);
|
2001-12-09 08:27:44 +08:00
|
|
|
if (!ts)
|
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 */
|
2001-12-14 06:28:53 +08:00
|
|
|
char *get_tok_str(int v, CValue *cv)
|
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;
|
|
|
|
|
2002-01-04 05:14:59 +08:00
|
|
|
if (v == TOK_CINT || v == TOK_CUINT) {
|
2001-12-14 06:28:53 +08:00
|
|
|
sprintf(buf, "%u", cv->ui);
|
2001-11-11 10:53:01 +08:00
|
|
|
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++ = '\'';
|
2001-12-14 06:28:53 +08:00
|
|
|
add_char(&p, cv->i);
|
2001-11-11 10:53:01 +08:00
|
|
|
*p++ = '\'';
|
|
|
|
*p = '\0';
|
|
|
|
return buf;
|
2001-11-29 06:48:43 +08:00
|
|
|
} else if (v == TOK_STR || v == TOK_LSTR) {
|
2001-12-14 06:28:53 +08:00
|
|
|
ts = cv->ts;
|
2001-11-11 10:53:01 +08:00
|
|
|
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-12-13 05:16:17 +08:00
|
|
|
#define HASH_SYM(v) ((unsigned)(v) % SYM_HASH_SIZE)
|
|
|
|
|
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
|
|
|
|
2001-12-13 05:16:17 +08:00
|
|
|
s = st->hash[HASH_SYM(v)];
|
2001-11-19 00:33:01 +08:00
|
|
|
while (s) {
|
|
|
|
if (s->v == v)
|
|
|
|
return s;
|
|
|
|
s = s->hash_next;
|
|
|
|
}
|
2002-01-04 05:14:59 +08:00
|
|
|
return NULL;
|
2001-11-19 00:33:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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 */
|
2001-12-13 05:16:17 +08:00
|
|
|
if (v) {
|
|
|
|
ps = &st->hash[HASH_SYM(v)];
|
|
|
|
s->hash_next = *ps;
|
|
|
|
*ps = s;
|
|
|
|
}
|
2001-11-01 22:48:10 +08:00
|
|
|
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 */
|
2001-12-23 01:05:23 +08:00
|
|
|
Sym *sym_push(int v, int t, int r, int c)
|
2001-11-01 22:48:10 +08:00
|
|
|
{
|
2001-12-23 01:05:23 +08:00
|
|
|
Sym *s;
|
2001-11-19 00:33:01 +08:00
|
|
|
if (local_stack.top)
|
2001-12-23 01:05:23 +08:00
|
|
|
s = sym_push1(&local_stack, v, t, c);
|
2001-11-01 22:48:10 +08:00
|
|
|
else
|
2001-12-23 01:05:23 +08:00
|
|
|
s = sym_push1(&global_stack, v, t, c);
|
|
|
|
s->r = r;
|
|
|
|
return s;
|
2001-11-01 22:48:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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-12-13 05:16:17 +08:00
|
|
|
/* free hash table entry, except if symbol was freed (only
|
|
|
|
used for #undef symbols) */
|
|
|
|
if (s->v)
|
|
|
|
st->hash[HASH_SYM(s->v)] = 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;
|
2001-12-13 05:16:17 +08:00
|
|
|
ss = &st->hash[HASH_SYM(s->v)];
|
2001-12-08 23:04:01 +08:00
|
|
|
while (*ss != NULL) {
|
|
|
|
if (*ss == s)
|
|
|
|
break;
|
|
|
|
ss = &(*ss)->hash_next;
|
|
|
|
}
|
|
|
|
*ss = s->hash_next;
|
|
|
|
s->v = 0;
|
|
|
|
}
|
|
|
|
|
2002-01-13 00:39:35 +08:00
|
|
|
/* I/O layer */
|
|
|
|
|
|
|
|
BufferedFile *tcc_open(const char *filename)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
BufferedFile *bf;
|
|
|
|
|
|
|
|
fd = open(filename, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return NULL;
|
|
|
|
bf = malloc(sizeof(BufferedFile));
|
|
|
|
if (!bf) {
|
|
|
|
close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
bf->fd = fd;
|
|
|
|
bf->buf_ptr = bf->buffer;
|
|
|
|
bf->buf_end = bf->buffer;
|
|
|
|
bf->buffer[0] = CH_EOB; /* put eob symbol */
|
|
|
|
pstrcpy(bf->filename, sizeof(bf->filename), filename);
|
|
|
|
bf->line_num = 1;
|
2002-01-27 02:05:29 +08:00
|
|
|
// printf("opening '%s'\n", filename);
|
2002-01-13 00:39:35 +08:00
|
|
|
return bf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tcc_close(BufferedFile *bf)
|
|
|
|
{
|
2002-01-27 02:05:29 +08:00
|
|
|
total_lines += bf->line_num;
|
2002-01-13 00:39:35 +08:00
|
|
|
close(bf->fd);
|
|
|
|
free(bf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read one char. MUST call tcc_fillbuf if CH_EOB is read */
|
|
|
|
#define TCC_GETC(bf) (*(bf)->buf_ptr++)
|
|
|
|
|
|
|
|
/* fill input buffer and return next char */
|
|
|
|
int tcc_getc_slow(BufferedFile *bf)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
/* only tries to read if really end of buffer */
|
|
|
|
if (bf->buf_ptr >= bf->buf_end) {
|
|
|
|
if (bf->fd != -1) {
|
|
|
|
len = read(bf->fd, bf->buffer, IO_BUF_SIZE);
|
|
|
|
if (len < 0)
|
|
|
|
len = 0;
|
|
|
|
} else {
|
|
|
|
len = 0;
|
|
|
|
}
|
2002-01-27 02:05:29 +08:00
|
|
|
total_bytes += len;
|
2002-01-13 00:39:35 +08:00
|
|
|
bf->buf_ptr = bf->buffer;
|
|
|
|
bf->buf_end = bf->buffer + len;
|
|
|
|
*bf->buf_end = CH_EOB;
|
|
|
|
}
|
|
|
|
if (bf->buf_ptr < bf->buf_end) {
|
|
|
|
return *bf->buf_ptr++;
|
|
|
|
} else {
|
|
|
|
bf->buf_ptr = bf->buf_end;
|
|
|
|
return CH_EOF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-07 07:57:36 +08:00
|
|
|
/* no need to put that inline */
|
2002-01-13 00:39:35 +08:00
|
|
|
void handle_eob(void)
|
2001-12-07 07:57:36 +08:00
|
|
|
{
|
2002-01-13 00:39:35 +08:00
|
|
|
for(;;) {
|
|
|
|
ch1 = tcc_getc_slow(file);
|
|
|
|
if (ch1 != CH_EOF)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (include_stack_ptr == include_stack)
|
|
|
|
return;
|
|
|
|
/* add end of include file debug info */
|
|
|
|
if (do_debug) {
|
|
|
|
put_stabd(N_EINCL, 0, 0);
|
|
|
|
}
|
|
|
|
/* pop include stack */
|
|
|
|
tcc_close(file);
|
|
|
|
include_stack_ptr--;
|
|
|
|
file = *include_stack_ptr;
|
2002-01-05 04:17:24 +08:00
|
|
|
}
|
2001-12-07 07:57:36 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2002-01-13 00:39:35 +08:00
|
|
|
ch1 = TCC_GETC(file);
|
|
|
|
/* end of buffer/file handling */
|
|
|
|
if (ch1 == CH_EOB)
|
|
|
|
handle_eob();
|
2001-11-06 09:20:20 +08:00
|
|
|
if (ch1 == '\n')
|
2002-01-13 00:39:35 +08:00
|
|
|
file->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 */
|
2002-01-06 05:13:17 +08:00
|
|
|
void preprocess_skip(void)
|
2001-11-08 09:12:31 +08:00
|
|
|
{
|
|
|
|
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-12-14 06:28:53 +08:00
|
|
|
/* return the number of additionnal 'ints' necessary to store the
|
|
|
|
token */
|
|
|
|
static inline int tok_ext_size(int t)
|
2001-11-29 06:48:43 +08:00
|
|
|
{
|
2001-12-14 06:28:53 +08:00
|
|
|
switch(t) {
|
|
|
|
/* 4 bytes */
|
2002-01-04 05:14:59 +08:00
|
|
|
case TOK_CINT:
|
|
|
|
case TOK_CUINT:
|
2001-12-14 06:28:53 +08:00
|
|
|
case TOK_CCHAR:
|
|
|
|
case TOK_LCHAR:
|
|
|
|
case TOK_STR:
|
|
|
|
case TOK_LSTR:
|
|
|
|
case TOK_CFLOAT:
|
|
|
|
return 1;
|
|
|
|
case TOK_CDOUBLE:
|
2002-01-04 05:14:59 +08:00
|
|
|
case TOK_CLLONG:
|
|
|
|
case TOK_CULLONG:
|
2001-12-14 06:28:53 +08:00
|
|
|
return 2;
|
|
|
|
case TOK_CLDOUBLE:
|
|
|
|
return LDOUBLE_SIZE / 4;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
2001-11-29 06:48:43 +08:00
|
|
|
}
|
|
|
|
|
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-12-14 06:28:53 +08:00
|
|
|
void tok_add2(int **tok_str, int *tok_len, int t, CValue *cv)
|
2001-11-08 09:12:31 +08:00
|
|
|
{
|
2001-12-14 06:28:53 +08:00
|
|
|
int n, i;
|
|
|
|
|
2001-11-11 10:53:01 +08:00
|
|
|
tok_add(tok_str, tok_len, t);
|
2001-12-14 06:28:53 +08:00
|
|
|
n = tok_ext_size(t);
|
|
|
|
for(i=0;i<n;i++)
|
|
|
|
tok_add(tok_str, tok_len, cv->tab[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get a token from an integer array and increment pointer accordingly */
|
|
|
|
int tok_get(int **tok_str, CValue *cv)
|
|
|
|
{
|
|
|
|
int *p, t, n, i;
|
|
|
|
|
|
|
|
p = *tok_str;
|
|
|
|
t = *p++;
|
|
|
|
n = tok_ext_size(t);
|
|
|
|
for(i=0;i<n;i++)
|
|
|
|
cv->tab[i] = *p++;
|
|
|
|
*tok_str = p;
|
|
|
|
return t;
|
2001-11-11 10:53:01 +08:00
|
|
|
}
|
2001-11-08 09:12:31 +08:00
|
|
|
|
2001-11-11 10:53:01 +08:00
|
|
|
/* eval an expression for #if/#elif */
|
2001-12-14 06:28:53 +08:00
|
|
|
int expr_preprocess(void)
|
2001-11-11 10:53:01 +08:00
|
|
|
{
|
|
|
|
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();
|
2002-01-04 05:14:59 +08:00
|
|
|
tok = TOK_CINT;
|
2001-12-14 06:28:53 +08:00
|
|
|
tokc.i = c;
|
2001-11-11 10:53:01 +08:00
|
|
|
} else if (tok >= TOK_IDENT) {
|
|
|
|
/* if undefined macro */
|
2002-01-04 05:14:59 +08:00
|
|
|
tok = TOK_CINT;
|
2001-12-14 06:28:53 +08:00
|
|
|
tokc.i = 0;
|
2001-11-11 10:53:01 +08:00
|
|
|
}
|
2001-12-14 06:28:53 +08:00
|
|
|
tok_add2(&str, &len, tok, &tokc);
|
2001-11-11 10:53:01 +08:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2002-01-27 02:05:29 +08:00
|
|
|
#if defined(DEBUG) || defined(PP_DEBUG)
|
2001-11-11 10:53:01 +08:00
|
|
|
void tok_print(int *str)
|
|
|
|
{
|
2001-12-14 06:28:53 +08:00
|
|
|
int t;
|
|
|
|
CValue cval;
|
2001-11-11 10:53:01 +08:00
|
|
|
|
|
|
|
while (1) {
|
2001-12-14 06:28:53 +08:00
|
|
|
t = tok_get(&str, &cval);
|
2001-11-11 10:53:01 +08:00
|
|
|
if (!t)
|
|
|
|
break;
|
2001-12-14 06:28:53 +08:00
|
|
|
printf(" %s", get_tok_str(t, &cval));
|
2001-11-11 10:53:01 +08:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-01-13 19:07:19 +08:00
|
|
|
/* parse after #define */
|
|
|
|
void parse_define(void)
|
|
|
|
{
|
|
|
|
Sym *s, *first, **ps;
|
|
|
|
int v, t, *str, len;
|
|
|
|
|
|
|
|
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 != ')') {
|
|
|
|
if (tok == TOK_DOTS)
|
|
|
|
tok = TOK___VA_ARGS__;
|
|
|
|
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, NULL), t);
|
|
|
|
tok_print(str);
|
|
|
|
#endif
|
|
|
|
s = sym_push1(&define_stack, v, t, (int)str);
|
|
|
|
s->next = first;
|
|
|
|
}
|
|
|
|
|
2001-12-17 01:58:32 +08:00
|
|
|
void preprocess(void)
|
2001-11-06 09:20:20 +08:00
|
|
|
{
|
2002-01-13 19:07:19 +08:00
|
|
|
int size, i, c;
|
2001-11-06 09:20:20 +08:00
|
|
|
char buf[1024], *q, *p;
|
|
|
|
char buf1[1024];
|
2002-01-13 00:39:35 +08:00
|
|
|
BufferedFile *f;
|
2002-01-13 19:07:19 +08:00
|
|
|
Sym *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();
|
2002-01-13 19:07:19 +08:00
|
|
|
parse_define();
|
2001-11-11 10:53:01 +08:00
|
|
|
} 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");
|
2002-01-04 06:43:10 +08:00
|
|
|
pstrcpy(buf, sizeof(buf), get_tok_str(tok, &tokc));
|
2001-11-18 01:22:38 +08:00
|
|
|
c = '\"';
|
2001-11-06 09:20:20 +08:00
|
|
|
}
|
2001-12-14 07:20:55 +08:00
|
|
|
/* eat all spaces and comments after include */
|
|
|
|
/* XXX: slightly incorrect */
|
|
|
|
while (ch1 != '\n' && ch1 != -1)
|
|
|
|
inp();
|
|
|
|
|
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" */
|
|
|
|
size = 0;
|
2002-01-13 00:39:35 +08:00
|
|
|
p = strrchr(file->filename, '/');
|
2001-11-18 01:22:38 +08:00
|
|
|
if (p)
|
2002-01-13 00:39:35 +08:00
|
|
|
size = p + 1 - file->filename;
|
2002-01-04 06:43:10 +08:00
|
|
|
if (size > sizeof(buf1) - 1)
|
|
|
|
size = sizeof(buf1) - 1;
|
2002-01-13 00:39:35 +08:00
|
|
|
memcpy(buf1, file->filename, size);
|
2001-11-18 01:22:38 +08:00
|
|
|
buf1[size] = '\0';
|
2002-01-04 06:43:10 +08:00
|
|
|
pstrcat(buf1, sizeof(buf1), buf);
|
2002-01-13 00:39:35 +08:00
|
|
|
f = tcc_open(buf1);
|
2001-11-18 01:22:38 +08:00
|
|
|
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);
|
2002-01-13 00:39:35 +08:00
|
|
|
f = tcc_open(buf1);
|
2001-11-18 01:22:38 +08:00
|
|
|
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 */
|
2002-01-13 00:39:35 +08:00
|
|
|
*include_stack_ptr++ = file;
|
2001-11-18 01:22:38 +08:00
|
|
|
file = f;
|
2002-01-05 04:17:24 +08:00
|
|
|
/* add include file debug info */
|
|
|
|
if (do_debug) {
|
2002-01-13 00:39:35 +08:00
|
|
|
put_stabs(file->filename, N_BINCL, 0, 0, 0);
|
2002-01-05 04:17:24 +08:00
|
|
|
}
|
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) {
|
2002-01-27 02:05:29 +08:00
|
|
|
if (ifdef_stack_ptr == ifdef_stack)
|
|
|
|
error("#else without matching #if");
|
|
|
|
if (ifdef_stack_ptr[-1] & 2)
|
2001-11-08 09:12:31 +08:00
|
|
|
error("#else after #else");
|
|
|
|
c = (ifdef_stack_ptr[-1] ^= 3);
|
|
|
|
goto test_skip;
|
|
|
|
} else if (tok == TOK_ELIF) {
|
2002-01-27 02:05:29 +08:00
|
|
|
if (ifdef_stack_ptr == ifdef_stack)
|
|
|
|
error("#elif without matching #if");
|
|
|
|
c = ifdef_stack_ptr[-1];
|
|
|
|
if (c > 1)
|
2001-11-08 09:12:31 +08:00
|
|
|
error("#elif after #else");
|
2002-01-27 02:05:29 +08:00
|
|
|
/* last #if/#elif expression was true: we skip */
|
|
|
|
if (c == 1)
|
|
|
|
goto skip;
|
2001-11-08 09:12:31 +08:00
|
|
|
c = expr_preprocess();
|
|
|
|
ifdef_stack_ptr[-1] = c;
|
|
|
|
test_skip:
|
|
|
|
if (!(c & 1)) {
|
2002-01-27 02:05:29 +08:00
|
|
|
skip:
|
2001-11-08 09:12:31 +08:00
|
|
|
preprocess_skip();
|
|
|
|
goto redo;
|
|
|
|
}
|
|
|
|
} else if (tok == TOK_ENDIF) {
|
|
|
|
if (ifdef_stack_ptr == ifdef_stack)
|
2002-01-27 02:05:29 +08:00
|
|
|
error("#endif without matching #if");
|
2001-11-08 09:12:31 +08:00
|
|
|
ifdef_stack_ptr--;
|
2001-11-18 01:22:38 +08:00
|
|
|
} else if (tok == TOK_LINE) {
|
|
|
|
next();
|
2002-01-04 05:14:59 +08:00
|
|
|
if (tok != TOK_CINT)
|
2001-11-18 01:22:38 +08:00
|
|
|
error("#line");
|
2002-01-13 00:39:35 +08:00
|
|
|
file->line_num = tokc.i;
|
2001-11-18 01:22:38 +08:00
|
|
|
skip_spaces();
|
|
|
|
if (ch != '\n') {
|
|
|
|
next();
|
|
|
|
if (tok != TOK_STR)
|
|
|
|
error("#line");
|
2002-01-13 00:39:35 +08:00
|
|
|
pstrcpy(file->filename, sizeof(file->filename),
|
|
|
|
get_tok_str(tok, &tokc));
|
2001-11-18 01:22:38 +08:00
|
|
|
}
|
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) {
|
2002-01-04 05:14:59 +08:00
|
|
|
if (ch >= 'a' && ch <= 'f')
|
2001-11-06 09:20:20 +08:00
|
|
|
t = ch - 'a' + 10;
|
2002-01-04 05:14:59 +08:00
|
|
|
else if (ch >= 'A' && ch <= 'F')
|
2001-11-06 09:20:20 +08:00
|
|
|
t = ch - 'A' + 10;
|
|
|
|
else if (isnum(ch))
|
|
|
|
t = ch - '0';
|
|
|
|
else
|
|
|
|
break;
|
2002-01-04 05:14:59 +08:00
|
|
|
if (t < 0 || t >= b)
|
2001-11-06 09:20:20 +08:00
|
|
|
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';
|
2001-12-13 05:16:17 +08:00
|
|
|
else if (ch == 'e' && gnu_ext)
|
|
|
|
c = 27;
|
2001-12-14 06:28:53 +08:00
|
|
|
else if (ch == '\'' || ch == '\"' || ch == '\\' || ch == '?')
|
2001-11-11 10:53:01 +08:00
|
|
|
c = ch;
|
2001-12-13 05:16:17 +08:00
|
|
|
else
|
|
|
|
error("invalid escaped char");
|
2001-11-11 10:53:01 +08:00
|
|
|
minp();
|
|
|
|
}
|
2001-10-31 06:43:19 +08:00
|
|
|
}
|
2001-11-11 10:53:01 +08:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2001-12-14 06:28:53 +08:00
|
|
|
/* we use 64 bit numbers */
|
|
|
|
#define BN_SIZE 2
|
|
|
|
|
|
|
|
/* bn = (bn << shift) | or_val */
|
|
|
|
void bn_lshift(unsigned int *bn, int shift, int or_val)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned int v;
|
|
|
|
for(i=0;i<BN_SIZE;i++) {
|
|
|
|
v = bn[i];
|
|
|
|
bn[i] = (v << shift) | or_val;
|
|
|
|
or_val = v >> (32 - shift);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void bn_zero(unsigned int *bn)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i=0;i<BN_SIZE;i++) {
|
|
|
|
bn[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void parse_number(void)
|
|
|
|
{
|
|
|
|
int b, t, shift, frac_bits, s, exp_val;
|
|
|
|
char *q;
|
|
|
|
unsigned int bn[BN_SIZE];
|
|
|
|
double d;
|
|
|
|
|
|
|
|
/* number */
|
|
|
|
q = token_buf;
|
|
|
|
t = ch;
|
|
|
|
cinp();
|
|
|
|
*q++ = t;
|
|
|
|
b = 10;
|
|
|
|
if (t == '.') {
|
|
|
|
/* special dot handling */
|
|
|
|
if (ch >= '0' && ch <= '9') {
|
|
|
|
goto float_frac_parse;
|
|
|
|
} else if (ch == '.') {
|
|
|
|
cinp();
|
|
|
|
if (ch != '.')
|
|
|
|
expect("'.'");
|
|
|
|
cinp();
|
|
|
|
tok = TOK_DOTS;
|
|
|
|
} else {
|
|
|
|
/* dots */
|
|
|
|
tok = t;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else if (t == '0') {
|
|
|
|
if (ch == 'x' || ch == 'X') {
|
|
|
|
q--;
|
|
|
|
cinp();
|
|
|
|
b = 16;
|
|
|
|
} else if (tcc_ext && (ch == 'b' || ch == 'B')) {
|
|
|
|
q--;
|
|
|
|
cinp();
|
|
|
|
b = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* parse all digits. cannot check octal numbers at this stage
|
|
|
|
because of floating point constants */
|
|
|
|
while (1) {
|
2002-01-04 05:14:59 +08:00
|
|
|
if (ch >= 'a' && ch <= 'f')
|
2001-12-14 06:28:53 +08:00
|
|
|
t = ch - 'a' + 10;
|
2002-01-04 05:14:59 +08:00
|
|
|
else if (ch >= 'A' && ch <= 'F')
|
2001-12-14 06:28:53 +08:00
|
|
|
t = ch - 'A' + 10;
|
|
|
|
else if (isnum(ch))
|
|
|
|
t = ch - '0';
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
if (t >= b)
|
|
|
|
break;
|
|
|
|
if (q >= token_buf + STRING_MAX_SIZE) {
|
|
|
|
num_too_long:
|
|
|
|
error("number too long");
|
|
|
|
}
|
|
|
|
*q++ = ch;
|
|
|
|
cinp();
|
|
|
|
}
|
2002-01-04 05:14:59 +08:00
|
|
|
if (ch == '.' ||
|
2001-12-14 06:28:53 +08:00
|
|
|
((ch == 'e' || ch == 'E') && b == 10) ||
|
|
|
|
((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
|
|
|
|
if (b != 10) {
|
|
|
|
/* NOTE: strtox should support that for hexa numbers, but
|
|
|
|
non ISOC99 libcs do not support it, so we prefer to do
|
|
|
|
it by hand */
|
|
|
|
/* hexadecimal or binary floats */
|
|
|
|
/* XXX: handle overflows */
|
|
|
|
*q = '\0';
|
|
|
|
if (b == 16)
|
|
|
|
shift = 4;
|
|
|
|
else
|
|
|
|
shift = 2;
|
|
|
|
bn_zero(bn);
|
|
|
|
q = token_buf;
|
|
|
|
while (1) {
|
|
|
|
t = *q++;
|
|
|
|
if (t == '\0') {
|
|
|
|
break;
|
|
|
|
} else if (t >= 'a') {
|
|
|
|
t = t - 'a' + 10;
|
|
|
|
} else if (t >= 'A') {
|
|
|
|
t = t - 'A' + 10;
|
|
|
|
} else {
|
|
|
|
t = t - '0';
|
|
|
|
}
|
|
|
|
bn_lshift(bn, shift, t);
|
|
|
|
}
|
|
|
|
frac_bits = 0;
|
|
|
|
if (ch == '.') {
|
|
|
|
cinp();
|
|
|
|
while (1) {
|
|
|
|
t = ch;
|
|
|
|
if (t >= 'a' && t <= 'f') {
|
|
|
|
t = t - 'a' + 10;
|
|
|
|
} else if (t >= 'A' && t <= 'F') {
|
|
|
|
t = t - 'A' + 10;
|
|
|
|
} else if (t >= '0' && t <= '9') {
|
|
|
|
t = t - '0';
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (t >= b)
|
|
|
|
error("invalid digit");
|
|
|
|
bn_lshift(bn, shift, t);
|
|
|
|
frac_bits += shift;
|
|
|
|
cinp();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ch != 'p' && ch != 'P')
|
|
|
|
error("exponent expected");
|
|
|
|
cinp();
|
|
|
|
s = 1;
|
|
|
|
exp_val = 0;
|
|
|
|
if (ch == '+') {
|
|
|
|
cinp();
|
|
|
|
} else if (ch == '-') {
|
|
|
|
s = -1;
|
|
|
|
cinp();
|
|
|
|
}
|
|
|
|
if (ch < '0' || ch > '9')
|
|
|
|
error("exponent digits expected");
|
|
|
|
while (ch >= '0' && ch <= '9') {
|
|
|
|
exp_val = exp_val * 10 + ch - '0';
|
|
|
|
cinp();
|
|
|
|
}
|
|
|
|
exp_val = exp_val * s;
|
|
|
|
|
|
|
|
/* now we can generate the number */
|
|
|
|
/* XXX: should patch directly float number */
|
|
|
|
d = (double)bn[1] * 4294967296.0 + (double)bn[0];
|
|
|
|
d = ldexp(d, exp_val - frac_bits);
|
|
|
|
t = toup(ch);
|
|
|
|
if (t == 'F') {
|
|
|
|
cinp();
|
|
|
|
tok = TOK_CFLOAT;
|
|
|
|
/* float : should handle overflow */
|
2001-12-18 05:56:48 +08:00
|
|
|
tokc.f = (float)d;
|
2001-12-14 06:28:53 +08:00
|
|
|
} else if (t == 'L') {
|
|
|
|
cinp();
|
|
|
|
tok = TOK_CLDOUBLE;
|
|
|
|
/* XXX: not large enough */
|
2001-12-18 05:56:48 +08:00
|
|
|
tokc.ld = (long double)d;
|
2001-12-14 06:28:53 +08:00
|
|
|
} else {
|
|
|
|
tok = TOK_CDOUBLE;
|
2001-12-18 05:56:48 +08:00
|
|
|
tokc.d = d;
|
2001-12-14 06:28:53 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* decimal floats */
|
|
|
|
if (ch == '.') {
|
|
|
|
if (q >= token_buf + STRING_MAX_SIZE)
|
|
|
|
goto num_too_long;
|
|
|
|
*q++ = ch;
|
|
|
|
cinp();
|
|
|
|
float_frac_parse:
|
|
|
|
while (ch >= '0' && ch <= '9') {
|
|
|
|
if (q >= token_buf + STRING_MAX_SIZE)
|
|
|
|
goto num_too_long;
|
|
|
|
*q++ = ch;
|
|
|
|
cinp();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ch == 'e' || ch == 'E') {
|
|
|
|
if (q >= token_buf + STRING_MAX_SIZE)
|
|
|
|
goto num_too_long;
|
|
|
|
*q++ = ch;
|
|
|
|
cinp();
|
|
|
|
if (ch == '-' || ch == '+') {
|
|
|
|
if (q >= token_buf + STRING_MAX_SIZE)
|
|
|
|
goto num_too_long;
|
|
|
|
*q++ = ch;
|
|
|
|
cinp();
|
|
|
|
}
|
|
|
|
if (ch < '0' || ch > '9')
|
|
|
|
error("exponent digits expected");
|
|
|
|
while (ch >= '0' && ch <= '9') {
|
|
|
|
if (q >= token_buf + STRING_MAX_SIZE)
|
|
|
|
goto num_too_long;
|
|
|
|
*q++ = ch;
|
|
|
|
cinp();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*q = '\0';
|
|
|
|
t = toup(ch);
|
|
|
|
errno = 0;
|
|
|
|
if (t == 'F') {
|
|
|
|
cinp();
|
|
|
|
tok = TOK_CFLOAT;
|
2001-12-18 05:56:48 +08:00
|
|
|
tokc.f = strtof(token_buf, NULL);
|
2001-12-14 06:28:53 +08:00
|
|
|
} else if (t == 'L') {
|
|
|
|
cinp();
|
|
|
|
tok = TOK_CLDOUBLE;
|
2001-12-18 05:56:48 +08:00
|
|
|
tokc.ld = strtold(token_buf, NULL);
|
2001-12-14 06:28:53 +08:00
|
|
|
} else {
|
|
|
|
tok = TOK_CDOUBLE;
|
2001-12-18 05:56:48 +08:00
|
|
|
tokc.d = strtod(token_buf, NULL);
|
2001-12-14 06:28:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2002-01-04 05:14:59 +08:00
|
|
|
unsigned long long n, n1;
|
|
|
|
int lcount;
|
|
|
|
|
2001-12-14 06:28:53 +08:00
|
|
|
/* integer number */
|
|
|
|
*q = '\0';
|
|
|
|
q = token_buf;
|
|
|
|
if (b == 10 && *q == '0') {
|
|
|
|
b = 8;
|
|
|
|
q++;
|
|
|
|
}
|
|
|
|
n = 0;
|
|
|
|
while(1) {
|
|
|
|
t = *q++;
|
|
|
|
/* no need for checks except for base 10 / 8 errors */
|
|
|
|
if (t == '\0') {
|
|
|
|
break;
|
|
|
|
} else if (t >= 'a') {
|
|
|
|
t = t - 'a' + 10;
|
|
|
|
} else if (t >= 'A') {
|
|
|
|
t = t - 'A' + 10;
|
|
|
|
} else {
|
|
|
|
t = t - '0';
|
|
|
|
if (t >= b)
|
|
|
|
error("invalid digit");
|
|
|
|
}
|
|
|
|
n1 = n;
|
|
|
|
n = n * b + t;
|
|
|
|
/* detect overflow */
|
|
|
|
if (n < n1)
|
|
|
|
error("integer constant overflow");
|
|
|
|
}
|
2002-01-04 05:14:59 +08:00
|
|
|
|
|
|
|
/* XXX: not exactly ANSI compliant */
|
|
|
|
if ((n & 0xffffffff00000000LL) != 0) {
|
|
|
|
if ((n >> 63) != 0)
|
|
|
|
tok = TOK_CULLONG;
|
|
|
|
else
|
|
|
|
tok = TOK_CLLONG;
|
|
|
|
} else if (n > 0x7fffffff) {
|
|
|
|
tok = TOK_CUINT;
|
|
|
|
} else {
|
|
|
|
tok = TOK_CINT;
|
|
|
|
}
|
|
|
|
lcount = 0;
|
|
|
|
for(;;) {
|
|
|
|
t = toup(ch);
|
|
|
|
if (t == 'L') {
|
|
|
|
if (lcount >= 2)
|
|
|
|
error("three 'l' in integer constant");
|
|
|
|
lcount++;
|
|
|
|
if (lcount == 2) {
|
|
|
|
if (tok == TOK_CINT)
|
|
|
|
tok = TOK_CLLONG;
|
|
|
|
else if (tok == TOK_CUINT)
|
|
|
|
tok = TOK_CULLONG;
|
|
|
|
}
|
|
|
|
cinp();
|
|
|
|
} else if (t == 'U') {
|
|
|
|
if (tok == TOK_CINT)
|
|
|
|
tok = TOK_CUINT;
|
|
|
|
else if (tok == TOK_CLLONG)
|
|
|
|
tok = TOK_CULLONG;
|
|
|
|
cinp();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tok == TOK_CINT || tok == TOK_CUINT)
|
|
|
|
tokc.ui = n;
|
|
|
|
else
|
|
|
|
tokc.ull = n;
|
2001-12-14 06:28:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-11 10:53:01 +08:00
|
|
|
/* return next token without macro substitution */
|
2001-12-14 06:28:53 +08:00
|
|
|
void next_nomacro1(void)
|
2001-11-11 10:53:01 +08:00
|
|
|
{
|
|
|
|
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();
|
2002-01-27 02:05:29 +08:00
|
|
|
while (ch == ' ' || ch == '\t')
|
2001-11-06 09:20:20 +08:00
|
|
|
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-12-13 05:16:17 +08:00
|
|
|
while (isid(ch) || isnum(ch)) {
|
2001-11-11 10:53:01 +08:00
|
|
|
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-12-14 06:28:53 +08:00
|
|
|
} else if (isnum(ch) || ch == '.') {
|
|
|
|
parse_number();
|
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();
|
2001-12-14 06:28:53 +08:00
|
|
|
tokc.i = getq();
|
2001-11-11 10:53:01 +08:00
|
|
|
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';
|
2001-12-14 06:28:53 +08:00
|
|
|
tokc.ts = tok_alloc(token_buf, q - token_buf);
|
2001-11-11 10:53:01 +08:00
|
|
|
minp();
|
2001-10-28 07:48:39 +08:00
|
|
|
} else {
|
2002-01-04 05:14:59 +08:00
|
|
|
q = "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\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-12-14 06:28:53 +08:00
|
|
|
if (*q == tok && q[1] == ch) {
|
2001-11-06 09:20:20 +08:00
|
|
|
cinp();
|
2001-10-28 07:48:39 +08:00
|
|
|
tok = q[2] & 0xff;
|
2001-11-06 09:20:20 +08:00
|
|
|
/* three chars tests */
|
2002-01-04 05:14:59 +08:00
|
|
|
if (tok == TOK_SHL || tok == TOK_SAR) {
|
2001-11-06 09:20:20 +08:00
|
|
|
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;
|
2001-12-14 06:28:53 +08:00
|
|
|
if (tok)
|
|
|
|
tok = tok_get(¯o_ptr, &tokc);
|
2001-11-11 10:53:01 +08:00
|
|
|
} else {
|
|
|
|
next_nomacro1();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* substitute args in macro_str and return allocated string */
|
|
|
|
int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args)
|
|
|
|
{
|
2001-12-14 06:28:53 +08:00
|
|
|
int *st, last_tok, t, notfirst, *str, len;
|
2001-11-11 10:53:01 +08:00
|
|
|
Sym *s;
|
|
|
|
TokenSym *ts;
|
2001-12-14 06:28:53 +08:00
|
|
|
CValue cval;
|
2001-11-11 10:53:01 +08:00
|
|
|
|
|
|
|
str = NULL;
|
|
|
|
len = 0;
|
|
|
|
last_tok = 0;
|
|
|
|
while(1) {
|
2001-12-14 06:28:53 +08:00
|
|
|
t = tok_get(¯o_str, &cval);
|
2001-11-11 10:53:01 +08:00
|
|
|
if (!t)
|
|
|
|
break;
|
|
|
|
if (t == '#') {
|
|
|
|
/* stringize */
|
2001-12-14 06:28:53 +08:00
|
|
|
t = tok_get(¯o_str, &cval);
|
2001-11-11 10:53:01 +08:00
|
|
|
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;
|
|
|
|
notfirst = 0;
|
|
|
|
while (*st) {
|
|
|
|
if (notfirst)
|
2002-01-04 06:43:10 +08:00
|
|
|
pstrcat(token_buf, sizeof(token_buf), " ");
|
2001-12-14 06:28:53 +08:00
|
|
|
t = tok_get(&st, &cval);
|
2002-01-04 06:43:10 +08:00
|
|
|
pstrcat(token_buf, sizeof(token_buf), get_tok_str(t, &cval));
|
2001-11-11 10:53:01 +08:00
|
|
|
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-12-14 06:28:53 +08:00
|
|
|
cval.ts = ts;
|
|
|
|
tok_add2(&str, &len, TOK_STR, &cval);
|
2001-11-11 10:53:01 +08:00
|
|
|
} else {
|
2001-12-14 06:28:53 +08:00
|
|
|
tok_add2(&str, &len, t, &cval);
|
2001-11-11 10:53:01 +08:00
|
|
|
}
|
2001-12-14 06:28:53 +08:00
|
|
|
} else if (t >= TOK_IDENT) {
|
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);
|
|
|
|
}
|
2001-12-14 06:28:53 +08:00
|
|
|
} else {
|
|
|
|
tok_add2(&str, &len, t, &cval);
|
2001-11-11 10:53:01 +08:00
|
|
|
}
|
|
|
|
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;
|
2001-12-14 06:28:53 +08:00
|
|
|
int t;
|
2001-11-11 10:53:01 +08:00
|
|
|
char *p;
|
2001-12-14 06:28:53 +08:00
|
|
|
CValue cval;
|
2001-11-11 10:53:01 +08:00
|
|
|
|
|
|
|
macro_str1 = NULL;
|
|
|
|
macro_str1_len = 0;
|
|
|
|
tok = 0;
|
|
|
|
while (1) {
|
|
|
|
next_nomacro();
|
|
|
|
if (tok == 0)
|
|
|
|
break;
|
2001-12-14 07:20:55 +08:00
|
|
|
while (*macro_ptr == TOK_TWOSHARPS) {
|
2001-11-11 10:53:01 +08:00
|
|
|
macro_ptr++;
|
|
|
|
macro_ptr1 = macro_ptr;
|
|
|
|
t = *macro_ptr;
|
|
|
|
if (t) {
|
2001-12-14 06:28:53 +08:00
|
|
|
t = tok_get(¯o_ptr, &cval);
|
2001-11-11 10:53:01 +08:00
|
|
|
/* XXX: we handle only most common cases:
|
|
|
|
ident + ident or ident + number */
|
|
|
|
if (tok >= TOK_IDENT &&
|
2002-01-04 05:14:59 +08:00
|
|
|
(t >= TOK_IDENT || t == TOK_CINT)) {
|
2001-12-14 06:28:53 +08:00
|
|
|
p = get_tok_str(tok, &tokc);
|
2002-01-04 06:43:10 +08:00
|
|
|
pstrcpy(token_buf, sizeof(token_buf), p);
|
2001-12-14 06:28:53 +08:00
|
|
|
p = get_tok_str(t, &cval);
|
2002-01-04 06:43:10 +08:00
|
|
|
pstrcat(token_buf, sizeof(token_buf), p);
|
2001-11-14 06:54:35 +08:00
|
|
|
ts = tok_alloc(token_buf, 0);
|
2001-12-14 07:20:55 +08:00
|
|
|
tok = ts->tok; /* modify current token */
|
2001-11-11 10:53:01 +08:00
|
|
|
} else {
|
|
|
|
/* cannot merge tokens: skip '##' */
|
|
|
|
macro_ptr = macro_ptr1;
|
2001-12-14 07:20:55 +08:00
|
|
|
break;
|
2001-11-11 10:53:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-12-14 07:20:55 +08:00
|
|
|
tok_add2(¯o_str1, ¯o_str1_len, tok, &tokc);
|
2001-11-11 10:53:01 +08:00
|
|
|
}
|
|
|
|
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;
|
2001-12-14 06:28:53 +08:00
|
|
|
CValue cval;
|
2001-11-11 10:53:01 +08:00
|
|
|
|
|
|
|
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__) {
|
2002-01-13 00:39:35 +08:00
|
|
|
cval.i = file->line_num;
|
2002-01-04 05:14:59 +08:00
|
|
|
tok_add2(tok_str, tok_len, TOK_CINT, &cval);
|
2001-11-14 06:54:35 +08:00
|
|
|
} else if (tok == TOK___FILE__) {
|
2002-01-13 00:39:35 +08:00
|
|
|
cval.ts = tok_alloc(file->filename, 0);
|
2001-12-14 06:28:53 +08:00
|
|
|
tok_add2(tok_str, tok_len, TOK_STR, &cval);
|
2001-11-14 06:54:35 +08:00
|
|
|
} else if (tok == TOK___DATE__) {
|
2001-12-14 06:28:53 +08:00
|
|
|
cval.ts = tok_alloc("Jan 1 1970", 0);
|
|
|
|
tok_add2(tok_str, tok_len, TOK_STR, &cval);
|
2001-11-14 06:54:35 +08:00
|
|
|
} else if (tok == TOK___TIME__) {
|
2001-12-14 06:28:53 +08:00
|
|
|
cval.ts = tok_alloc("00:00:00", 0);
|
|
|
|
tok_add2(tok_str, tok_len, TOK_STR, &cval);
|
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;
|
2002-01-27 02:05:29 +08:00
|
|
|
/* NOTE: empty args are allowed, except if no args */
|
|
|
|
for(;;) {
|
|
|
|
/* handle '()' case */
|
|
|
|
if (!args && tok == ')')
|
|
|
|
break;
|
|
|
|
if (!sa)
|
|
|
|
error("macro '%s' used with too many args",
|
|
|
|
get_tok_str(s->v, 0));
|
2001-11-11 10:53:01 +08:00
|
|
|
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--;
|
2001-12-14 06:28:53 +08:00
|
|
|
tok_add2(&str, &len, tok, &tokc);
|
2001-11-11 10:53:01 +08:00
|
|
|
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);
|
2002-01-27 02:05:29 +08:00
|
|
|
if (tok == ')')
|
2001-11-11 10:53:01 +08:00
|
|
|
break;
|
2002-01-27 02:05:29 +08:00
|
|
|
if (tok != ',')
|
|
|
|
expect(",");
|
2001-11-11 10:53:01 +08:00
|
|
|
next_nomacro();
|
|
|
|
sa = sa->next;
|
|
|
|
}
|
2002-01-27 02:05:29 +08:00
|
|
|
if (sa->next)
|
|
|
|
error("macro '%s' used with too few args",
|
|
|
|
get_tok_str(s->v, 0));
|
|
|
|
|
2001-11-11 10:53:01 +08:00
|
|
|
/* 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;
|
2001-12-14 06:28:53 +08:00
|
|
|
tok_add2(tok_str, tok_len, tok, &tokc);
|
2001-11-11 10:53:01 +08:00
|
|
|
}
|
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 */
|
2001-12-09 08:27:44 +08:00
|
|
|
void next(void)
|
2001-11-11 10:53:01 +08:00
|
|
|
{
|
|
|
|
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) {
|
2002-01-27 02:05:29 +08:00
|
|
|
/* if not reading from macro substituted string, then try to substitute */
|
2001-11-11 10:53:01 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2001-12-20 09:05:21 +08:00
|
|
|
void vsetc(int t, int r, CValue *vc)
|
2001-11-06 09:20:20 +08:00
|
|
|
{
|
2001-12-17 01:58:32 +08:00
|
|
|
if (vtop >= vstack + VSTACK_SIZE)
|
|
|
|
error("memory full");
|
|
|
|
/* cannot let cpu flags if other instruction are generated */
|
|
|
|
/* XXX: VT_JMP test too ? */
|
2001-12-20 09:05:21 +08:00
|
|
|
if ((vtop->r & VT_VALMASK) == VT_CMP)
|
2001-12-23 08:34:26 +08:00
|
|
|
gv(RC_INT);
|
2001-12-17 01:58:32 +08:00
|
|
|
vtop++;
|
|
|
|
vtop->t = t;
|
2001-12-20 09:05:21 +08:00
|
|
|
vtop->r = r;
|
2002-01-04 05:14:59 +08:00
|
|
|
vtop->r2 = VT_CONST;
|
2001-12-18 05:56:48 +08:00
|
|
|
vtop->c = *vc;
|
2001-12-17 01:58:32 +08:00
|
|
|
}
|
|
|
|
|
2001-12-20 09:05:21 +08:00
|
|
|
/* push integer constant */
|
|
|
|
void vpushi(int v)
|
|
|
|
{
|
|
|
|
CValue cval;
|
|
|
|
cval.i = v;
|
|
|
|
vsetc(VT_INT, VT_CONST, &cval);
|
|
|
|
}
|
|
|
|
|
|
|
|
void vset(int t, int r, int v)
|
2001-12-17 01:58:32 +08:00
|
|
|
{
|
2001-12-18 05:56:48 +08:00
|
|
|
CValue cval;
|
|
|
|
|
|
|
|
cval.i = v;
|
2001-12-20 09:05:21 +08:00
|
|
|
vsetc(t, r, &cval);
|
2001-12-17 01:58:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void vswap(void)
|
|
|
|
{
|
|
|
|
SValue tmp;
|
|
|
|
|
|
|
|
tmp = vtop[0];
|
|
|
|
vtop[0] = vtop[-1];
|
|
|
|
vtop[-1] = tmp;
|
|
|
|
}
|
|
|
|
|
2002-01-04 05:14:59 +08:00
|
|
|
void vpushv(SValue *v)
|
2001-12-17 01:58:32 +08:00
|
|
|
{
|
|
|
|
if (vtop >= vstack + VSTACK_SIZE)
|
|
|
|
error("memory full");
|
|
|
|
vtop++;
|
2002-01-04 05:14:59 +08:00
|
|
|
*vtop = *v;
|
2001-11-06 09:20:20 +08:00
|
|
|
}
|
|
|
|
|
2002-01-04 05:14:59 +08:00
|
|
|
void vdup(void)
|
2001-11-06 09:20:20 +08:00
|
|
|
{
|
2002-01-04 05:14:59 +08:00
|
|
|
vpushv(vtop);
|
|
|
|
}
|
2001-12-13 05:16:17 +08:00
|
|
|
|
2002-01-04 05:14:59 +08:00
|
|
|
/* save r to the memory stack, and mark it as being free */
|
|
|
|
void save_reg(int r)
|
|
|
|
{
|
|
|
|
int l, i, saved, t, size, align;
|
|
|
|
SValue *p, sv;
|
2001-11-06 09:20:20 +08:00
|
|
|
|
|
|
|
/* modify all stack values */
|
2002-01-04 05:14:59 +08:00
|
|
|
saved = 0;
|
|
|
|
l = 0;
|
2001-12-17 01:58:32 +08:00
|
|
|
for(p=vstack;p<=vtop;p++) {
|
2001-12-20 09:05:21 +08:00
|
|
|
i = p->r & VT_VALMASK;
|
2002-01-04 05:14:59 +08:00
|
|
|
if ((p->r & VT_VALMASK) == r ||
|
|
|
|
(p->r2 & VT_VALMASK) == r) {
|
|
|
|
/* must save value on stack if not already done */
|
|
|
|
if (!saved) {
|
|
|
|
/* store register in the stack */
|
|
|
|
t = p->t;
|
2002-01-27 02:05:29 +08:00
|
|
|
if ((p->r & VT_LVAL) ||
|
|
|
|
(!is_float(t) && (t & VT_BTYPE) != VT_LLONG))
|
2002-01-04 05:14:59 +08:00
|
|
|
t = VT_INT;
|
|
|
|
size = type_size(t, &align);
|
|
|
|
loc = (loc - size) & -align;
|
|
|
|
sv.t = t;
|
|
|
|
sv.r = VT_LOCAL | VT_LVAL;
|
|
|
|
sv.c.ul = loc;
|
|
|
|
store(r, &sv);
|
2002-02-11 00:14:03 +08:00
|
|
|
#ifdef TCC_TARGET_I386
|
|
|
|
/* x86 specific: need to pop fp register ST0 if saved */
|
2002-01-04 05:14:59 +08:00
|
|
|
if (r == REG_ST0) {
|
|
|
|
o(0xd9dd); /* fstp %st(1) */
|
|
|
|
}
|
2002-02-11 00:14:03 +08:00
|
|
|
#endif
|
2002-01-04 05:14:59 +08:00
|
|
|
/* special long long case */
|
|
|
|
if ((p->t & VT_BTYPE) == VT_LLONG) {
|
|
|
|
sv.c.ul += 4;
|
|
|
|
store(p->r2, &sv);
|
|
|
|
}
|
|
|
|
l = loc;
|
|
|
|
saved = 1;
|
|
|
|
}
|
|
|
|
/* mark that stack entry as being saved on the stack */
|
2001-12-20 09:05:21 +08:00
|
|
|
if (p->r & VT_LVAL)
|
2001-11-06 09:20:20 +08:00
|
|
|
t = VT_LLOCAL;
|
|
|
|
else
|
|
|
|
t = VT_LOCAL;
|
2001-12-20 09:05:21 +08:00
|
|
|
p->r = VT_LVAL | t;
|
2002-01-04 05:14:59 +08:00
|
|
|
p->r2 = VT_CONST;
|
2001-12-18 05:56:48 +08:00
|
|
|
p->c.ul = l;
|
2001-11-06 09:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-13 05:16:17 +08:00
|
|
|
/* find a free register of class 'rc'. If none, save one register */
|
|
|
|
int get_reg(int rc)
|
2001-11-06 09:20:20 +08:00
|
|
|
{
|
2002-01-04 05:14:59 +08:00
|
|
|
int r;
|
2001-12-17 01:58:32 +08:00
|
|
|
SValue *p;
|
2001-11-06 09:20:20 +08:00
|
|
|
|
|
|
|
/* find a free register */
|
|
|
|
for(r=0;r<NB_REGS;r++) {
|
2001-12-13 05:16:17 +08:00
|
|
|
if (reg_classes[r] & rc) {
|
2001-12-17 01:58:32 +08:00
|
|
|
for(p=vstack;p<=vtop;p++) {
|
2002-01-04 05:14:59 +08:00
|
|
|
if ((p->r & VT_VALMASK) == r ||
|
|
|
|
(p->r2 & VT_VALMASK) == r)
|
2001-12-13 05:16:17 +08:00
|
|
|
goto notfound;
|
|
|
|
}
|
|
|
|
return r;
|
2001-11-06 09:20:20 +08:00
|
|
|
}
|
|
|
|
notfound: ;
|
|
|
|
}
|
|
|
|
|
2002-01-04 05:14:59 +08:00
|
|
|
/* 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_opi()) */
|
2001-12-17 01:58:32 +08:00
|
|
|
for(p=vstack;p<=vtop;p++) {
|
2001-12-20 09:05:21 +08:00
|
|
|
r = p->r & VT_VALMASK;
|
2001-12-13 05:16:17 +08:00
|
|
|
if (r < VT_CONST && (reg_classes[r] & rc)) {
|
2001-11-06 09:20:20 +08:00
|
|
|
save_reg(r);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2001-12-20 09:05:21 +08:00
|
|
|
void save_regs(void)
|
2001-11-06 09:20:20 +08:00
|
|
|
{
|
2001-12-17 01:58:32 +08:00
|
|
|
int r;
|
|
|
|
SValue *p;
|
|
|
|
|
|
|
|
for(p=vstack;p<=vtop;p++) {
|
2001-12-20 09:05:21 +08:00
|
|
|
r = p->r & VT_VALMASK;
|
2001-11-06 09:20:20 +08:00
|
|
|
if (r < VT_CONST) {
|
|
|
|
save_reg(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* move register 's' to 'r', and flush previous value of r to memory
|
|
|
|
if needed */
|
2001-12-13 05:16:17 +08:00
|
|
|
void move_reg(int r, int s)
|
2001-11-06 09:20:20 +08:00
|
|
|
{
|
2001-12-20 09:05:21 +08:00
|
|
|
SValue sv;
|
|
|
|
|
2001-11-06 09:20:20 +08:00
|
|
|
if (r != s) {
|
|
|
|
save_reg(r);
|
2001-12-20 09:05:21 +08:00
|
|
|
sv.t = VT_INT;
|
|
|
|
sv.r = s;
|
|
|
|
sv.c.ul = 0;
|
|
|
|
load(r, &sv);
|
2001-11-06 09:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-01-05 08:40:47 +08:00
|
|
|
/* get address of vtop (vtop MUST BE an lvalue) */
|
|
|
|
void gaddrof(void)
|
|
|
|
{
|
|
|
|
vtop->r &= ~VT_LVAL;
|
|
|
|
/* tricky: if saved lvalue, then we can go back to lvalue */
|
|
|
|
if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
|
|
|
|
vtop->r = (vtop->r & ~VT_VALMASK) | VT_LOCAL | VT_LVAL;
|
|
|
|
}
|
|
|
|
|
2002-03-04 06:45:55 +08:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2002-01-06 00:15:57 +08:00
|
|
|
/* generate lvalue bound code */
|
|
|
|
void gbound(void)
|
|
|
|
{
|
|
|
|
vtop->r &= ~VT_MUSTBOUND;
|
|
|
|
/* if lvalue, then use checking code before dereferencing */
|
|
|
|
if (vtop->r & VT_LVAL) {
|
|
|
|
gaddrof();
|
|
|
|
vpushi(0);
|
|
|
|
gen_bounded_ptr_add1();
|
|
|
|
gen_bounded_ptr_add2(1);
|
|
|
|
vtop->r |= VT_LVAL;
|
|
|
|
}
|
|
|
|
}
|
2002-03-04 06:45:55 +08:00
|
|
|
#endif
|
2002-01-06 00:15:57 +08:00
|
|
|
|
2001-12-23 08:34:26 +08:00
|
|
|
/* store vtop a register belonging to class 'rc'. lvalues are
|
2002-01-04 05:14:59 +08:00
|
|
|
converted to values. Cannot be used if cannot be converted to
|
2001-12-23 08:34:26 +08:00
|
|
|
register value (such as structures). */
|
|
|
|
int gv(int rc)
|
2001-11-06 09:20:20 +08:00
|
|
|
{
|
2002-01-04 05:14:59 +08:00
|
|
|
int r, r2, rc2, bit_pos, bit_size, size, align, i, data_offset;
|
|
|
|
unsigned long long ll;
|
2001-12-10 06:04:15 +08:00
|
|
|
|
|
|
|
/* NOTE: get_reg can modify vstack[] */
|
2001-12-17 01:58:32 +08:00
|
|
|
if (vtop->t & VT_BITFIELD) {
|
|
|
|
bit_pos = (vtop->t >> VT_STRUCT_SHIFT) & 0x3f;
|
|
|
|
bit_size = (vtop->t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
|
2001-12-10 06:04:15 +08:00
|
|
|
/* remove bit field info to avoid loops */
|
2001-12-17 01:58:32 +08:00
|
|
|
vtop->t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
|
2001-12-10 06:04:15 +08:00
|
|
|
/* generate shifts */
|
2001-12-20 09:05:21 +08:00
|
|
|
vpushi(32 - (bit_pos + bit_size));
|
2001-12-10 06:04:15 +08:00
|
|
|
gen_op(TOK_SHL);
|
2001-12-20 09:05:21 +08:00
|
|
|
vpushi(32 - bit_size);
|
2001-12-10 06:04:15 +08:00
|
|
|
/* NOTE: transformed to SHR if unsigned */
|
|
|
|
gen_op(TOK_SAR);
|
2001-12-23 08:34:26 +08:00
|
|
|
r = gv(rc);
|
2001-12-10 06:04:15 +08:00
|
|
|
} else {
|
2001-12-20 09:05:21 +08:00
|
|
|
if (is_float(vtop->t) &&
|
|
|
|
(vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
|
2001-12-18 05:56:48 +08:00
|
|
|
/* CPUs usually cannot use float constants, so we store them
|
|
|
|
generically in data segment */
|
|
|
|
size = type_size(vtop->t, &align);
|
2002-01-04 05:14:59 +08:00
|
|
|
data_offset = (int)data_section->data_ptr;
|
|
|
|
data_offset = (data_offset + align - 1) & -align;
|
2001-12-18 05:56:48 +08:00
|
|
|
/* XXX: not portable yet */
|
|
|
|
size = size >> 2;
|
|
|
|
for(i=0;i<size;i++)
|
2002-01-04 05:14:59 +08:00
|
|
|
((int *)data_offset)[i] = vtop->c.tab[i];
|
2001-12-20 09:05:21 +08:00
|
|
|
vtop->r |= VT_LVAL;
|
2002-01-04 05:14:59 +08:00
|
|
|
vtop->c.ul = data_offset;
|
|
|
|
data_offset += size << 2;
|
|
|
|
data_section->data_ptr = (unsigned char *)data_offset;
|
2001-12-18 05:56:48 +08:00
|
|
|
}
|
2002-03-04 06:45:55 +08:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2002-01-06 00:15:57 +08:00
|
|
|
if (vtop->r & VT_MUSTBOUND)
|
|
|
|
gbound();
|
2002-03-04 06:45:55 +08:00
|
|
|
#endif
|
2002-01-06 00:15:57 +08:00
|
|
|
|
2001-12-20 09:05:21 +08:00
|
|
|
r = vtop->r & VT_VALMASK;
|
2001-12-23 08:34:26 +08:00
|
|
|
/* need to reload if:
|
|
|
|
- constant
|
|
|
|
- lvalue (need to dereference pointer)
|
|
|
|
- already a register, but not in the right class */
|
|
|
|
if (r >= VT_CONST ||
|
|
|
|
(vtop->r & VT_LVAL) ||
|
2002-01-04 05:14:59 +08:00
|
|
|
!(reg_classes[r] & rc) ||
|
|
|
|
((vtop->t & VT_BTYPE) == VT_LLONG &&
|
|
|
|
!(reg_classes[vtop->r2] & rc))) {
|
2001-12-13 05:16:17 +08:00
|
|
|
r = get_reg(rc);
|
2002-01-04 05:14:59 +08:00
|
|
|
if ((vtop->t & VT_BTYPE) == VT_LLONG) {
|
|
|
|
/* two register type load : expand to two words
|
|
|
|
temporarily */
|
|
|
|
if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
|
|
|
|
/* load constant */
|
|
|
|
ll = vtop->c.ull;
|
|
|
|
vtop->c.ui = ll; /* first word */
|
|
|
|
load(r, vtop);
|
|
|
|
vtop->r = r; /* save register value */
|
|
|
|
vpushi(ll >> 32); /* second word */
|
|
|
|
} else if (r >= VT_CONST ||
|
|
|
|
(vtop->r & VT_LVAL)) {
|
|
|
|
/* load from memory */
|
|
|
|
load(r, vtop);
|
|
|
|
vdup();
|
|
|
|
vtop[-1].r = r; /* save register value */
|
|
|
|
/* increment pointer to get second word */
|
|
|
|
vtop->t = VT_INT;
|
2002-01-05 08:40:47 +08:00
|
|
|
gaddrof();
|
2002-01-04 05:14:59 +08:00
|
|
|
vpushi(4);
|
|
|
|
gen_op('+');
|
|
|
|
vtop->r |= VT_LVAL;
|
|
|
|
} else {
|
|
|
|
/* move registers */
|
|
|
|
load(r, vtop);
|
|
|
|
vdup();
|
|
|
|
vtop[-1].r = r; /* save register value */
|
|
|
|
vtop->r = vtop[-1].r2;
|
|
|
|
}
|
|
|
|
/* allocate second register */
|
|
|
|
rc2 = RC_INT;
|
|
|
|
if (rc == RC_IRET)
|
|
|
|
rc2 = RC_LRET;
|
|
|
|
r2 = get_reg(rc2);
|
|
|
|
load(r2, vtop);
|
|
|
|
vpop();
|
|
|
|
/* write second register */
|
|
|
|
vtop->r2 = r2;
|
|
|
|
} else {
|
|
|
|
/* one register type load */
|
|
|
|
load(r, vtop);
|
|
|
|
}
|
2001-12-13 05:16:17 +08:00
|
|
|
}
|
2001-12-20 09:05:21 +08:00
|
|
|
vtop->r = r;
|
2001-12-10 06:04:15 +08:00
|
|
|
}
|
2001-11-06 09:20:20 +08:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2002-01-06 00:15:57 +08:00
|
|
|
/* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
|
|
|
|
void gv2(int rc1, int rc2)
|
|
|
|
{
|
|
|
|
/* generate more generic register first */
|
|
|
|
if (rc1 <= rc2) {
|
|
|
|
vswap();
|
|
|
|
gv(rc1);
|
|
|
|
vswap();
|
|
|
|
gv(rc2);
|
|
|
|
/* test if reload is needed for first register */
|
|
|
|
if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
|
|
|
|
vswap();
|
|
|
|
gv(rc1);
|
|
|
|
vswap();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gv(rc2);
|
|
|
|
vswap();
|
|
|
|
gv(rc1);
|
|
|
|
vswap();
|
|
|
|
/* test if reload is needed for first register */
|
|
|
|
if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
|
|
|
|
gv(rc2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-01-04 05:14:59 +08:00
|
|
|
/* expand long long on stack in two int registers */
|
|
|
|
void lexpand(void)
|
|
|
|
{
|
|
|
|
int u;
|
|
|
|
|
|
|
|
u = vtop->t & VT_UNSIGNED;
|
|
|
|
gv(RC_INT);
|
|
|
|
vdup();
|
|
|
|
vtop[0].r = vtop[-1].r2;
|
|
|
|
vtop[0].r2 = VT_CONST;
|
|
|
|
vtop[-1].r2 = VT_CONST;
|
|
|
|
vtop[0].t = VT_INT | u;
|
|
|
|
vtop[-1].t = VT_INT | u;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* build a long long from two ints */
|
|
|
|
void lbuild(int t)
|
|
|
|
{
|
2002-01-06 00:15:57 +08:00
|
|
|
gv2(RC_INT, RC_INT);
|
2002-01-04 05:14:59 +08:00
|
|
|
vtop[-1].r2 = vtop[0].r;
|
|
|
|
vtop[-1].t = t;
|
|
|
|
vpop();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* rotate n first stack elements to the bottom */
|
|
|
|
void vrotb(int n)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
SValue tmp;
|
|
|
|
|
|
|
|
tmp = vtop[-n + 1];
|
|
|
|
for(i=-n+1;i!=0;i++)
|
|
|
|
vtop[i] = vtop[i+1];
|
|
|
|
vtop[0] = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pop stack value */
|
|
|
|
void vpop(void)
|
|
|
|
{
|
2002-02-11 00:14:03 +08:00
|
|
|
#ifdef TCC_TARGET_I386
|
2002-01-04 05:14:59 +08:00
|
|
|
/* for x86, we need to pop the FP stack */
|
|
|
|
if ((vtop->r & VT_VALMASK) == REG_ST0) {
|
|
|
|
o(0xd9dd); /* fstp %st(1) */
|
|
|
|
}
|
2002-02-11 00:14:03 +08:00
|
|
|
#endif
|
2002-01-04 05:14:59 +08:00
|
|
|
vtop--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* convert stack entry to register and duplicate its value in another
|
|
|
|
register */
|
|
|
|
void gv_dup(void)
|
|
|
|
{
|
|
|
|
int rc, t, r, r1;
|
|
|
|
SValue sv;
|
|
|
|
|
|
|
|
t = vtop->t;
|
|
|
|
if ((t & VT_BTYPE) == VT_LLONG) {
|
|
|
|
lexpand();
|
|
|
|
gv_dup();
|
|
|
|
vswap();
|
|
|
|
vrotb(3);
|
|
|
|
gv_dup();
|
|
|
|
vrotb(4);
|
|
|
|
/* stack: H L L1 H1 */
|
|
|
|
lbuild(t);
|
|
|
|
vrotb(3);
|
|
|
|
vrotb(3);
|
|
|
|
vswap();
|
|
|
|
lbuild(t);
|
|
|
|
vswap();
|
|
|
|
} else {
|
|
|
|
/* duplicate value */
|
|
|
|
rc = RC_INT;
|
|
|
|
sv.t = VT_INT;
|
|
|
|
if (is_float(t)) {
|
|
|
|
rc = RC_FLOAT;
|
|
|
|
sv.t = t;
|
|
|
|
}
|
|
|
|
r = gv(rc);
|
|
|
|
r1 = get_reg(rc);
|
|
|
|
sv.r = r;
|
|
|
|
sv.c.ul = 0;
|
|
|
|
load(r1, &sv); /* move r to r1 */
|
|
|
|
vdup();
|
|
|
|
/* duplicates value */
|
|
|
|
vtop->r = r1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generate CPU independent (unsigned) long long operations */
|
|
|
|
void gen_opl(int op)
|
|
|
|
{
|
|
|
|
int t, a, b, op1, c, i;
|
|
|
|
void *func;
|
|
|
|
GFuncContext gf;
|
|
|
|
SValue tmp;
|
|
|
|
|
|
|
|
switch(op) {
|
|
|
|
case '/':
|
|
|
|
case TOK_PDIV:
|
|
|
|
func = __divll;
|
|
|
|
goto gen_func;
|
|
|
|
case TOK_UDIV:
|
|
|
|
func = __divull;
|
|
|
|
goto gen_func;
|
|
|
|
case '%':
|
|
|
|
func = __modll;
|
|
|
|
goto gen_func;
|
|
|
|
case TOK_UMOD:
|
|
|
|
func = __modull;
|
|
|
|
gen_func:
|
|
|
|
/* call generic long long function */
|
2002-01-27 02:05:29 +08:00
|
|
|
gfunc_start(&gf, FUNC_CDECL);
|
2002-01-04 05:14:59 +08:00
|
|
|
gfunc_param(&gf);
|
|
|
|
gfunc_param(&gf);
|
|
|
|
vpushi((int)func);
|
|
|
|
gfunc_call(&gf);
|
|
|
|
vpushi(0);
|
|
|
|
vtop->r = REG_IRET;
|
|
|
|
vtop->r2 = REG_LRET;
|
|
|
|
break;
|
|
|
|
case '^':
|
|
|
|
case '&':
|
|
|
|
case '|':
|
|
|
|
case '*':
|
|
|
|
case '+':
|
|
|
|
case '-':
|
|
|
|
t = vtop->t;
|
|
|
|
vswap();
|
|
|
|
lexpand();
|
|
|
|
vrotb(3);
|
|
|
|
lexpand();
|
|
|
|
/* stack: L1 H1 L2 H2 */
|
|
|
|
tmp = vtop[0];
|
|
|
|
vtop[0] = vtop[-3];
|
|
|
|
vtop[-3] = tmp;
|
|
|
|
tmp = vtop[-2];
|
|
|
|
vtop[-2] = vtop[-3];
|
|
|
|
vtop[-3] = tmp;
|
|
|
|
vswap();
|
|
|
|
/* stack: H1 H2 L1 L2 */
|
|
|
|
if (op == '*') {
|
|
|
|
vpushv(vtop - 1);
|
|
|
|
vpushv(vtop - 1);
|
|
|
|
gen_op(TOK_UMULL);
|
|
|
|
lexpand();
|
|
|
|
/* stack: H1 H2 L1 L2 ML MH */
|
|
|
|
for(i=0;i<4;i++)
|
|
|
|
vrotb(6);
|
|
|
|
/* stack: ML MH H1 H2 L1 L2 */
|
|
|
|
tmp = vtop[0];
|
|
|
|
vtop[0] = vtop[-2];
|
|
|
|
vtop[-2] = tmp;
|
|
|
|
/* stack: ML MH H1 L2 H2 L1 */
|
|
|
|
gen_op('*');
|
|
|
|
vrotb(3);
|
|
|
|
vrotb(3);
|
|
|
|
gen_op('*');
|
|
|
|
/* stack: ML MH M1 M2 */
|
|
|
|
gen_op('+');
|
|
|
|
gen_op('+');
|
|
|
|
} else if (op == '+' || op == '-') {
|
2002-01-06 03:55:43 +08:00
|
|
|
/* XXX: add non carry method too (for MIPS or alpha) */
|
2002-01-04 05:14:59 +08:00
|
|
|
if (op == '+')
|
|
|
|
op1 = TOK_ADDC1;
|
|
|
|
else
|
|
|
|
op1 = TOK_SUBC1;
|
|
|
|
gen_op(op1);
|
|
|
|
/* stack: H1 H2 (L1 op L2) */
|
|
|
|
vrotb(3);
|
|
|
|
vrotb(3);
|
|
|
|
gen_op(op1 + 1); /* TOK_xxxC2 */
|
|
|
|
} else {
|
|
|
|
gen_op(op);
|
|
|
|
/* stack: H1 H2 (L1 op L2) */
|
|
|
|
vrotb(3);
|
|
|
|
vrotb(3);
|
|
|
|
/* stack: (L1 op L2) H1 H2 */
|
|
|
|
gen_op(op);
|
|
|
|
/* stack: (L1 op L2) (H1 op H2) */
|
|
|
|
}
|
|
|
|
/* stack: L H */
|
|
|
|
lbuild(t);
|
|
|
|
break;
|
|
|
|
case TOK_SAR:
|
|
|
|
case TOK_SHR:
|
|
|
|
case TOK_SHL:
|
|
|
|
if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST) {
|
|
|
|
t = vtop[-1].t;
|
|
|
|
vswap();
|
|
|
|
lexpand();
|
|
|
|
vrotb(3);
|
|
|
|
/* stack: L H shift */
|
|
|
|
c = (int)vtop->c.i;
|
|
|
|
/* constant: simpler */
|
|
|
|
/* NOTE: all comments are for SHL. the other cases are
|
|
|
|
done by swaping words */
|
|
|
|
vpop();
|
|
|
|
if (op != TOK_SHL)
|
|
|
|
vswap();
|
|
|
|
if (c >= 32) {
|
|
|
|
/* stack: L H */
|
|
|
|
vpop();
|
|
|
|
if (c > 32) {
|
|
|
|
vpushi(c - 32);
|
|
|
|
gen_op(op);
|
|
|
|
}
|
|
|
|
if (op != TOK_SAR) {
|
|
|
|
vpushi(0);
|
|
|
|
} else {
|
|
|
|
gv_dup();
|
|
|
|
vpushi(31);
|
|
|
|
gen_op(TOK_SAR);
|
|
|
|
}
|
|
|
|
vswap();
|
|
|
|
} else {
|
|
|
|
vswap();
|
|
|
|
gv_dup();
|
|
|
|
/* stack: H L L */
|
|
|
|
vpushi(c);
|
|
|
|
gen_op(op);
|
|
|
|
vswap();
|
|
|
|
vpushi(32 - c);
|
|
|
|
if (op == TOK_SHL)
|
|
|
|
gen_op(TOK_SHR);
|
|
|
|
else
|
|
|
|
gen_op(TOK_SHL);
|
|
|
|
vrotb(3);
|
|
|
|
/* stack: L L H */
|
|
|
|
vpushi(c);
|
|
|
|
gen_op(op);
|
|
|
|
gen_op('|');
|
|
|
|
}
|
|
|
|
if (op != TOK_SHL)
|
|
|
|
vswap();
|
|
|
|
lbuild(t);
|
|
|
|
} else {
|
|
|
|
/* XXX: should provide a faster fallback on x86 ? */
|
|
|
|
switch(op) {
|
|
|
|
case TOK_SAR:
|
|
|
|
func = __sardi3;
|
|
|
|
goto gen_func;
|
|
|
|
case TOK_SHR:
|
|
|
|
func = __shrdi3;
|
|
|
|
goto gen_func;
|
|
|
|
case TOK_SHL:
|
|
|
|
func = __shldi3;
|
|
|
|
goto gen_func;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* compare operations */
|
|
|
|
t = vtop->t;
|
|
|
|
vswap();
|
|
|
|
lexpand();
|
|
|
|
vrotb(3);
|
|
|
|
lexpand();
|
|
|
|
/* stack: L1 H1 L2 H2 */
|
|
|
|
tmp = vtop[-1];
|
|
|
|
vtop[-1] = vtop[-2];
|
|
|
|
vtop[-2] = tmp;
|
|
|
|
/* stack: L1 L2 H1 H2 */
|
|
|
|
/* compare high */
|
|
|
|
op1 = op;
|
|
|
|
/* when values are equal, we need to compare low words. since
|
|
|
|
the jump is inverted, we invert the test too. */
|
|
|
|
if (op1 == TOK_LT)
|
|
|
|
op1 = TOK_LE;
|
|
|
|
else if (op1 == TOK_GT)
|
|
|
|
op1 = TOK_GE;
|
|
|
|
else if (op1 == TOK_ULT)
|
|
|
|
op1 = TOK_ULE;
|
|
|
|
else if (op1 == TOK_UGT)
|
|
|
|
op1 = TOK_UGE;
|
|
|
|
a = 0;
|
|
|
|
b = 0;
|
|
|
|
gen_op(op1);
|
|
|
|
if (op1 != TOK_NE) {
|
|
|
|
a = gtst(1, 0);
|
|
|
|
}
|
|
|
|
if (op != TOK_EQ) {
|
|
|
|
/* generate non equal test */
|
|
|
|
/* XXX: NOT PORTABLE yet */
|
2002-02-11 00:14:03 +08:00
|
|
|
if (a == 0) {
|
2002-01-04 05:14:59 +08:00
|
|
|
b = gtst(0, 0);
|
2002-02-11 00:14:03 +08:00
|
|
|
} else {
|
|
|
|
#ifdef TCC_TARGET_I386
|
2002-01-04 05:14:59 +08:00
|
|
|
b = psym(0x850f, 0);
|
2002-02-11 00:14:03 +08:00
|
|
|
#else
|
|
|
|
error("not implemented");
|
|
|
|
#endif
|
|
|
|
}
|
2002-01-04 05:14:59 +08:00
|
|
|
}
|
|
|
|
/* compare low */
|
|
|
|
gen_op(op);
|
|
|
|
a = gtst(1, a);
|
|
|
|
gsym(b);
|
|
|
|
vset(VT_INT, VT_JMPI, a);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-17 19:15:05 +08:00
|
|
|
/* handle integer constant optimizations and various machine
|
|
|
|
independant opt */
|
|
|
|
void gen_opic(int op)
|
2001-11-06 09:20:20 +08:00
|
|
|
{
|
2001-12-17 01:58:32 +08:00
|
|
|
int fc, c1, c2, n;
|
|
|
|
SValue *v1, *v2;
|
2001-11-06 09:20:20 +08:00
|
|
|
|
2001-12-17 01:58:32 +08:00
|
|
|
v1 = vtop - 1;
|
|
|
|
v2 = vtop;
|
2001-12-13 05:16:17 +08:00
|
|
|
/* currently, we cannot do computations with forward symbols */
|
2001-12-20 09:05:21 +08:00
|
|
|
c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST;
|
|
|
|
c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST;
|
2001-11-06 09:20:20 +08:00
|
|
|
if (c1 && c2) {
|
2001-12-18 05:56:48 +08:00
|
|
|
fc = v2->c.i;
|
2001-11-06 09:20:20 +08:00
|
|
|
switch(op) {
|
2001-12-18 05:56:48 +08:00
|
|
|
case '+': v1->c.i += fc; break;
|
|
|
|
case '-': v1->c.i -= fc; break;
|
|
|
|
case '&': v1->c.i &= fc; break;
|
|
|
|
case '^': v1->c.i ^= fc; break;
|
|
|
|
case '|': v1->c.i |= fc; break;
|
|
|
|
case '*': v1->c.i *= fc; break;
|
2002-01-06 03:55:43 +08:00
|
|
|
|
2001-11-06 09:20:20 +08:00
|
|
|
case TOK_PDIV:
|
2002-01-06 03:55:43 +08:00
|
|
|
case '/':
|
|
|
|
case '%':
|
|
|
|
case TOK_UDIV:
|
|
|
|
case TOK_UMOD:
|
|
|
|
/* if division by zero, generate explicit division */
|
|
|
|
if (fc == 0) {
|
|
|
|
if (const_wanted)
|
|
|
|
error("division by zero in constant");
|
|
|
|
goto general_case;
|
|
|
|
}
|
|
|
|
switch(op) {
|
|
|
|
default: v1->c.i /= fc; break;
|
|
|
|
case '%': v1->c.i %= fc; break;
|
|
|
|
case TOK_UDIV: v1->c.i = (unsigned)v1->c.i / fc; break;
|
|
|
|
case TOK_UMOD: v1->c.i = (unsigned)v1->c.i % fc; break;
|
|
|
|
}
|
|
|
|
break;
|
2001-12-18 05:56:48 +08:00
|
|
|
case TOK_SHL: v1->c.i <<= fc; break;
|
|
|
|
case TOK_SHR: v1->c.i = (unsigned)v1->c.i >> fc; break;
|
|
|
|
case TOK_SAR: v1->c.i >>= fc; break;
|
2001-11-11 10:53:01 +08:00
|
|
|
/* tests */
|
2001-12-18 05:56:48 +08:00
|
|
|
case TOK_ULT: v1->c.i = (unsigned)v1->c.i < (unsigned)fc; break;
|
|
|
|
case TOK_UGE: v1->c.i = (unsigned)v1->c.i >= (unsigned)fc; break;
|
|
|
|
case TOK_EQ: v1->c.i = v1->c.i == fc; break;
|
|
|
|
case TOK_NE: v1->c.i = v1->c.i != fc; break;
|
|
|
|
case TOK_ULE: v1->c.i = (unsigned)v1->c.i <= (unsigned)fc; break;
|
|
|
|
case TOK_UGT: v1->c.i = (unsigned)v1->c.i > (unsigned)fc; break;
|
|
|
|
case TOK_LT: v1->c.i = v1->c.i < fc; break;
|
|
|
|
case TOK_GE: v1->c.i = v1->c.i >= fc; break;
|
|
|
|
case TOK_LE: v1->c.i = v1->c.i <= fc; break;
|
|
|
|
case TOK_GT: v1->c.i = v1->c.i > fc; break;
|
2001-11-11 10:53:01 +08:00
|
|
|
/* logical */
|
2001-12-18 05:56:48 +08:00
|
|
|
case TOK_LAND: v1->c.i = v1->c.i && fc; break;
|
|
|
|
case TOK_LOR: v1->c.i = v1->c.i || fc; break;
|
2001-11-06 09:20:20 +08:00
|
|
|
default:
|
|
|
|
goto general_case;
|
|
|
|
}
|
2001-12-17 01:58:32 +08:00
|
|
|
vtop--;
|
2001-11-06 09:20:20 +08:00
|
|
|
} else {
|
|
|
|
/* if commutative ops, put c2 as constant */
|
|
|
|
if (c1 && (op == '+' || op == '&' || op == '^' ||
|
|
|
|
op == '|' || op == '*')) {
|
2001-12-17 01:58:32 +08:00
|
|
|
vswap();
|
2001-11-06 09:20:20 +08:00
|
|
|
swap(&c1, &c2);
|
|
|
|
}
|
2001-12-18 05:56:48 +08:00
|
|
|
fc = vtop->c.i;
|
2001-11-06 09:20:20 +08:00
|
|
|
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))) {
|
2001-12-17 01:58:32 +08:00
|
|
|
/* nothing to do */
|
|
|
|
vtop--;
|
2001-11-06 09:20:20 +08:00
|
|
|
} 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++;
|
|
|
|
}
|
2001-12-18 05:56:48 +08:00
|
|
|
vtop->c.i = n;
|
2001-11-06 09:20:20 +08:00
|
|
|
if (op == '*')
|
|
|
|
op = TOK_SHL;
|
|
|
|
else if (op == TOK_PDIV)
|
|
|
|
op = TOK_SAR;
|
|
|
|
else
|
|
|
|
op = TOK_SHR;
|
|
|
|
}
|
|
|
|
goto general_case;
|
|
|
|
} else {
|
|
|
|
general_case:
|
|
|
|
/* call low level op generator */
|
2001-12-17 01:58:32 +08:00
|
|
|
gen_opi(op);
|
2001-11-06 09:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-17 19:15:05 +08:00
|
|
|
/* generate a floating point operation with constant propagation */
|
|
|
|
void gen_opif(int op)
|
|
|
|
{
|
|
|
|
int c1, c2;
|
|
|
|
SValue *v1, *v2;
|
|
|
|
long double f1, f2;
|
|
|
|
|
|
|
|
v1 = vtop - 1;
|
|
|
|
v2 = vtop;
|
|
|
|
/* currently, we cannot do computations with forward symbols */
|
|
|
|
c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST;
|
|
|
|
c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST;
|
|
|
|
if (c1 && c2) {
|
|
|
|
if (v1->t == VT_FLOAT) {
|
|
|
|
f1 = v1->c.f;
|
|
|
|
f2 = v2->c.f;
|
|
|
|
} else if (v1->t == VT_DOUBLE) {
|
|
|
|
f1 = v1->c.d;
|
|
|
|
f2 = v2->c.d;
|
|
|
|
} else {
|
|
|
|
f1 = v1->c.ld;
|
|
|
|
f2 = v2->c.ld;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* NOTE: we only do constant propagation if finite number (not
|
|
|
|
NaN or infinity) (ANSI spec) */
|
2002-03-17 19:32:48 +08:00
|
|
|
if (!ieee_finite(f1) || !ieee_finite(f2))
|
2002-03-17 19:15:05 +08:00
|
|
|
goto general_case;
|
|
|
|
|
|
|
|
switch(op) {
|
|
|
|
case '+': f1 += f2; break;
|
|
|
|
case '-': f1 -= f2; break;
|
|
|
|
case '*': f1 *= f2; break;
|
|
|
|
case '/':
|
|
|
|
if (f2 == 0.0) {
|
|
|
|
if (const_wanted)
|
|
|
|
error("division by zero in constant");
|
|
|
|
goto general_case;
|
|
|
|
}
|
|
|
|
f1 /= f2;
|
|
|
|
break;
|
|
|
|
/* XXX: also handles tests ? */
|
|
|
|
default:
|
|
|
|
goto general_case;
|
|
|
|
}
|
|
|
|
/* XXX: overflow test ? */
|
|
|
|
if (v1->t == VT_FLOAT) {
|
|
|
|
v1->c.f = f1;
|
|
|
|
} else if (v1->t == VT_DOUBLE) {
|
|
|
|
v1->c.d = f1;
|
|
|
|
} else {
|
|
|
|
v1->c.ld = f1;
|
|
|
|
}
|
|
|
|
vtop--;
|
|
|
|
} else {
|
|
|
|
general_case:
|
|
|
|
gen_opf(op);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2002-01-04 05:14:59 +08:00
|
|
|
#if 0
|
|
|
|
void check_pointer_types(SValue *p1, SValue *p2)
|
|
|
|
{
|
|
|
|
char buf1[256], buf2[256];
|
|
|
|
int t1, t2;
|
|
|
|
t1 = p1->t;
|
|
|
|
t2 = p2->t;
|
|
|
|
if (!is_compatible_types(t1, t2)) {
|
|
|
|
type_to_str(buf1, sizeof(buf1), t1, NULL);
|
|
|
|
type_to_str(buf2, sizeof(buf2), t2, NULL);
|
|
|
|
error("incompatible pointers '%s' and '%s'", buf1, buf2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-11-06 09:20:20 +08:00
|
|
|
/* 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
|
|
|
{
|
2001-12-13 05:16:17 +08:00
|
|
|
int u, t1, t2, bt1, bt2, t;
|
2001-11-06 09:20:20 +08:00
|
|
|
|
2001-12-17 01:58:32 +08:00
|
|
|
t1 = vtop[-1].t;
|
|
|
|
t2 = vtop[0].t;
|
2001-12-13 05:16:17 +08:00
|
|
|
bt1 = t1 & VT_BTYPE;
|
|
|
|
bt2 = t2 & VT_BTYPE;
|
|
|
|
|
2002-01-04 05:14:59 +08:00
|
|
|
if (bt1 == VT_PTR || bt2 == VT_PTR) {
|
|
|
|
/* at least one operand is a pointer */
|
|
|
|
/* relationnal op: must be both pointers */
|
|
|
|
if (op >= TOK_ULT && op <= TOK_GT) {
|
|
|
|
// check_pointer_types(vtop, vtop - 1);
|
|
|
|
/* pointers are handled are unsigned */
|
|
|
|
t = VT_INT | VT_UNSIGNED;
|
|
|
|
goto std_op;
|
2001-12-13 05:16:17 +08:00
|
|
|
}
|
2002-01-04 05:14:59 +08:00
|
|
|
/* if both pointers, then it must be the '-' op */
|
2001-12-09 06:51:04 +08:00
|
|
|
if ((t1 & VT_BTYPE) == VT_PTR &&
|
|
|
|
(t2 & VT_BTYPE) == VT_PTR) {
|
2001-11-06 09:20:20 +08:00
|
|
|
if (op != '-')
|
2002-01-04 05:14:59 +08:00
|
|
|
error("cannot use pointers here");
|
|
|
|
// check_pointer_types(vtop - 1, vtop);
|
2001-11-06 09:20:20 +08:00
|
|
|
/* XXX: check that types are compatible */
|
|
|
|
u = pointed_size(t1);
|
2002-03-17 19:15:05 +08:00
|
|
|
gen_opic(op);
|
2001-12-17 01:58:32 +08:00
|
|
|
/* set to integer type */
|
2001-12-20 09:05:21 +08:00
|
|
|
vtop->t = VT_INT;
|
|
|
|
vpushi(u);
|
2001-11-06 09:20:20 +08:00
|
|
|
gen_op(TOK_PDIV);
|
2002-01-04 05:14:59 +08:00
|
|
|
} else {
|
|
|
|
/* exactly one pointer : must be '+' or '-'. */
|
|
|
|
if (op != '-' && op != '+')
|
|
|
|
error("cannot use pointers here");
|
|
|
|
/* Put pointer as first operand */
|
2001-12-09 06:51:04 +08:00
|
|
|
if ((t2 & VT_BTYPE) == VT_PTR) {
|
2001-12-17 01:58:32 +08:00
|
|
|
vswap();
|
2001-11-06 09:20:20 +08:00
|
|
|
swap(&t1, &t2);
|
|
|
|
}
|
2002-01-04 05:14:59 +08:00
|
|
|
/* XXX: cast to int ? (long long case) */
|
2001-12-20 09:05:21 +08:00
|
|
|
vpushi(pointed_size(vtop[-1].t));
|
2001-11-06 09:20:20 +08:00
|
|
|
gen_op('*');
|
2002-03-04 06:45:55 +08:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2002-01-27 03:13:31 +08:00
|
|
|
/* if evaluating constant expression, no code should be
|
|
|
|
generated, so no bound check */
|
|
|
|
if (do_bounds_check && !const_wanted) {
|
2002-01-06 00:15:57 +08:00
|
|
|
/* if bounded pointers, we generate a special code to
|
|
|
|
test bounds */
|
2002-01-05 04:17:24 +08:00
|
|
|
if (op == '-') {
|
|
|
|
vpushi(0);
|
|
|
|
vswap();
|
|
|
|
gen_op('-');
|
|
|
|
}
|
2002-01-06 00:15:57 +08:00
|
|
|
gen_bounded_ptr_add1();
|
|
|
|
gen_bounded_ptr_add2(0);
|
2002-03-04 06:45:55 +08:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2002-03-17 19:15:05 +08:00
|
|
|
gen_opic(op);
|
2002-01-05 04:17:24 +08:00
|
|
|
}
|
2002-03-17 19:15:05 +08:00
|
|
|
/* put again type if gen_opic() swaped operands */
|
2001-12-20 09:05:21 +08:00
|
|
|
vtop->t = t1;
|
2002-01-04 05:14:59 +08:00
|
|
|
}
|
|
|
|
} else if (is_float(bt1) || is_float(bt2)) {
|
|
|
|
/* compute bigger type and do implicit casts */
|
|
|
|
if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
|
|
|
|
t = VT_LDOUBLE;
|
|
|
|
} else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
|
|
|
|
t = VT_DOUBLE;
|
2001-11-06 09:20:20 +08:00
|
|
|
} else {
|
2002-01-04 05:14:59 +08:00
|
|
|
t = VT_FLOAT;
|
2001-11-06 09:20:20 +08:00
|
|
|
}
|
2002-01-04 05:14:59 +08:00
|
|
|
/* floats can only be used for a few operations */
|
|
|
|
if (op != '+' && op != '-' && op != '*' && op != '/' &&
|
|
|
|
(op < TOK_ULT || op > TOK_GT))
|
|
|
|
error("invalid operands for binary operation");
|
|
|
|
goto std_op;
|
|
|
|
} else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
|
|
|
|
/* cast to biggest op */
|
|
|
|
t = VT_LLONG;
|
|
|
|
/* convert to unsigned if it does not fit in a long long */
|
|
|
|
if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
|
|
|
|
(t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
|
|
|
|
t |= VT_UNSIGNED;
|
|
|
|
goto std_op;
|
2001-11-06 09:20:20 +08:00
|
|
|
} else {
|
2002-01-04 05:14:59 +08:00
|
|
|
/* integer operations */
|
|
|
|
t = VT_INT;
|
|
|
|
/* convert to unsigned if it does not fit in an integer */
|
|
|
|
if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
|
|
|
|
(t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
|
|
|
|
t |= VT_UNSIGNED;
|
|
|
|
std_op:
|
|
|
|
/* XXX: currently, some unsigned operations are explicit, so
|
|
|
|
we modify them here */
|
|
|
|
if (t & VT_UNSIGNED) {
|
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
|
|
|
}
|
2002-01-04 05:14:59 +08:00
|
|
|
vswap();
|
|
|
|
gen_cast(t);
|
|
|
|
vswap();
|
|
|
|
/* special case for shifts and long long: we keep the shift as
|
|
|
|
an integer */
|
|
|
|
if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
|
|
|
|
gen_cast(VT_INT);
|
|
|
|
else
|
|
|
|
gen_cast(t);
|
|
|
|
if (is_float(t))
|
2002-03-17 19:15:05 +08:00
|
|
|
gen_opif(op);
|
2002-01-04 05:14:59 +08:00
|
|
|
else if ((t & VT_BTYPE) == VT_LLONG)
|
|
|
|
gen_opl(op);
|
|
|
|
else
|
2002-03-17 19:15:05 +08:00
|
|
|
gen_opic(op);
|
2002-01-04 05:14:59 +08:00
|
|
|
if (op >= TOK_ULT && op <= TOK_GT) {
|
|
|
|
/* relationnal op: the result is an int */
|
|
|
|
vtop->t = VT_INT;
|
|
|
|
} else {
|
|
|
|
vtop->t = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generic itof for unsigned long long case */
|
|
|
|
void gen_cvt_itof1(int t)
|
|
|
|
{
|
|
|
|
GFuncContext gf;
|
|
|
|
|
|
|
|
if ((vtop->t & (VT_BTYPE | VT_UNSIGNED)) ==
|
|
|
|
(VT_LLONG | VT_UNSIGNED)) {
|
|
|
|
|
2002-01-27 02:05:29 +08:00
|
|
|
gfunc_start(&gf, FUNC_CDECL);
|
2002-01-04 05:14:59 +08:00
|
|
|
gfunc_param(&gf);
|
|
|
|
if (t == VT_FLOAT)
|
|
|
|
vpushi((int)&__ulltof);
|
|
|
|
else if (t == VT_DOUBLE)
|
|
|
|
vpushi((int)&__ulltod);
|
|
|
|
else
|
|
|
|
vpushi((int)&__ulltold);
|
|
|
|
gfunc_call(&gf);
|
|
|
|
vpushi(0);
|
|
|
|
vtop->r = REG_FRET;
|
|
|
|
} else {
|
|
|
|
gen_cvt_itof(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generic ftoi for unsigned long long case */
|
|
|
|
void gen_cvt_ftoi1(int t)
|
|
|
|
{
|
|
|
|
GFuncContext gf;
|
|
|
|
int st;
|
|
|
|
|
|
|
|
if (t == (VT_LLONG | VT_UNSIGNED)) {
|
|
|
|
/* not handled natively */
|
2002-01-27 02:05:29 +08:00
|
|
|
gfunc_start(&gf, FUNC_CDECL);
|
2002-01-04 05:14:59 +08:00
|
|
|
st = vtop->t & VT_BTYPE;
|
|
|
|
gfunc_param(&gf);
|
|
|
|
if (st == VT_FLOAT)
|
|
|
|
vpushi((int)&__ftoull);
|
|
|
|
else if (st == VT_DOUBLE)
|
|
|
|
vpushi((int)&__dtoull);
|
|
|
|
else
|
|
|
|
vpushi((int)&__ldtoull);
|
|
|
|
gfunc_call(&gf);
|
|
|
|
vpushi(0);
|
|
|
|
vtop->r = REG_IRET;
|
|
|
|
vtop->r2 = REG_LRET;
|
|
|
|
} else {
|
|
|
|
gen_cvt_ftoi(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* force char or short cast */
|
|
|
|
void force_charshort_cast(int t)
|
|
|
|
{
|
|
|
|
int bits, dbt;
|
|
|
|
dbt = t & VT_BTYPE;
|
|
|
|
/* XXX: add optimization if lvalue : just change type and offset */
|
|
|
|
if (dbt == VT_BYTE)
|
|
|
|
bits = 8;
|
|
|
|
else
|
|
|
|
bits = 16;
|
|
|
|
if (t & VT_UNSIGNED) {
|
|
|
|
vpushi((1 << bits) - 1);
|
|
|
|
gen_op('&');
|
|
|
|
} else {
|
|
|
|
bits = 32 - bits;
|
|
|
|
vpushi(bits);
|
|
|
|
gen_op(TOK_SHL);
|
|
|
|
vpushi(bits);
|
|
|
|
gen_op(TOK_SAR);
|
2001-11-06 09:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-17 01:58:32 +08:00
|
|
|
/* cast 'vtop' to 't' type */
|
2001-12-03 05:24:43 +08:00
|
|
|
void gen_cast(int t)
|
|
|
|
{
|
2002-01-04 05:14:59 +08:00
|
|
|
int sbt, dbt, sf, df, c, st1, dt1;
|
|
|
|
|
|
|
|
/* special delayed cast for char/short */
|
|
|
|
/* XXX: in some cases (multiple cascaded casts), it may still
|
|
|
|
be incorrect */
|
|
|
|
if (vtop->r & VT_MUSTCAST) {
|
|
|
|
vtop->r &= ~VT_MUSTCAST;
|
|
|
|
force_charshort_cast(vtop->t);
|
|
|
|
}
|
2001-12-20 09:05:21 +08:00
|
|
|
|
|
|
|
dbt = t & VT_BTYPE;
|
|
|
|
sbt = vtop->t & VT_BTYPE;
|
2002-01-04 05:14:59 +08:00
|
|
|
|
2001-12-20 09:05:21 +08:00
|
|
|
if (sbt != dbt) {
|
|
|
|
sf = is_float(sbt);
|
|
|
|
df = is_float(dbt);
|
|
|
|
c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST;
|
|
|
|
if (sf && df) {
|
|
|
|
/* convert from fp to fp */
|
|
|
|
if (c) {
|
|
|
|
/* constant case: we can do it now */
|
|
|
|
/* XXX: in ISOC, cannot do it if error in convert */
|
|
|
|
if (dbt == VT_FLOAT && sbt == VT_DOUBLE)
|
|
|
|
vtop->c.f = (float)vtop->c.d;
|
|
|
|
else if (dbt == VT_FLOAT && sbt == VT_LDOUBLE)
|
|
|
|
vtop->c.f = (float)vtop->c.ld;
|
|
|
|
else if (dbt == VT_DOUBLE && sbt == VT_FLOAT)
|
|
|
|
vtop->c.d = (double)vtop->c.f;
|
|
|
|
else if (dbt == VT_DOUBLE && sbt == VT_LDOUBLE)
|
|
|
|
vtop->c.d = (double)vtop->c.ld;
|
|
|
|
else if (dbt == VT_LDOUBLE && sbt == VT_FLOAT)
|
|
|
|
vtop->c.ld = (long double)vtop->c.f;
|
|
|
|
else if (dbt == VT_LDOUBLE && sbt == VT_DOUBLE)
|
|
|
|
vtop->c.ld = (long double)vtop->c.d;
|
|
|
|
} else {
|
|
|
|
/* non constant case: generate code */
|
|
|
|
gen_cvt_ftof(dbt);
|
|
|
|
}
|
|
|
|
} else if (df) {
|
|
|
|
/* convert int to fp */
|
|
|
|
st1 = vtop->t & (VT_BTYPE | VT_UNSIGNED);
|
|
|
|
if (c) {
|
|
|
|
switch(st1) {
|
|
|
|
case VT_LLONG | VT_UNSIGNED:
|
|
|
|
case VT_LLONG:
|
2002-01-04 05:14:59 +08:00
|
|
|
/* XXX: add const cases for long long */
|
2001-12-20 09:05:21 +08:00
|
|
|
goto do_itof;
|
|
|
|
case VT_INT | VT_UNSIGNED:
|
|
|
|
switch(dbt) {
|
|
|
|
case VT_FLOAT: vtop->c.f = (float)vtop->c.ui; break;
|
|
|
|
case VT_DOUBLE: vtop->c.d = (double)vtop->c.ui; break;
|
|
|
|
case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.ui; break;
|
2001-12-18 05:56:48 +08:00
|
|
|
}
|
2001-12-20 09:05:21 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
switch(dbt) {
|
|
|
|
case VT_FLOAT: vtop->c.f = (float)vtop->c.i; break;
|
|
|
|
case VT_DOUBLE: vtop->c.d = (double)vtop->c.i; break;
|
|
|
|
case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.i; break;
|
2001-12-18 05:56:48 +08:00
|
|
|
}
|
2001-12-20 09:05:21 +08:00
|
|
|
break;
|
2001-12-18 05:56:48 +08:00
|
|
|
}
|
2001-12-20 09:05:21 +08:00
|
|
|
} else {
|
|
|
|
do_itof:
|
2002-01-04 05:14:59 +08:00
|
|
|
gen_cvt_itof1(dbt);
|
2001-12-20 09:05:21 +08:00
|
|
|
}
|
|
|
|
} else if (sf) {
|
|
|
|
/* convert fp to int */
|
|
|
|
dt1 = t & (VT_BTYPE | VT_UNSIGNED);
|
|
|
|
/* we handle char/short/etc... with generic code */
|
2002-01-04 05:14:59 +08:00
|
|
|
if (dt1 != (VT_INT | VT_UNSIGNED) &&
|
|
|
|
dt1 != (VT_LLONG | VT_UNSIGNED) &&
|
2001-12-20 09:05:21 +08:00
|
|
|
dt1 != VT_LLONG)
|
|
|
|
dt1 = VT_INT;
|
|
|
|
if (c) {
|
|
|
|
switch(dt1) {
|
|
|
|
case VT_LLONG | VT_UNSIGNED:
|
|
|
|
case VT_LLONG:
|
2002-01-04 05:14:59 +08:00
|
|
|
/* XXX: add const cases for long long */
|
2001-12-20 09:05:21 +08:00
|
|
|
goto do_ftoi;
|
|
|
|
case VT_INT | VT_UNSIGNED:
|
|
|
|
switch(sbt) {
|
|
|
|
case VT_FLOAT: vtop->c.ui = (unsigned int)vtop->c.d; break;
|
|
|
|
case VT_DOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
|
|
|
|
case VT_LDOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* int case */
|
|
|
|
switch(sbt) {
|
|
|
|
case VT_FLOAT: vtop->c.i = (int)vtop->c.d; break;
|
|
|
|
case VT_DOUBLE: vtop->c.i = (int)vtop->c.d; break;
|
|
|
|
case VT_LDOUBLE: vtop->c.i = (int)vtop->c.d; break;
|
|
|
|
}
|
|
|
|
break;
|
2001-12-14 06:28:53 +08:00
|
|
|
}
|
2001-12-20 09:05:21 +08:00
|
|
|
} else {
|
|
|
|
do_ftoi:
|
2002-01-04 05:14:59 +08:00
|
|
|
gen_cvt_ftoi1(dt1);
|
2001-12-20 09:05:21 +08:00
|
|
|
}
|
2001-12-23 01:05:23 +08:00
|
|
|
if (dt1 == VT_INT && (t & (VT_BTYPE | VT_UNSIGNED)) != dt1) {
|
2001-12-20 09:05:21 +08:00
|
|
|
/* additionnal cast for char/short/bool... */
|
2001-12-23 01:05:23 +08:00
|
|
|
vtop->t = dt1;
|
2001-12-20 09:05:21 +08:00
|
|
|
gen_cast(t);
|
|
|
|
}
|
2002-01-04 05:14:59 +08:00
|
|
|
} else if (dbt == VT_LLONG) {
|
|
|
|
/* scalar to long long */
|
|
|
|
if (c) {
|
|
|
|
if ((vtop->t & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
|
|
|
|
vtop->c.ll = vtop->c.ui;
|
|
|
|
else
|
|
|
|
vtop->c.ll = vtop->c.i;
|
|
|
|
} else {
|
|
|
|
/* machine independant conversion */
|
|
|
|
gv(RC_INT);
|
|
|
|
/* generate high word */
|
|
|
|
if ((vtop->t & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED)) {
|
|
|
|
vpushi(0);
|
|
|
|
gv(RC_INT);
|
|
|
|
} else {
|
|
|
|
gv_dup();
|
|
|
|
vpushi(31);
|
|
|
|
gen_op(TOK_SAR);
|
|
|
|
}
|
|
|
|
/* patch second register */
|
|
|
|
vtop[-1].r2 = vtop->r;
|
|
|
|
vpop();
|
|
|
|
}
|
2001-12-20 09:05:21 +08:00
|
|
|
} else if (dbt == VT_BOOL) {
|
2002-01-04 05:14:59 +08:00
|
|
|
/* scalar to bool */
|
2001-12-20 09:05:21 +08:00
|
|
|
vpushi(0);
|
|
|
|
gen_op(TOK_NE);
|
2002-01-04 05:14:59 +08:00
|
|
|
} else if (dbt == VT_BYTE || dbt == VT_SHORT) {
|
|
|
|
force_charshort_cast(t);
|
|
|
|
} else if (dbt == VT_INT) {
|
|
|
|
/* scalar to int */
|
|
|
|
if (sbt == VT_LLONG) {
|
|
|
|
/* from long long: just take low order word */
|
|
|
|
lexpand();
|
|
|
|
vpop();
|
2002-01-27 02:05:29 +08:00
|
|
|
} else if (sbt == VT_PTR) {
|
|
|
|
/* ok to cast */
|
2002-01-04 05:14:59 +08:00
|
|
|
} else if (vtop->r & VT_LVAL) {
|
|
|
|
/* if lvalue and single word type, nothing to do (XXX:
|
|
|
|
maybe incorrect for sizeof op) */
|
|
|
|
goto no_cast;
|
2001-12-13 05:16:17 +08:00
|
|
|
}
|
2001-12-03 05:24:43 +08:00
|
|
|
}
|
|
|
|
}
|
2001-12-20 09:05:21 +08:00
|
|
|
vtop->t = t;
|
2002-01-04 05:14:59 +08:00
|
|
|
no_cast: ;
|
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;
|
2001-12-09 06:51:04 +08:00
|
|
|
int bt;
|
2001-11-01 22:48:10 +08:00
|
|
|
|
2001-12-09 06:51:04 +08:00
|
|
|
bt = t & VT_BTYPE;
|
|
|
|
if (bt == 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-12-10 06:04:15 +08:00
|
|
|
} else if (bt == VT_PTR) {
|
|
|
|
if (t & VT_ARRAY) {
|
|
|
|
s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT));
|
|
|
|
return type_size(s->t, a) * s->c;
|
|
|
|
} else {
|
2001-12-14 06:28:53 +08:00
|
|
|
*a = PTR_SIZE;
|
|
|
|
return PTR_SIZE;
|
2001-12-10 06:04:15 +08:00
|
|
|
}
|
2001-12-14 06:28:53 +08:00
|
|
|
} else if (bt == VT_LDOUBLE) {
|
|
|
|
*a = LDOUBLE_ALIGN;
|
|
|
|
return LDOUBLE_SIZE;
|
2001-12-17 01:58:32 +08:00
|
|
|
} else if (bt == VT_DOUBLE || bt == VT_LLONG) {
|
2001-12-13 05:16:17 +08:00
|
|
|
*a = 8;
|
|
|
|
return 8;
|
|
|
|
} else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
|
2001-11-06 09:20:20 +08:00
|
|
|
*a = 4;
|
|
|
|
return 4;
|
2001-12-09 06:51:04 +08:00
|
|
|
} else if (bt == VT_SHORT) {
|
2001-11-12 02:00:58 +08:00
|
|
|
*a = 2;
|
|
|
|
return 2;
|
2001-11-01 22:48:10 +08:00
|
|
|
} else {
|
2001-12-13 05:16:17 +08:00
|
|
|
/* char, void, function, _Bool */
|
2001-11-01 22:48:10 +08:00
|
|
|
*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++;
|
2001-12-23 01:05:23 +08:00
|
|
|
sym_push(p, t, 0, -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-12-18 05:56:48 +08:00
|
|
|
int is_compatible_types(int t1, int t2)
|
|
|
|
{
|
|
|
|
Sym *s1, *s2;
|
|
|
|
int bt1, bt2;
|
|
|
|
|
|
|
|
t1 &= VT_TYPE;
|
|
|
|
t2 &= VT_TYPE;
|
|
|
|
bt1 = t1 & VT_BTYPE;
|
|
|
|
bt2 = t2 & VT_BTYPE;
|
|
|
|
if (bt1 == VT_PTR) {
|
|
|
|
t1 = pointed_type(t1);
|
|
|
|
/* if function, then convert implicitely to function pointer */
|
|
|
|
if (bt2 != VT_FUNC) {
|
|
|
|
if (bt2 != VT_PTR)
|
|
|
|
return 0;
|
|
|
|
t2 = pointed_type(t2);
|
|
|
|
}
|
|
|
|
/* void matches everything */
|
|
|
|
t1 &= VT_TYPE;
|
|
|
|
t2 &= VT_TYPE;
|
|
|
|
if (t1 == VT_VOID || t2 == VT_VOID)
|
|
|
|
return 1;
|
|
|
|
return is_compatible_types(t1, t2);
|
|
|
|
} else if (bt1 == VT_STRUCT) {
|
|
|
|
return (t2 == t1);
|
|
|
|
} else if (bt1 == VT_FUNC) {
|
|
|
|
if (bt2 != 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;
|
|
|
|
} else {
|
|
|
|
/* XXX: not complete */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* print a type. If 'varstr' is not NULL, then the variable is also
|
|
|
|
printed in the type */
|
2002-01-04 05:14:59 +08:00
|
|
|
/* XXX: union */
|
2001-12-18 05:56:48 +08:00
|
|
|
/* XXX: add array and function pointers */
|
|
|
|
void type_to_str(char *buf, int buf_size,
|
|
|
|
int t, const char *varstr)
|
|
|
|
{
|
|
|
|
int bt, v;
|
|
|
|
Sym *s, *sa;
|
|
|
|
char buf1[256];
|
2002-01-04 06:43:10 +08:00
|
|
|
const char *tstr;
|
2001-12-18 05:56:48 +08:00
|
|
|
|
|
|
|
t = t & VT_TYPE;
|
|
|
|
bt = t & VT_BTYPE;
|
|
|
|
buf[0] = '\0';
|
|
|
|
if (t & VT_UNSIGNED)
|
2002-01-04 06:43:10 +08:00
|
|
|
pstrcat(buf, buf_size, "unsigned ");
|
2001-12-18 05:56:48 +08:00
|
|
|
switch(bt) {
|
|
|
|
case VT_VOID:
|
2002-01-04 06:43:10 +08:00
|
|
|
tstr = "void";
|
|
|
|
goto add_tstr;
|
2001-12-18 05:56:48 +08:00
|
|
|
case VT_BOOL:
|
2002-01-04 06:43:10 +08:00
|
|
|
tstr = "_Bool";
|
|
|
|
goto add_tstr;
|
2001-12-18 05:56:48 +08:00
|
|
|
case VT_BYTE:
|
2002-01-04 06:43:10 +08:00
|
|
|
tstr = "char";
|
|
|
|
goto add_tstr;
|
2001-12-18 05:56:48 +08:00
|
|
|
case VT_SHORT:
|
2002-01-04 06:43:10 +08:00
|
|
|
tstr = "short";
|
|
|
|
goto add_tstr;
|
2001-12-18 05:56:48 +08:00
|
|
|
case VT_INT:
|
2002-01-04 06:43:10 +08:00
|
|
|
tstr = "int";
|
|
|
|
goto add_tstr;
|
2001-12-18 05:56:48 +08:00
|
|
|
case VT_LONG:
|
2002-01-04 06:43:10 +08:00
|
|
|
tstr = "long";
|
|
|
|
goto add_tstr;
|
2001-12-18 05:56:48 +08:00
|
|
|
case VT_LLONG:
|
2002-01-04 06:43:10 +08:00
|
|
|
tstr = "long long";
|
|
|
|
goto add_tstr;
|
2001-12-18 05:56:48 +08:00
|
|
|
case VT_FLOAT:
|
2002-01-04 06:43:10 +08:00
|
|
|
tstr = "float";
|
|
|
|
goto add_tstr;
|
2001-12-18 05:56:48 +08:00
|
|
|
case VT_DOUBLE:
|
2002-01-04 06:43:10 +08:00
|
|
|
tstr = "double";
|
|
|
|
goto add_tstr;
|
2001-12-18 05:56:48 +08:00
|
|
|
case VT_LDOUBLE:
|
2002-01-04 06:43:10 +08:00
|
|
|
tstr = "long double";
|
|
|
|
add_tstr:
|
|
|
|
pstrcat(buf, buf_size, tstr);
|
2001-12-18 05:56:48 +08:00
|
|
|
break;
|
|
|
|
case VT_ENUM:
|
|
|
|
case VT_STRUCT:
|
|
|
|
if (bt == VT_STRUCT)
|
2002-01-04 06:43:10 +08:00
|
|
|
tstr = "struct ";
|
2001-12-18 05:56:48 +08:00
|
|
|
else
|
2002-01-04 06:43:10 +08:00
|
|
|
tstr = "enum ";
|
|
|
|
pstrcat(buf, buf_size, tstr);
|
2001-12-18 05:56:48 +08:00
|
|
|
v = (unsigned)t >> VT_STRUCT_SHIFT;
|
|
|
|
if (v >= SYM_FIRST_ANOM)
|
2002-01-04 06:43:10 +08:00
|
|
|
pstrcat(buf, buf_size, "<anonymous>");
|
2001-12-18 05:56:48 +08:00
|
|
|
else
|
2002-01-04 06:43:10 +08:00
|
|
|
pstrcat(buf, buf_size, get_tok_str(v, NULL));
|
2001-12-18 05:56:48 +08:00
|
|
|
break;
|
|
|
|
case VT_FUNC:
|
|
|
|
s = sym_find((unsigned)t >> VT_STRUCT_SHIFT);
|
|
|
|
type_to_str(buf, buf_size, s->t, varstr);
|
2002-01-04 06:43:10 +08:00
|
|
|
pstrcat(buf, buf_size, "(");
|
2001-12-18 05:56:48 +08:00
|
|
|
sa = s->next;
|
|
|
|
while (sa != NULL) {
|
|
|
|
type_to_str(buf1, sizeof(buf1), sa->t, NULL);
|
2002-01-04 06:43:10 +08:00
|
|
|
pstrcat(buf, buf_size, buf1);
|
2001-12-18 05:56:48 +08:00
|
|
|
sa = sa->next;
|
|
|
|
if (sa)
|
2002-01-04 06:43:10 +08:00
|
|
|
pstrcat(buf, buf_size, ", ");
|
2001-12-18 05:56:48 +08:00
|
|
|
}
|
2002-01-04 06:43:10 +08:00
|
|
|
pstrcat(buf, buf_size, ")");
|
2001-12-18 05:56:48 +08:00
|
|
|
goto no_var;
|
|
|
|
case VT_PTR:
|
|
|
|
s = sym_find((unsigned)t >> VT_STRUCT_SHIFT);
|
2002-01-04 06:43:10 +08:00
|
|
|
pstrcpy(buf1, sizeof(buf1), "*");
|
2001-12-18 05:56:48 +08:00
|
|
|
if (varstr)
|
2002-01-04 06:43:10 +08:00
|
|
|
pstrcat(buf1, sizeof(buf1), varstr);
|
2001-12-18 05:56:48 +08:00
|
|
|
type_to_str(buf, buf_size, s->t, buf1);
|
|
|
|
goto no_var;
|
|
|
|
}
|
|
|
|
if (varstr) {
|
2002-01-04 06:43:10 +08:00
|
|
|
pstrcat(buf, buf_size, " ");
|
|
|
|
pstrcat(buf, buf_size, varstr);
|
2001-12-18 05:56:48 +08:00
|
|
|
}
|
|
|
|
no_var: ;
|
|
|
|
}
|
|
|
|
|
2002-01-04 05:29:54 +08:00
|
|
|
/* verify type compatibility to store vtop in 'dt' type, and generate
|
2002-01-04 05:14:59 +08:00
|
|
|
casts if needed. */
|
2001-12-18 05:56:48 +08:00
|
|
|
void gen_assign_cast(int dt)
|
|
|
|
{
|
|
|
|
int st;
|
|
|
|
char buf1[256], buf2[256];
|
|
|
|
|
2002-01-04 05:29:54 +08:00
|
|
|
st = vtop->t; /* source type */
|
|
|
|
if ((dt & VT_BTYPE) == VT_PTR) {
|
|
|
|
/* special cases for pointers */
|
|
|
|
/* a function is implicitely a function pointer */
|
|
|
|
if ((st & VT_BTYPE) == VT_FUNC) {
|
|
|
|
if (!is_compatible_types(pointed_type(dt), st))
|
|
|
|
goto error;
|
|
|
|
else
|
|
|
|
goto type_ok;
|
|
|
|
}
|
|
|
|
/* '0' can also be a pointer */
|
|
|
|
if ((st & VT_BTYPE) == VT_INT &&
|
|
|
|
((vtop->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST) &&
|
|
|
|
vtop->c.i == 0)
|
|
|
|
goto type_ok;
|
|
|
|
}
|
|
|
|
if (!is_compatible_types(dt, st)) {
|
|
|
|
error:
|
2001-12-18 05:56:48 +08:00
|
|
|
type_to_str(buf1, sizeof(buf1), st, NULL);
|
|
|
|
type_to_str(buf2, sizeof(buf2), dt, NULL);
|
|
|
|
error("cannot cast '%s' to '%s'", buf1, buf2);
|
|
|
|
}
|
2002-01-04 05:29:54 +08:00
|
|
|
type_ok:
|
2002-01-04 05:14:59 +08:00
|
|
|
gen_cast(dt);
|
2001-12-18 05:56:48 +08:00
|
|
|
}
|
|
|
|
|
2001-12-17 01:58:32 +08:00
|
|
|
/* store vtop in lvalue pushed on stack */
|
2001-12-03 05:24:43 +08:00
|
|
|
void vstore(void)
|
2001-10-28 07:48:39 +08:00
|
|
|
{
|
2002-01-04 05:14:59 +08:00
|
|
|
int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
|
2001-12-03 00:25:49 +08:00
|
|
|
GFuncContext gf;
|
2001-11-06 09:20:20 +08:00
|
|
|
|
2001-12-17 01:58:32 +08:00
|
|
|
ft = vtop[-1].t;
|
2002-01-04 05:14:59 +08:00
|
|
|
sbt = vtop->t & VT_BTYPE;
|
|
|
|
dbt = ft & VT_BTYPE;
|
|
|
|
if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
|
|
|
|
(sbt == VT_INT && dbt == VT_SHORT)) {
|
|
|
|
/* optimize char/short casts */
|
|
|
|
delayed_cast = VT_MUSTCAST;
|
|
|
|
vtop->t = ft & VT_TYPE;
|
|
|
|
} else {
|
|
|
|
delayed_cast = 0;
|
|
|
|
gen_assign_cast(ft & VT_TYPE);
|
|
|
|
}
|
2001-12-18 05:56:48 +08:00
|
|
|
|
2002-01-04 05:14:59 +08:00
|
|
|
if (sbt == VT_STRUCT) {
|
2001-12-03 00:25:49 +08:00
|
|
|
/* if structure, only generate pointer */
|
|
|
|
/* structure assignment : generate memcpy */
|
|
|
|
/* XXX: optimize if small size */
|
|
|
|
|
2001-12-17 01:58:32 +08:00
|
|
|
vdup();
|
2002-01-27 02:05:29 +08:00
|
|
|
gfunc_start(&gf, FUNC_CDECL);
|
2001-12-03 00:25:49 +08:00
|
|
|
/* type size */
|
2001-12-17 01:58:32 +08:00
|
|
|
size = type_size(vtop->t, &align);
|
2001-12-20 09:05:21 +08:00
|
|
|
vpushi(size);
|
2001-12-03 00:25:49 +08:00
|
|
|
gfunc_param(&gf);
|
|
|
|
/* source */
|
2001-12-20 09:05:21 +08:00
|
|
|
vtop->t = VT_INT;
|
2002-01-05 08:40:47 +08:00
|
|
|
gaddrof();
|
2001-12-03 00:25:49 +08:00
|
|
|
gfunc_param(&gf);
|
|
|
|
/* destination */
|
2001-12-17 01:58:32 +08:00
|
|
|
vswap();
|
2001-12-20 09:05:21 +08:00
|
|
|
vtop->t = VT_INT;
|
2002-01-05 08:40:47 +08:00
|
|
|
gaddrof();
|
2001-12-03 00:25:49 +08:00
|
|
|
gfunc_param(&gf);
|
|
|
|
|
|
|
|
save_regs();
|
2001-12-20 09:05:21 +08:00
|
|
|
vpushi((int)&memcpy);
|
2001-12-09 04:24:33 +08:00
|
|
|
gfunc_call(&gf);
|
2001-12-17 01:58:32 +08:00
|
|
|
/* leave source on stack */
|
|
|
|
} else if (ft & VT_BITFIELD) {
|
2001-12-10 06:04:15 +08:00
|
|
|
/* bitfield store handling */
|
|
|
|
bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
|
|
|
|
bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
|
|
|
|
/* remove bit field info to avoid loops */
|
2001-12-17 01:58:32 +08:00
|
|
|
vtop[-1].t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
|
|
|
|
|
|
|
|
/* duplicate destination */
|
|
|
|
vdup();
|
|
|
|
vtop[-1] = vtop[-2];
|
2001-12-10 06:04:15 +08:00
|
|
|
|
|
|
|
/* mask and shift source */
|
2001-12-20 09:05:21 +08:00
|
|
|
vpushi((1 << bit_size) - 1);
|
2001-12-10 06:04:15 +08:00
|
|
|
gen_op('&');
|
2001-12-20 09:05:21 +08:00
|
|
|
vpushi(bit_pos);
|
2001-12-10 06:04:15 +08:00
|
|
|
gen_op(TOK_SHL);
|
|
|
|
/* load destination, mask and or with source */
|
2001-12-17 01:58:32 +08:00
|
|
|
vswap();
|
2001-12-20 09:05:21 +08:00
|
|
|
vpushi(~(((1 << bit_size) - 1) << bit_pos));
|
2001-12-10 06:04:15 +08:00
|
|
|
gen_op('&');
|
|
|
|
gen_op('|');
|
|
|
|
/* store result */
|
|
|
|
vstore();
|
2001-12-03 00:25:49 +08:00
|
|
|
} else {
|
2002-03-04 06:45:55 +08:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2002-01-06 00:15:57 +08:00
|
|
|
/* bound check case */
|
|
|
|
if (vtop[-1].r & VT_MUSTBOUND) {
|
|
|
|
vswap();
|
|
|
|
gbound();
|
|
|
|
vswap();
|
|
|
|
}
|
2002-03-04 06:45:55 +08:00
|
|
|
#endif
|
2001-12-23 08:34:26 +08:00
|
|
|
rc = RC_INT;
|
|
|
|
if (is_float(ft))
|
|
|
|
rc = RC_FLOAT;
|
2002-01-06 00:15:57 +08:00
|
|
|
r = gv(rc); /* generate value */
|
2001-12-03 00:25:49 +08:00
|
|
|
/* if lvalue was saved on stack, must read it */
|
2001-12-20 09:05:21 +08:00
|
|
|
if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
|
|
|
|
SValue sv;
|
2001-12-23 08:34:26 +08:00
|
|
|
t = get_reg(RC_INT);
|
2001-12-20 09:05:21 +08:00
|
|
|
sv.t = VT_INT;
|
|
|
|
sv.r = VT_LOCAL | VT_LVAL;
|
|
|
|
sv.c.ul = vtop[-1].c.ul;
|
|
|
|
load(t, &sv);
|
|
|
|
vtop[-1].r = t | VT_LVAL;
|
2001-12-03 00:25:49 +08:00
|
|
|
}
|
2001-12-20 09:05:21 +08:00
|
|
|
store(r, vtop - 1);
|
2002-01-04 05:14:59 +08:00
|
|
|
/* two word case handling : store second register at word + 4 */
|
|
|
|
if ((ft & VT_BTYPE) == VT_LLONG) {
|
|
|
|
vswap();
|
|
|
|
/* convert to int to increment easily */
|
|
|
|
vtop->t = VT_INT;
|
2002-01-05 08:40:47 +08:00
|
|
|
gaddrof();
|
2002-01-04 05:14:59 +08:00
|
|
|
vpushi(4);
|
|
|
|
gen_op('+');
|
|
|
|
vtop->r |= VT_LVAL;
|
|
|
|
vswap();
|
|
|
|
/* XXX: it works because r2 is spilled last ! */
|
|
|
|
store(vtop->r2, vtop - 1);
|
|
|
|
}
|
|
|
|
vswap();
|
|
|
|
vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
|
|
|
|
vtop->r |= delayed_cast;
|
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 -- */
|
2001-12-10 06:04:15 +08:00
|
|
|
void inc(int post, int c)
|
2001-11-06 09:20:20 +08:00
|
|
|
{
|
|
|
|
test_lvalue();
|
2001-12-17 01:58:32 +08:00
|
|
|
vdup(); /* save lvalue */
|
2001-11-06 09:20:20 +08:00
|
|
|
if (post) {
|
2002-01-04 05:14:59 +08:00
|
|
|
gv_dup(); /* duplicate value */
|
|
|
|
vrotb(3);
|
|
|
|
vrotb(3);
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
2001-11-06 09:20:20 +08:00
|
|
|
/* add constant */
|
2001-12-20 09:05:21 +08:00
|
|
|
vpushi(c - TOK_MID);
|
2001-11-06 09:20:20 +08:00
|
|
|
gen_op('+');
|
|
|
|
vstore(); /* store value */
|
|
|
|
if (post)
|
2001-12-17 01:58:32 +08:00
|
|
|
vpop(); /* if post op, return saved value */
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
|
|
|
|
2002-01-04 05:14:59 +08:00
|
|
|
/* Parse GNUC __attribute__ extension. Currently, the following
|
|
|
|
extensions are recognized:
|
|
|
|
- aligned(n) : set data/function alignment.
|
|
|
|
- section(x) : generate data/code in this section.
|
|
|
|
- unused : currently ignored, but may be used someday.
|
|
|
|
*/
|
|
|
|
void parse_attribute(AttributeDef *ad)
|
|
|
|
{
|
|
|
|
int t, n;
|
|
|
|
|
|
|
|
next();
|
|
|
|
skip('(');
|
|
|
|
skip('(');
|
|
|
|
while (tok != ')') {
|
|
|
|
if (tok < TOK_IDENT)
|
|
|
|
expect("attribute name");
|
|
|
|
t = tok;
|
|
|
|
next();
|
|
|
|
switch(t) {
|
|
|
|
case TOK_SECTION:
|
|
|
|
case TOK___SECTION__:
|
|
|
|
skip('(');
|
|
|
|
if (tok != TOK_STR)
|
|
|
|
expect("section name");
|
|
|
|
ad->section = find_section(tokc.ts->str);
|
|
|
|
next();
|
|
|
|
skip(')');
|
|
|
|
break;
|
|
|
|
case TOK_ALIGNED:
|
|
|
|
case TOK___ALIGNED__:
|
|
|
|
skip('(');
|
|
|
|
n = expr_const();
|
|
|
|
if (n <= 0 || (n & (n - 1)) != 0)
|
|
|
|
error("alignment must be a positive power of two");
|
|
|
|
ad->aligned = n;
|
|
|
|
skip(')');
|
|
|
|
break;
|
|
|
|
case TOK_UNUSED:
|
|
|
|
case TOK___UNUSED__:
|
|
|
|
/* currently, no need to handle it because tcc does not
|
|
|
|
track unused objects */
|
|
|
|
break;
|
2002-01-27 02:05:29 +08:00
|
|
|
case TOK_NORETURN:
|
|
|
|
case TOK___NORETURN__:
|
|
|
|
/* currently, no need to handle it because tcc does not
|
|
|
|
track unused objects */
|
|
|
|
break;
|
|
|
|
case TOK_CDECL:
|
|
|
|
case TOK___CDECL:
|
|
|
|
case TOK___CDECL__:
|
|
|
|
ad->func_call = FUNC_CDECL;
|
|
|
|
break;
|
|
|
|
case TOK_STDCALL:
|
|
|
|
case TOK___STDCALL:
|
|
|
|
case TOK___STDCALL__:
|
|
|
|
ad->func_call = FUNC_STDCALL;
|
|
|
|
break;
|
2002-01-04 05:14:59 +08:00
|
|
|
default:
|
|
|
|
warning("'%s' attribute ignored", get_tok_str(t, NULL));
|
|
|
|
/* skip parameters */
|
|
|
|
/* XXX: skip parenthesis too */
|
|
|
|
if (tok == '(') {
|
|
|
|
next();
|
|
|
|
while (tok != ')' && tok != -1)
|
|
|
|
next();
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (tok != ',')
|
|
|
|
break;
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
skip(')');
|
|
|
|
skip(')');
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2001-12-10 06:04:15 +08:00
|
|
|
int a, t, b, v, size, align, maxalign, c, offset;
|
|
|
|
int bit_size, bit_pos, bsize, bt, lbit_pos;
|
2001-11-18 01:22:38 +08:00
|
|
|
Sym *s, *ss, **ps;
|
2002-01-04 05:14:59 +08:00
|
|
|
AttributeDef ad;
|
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 */
|
2002-01-04 05:14:59 +08:00
|
|
|
s = sym_find(v | SYM_STRUCT);
|
|
|
|
if (s) {
|
2001-11-01 22:48:10 +08:00
|
|
|
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
|
|
|
}
|
2001-12-23 01:05:23 +08:00
|
|
|
s = sym_push(v | SYM_STRUCT, a, 0, 0);
|
2001-11-01 22:48:10 +08:00
|
|
|
/* 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-12-10 06:04:15 +08:00
|
|
|
bit_pos = 0;
|
|
|
|
offset = 0;
|
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 */
|
2001-12-23 01:05:23 +08:00
|
|
|
sym_push(v, VT_STATIC | VT_INT, VT_CONST, c);
|
2001-11-01 22:48:10 +08:00
|
|
|
if (tok == ',')
|
|
|
|
next();
|
|
|
|
c++;
|
|
|
|
} else {
|
2002-01-04 05:14:59 +08:00
|
|
|
parse_btype(&b, &ad);
|
2001-11-01 22:48:10 +08:00
|
|
|
while (1) {
|
2001-12-10 06:04:15 +08:00
|
|
|
bit_size = -1;
|
|
|
|
v = 0;
|
|
|
|
if (tok != ':') {
|
2002-01-27 02:05:29 +08:00
|
|
|
t = type_decl(&ad, &v, b, TYPE_DIRECT);
|
2001-12-10 06:04:15 +08:00
|
|
|
if ((t & VT_BTYPE) == VT_FUNC ||
|
|
|
|
(t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN)))
|
2001-12-18 05:56:48 +08:00
|
|
|
error("invalid type for '%s'",
|
|
|
|
get_tok_str(v, NULL));
|
2001-12-10 06:04:15 +08:00
|
|
|
} else {
|
|
|
|
t = b;
|
|
|
|
}
|
|
|
|
if (tok == ':') {
|
|
|
|
next();
|
|
|
|
bit_size = expr_const();
|
|
|
|
/* XXX: handle v = 0 case for messages */
|
|
|
|
if (bit_size < 0)
|
|
|
|
error("negative width in bit-field '%s'",
|
2001-12-18 05:56:48 +08:00
|
|
|
get_tok_str(v, NULL));
|
2001-12-10 06:04:15 +08:00
|
|
|
if (v && bit_size == 0)
|
|
|
|
error("zero width for bit-field '%s'",
|
2001-12-18 05:56:48 +08:00
|
|
|
get_tok_str(v, NULL));
|
2001-12-10 06:04:15 +08:00
|
|
|
}
|
2001-11-01 22:48:10 +08:00
|
|
|
size = type_size(t, &align);
|
2001-12-10 06:04:15 +08:00
|
|
|
lbit_pos = 0;
|
|
|
|
if (bit_size >= 0) {
|
|
|
|
bt = t & VT_BTYPE;
|
|
|
|
if (bt != VT_INT &&
|
|
|
|
bt != VT_BYTE &&
|
|
|
|
bt != VT_SHORT)
|
|
|
|
error("bitfields must have scalar type");
|
|
|
|
bsize = size * 8;
|
|
|
|
if (bit_size > bsize) {
|
|
|
|
error("width of '%s' exceeds its type",
|
2001-12-18 05:56:48 +08:00
|
|
|
get_tok_str(v, NULL));
|
2001-12-10 06:04:15 +08:00
|
|
|
} else if (bit_size == bsize) {
|
|
|
|
/* no need for bit fields */
|
|
|
|
bit_pos = 0;
|
|
|
|
} else if (bit_size == 0) {
|
|
|
|
/* XXX: what to do if only padding in a
|
|
|
|
structure ? */
|
|
|
|
/* zero size: means to pad */
|
|
|
|
if (bit_pos > 0)
|
|
|
|
bit_pos = bsize;
|
|
|
|
} else {
|
|
|
|
/* we do not have enough room ? */
|
|
|
|
if ((bit_pos + bit_size) > bsize)
|
|
|
|
bit_pos = 0;
|
|
|
|
lbit_pos = bit_pos;
|
|
|
|
/* XXX: handle LSB first */
|
|
|
|
t |= VT_BITFIELD |
|
|
|
|
(bit_pos << VT_STRUCT_SHIFT) |
|
|
|
|
(bit_size << (VT_STRUCT_SHIFT + 6));
|
|
|
|
bit_pos += bit_size;
|
|
|
|
}
|
2001-11-01 22:48:10 +08:00
|
|
|
} else {
|
2001-12-10 06:04:15 +08:00
|
|
|
bit_pos = 0;
|
|
|
|
}
|
|
|
|
if (v) {
|
|
|
|
/* add new memory data only if starting
|
|
|
|
bit field */
|
|
|
|
if (lbit_pos == 0) {
|
|
|
|
if (a == TOK_STRUCT) {
|
|
|
|
c = (c + align - 1) & -align;
|
|
|
|
offset = c;
|
|
|
|
c += size;
|
|
|
|
} else {
|
|
|
|
offset = 0;
|
|
|
|
if (size > c)
|
|
|
|
c = size;
|
|
|
|
}
|
|
|
|
if (align > maxalign)
|
|
|
|
maxalign = align;
|
|
|
|
}
|
|
|
|
#if 0
|
|
|
|
printf("add field %s offset=%d",
|
2001-12-18 05:56:48 +08:00
|
|
|
get_tok_str(v, NULL), offset);
|
2001-12-10 06:04:15 +08:00
|
|
|
if (t & VT_BITFIELD) {
|
|
|
|
printf(" pos=%d size=%d",
|
|
|
|
(t >> VT_STRUCT_SHIFT) & 0x3f,
|
|
|
|
(t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
#endif
|
2001-12-23 01:05:23 +08:00
|
|
|
ss = sym_push(v | SYM_FIELD, t, 0, offset);
|
2001-12-10 06:04:15 +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.
|
|
|
|
*/
|
2002-01-04 05:14:59 +08:00
|
|
|
int parse_btype(int *type_ptr, AttributeDef *ad)
|
2001-10-28 07:48:39 +08:00
|
|
|
{
|
2001-12-23 01:05:23 +08:00
|
|
|
int t, u, type_found;
|
2001-11-01 22:48:10 +08:00
|
|
|
Sym *s;
|
2001-10-31 06:43:19 +08:00
|
|
|
|
2002-01-04 05:14:59 +08:00
|
|
|
memset(ad, 0, sizeof(AttributeDef));
|
2001-12-23 01:05:23 +08:00
|
|
|
type_found = 0;
|
2001-10-30 07:23:01 +08:00
|
|
|
t = 0;
|
|
|
|
while(1) {
|
2001-12-09 08:27:44 +08:00
|
|
|
switch(tok) {
|
|
|
|
/* basic types */
|
|
|
|
case TOK_CHAR:
|
|
|
|
u = VT_BYTE;
|
|
|
|
basic_type:
|
|
|
|
next();
|
|
|
|
basic_type1:
|
|
|
|
if ((t & VT_BTYPE) != 0)
|
2001-12-20 07:01:51 +08:00
|
|
|
error("too many basic types");
|
2001-12-09 08:27:44 +08:00
|
|
|
t |= u;
|
|
|
|
break;
|
|
|
|
case TOK_VOID:
|
|
|
|
u = VT_VOID;
|
|
|
|
goto basic_type;
|
|
|
|
case TOK_SHORT:
|
|
|
|
u = VT_SHORT;
|
|
|
|
goto basic_type;
|
|
|
|
case TOK_INT:
|
|
|
|
next();
|
|
|
|
break;
|
|
|
|
case TOK_LONG:
|
2001-12-14 06:28:53 +08:00
|
|
|
next();
|
|
|
|
if ((t & VT_BTYPE) == VT_DOUBLE) {
|
|
|
|
t = (t & ~VT_BTYPE) | VT_LDOUBLE;
|
|
|
|
} else if ((t & VT_BTYPE) == VT_LONG) {
|
|
|
|
t = (t & ~VT_BTYPE) | VT_LLONG;
|
|
|
|
} else {
|
|
|
|
u = VT_LONG;
|
|
|
|
goto basic_type1;
|
|
|
|
}
|
|
|
|
break;
|
2001-12-13 05:16:17 +08:00
|
|
|
case TOK_BOOL:
|
|
|
|
u = VT_BOOL;
|
|
|
|
goto basic_type;
|
2001-12-09 08:27:44 +08:00
|
|
|
case TOK_FLOAT:
|
2001-12-13 05:16:17 +08:00
|
|
|
u = VT_FLOAT;
|
|
|
|
goto basic_type;
|
2001-12-09 08:27:44 +08:00
|
|
|
case TOK_DOUBLE:
|
2001-12-14 06:28:53 +08:00
|
|
|
next();
|
|
|
|
if ((t & VT_BTYPE) == VT_LONG) {
|
|
|
|
t = (t & ~VT_BTYPE) | VT_LDOUBLE;
|
|
|
|
} else {
|
|
|
|
u = VT_DOUBLE;
|
|
|
|
goto basic_type1;
|
|
|
|
}
|
|
|
|
break;
|
2001-12-09 08:27:44 +08:00
|
|
|
case TOK_ENUM:
|
|
|
|
u = struct_decl(VT_ENUM);
|
|
|
|
goto basic_type1;
|
|
|
|
case TOK_STRUCT:
|
|
|
|
case TOK_UNION:
|
|
|
|
u = struct_decl(VT_STRUCT);
|
|
|
|
goto basic_type1;
|
|
|
|
|
|
|
|
/* type modifiers */
|
|
|
|
case TOK_CONST:
|
|
|
|
case TOK_VOLATILE:
|
|
|
|
case TOK_REGISTER:
|
|
|
|
case TOK_SIGNED:
|
2002-01-13 00:39:35 +08:00
|
|
|
case TOK___SIGNED__:
|
2001-12-09 08:27:44 +08:00
|
|
|
case TOK_AUTO:
|
|
|
|
case TOK_INLINE:
|
2002-01-13 00:39:35 +08:00
|
|
|
case TOK___INLINE__:
|
2001-12-09 08:27:44 +08:00
|
|
|
case TOK_RESTRICT:
|
|
|
|
next();
|
|
|
|
break;
|
|
|
|
case TOK_UNSIGNED:
|
|
|
|
t |= VT_UNSIGNED;
|
|
|
|
next();
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* storage */
|
|
|
|
case TOK_EXTERN:
|
|
|
|
t |= VT_EXTERN;
|
|
|
|
next();
|
|
|
|
break;
|
|
|
|
case TOK_STATIC:
|
|
|
|
t |= VT_STATIC;
|
|
|
|
next();
|
|
|
|
break;
|
|
|
|
case TOK_TYPEDEF:
|
|
|
|
t |= VT_TYPEDEF;
|
|
|
|
next();
|
|
|
|
break;
|
2002-01-04 05:14:59 +08:00
|
|
|
/* GNUC attribute */
|
|
|
|
case TOK___ATTRIBUTE__:
|
|
|
|
parse_attribute(ad);
|
|
|
|
break;
|
2001-12-09 08:27:44 +08:00
|
|
|
default:
|
|
|
|
s = sym_find(tok);
|
|
|
|
if (!s || !(s->t & VT_TYPEDEF))
|
|
|
|
goto the_end;
|
|
|
|
t |= (s->t & ~VT_TYPEDEF);
|
2001-10-31 06:43:19 +08:00
|
|
|
next();
|
2001-12-09 08:27:44 +08:00
|
|
|
break;
|
2001-10-30 07:23:01 +08:00
|
|
|
}
|
2001-12-23 01:05:23 +08:00
|
|
|
type_found = 1;
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
2001-12-09 08:27:44 +08:00
|
|
|
the_end:
|
2001-12-14 06:28:53 +08:00
|
|
|
/* long is never used as type */
|
|
|
|
if ((t & VT_BTYPE) == VT_LONG)
|
|
|
|
t = (t & ~VT_BTYPE) | VT_INT;
|
2001-12-23 01:05:23 +08:00
|
|
|
*type_ptr = t;
|
|
|
|
return type_found;
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
|
|
|
|
2002-01-27 02:05:29 +08:00
|
|
|
int post_type(int t, AttributeDef *ad)
|
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;
|
2002-01-27 02:05:29 +08:00
|
|
|
AttributeDef ad1;
|
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) {
|
2002-01-27 02:05:29 +08:00
|
|
|
if (!parse_btype(&pt, &ad1)) {
|
2001-11-06 09:20:20 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2001-12-18 05:56:48 +08:00
|
|
|
l = FUNC_NEW;
|
2001-12-09 06:51:04 +08:00
|
|
|
if ((pt & VT_BTYPE) == VT_VOID && tok == ')')
|
2001-11-06 09:20:20 +08:00
|
|
|
break;
|
2002-01-27 02:05:29 +08:00
|
|
|
pt = type_decl(&ad1, &n, pt, TYPE_DIRECT | TYPE_ABSTRACT);
|
2001-12-18 05:56:48 +08:00
|
|
|
if ((pt & VT_BTYPE) == VT_VOID)
|
|
|
|
error("parameter declared as void");
|
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-23 01:05:23 +08:00
|
|
|
s = sym_push(n | SYM_FIELD, pt, 0, 0);
|
2001-12-03 05:24:43 +08:00
|
|
|
*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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-12-18 05:56:48 +08:00
|
|
|
/* if no parameters, then old type prototype */
|
|
|
|
if (l == 0)
|
|
|
|
l = FUNC_OLD;
|
2001-11-06 09:20:20 +08:00
|
|
|
skip(')');
|
2001-12-08 23:04:01 +08:00
|
|
|
t1 = t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN);
|
2002-01-27 02:05:29 +08:00
|
|
|
t = post_type(t & ~(VT_TYPEDEF | VT_STATIC | VT_EXTERN), ad);
|
2001-11-08 07:13:29 +08:00
|
|
|
/* we push a anonymous symbol which will contain the function prototype */
|
|
|
|
p = anon_sym++;
|
2002-01-27 02:05:29 +08:00
|
|
|
s = sym_push(p, t, ad->func_call, 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);
|
2002-01-27 02:05:29 +08:00
|
|
|
t = post_type(t & ~(VT_TYPEDEF | VT_STATIC | VT_EXTERN), ad);
|
2001-11-06 09:20:20 +08:00
|
|
|
|
|
|
|
/* we push a anonymous symbol which will contain the array
|
|
|
|
element type */
|
|
|
|
p = anon_sym++;
|
2001-12-23 01:05:23 +08:00
|
|
|
sym_push(p, t, 0, 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
|
2002-01-27 02:05:29 +08:00
|
|
|
type. 'td' is a bitmask indicating which kind of type decl is
|
|
|
|
expected. 't' should contain the basic type. 'ad' is the attribute
|
|
|
|
definition of the basic type. It can be modified by type_decl(). */
|
|
|
|
int type_decl(AttributeDef *ad, 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
|
|
|
|
|
|
|
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();
|
2002-01-27 02:05:29 +08:00
|
|
|
/* XXX: this is not correct to modify 'ad' at this point, but
|
|
|
|
the syntax is not clear */
|
|
|
|
if (tok == TOK___ATTRIBUTE__)
|
|
|
|
parse_attribute(ad);
|
|
|
|
u = type_decl(ad, 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 */
|
2002-01-27 02:05:29 +08:00
|
|
|
t = post_type(t, ad);
|
|
|
|
if (tok == TOK___ATTRIBUTE__)
|
|
|
|
parse_attribute(ad);
|
|
|
|
if (!u)
|
2001-11-08 07:13:29 +08:00
|
|
|
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-23 01:05:23 +08:00
|
|
|
Sym *external_sym(int v, int u, int r)
|
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,
|
2001-12-23 01:05:23 +08:00
|
|
|
v, u, 0);
|
|
|
|
s->r = r | VT_CONST | VT_FORWARD;
|
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-12-17 01:58:32 +08:00
|
|
|
if ((vtop->t & VT_BTYPE) != VT_PTR)
|
2001-11-06 09:20:20 +08:00
|
|
|
expect("pointer");
|
2001-12-23 08:34:26 +08:00
|
|
|
if (vtop->r & VT_LVAL)
|
|
|
|
gv(RC_INT);
|
2001-12-17 01:58:32 +08:00
|
|
|
vtop->t = pointed_type(vtop->t);
|
2002-01-06 00:15:57 +08:00
|
|
|
/* an array is never an lvalue */
|
|
|
|
if (!(vtop->t & VT_ARRAY)) {
|
2001-12-20 09:05:21 +08:00
|
|
|
vtop->r |= VT_LVAL;
|
2002-01-06 00:15:57 +08:00
|
|
|
/* if bound checking, the referenced pointer must be checked */
|
|
|
|
if (do_bounds_check)
|
|
|
|
vtop->r |= VT_MUSTBOUND;
|
|
|
|
}
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
|
|
|
|
2001-12-18 05:56:48 +08:00
|
|
|
/* pass a parameter to a function and do type checking and casting */
|
|
|
|
void gfunc_param_typed(GFuncContext *gf, Sym *func, Sym *arg)
|
|
|
|
{
|
|
|
|
int func_type;
|
|
|
|
func_type = func->c;
|
|
|
|
if (func_type == FUNC_OLD ||
|
|
|
|
(func_type == FUNC_ELLIPSIS && arg == NULL)) {
|
|
|
|
/* default casting : only need to convert float to double */
|
|
|
|
if ((vtop->t & VT_BTYPE) == VT_FLOAT)
|
|
|
|
gen_cast(VT_DOUBLE);
|
|
|
|
} else if (arg == NULL) {
|
|
|
|
error("too many arguments to function");
|
|
|
|
} else {
|
|
|
|
gen_assign_cast(arg->t);
|
|
|
|
}
|
|
|
|
gfunc_param(gf);
|
|
|
|
}
|
|
|
|
|
2001-12-05 08:45:08 +08:00
|
|
|
void unary(void)
|
2001-10-28 07:48:39 +08:00
|
|
|
{
|
2002-01-04 05:14:59 +08:00
|
|
|
int n, t, ft, fc, p, align, size, r, data_offset;
|
2001-11-01 22:48:10 +08:00
|
|
|
Sym *s;
|
2001-12-03 00:25:49 +08:00
|
|
|
GFuncContext gf;
|
2002-01-04 05:14:59 +08:00
|
|
|
AttributeDef ad;
|
2001-10-28 07:48:39 +08:00
|
|
|
|
2002-01-04 05:14:59 +08:00
|
|
|
if (tok == TOK_CINT || tok == TOK_CCHAR || tok == TOK_LCHAR) {
|
2001-12-20 09:05:21 +08:00
|
|
|
vpushi(tokc.i);
|
2001-12-14 07:20:55 +08:00
|
|
|
next();
|
2002-01-04 05:14:59 +08:00
|
|
|
} else if (tok == TOK_CUINT) {
|
|
|
|
vsetc(VT_INT | VT_UNSIGNED, VT_CONST, &tokc);
|
|
|
|
next();
|
|
|
|
} else if (tok == TOK_CLLONG) {
|
|
|
|
vsetc(VT_LLONG, VT_CONST, &tokc);
|
|
|
|
next();
|
|
|
|
} else if (tok == TOK_CULLONG) {
|
|
|
|
vsetc(VT_LLONG | VT_UNSIGNED, VT_CONST, &tokc);
|
|
|
|
next();
|
2001-12-14 07:20:55 +08:00
|
|
|
} else if (tok == TOK_CFLOAT) {
|
2001-12-20 09:05:21 +08:00
|
|
|
vsetc(VT_FLOAT, VT_CONST, &tokc);
|
2001-12-14 07:20:55 +08:00
|
|
|
next();
|
|
|
|
} else if (tok == TOK_CDOUBLE) {
|
2001-12-20 09:05:21 +08:00
|
|
|
vsetc(VT_DOUBLE, VT_CONST, &tokc);
|
2001-12-14 07:20:55 +08:00
|
|
|
next();
|
|
|
|
} else if (tok == TOK_CLDOUBLE) {
|
2001-12-20 09:05:21 +08:00
|
|
|
vsetc(VT_LDOUBLE, 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 */
|
2002-01-04 05:14:59 +08:00
|
|
|
data_offset = (int)data_section->data_ptr;
|
|
|
|
vset(mk_pointer(VT_BYTE), VT_CONST, data_offset);
|
|
|
|
strcpy((void *)data_offset, funcname);
|
|
|
|
data_offset += strlen(funcname) + 1;
|
|
|
|
data_section->data_ptr = (unsigned char *)data_offset;
|
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);
|
2002-01-04 05:14:59 +08:00
|
|
|
data_offset = (int)data_section->data_ptr;
|
|
|
|
data_offset = (data_offset + align - 1) & -align;
|
|
|
|
fc = data_offset;
|
2001-11-29 06:48:43 +08:00
|
|
|
/* we must declare it as an array first to use initializer parser */
|
2001-12-23 01:05:23 +08:00
|
|
|
t = VT_ARRAY | mk_pointer(t);
|
2002-01-04 05:14:59 +08:00
|
|
|
decl_initializer(t, VT_CONST, data_offset, 1, 0);
|
|
|
|
data_offset += type_size(t, &align);
|
2001-11-29 06:48:43 +08:00
|
|
|
/* put it as pointer */
|
2001-12-20 09:05:21 +08:00
|
|
|
vset(t & ~VT_ARRAY, VT_CONST, fc);
|
2002-01-04 05:14:59 +08:00
|
|
|
data_section->data_ptr = (unsigned char *)data_offset;
|
2001-10-28 07:48:39 +08:00
|
|
|
} else {
|
|
|
|
t = tok;
|
|
|
|
next();
|
|
|
|
if (t == '(') {
|
|
|
|
/* cast ? */
|
2002-01-04 05:14:59 +08:00
|
|
|
if (parse_btype(&t, &ad)) {
|
2002-01-27 02:05:29 +08:00
|
|
|
ft = type_decl(&ad, &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)
|
2001-12-23 01:05:23 +08:00
|
|
|
r = VT_CONST;
|
2001-12-02 22:47:48 +08:00
|
|
|
else
|
2001-12-23 01:05:23 +08:00
|
|
|
r = VT_LOCAL;
|
2001-12-02 22:47:48 +08:00
|
|
|
/* all except arrays are lvalues */
|
|
|
|
if (!(ft & VT_ARRAY))
|
2001-12-23 01:05:23 +08:00
|
|
|
r |= VT_LVAL;
|
2002-01-04 05:14:59 +08:00
|
|
|
memset(&ad, 0, sizeof(AttributeDef));
|
|
|
|
fc = decl_initializer_alloc(ft, &ad, r, 1);
|
2001-12-23 01:05:23 +08:00
|
|
|
vset(ft, r, fc);
|
2001-12-02 22:47:48 +08:00
|
|
|
} 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 {
|
2001-12-18 05:56:48 +08:00
|
|
|
gexpr();
|
2001-10-28 07:48:39 +08:00
|
|
|
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. */
|
2001-12-17 01:58:32 +08:00
|
|
|
if ((vtop->t & VT_BTYPE) != VT_FUNC)
|
2001-12-05 08:45:08 +08:00
|
|
|
test_lvalue();
|
2001-12-20 09:05:21 +08:00
|
|
|
vtop->t = mk_pointer(vtop->t);
|
2002-01-05 08:40:47 +08:00
|
|
|
gaddrof();
|
2001-10-28 07:48:39 +08:00
|
|
|
} else
|
|
|
|
if (t == '!') {
|
|
|
|
unary();
|
2001-12-20 09:05:21 +08:00
|
|
|
if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
|
2001-12-18 05:56:48 +08:00
|
|
|
vtop->c.i = !vtop->c.i;
|
2001-12-20 09:05:21 +08:00
|
|
|
else if ((vtop->r & VT_VALMASK) == VT_CMP)
|
2001-12-18 05:56:48 +08:00
|
|
|
vtop->c.i = vtop->c.i ^ 1;
|
2001-10-28 07:48:39 +08:00
|
|
|
else
|
2001-12-20 09:05:21 +08:00
|
|
|
vset(VT_INT, 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-12-20 09:05:21 +08:00
|
|
|
vpushi(-1);
|
2001-11-06 09:20:20 +08:00
|
|
|
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) {
|
|
|
|
if (tok == '(') {
|
|
|
|
next();
|
2002-01-04 05:14:59 +08:00
|
|
|
if (parse_btype(&t, &ad)) {
|
2002-01-27 02:05:29 +08:00
|
|
|
t = type_decl(&ad, &n, t, TYPE_ABSTRACT);
|
2001-12-23 01:05:23 +08:00
|
|
|
} else {
|
2001-12-17 01:58:32 +08:00
|
|
|
/* XXX: some code could be generated: add eval
|
|
|
|
flag */
|
2001-12-18 05:56:48 +08:00
|
|
|
gexpr();
|
2001-12-17 01:58:32 +08:00
|
|
|
t = vtop->t;
|
|
|
|
vpop();
|
|
|
|
}
|
2001-11-06 09:20:20 +08:00
|
|
|
skip(')');
|
|
|
|
} else {
|
|
|
|
unary();
|
2001-12-17 01:58:32 +08:00
|
|
|
t = vtop->t;
|
|
|
|
vpop();
|
2001-11-06 09:20:20 +08:00
|
|
|
}
|
2001-12-20 09:05:21 +08:00
|
|
|
vpushi(type_size(t, &t));
|
2001-11-06 09:20:20 +08:00
|
|
|
} else
|
2001-12-17 01:58:32 +08:00
|
|
|
if (t == TOK_INC || t == TOK_DEC) {
|
2001-10-28 07:48:39 +08:00
|
|
|
unary();
|
2001-11-06 09:20:20 +08:00
|
|
|
inc(0, t);
|
2001-10-28 07:48:39 +08:00
|
|
|
} else if (t == '-') {
|
2001-12-20 09:05:21 +08:00
|
|
|
vpushi(0);
|
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-12-18 05:56:48 +08:00
|
|
|
error("'%s' undeclared", get_tok_str(t, NULL));
|
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-23 01:05:23 +08:00
|
|
|
s = external_sym(t, VT_FUNC | (p << VT_STRUCT_SHIFT), 0);
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
2001-12-23 01:05:23 +08:00
|
|
|
vset(s->t, s->r, s->c);
|
2001-12-05 08:45:08 +08:00
|
|
|
/* if forward reference, we must point to s */
|
2001-12-20 09:05:21 +08:00
|
|
|
if (vtop->r & VT_FORWARD)
|
2001-12-18 05:56:48 +08:00
|
|
|
vtop->c.sym = s;
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* post operations */
|
2001-11-01 22:48:10 +08:00
|
|
|
while (1) {
|
2002-01-04 05:14:59 +08:00
|
|
|
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();
|
2002-01-04 05:14:59 +08:00
|
|
|
} else if (tok == '.' || tok == TOK_ARROW) {
|
2001-11-01 22:48:10 +08:00
|
|
|
/* field */
|
2001-11-06 09:20:20 +08:00
|
|
|
if (tok == TOK_ARROW)
|
|
|
|
indir();
|
|
|
|
test_lvalue();
|
2002-01-05 08:40:47 +08:00
|
|
|
gaddrof();
|
2001-11-01 22:48:10 +08:00
|
|
|
next();
|
|
|
|
/* expect pointer on structure */
|
2001-12-17 01:58:32 +08:00
|
|
|
if ((vtop->t & VT_BTYPE) != VT_STRUCT)
|
2001-11-01 22:48:10 +08:00
|
|
|
expect("struct or union");
|
2001-12-17 01:58:32 +08:00
|
|
|
s = sym_find(((unsigned)vtop->t >> VT_STRUCT_SHIFT) | SYM_STRUCT);
|
2001-11-01 22:48:10 +08:00
|
|
|
/* find field */
|
|
|
|
tok |= SYM_FIELD;
|
2002-01-04 05:14:59 +08:00
|
|
|
while ((s = s->next) != NULL) {
|
2001-11-01 22:48:10 +08:00
|
|
|
if (s->v == tok)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!s)
|
|
|
|
error("field not found");
|
|
|
|
/* add field offset to pointer */
|
2002-01-05 04:17:24 +08:00
|
|
|
vtop->t = char_pointer_type; /* change type to 'char *' */
|
2001-12-20 09:05:21 +08:00
|
|
|
vpushi(s->c);
|
2001-11-06 09:20:20 +08:00
|
|
|
gen_op('+');
|
2001-11-01 22:48:10 +08:00
|
|
|
/* change type to field type, and set to lvalue */
|
2001-12-20 09:05:21 +08:00
|
|
|
vtop->t = s->t;
|
2001-11-11 10:53:01 +08:00
|
|
|
/* an array is never an lvalue */
|
2001-12-17 01:58:32 +08:00
|
|
|
if (!(vtop->t & VT_ARRAY))
|
2001-12-20 09:05:21 +08:00
|
|
|
vtop->r |= VT_LVAL;
|
2001-11-01 22:48:10 +08:00
|
|
|
next();
|
|
|
|
} else if (tok == '[') {
|
2001-11-06 09:20:20 +08:00
|
|
|
next();
|
2001-12-18 05:56:48 +08:00
|
|
|
gexpr();
|
2001-11-06 09:20:20 +08:00
|
|
|
gen_op('+');
|
|
|
|
indir();
|
2001-11-01 22:48:10 +08:00
|
|
|
skip(']');
|
|
|
|
} else if (tok == '(') {
|
2001-12-20 09:05:21 +08:00
|
|
|
SValue ret;
|
2001-12-18 05:56:48 +08:00
|
|
|
Sym *sa;
|
2001-12-03 05:24:43 +08:00
|
|
|
|
2001-11-01 22:48:10 +08:00
|
|
|
/* function call */
|
2001-12-17 01:58:32 +08:00
|
|
|
if ((vtop->t & VT_BTYPE) != VT_FUNC) {
|
2001-12-09 06:51:04 +08:00
|
|
|
/* pointer test (no array accepted) */
|
2001-12-17 01:58:32 +08:00
|
|
|
if ((vtop->t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
|
|
|
|
vtop->t = pointed_type(vtop->t);
|
|
|
|
if ((vtop->t & VT_BTYPE) != VT_FUNC)
|
2001-12-05 08:45:08 +08:00
|
|
|
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 {
|
2001-12-20 09:05:21 +08:00
|
|
|
vtop->r &= ~VT_LVAL; /* no lvalue */
|
2001-12-05 08:45:08 +08:00
|
|
|
}
|
2001-12-03 05:24:43 +08:00
|
|
|
/* get return type */
|
2001-12-17 01:58:32 +08:00
|
|
|
s = sym_find((unsigned)vtop->t >> VT_STRUCT_SHIFT);
|
2001-11-06 09:20:20 +08:00
|
|
|
save_regs(); /* save used temporary registers */
|
2002-01-27 02:05:29 +08:00
|
|
|
gfunc_start(&gf, s->r);
|
2001-11-01 22:48:10 +08:00
|
|
|
next();
|
2001-12-18 05:56:48 +08:00
|
|
|
sa = s->next; /* first parameter */
|
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--;
|
2001-12-14 06:28:53 +08:00
|
|
|
tok_add2(&str, &len, tok, &tokc);
|
2001-11-12 02:00:58 +08:00
|
|
|
next();
|
|
|
|
}
|
|
|
|
tok_add(&str, &len, -1); /* end of file added */
|
|
|
|
tok_add(&str, &len, 0);
|
2001-12-18 05:56:48 +08:00
|
|
|
s1 = sym_push2(&args, 0, 0, (int)str);
|
|
|
|
s1->next = sa; /* add reference to argument */
|
|
|
|
if (sa)
|
|
|
|
sa = sa->next;
|
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-18 05:56:48 +08:00
|
|
|
gfunc_param_typed(&gf, s, args->next);
|
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 */
|
2001-12-09 06:51:04 +08:00
|
|
|
if ((s->t & VT_BTYPE) == VT_STRUCT) {
|
2001-12-03 05:24:43 +08:00
|
|
|
/* get some space for the returned structure */
|
|
|
|
size = type_size(s->t, &align);
|
|
|
|
loc = (loc - size) & -align;
|
2001-12-20 09:05:21 +08:00
|
|
|
ret.t = s->t;
|
|
|
|
ret.r = VT_LOCAL | VT_LVAL;
|
2001-12-03 05:24:43 +08:00
|
|
|
/* pass it as 'int' to avoid structure arg passing
|
|
|
|
problems */
|
2001-12-20 09:05:21 +08:00
|
|
|
vset(VT_INT, VT_LOCAL, loc);
|
|
|
|
ret.c = vtop->c;
|
2001-12-03 05:24:43 +08:00
|
|
|
gfunc_param(&gf);
|
|
|
|
} else {
|
2001-12-20 09:05:21 +08:00
|
|
|
ret.t = s->t;
|
2002-01-04 05:14:59 +08:00
|
|
|
ret.r2 = VT_CONST;
|
2001-12-23 08:34:26 +08:00
|
|
|
/* return in register */
|
2002-01-04 05:14:59 +08:00
|
|
|
if (is_float(ret.t)) {
|
2001-12-23 08:34:26 +08:00
|
|
|
ret.r = REG_FRET;
|
2002-01-04 05:14:59 +08:00
|
|
|
} else {
|
|
|
|
if ((ret.t & VT_BTYPE) == VT_LLONG)
|
|
|
|
ret.r2 = REG_LRET;
|
2001-12-23 08:34:26 +08:00
|
|
|
ret.r = REG_IRET;
|
2002-01-04 05:14:59 +08:00
|
|
|
}
|
2001-12-20 09:05:21 +08:00
|
|
|
ret.c.i = 0;
|
2001-12-03 05:24:43 +08:00
|
|
|
}
|
|
|
|
#ifndef INVERT_FUNC_PARAMS
|
2001-11-01 22:48:10 +08:00
|
|
|
while (tok != ')') {
|
|
|
|
expr_eq();
|
2001-12-18 05:56:48 +08:00
|
|
|
gfunc_param_typed(&gf, s, sa);
|
|
|
|
if (sa)
|
|
|
|
sa = sa->next;
|
2001-11-01 22:48:10 +08:00
|
|
|
if (tok == ',')
|
|
|
|
next();
|
|
|
|
}
|
2001-11-12 02:00:58 +08:00
|
|
|
#endif
|
2001-12-18 05:56:48 +08:00
|
|
|
if (sa)
|
2001-12-20 07:01:51 +08:00
|
|
|
error("too few arguments to function");
|
2001-11-01 22:48:10 +08:00
|
|
|
skip(')');
|
2001-12-09 04:24:33 +08:00
|
|
|
gfunc_call(&gf);
|
2001-12-03 05:24:43 +08:00
|
|
|
/* return value */
|
2001-12-20 09:05:21 +08:00
|
|
|
vsetc(ret.t, ret.r, &ret.c);
|
2002-01-04 05:14:59 +08:00
|
|
|
vtop->r2 = ret.r2;
|
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-10 06:04:15 +08:00
|
|
|
void uneq(void)
|
2001-10-28 07:48:39 +08:00
|
|
|
{
|
2001-11-06 09:20:20 +08:00
|
|
|
int t;
|
2001-10-28 07:48:39 +08:00
|
|
|
|
|
|
|
unary();
|
2001-12-10 06:04:15 +08:00
|
|
|
if (tok == '=' ||
|
|
|
|
(tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
|
|
|
|
tok == TOK_A_XOR || tok == TOK_A_OR ||
|
|
|
|
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
|
|
|
t = tok;
|
|
|
|
next();
|
|
|
|
if (t == '=') {
|
2001-10-29 06:24:16 +08:00
|
|
|
expr_eq();
|
2001-11-06 09:20:20 +08:00
|
|
|
} else {
|
2001-12-17 01:58:32 +08:00
|
|
|
vdup();
|
2001-11-06 09:20:20 +08:00
|
|
|
expr_eq();
|
|
|
|
gen_op(t & 0x7f);
|
|
|
|
}
|
|
|
|
vstore();
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-14 06:28:53 +08:00
|
|
|
void sum(int l)
|
2001-10-28 07:48:39 +08:00
|
|
|
{
|
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);
|
2002-01-04 05:14:59 +08:00
|
|
|
while ((l == 0 && (tok == '*' || tok == '/' || tok == '%')) ||
|
|
|
|
(l == 1 && (tok == '+' || tok == '-')) ||
|
|
|
|
(l == 2 && (tok == TOK_SHL || tok == TOK_SAR)) ||
|
|
|
|
(l == 3 && ((tok >= TOK_ULE && tok <= TOK_GT) ||
|
|
|
|
tok == TOK_ULT || tok == TOK_UGE)) ||
|
|
|
|
(l == 4 && (tok == TOK_EQ || tok == TOK_NE)) ||
|
|
|
|
(l == 5 && tok == '&') ||
|
|
|
|
(l == 6 && tok == '^') ||
|
|
|
|
(l == 7 && tok == '|') ||
|
|
|
|
(l == 8 && tok == TOK_LAND) ||
|
|
|
|
(l == 9 && tok == TOK_LOR)) {
|
2001-11-06 09:20:20 +08:00
|
|
|
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-12-20 09:05:21 +08:00
|
|
|
vset(VT_INT, 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);
|
2001-12-20 09:05:21 +08:00
|
|
|
vset(VT_INT, VT_JMP, t);
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|
|
|
|
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-12-14 06:28:53 +08:00
|
|
|
void expr_eq(void)
|
2001-10-28 10:51:52 +08:00
|
|
|
{
|
2001-12-23 08:34:26 +08:00
|
|
|
int t, u, c, r1, r2, rc;
|
2001-11-11 10:53:01 +08:00
|
|
|
|
|
|
|
if (const_wanted) {
|
|
|
|
sum(10);
|
|
|
|
if (tok == '?') {
|
2001-12-18 05:56:48 +08:00
|
|
|
c = vtop->c.i;
|
2001-12-17 01:58:32 +08:00
|
|
|
vpop();
|
2001-11-11 10:53:01 +08:00
|
|
|
next();
|
2001-12-18 05:56:48 +08:00
|
|
|
gexpr();
|
|
|
|
t = vtop->c.i;
|
2001-12-17 01:58:32 +08:00
|
|
|
vpop();
|
2001-11-11 10:53:01 +08:00
|
|
|
skip(':');
|
|
|
|
expr_eq();
|
|
|
|
if (c)
|
2001-12-18 05:56:48 +08:00
|
|
|
vtop->c.i = t;
|
2001-11-11 10:53:01 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
eor();
|
|
|
|
if (tok == '?') {
|
|
|
|
next();
|
|
|
|
t = gtst(1, 0);
|
2001-12-18 05:56:48 +08:00
|
|
|
gexpr();
|
2002-01-04 05:14:59 +08:00
|
|
|
/* XXX: long long handling ? */
|
2001-12-23 08:34:26 +08:00
|
|
|
rc = RC_INT;
|
|
|
|
if (is_float(vtop->t))
|
|
|
|
rc = RC_FLOAT;
|
|
|
|
r1 = gv(rc);
|
2002-01-27 02:05:29 +08:00
|
|
|
vtop--; /* no vpop so that FP stack is not flushed */
|
2001-11-11 10:53:01 +08:00
|
|
|
skip(':');
|
|
|
|
u = gjmp(0);
|
2001-12-17 01:58:32 +08:00
|
|
|
|
2001-11-11 10:53:01 +08:00
|
|
|
gsym(t);
|
|
|
|
expr_eq();
|
2001-12-23 08:34:26 +08:00
|
|
|
r2 = gv(rc);
|
2001-11-12 02:00:58 +08:00
|
|
|
move_reg(r1, r2);
|
2001-12-20 09:05:21 +08:00
|
|
|
vtop->r = r1;
|
2001-11-11 10:53:01 +08:00
|
|
|
gsym(u);
|
|
|
|
}
|
2001-10-28 10:51:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-18 05:56:48 +08:00
|
|
|
void gexpr(void)
|
2001-10-28 10:51:52 +08:00
|
|
|
{
|
|
|
|
while (1) {
|
|
|
|
expr_eq();
|
|
|
|
if (tok != ',')
|
|
|
|
break;
|
2001-12-17 01:58:32 +08:00
|
|
|
vpop();
|
2001-10-28 10:51:52 +08:00
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-18 05:56:48 +08:00
|
|
|
/* parse a constant expression and return value in vtop */
|
|
|
|
void expr_const1(void)
|
2001-11-11 10:53:01 +08:00
|
|
|
{
|
2001-12-18 05:56:48 +08:00
|
|
|
int a;
|
2001-11-11 10:53:01 +08:00
|
|
|
a = const_wanted;
|
|
|
|
const_wanted = 1;
|
|
|
|
expr_eq();
|
2001-12-20 09:05:21 +08:00
|
|
|
if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
|
2001-11-11 10:53:01 +08:00
|
|
|
expect("constant");
|
|
|
|
const_wanted = a;
|
2001-12-18 05:56:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* parse an integer constant and return its value */
|
|
|
|
int expr_const(void)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
expr_const1();
|
|
|
|
c = vtop->c.i;
|
2001-12-17 01:58:32 +08:00
|
|
|
vpop();
|
|
|
|
return c;
|
2001-11-11 10:53:01 +08:00
|
|
|
}
|
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)
|
|
|
|
{
|
2001-12-14 06:28:53 +08:00
|
|
|
int t;
|
|
|
|
CValue c;
|
2001-12-05 10:02:25 +08:00
|
|
|
|
|
|
|
/* fast test first */
|
|
|
|
if (tok < TOK_UIDENT)
|
|
|
|
return 0;
|
2001-12-14 06:28:53 +08:00
|
|
|
/* no need to save tokc since we expect an identifier */
|
2001-12-05 10:02:25 +08:00
|
|
|
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
|
|
|
|
2002-01-05 04:17:24 +08:00
|
|
|
/* generate line number info */
|
|
|
|
if (do_debug &&
|
2002-01-13 00:39:35 +08:00
|
|
|
(last_line_num != file->line_num || last_ind != ind)) {
|
|
|
|
put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
|
2002-01-05 04:17:24 +08:00
|
|
|
last_ind = ind;
|
2002-01-13 00:39:35 +08:00
|
|
|
last_line_num = file->line_num;
|
2002-01-05 04:17:24 +08:00
|
|
|
}
|
|
|
|
|
2001-10-28 07:48:39 +08:00
|
|
|
if (tok == TOK_IF) {
|
|
|
|
/* if test */
|
|
|
|
next();
|
|
|
|
skip('(');
|
2001-12-18 05:56:48 +08:00
|
|
|
gexpr();
|
2001-10-28 07:48:39 +08:00
|
|
|
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('(');
|
2001-12-18 05:56:48 +08:00
|
|
|
gexpr();
|
2001-10-28 07:48:39 +08:00
|
|
|
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);
|
2002-02-11 00:14:03 +08:00
|
|
|
gjmp_addr(d);
|
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-18 05:56:48 +08:00
|
|
|
gexpr();
|
2002-01-04 05:14:59 +08:00
|
|
|
gen_assign_cast(func_vt);
|
2001-12-09 06:51:04 +08:00
|
|
|
if ((func_vt & VT_BTYPE) == VT_STRUCT) {
|
2001-12-03 05:24:43 +08:00
|
|
|
/* if returning structure, must copy it to implicit
|
|
|
|
first pointer arg location */
|
2001-12-20 09:05:21 +08:00
|
|
|
vset(mk_pointer(func_vt), VT_LOCAL | VT_LVAL, func_vc);
|
2001-12-03 05:24:43 +08:00
|
|
|
indir();
|
2001-12-17 01:58:32 +08:00
|
|
|
vswap();
|
2001-12-03 05:24:43 +08:00
|
|
|
/* copy structure value to pointer */
|
|
|
|
vstore();
|
2001-12-18 05:56:48 +08:00
|
|
|
} else if (is_float(func_vt)) {
|
2001-12-23 08:34:26 +08:00
|
|
|
gv(RC_FRET);
|
2001-12-03 05:24:43 +08:00
|
|
|
} else {
|
2001-12-23 08:34:26 +08:00
|
|
|
gv(RC_IRET);
|
2001-12-03 05:24:43 +08:00
|
|
|
}
|
2002-01-06 00:15:57 +08:00
|
|
|
vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
|
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('(');
|
2001-12-17 01:58:32 +08:00
|
|
|
if (tok != ';') {
|
2001-12-18 05:56:48 +08:00
|
|
|
gexpr();
|
2001-12-17 01:58:32 +08:00
|
|
|
vpop();
|
|
|
|
}
|
2001-10-28 10:51:52 +08:00
|
|
|
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 != ';') {
|
2001-12-18 05:56:48 +08:00
|
|
|
gexpr();
|
2001-10-28 10:51:52 +08:00
|
|
|
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;
|
2001-12-18 05:56:48 +08:00
|
|
|
gexpr();
|
2001-12-17 01:58:32 +08:00
|
|
|
vpop();
|
2002-02-11 00:14:03 +08:00
|
|
|
gjmp_addr(d);
|
2001-10-28 10:51:52 +08:00
|
|
|
gsym(e);
|
|
|
|
}
|
|
|
|
skip(')');
|
2001-11-06 09:20:20 +08:00
|
|
|
block(&a, &b, case_sym, def_sym, case_reg);
|
2002-02-11 00:14:03 +08:00
|
|
|
gjmp_addr(c);
|
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);
|
2001-12-18 05:56:48 +08:00
|
|
|
gexpr();
|
2001-10-31 04:27:54 +08:00
|
|
|
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('(');
|
2001-12-18 05:56:48 +08:00
|
|
|
gexpr();
|
2002-01-04 05:14:59 +08:00
|
|
|
/* XXX: other types than integer */
|
2001-12-23 08:34:26 +08:00
|
|
|
case_reg = gv(RC_INT);
|
2001-12-17 01:58:32 +08:00
|
|
|
vpop();
|
2001-10-31 06:43:19 +08:00
|
|
|
skip(')');
|
|
|
|
a = 0;
|
2001-12-09 08:27:44 +08:00
|
|
|
b = gjmp(0); /* jump to first case */
|
2001-10-31 06:43:19 +08:00
|
|
|
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) {
|
2002-01-04 05:14:59 +08:00
|
|
|
int v1, v2;
|
2001-10-31 06:43:19 +08:00
|
|
|
if (!case_sym)
|
2001-11-01 22:48:10 +08:00
|
|
|
expect("switch");
|
2002-01-04 05:14:59 +08:00
|
|
|
next();
|
|
|
|
v1 = expr_const();
|
|
|
|
v2 = v1;
|
|
|
|
if (gnu_ext && tok == TOK_DOTS) {
|
|
|
|
next();
|
|
|
|
v2 = expr_const();
|
|
|
|
if (v2 < v1)
|
|
|
|
warning("empty case range");
|
|
|
|
}
|
2001-12-09 08:27:44 +08:00
|
|
|
/* since a case is like a label, we must skip it with a jmp */
|
|
|
|
b = gjmp(0);
|
2001-10-31 06:43:19 +08:00
|
|
|
gsym(*case_sym);
|
2001-12-20 09:05:21 +08:00
|
|
|
vset(VT_INT, case_reg, 0);
|
2002-01-04 05:14:59 +08:00
|
|
|
vpushi(v1);
|
|
|
|
if (v1 == v2) {
|
|
|
|
gen_op(TOK_EQ);
|
|
|
|
*case_sym = gtst(1, 0);
|
|
|
|
} else {
|
|
|
|
gen_op(TOK_GE);
|
|
|
|
*case_sym = gtst(1, 0);
|
|
|
|
vset(VT_INT, case_reg, 0);
|
|
|
|
vpushi(v2);
|
|
|
|
gen_op(TOK_LE);
|
|
|
|
*case_sym = gtst(1, *case_sym);
|
|
|
|
}
|
2001-12-09 08:27:44 +08:00
|
|
|
gsym(b);
|
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)
|
2002-02-11 00:14:03 +08:00
|
|
|
s->c = gjmp(s->c);
|
2001-10-31 06:43:19 +08:00
|
|
|
else
|
2002-02-11 00:14:03 +08:00
|
|
|
gjmp_addr(s->c);
|
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 != ';') {
|
2001-12-18 05:56:48 +08:00
|
|
|
gexpr();
|
2001-12-17 01:58:32 +08:00
|
|
|
vpop();
|
2001-10-31 06:43:19 +08:00
|
|
|
}
|
|
|
|
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-12-23 01:05:23 +08:00
|
|
|
void decl_designator(int t, int r, int c,
|
2001-11-29 06:48:43 +08:00
|
|
|
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-12-09 06:51:04 +08:00
|
|
|
if ((t & VT_BTYPE) != VT_STRUCT)
|
2001-11-18 01:22:38 +08:00
|
|
|
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-12-23 01:05:23 +08:00
|
|
|
decl_initializer(t, r, c, 0, size_only);
|
2001-11-27 06:57:27 +08:00
|
|
|
}
|
|
|
|
|
2001-12-23 01:05:23 +08:00
|
|
|
#define EXPR_VAL 0
|
|
|
|
#define EXPR_CONST 1
|
|
|
|
#define EXPR_ANY 2
|
2001-11-27 06:57:27 +08:00
|
|
|
|
2001-12-23 01:05:23 +08:00
|
|
|
/* store a value or an expression directly in global data or in local array */
|
|
|
|
void init_putv(int t, int r, int c,
|
|
|
|
int v, int expr_type)
|
2001-11-27 06:57:27 +08:00
|
|
|
{
|
2001-12-18 05:56:48 +08:00
|
|
|
int saved_global_expr, bt;
|
2001-12-02 22:47:48 +08:00
|
|
|
|
2001-12-23 01:05:23 +08:00
|
|
|
switch(expr_type) {
|
|
|
|
case EXPR_VAL:
|
|
|
|
vpushi(v);
|
|
|
|
break;
|
|
|
|
case EXPR_CONST:
|
|
|
|
/* compound literals must be allocated globally in this case */
|
|
|
|
saved_global_expr = global_expr;
|
|
|
|
global_expr = 1;
|
|
|
|
expr_const1();
|
|
|
|
global_expr = saved_global_expr;
|
|
|
|
break;
|
|
|
|
case EXPR_ANY:
|
|
|
|
expr_eq();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((r & VT_VALMASK) == VT_CONST) {
|
2001-12-18 05:56:48 +08:00
|
|
|
/* XXX: not portable */
|
2002-01-06 03:55:43 +08:00
|
|
|
gen_assign_cast(t);
|
|
|
|
bt = t & VT_BTYPE;
|
2001-12-18 05:56:48 +08:00
|
|
|
switch(bt) {
|
|
|
|
case VT_BYTE:
|
|
|
|
*(char *)c = vtop->c.i;
|
|
|
|
break;
|
|
|
|
case VT_SHORT:
|
|
|
|
*(short *)c = vtop->c.i;
|
|
|
|
break;
|
|
|
|
case VT_DOUBLE:
|
|
|
|
*(double *)c = vtop->c.d;
|
|
|
|
break;
|
|
|
|
case VT_LDOUBLE:
|
|
|
|
*(long double *)c = vtop->c.ld;
|
|
|
|
break;
|
|
|
|
case VT_LLONG:
|
|
|
|
*(long long *)c = vtop->c.ll;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*(int *)c = vtop->c.i;
|
|
|
|
break;
|
|
|
|
}
|
2002-01-06 03:55:43 +08:00
|
|
|
vtop--;
|
2001-11-27 06:57:27 +08:00
|
|
|
} else {
|
2001-12-23 01:05:23 +08:00
|
|
|
vset(t, r, c);
|
|
|
|
vswap();
|
2001-11-27 06:57:27 +08:00
|
|
|
vstore();
|
2001-12-17 01:58:32 +08:00
|
|
|
vpop();
|
2001-11-27 06:57:27 +08:00
|
|
|
}
|
2001-11-18 01:22:38 +08:00
|
|
|
}
|
|
|
|
|
2001-11-29 06:48:43 +08:00
|
|
|
/* put zeros for variable based init */
|
2001-12-23 01:05:23 +08:00
|
|
|
void init_putz(int t, int r, int c, int size)
|
2001-11-29 06:48:43 +08:00
|
|
|
{
|
2001-12-03 00:25:49 +08:00
|
|
|
GFuncContext gf;
|
2001-11-29 06:48:43 +08:00
|
|
|
|
2001-12-23 01:05:23 +08:00
|
|
|
if ((r & VT_VALMASK) == VT_CONST) {
|
2002-01-06 03:55:43 +08:00
|
|
|
/* nothing to do because globals are already set to zero */
|
2001-11-29 06:48:43 +08:00
|
|
|
} else {
|
2002-01-27 02:05:29 +08:00
|
|
|
gfunc_start(&gf, FUNC_CDECL);
|
2001-12-20 09:05:21 +08:00
|
|
|
vpushi(size);
|
2001-12-03 00:25:49 +08:00
|
|
|
gfunc_param(&gf);
|
2001-12-20 09:05:21 +08:00
|
|
|
vpushi(0);
|
2001-12-03 00:25:49 +08:00
|
|
|
gfunc_param(&gf);
|
2001-12-20 09:05:21 +08:00
|
|
|
vset(VT_INT, VT_LOCAL, c);
|
2001-12-03 00:25:49 +08:00
|
|
|
gfunc_param(&gf);
|
2001-12-20 09:05:21 +08:00
|
|
|
vpushi((int)&memset);
|
2001-12-09 04:24:33 +08:00
|
|
|
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). */
|
2001-12-23 01:05:23 +08:00
|
|
|
void decl_initializer(int t, int r, 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;
|
2001-12-23 01:05:23 +08:00
|
|
|
int t1, size1, align1, expr_type;
|
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 &&
|
2001-12-09 06:51:04 +08:00
|
|
|
(t1 & VT_BTYPE) == VT_INT) ||
|
2001-12-02 22:47:48 +08:00
|
|
|
(tok == TOK_STR &&
|
2001-12-09 06:51:04 +08:00
|
|
|
(t1 & VT_BTYPE) == 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-12-14 06:28:53 +08:00
|
|
|
ts = tokc.ts;
|
2001-11-27 06:57:27 +08:00
|
|
|
/* 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-12-23 01:05:23 +08:00
|
|
|
init_putv(t1, r, c + (array_length + i) * size1,
|
|
|
|
ts->str[i], EXPR_VAL);
|
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-12-23 01:05:23 +08:00
|
|
|
init_putv(t1, r, c + (array_length * size1), 0, EXPR_VAL);
|
2001-11-27 06:57:27 +08:00
|
|
|
}
|
|
|
|
array_length++;
|
2001-11-18 01:22:38 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
index = 0;
|
|
|
|
while (tok != '}') {
|
2001-12-23 01:05:23 +08:00
|
|
|
decl_designator(t, r, 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) {
|
2001-12-23 01:05:23 +08:00
|
|
|
init_putz(t1, r, c + array_length * size1,
|
2001-11-29 06:48:43 +08:00
|
|
|
(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) {
|
2001-12-23 01:05:23 +08:00
|
|
|
init_putz(t1, r, c + array_length * size1,
|
2001-11-29 06:48:43 +08:00
|
|
|
(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-09 06:51:04 +08:00
|
|
|
} else if ((t & VT_BTYPE) == 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-12-23 01:05:23 +08:00
|
|
|
decl_designator(t, r, 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) {
|
2001-12-23 01:05:23 +08:00
|
|
|
init_putz(t, r, c + array_length,
|
2001-11-29 06:48:43 +08:00
|
|
|
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) {
|
2001-12-23 01:05:23 +08:00
|
|
|
init_putz(t, r, c + array_length,
|
2001-11-29 06:48:43 +08:00
|
|
|
n - array_length);
|
|
|
|
}
|
|
|
|
skip('}');
|
|
|
|
} else if (tok == '{') {
|
|
|
|
next();
|
2001-12-23 01:05:23 +08:00
|
|
|
decl_initializer(t, r, 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-12-23 01:05:23 +08:00
|
|
|
/* currently, we always use constant expression for globals
|
|
|
|
(may change for scripting case) */
|
|
|
|
expr_type = EXPR_CONST;
|
|
|
|
if ((r & VT_VALMASK) == VT_LOCAL)
|
|
|
|
expr_type = EXPR_ANY;
|
|
|
|
init_putv(t, r, c, 0, expr_type);
|
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
|
2001-12-23 01:05:23 +08:00
|
|
|
allocate space in local or global data space ('r' is either
|
|
|
|
VT_LOCAL or VT_CONST). The allocated address in returned */
|
2002-01-04 05:14:59 +08:00
|
|
|
int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init)
|
2001-12-02 22:47:48 +08:00
|
|
|
{
|
2002-01-04 05:14:59 +08:00
|
|
|
int size, align, addr, tok1, data_offset;
|
2001-12-02 22:47:48 +08:00
|
|
|
int *init_str, init_len, level, *saved_macro_ptr;
|
2002-01-04 05:14:59 +08:00
|
|
|
Section *sec;
|
2001-12-02 22:47:48 +08:00
|
|
|
|
|
|
|
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)
|
2001-12-20 07:01:51 +08:00
|
|
|
error("unexpected end of file in initializer");
|
2001-12-14 06:28:53 +08:00
|
|
|
tok_add2(&init_str, &init_len, tok, &tokc);
|
2001-12-02 22:47:48 +08:00
|
|
|
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();
|
2001-12-23 01:05:23 +08:00
|
|
|
decl_initializer(t, r, 0, 1, 1);
|
2001-12-02 22:47:48 +08:00
|
|
|
/* 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");
|
|
|
|
}
|
2002-01-04 05:14:59 +08:00
|
|
|
/* take into account specified alignment if bigger */
|
|
|
|
if (ad->aligned > align)
|
|
|
|
align = ad->aligned;
|
2001-12-23 01:05:23 +08:00
|
|
|
if ((r & VT_VALMASK) == VT_LOCAL) {
|
2002-01-06 00:15:57 +08:00
|
|
|
if (do_bounds_check && (t & VT_ARRAY))
|
|
|
|
loc--;
|
2002-02-11 00:14:03 +08:00
|
|
|
#ifdef TCC_TARGET_IL
|
|
|
|
/* XXX: ugly patch to allocate local variables for IL, just
|
|
|
|
for testing */
|
|
|
|
addr = loc;
|
|
|
|
loc++;
|
|
|
|
#else
|
2001-12-02 22:47:48 +08:00
|
|
|
loc = (loc - size) & -align;
|
|
|
|
addr = loc;
|
2002-02-11 00:14:03 +08:00
|
|
|
#endif
|
2002-01-06 00:15:57 +08:00
|
|
|
/* handles bounds */
|
|
|
|
/* XXX: currently, since we do only one pass, we cannot track
|
|
|
|
'&' operators, so we add only arrays */
|
|
|
|
if (do_bounds_check && (t & VT_ARRAY)) {
|
|
|
|
int *bounds_ptr;
|
|
|
|
/* add padding between regions */
|
|
|
|
loc--;
|
|
|
|
/* then add local bound info */
|
|
|
|
bounds_ptr = (int *)lbounds_section->data_ptr;
|
|
|
|
*bounds_ptr++ = addr;
|
|
|
|
*bounds_ptr++ = size;
|
|
|
|
lbounds_section->data_ptr = (unsigned char *)bounds_ptr;
|
|
|
|
}
|
2001-12-02 22:47:48 +08:00
|
|
|
} else {
|
2002-01-04 05:14:59 +08:00
|
|
|
/* compute section */
|
|
|
|
sec = ad->section;
|
|
|
|
if (!sec) {
|
|
|
|
if (has_init)
|
|
|
|
sec = data_section;
|
|
|
|
else
|
|
|
|
sec = bss_section;
|
|
|
|
}
|
|
|
|
data_offset = (int)sec->data_ptr;
|
|
|
|
data_offset = (data_offset + align - 1) & -align;
|
|
|
|
addr = data_offset;
|
2001-12-02 22:47:48 +08:00
|
|
|
/* very important to increment global
|
|
|
|
pointer at this time because
|
|
|
|
initializers themselves can create new
|
|
|
|
initializers */
|
2002-01-04 05:14:59 +08:00
|
|
|
data_offset += size;
|
2002-01-05 04:17:24 +08:00
|
|
|
/* handles bounds */
|
|
|
|
if (do_bounds_check) {
|
|
|
|
int *bounds_ptr;
|
|
|
|
/* first, we need to add at least one byte between each region */
|
|
|
|
data_offset++;
|
2002-01-06 00:15:57 +08:00
|
|
|
/* then add global bound info */
|
2002-01-05 04:17:24 +08:00
|
|
|
bounds_ptr = (int *)bounds_section->data_ptr;
|
|
|
|
*bounds_ptr++ = addr;
|
|
|
|
*bounds_ptr++ = size;
|
|
|
|
bounds_section->data_ptr = (unsigned char *)bounds_ptr;
|
|
|
|
}
|
2002-01-04 05:14:59 +08:00
|
|
|
sec->data_ptr = (unsigned char *)data_offset;
|
2001-12-02 22:47:48 +08:00
|
|
|
}
|
|
|
|
if (has_init) {
|
2001-12-23 01:05:23 +08:00
|
|
|
decl_initializer(t, r, addr, 1, 0);
|
2001-12-02 22:47:48 +08:00
|
|
|
/* restore parse state if needed */
|
|
|
|
if (init_str) {
|
|
|
|
free(init_str);
|
|
|
|
macro_ptr = saved_macro_ptr;
|
|
|
|
tok = tok1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
2002-01-05 04:17:24 +08:00
|
|
|
void put_func_debug(int t)
|
|
|
|
{
|
|
|
|
int bind;
|
|
|
|
char buf[512];
|
|
|
|
|
|
|
|
if (t & VT_STATIC)
|
|
|
|
bind = STB_LOCAL;
|
|
|
|
else
|
|
|
|
bind = STB_GLOBAL;
|
|
|
|
put_elf_sym(symtab_section, ind, 0,
|
|
|
|
ELF32_ST_INFO(bind, STT_FUNC), 0,
|
|
|
|
cur_text_section->sh_num, funcname);
|
|
|
|
/* stabs info */
|
|
|
|
/* XXX: we put here a dummy type */
|
|
|
|
snprintf(buf, sizeof(buf), "%s:%c1",
|
|
|
|
funcname, t & VT_STATIC ? 'f' : 'F');
|
2002-01-13 00:39:35 +08:00
|
|
|
put_stabs(buf, N_FUN, 0, file->line_num, ind);
|
2002-01-05 04:17:24 +08:00
|
|
|
func_ind = ind;
|
|
|
|
last_ind = 0;
|
|
|
|
last_line_num = 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2002-01-04 06:43:10 +08:00
|
|
|
int t, b, v, addr, has_init, r;
|
2001-11-06 09:20:20 +08:00
|
|
|
Sym *sym;
|
2002-01-04 05:14:59 +08:00
|
|
|
AttributeDef ad;
|
|
|
|
|
2001-12-02 22:59:18 +08:00
|
|
|
while (1) {
|
2002-01-04 05:14:59 +08:00
|
|
|
if (!parse_btype(&b, &ad)) {
|
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-12-09 06:51:04 +08:00
|
|
|
if (((b & VT_BTYPE) == VT_ENUM ||
|
|
|
|
(b & VT_BTYPE) == 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 */
|
2002-01-27 02:05:29 +08:00
|
|
|
t = type_decl(&ad, &v, b, TYPE_DIRECT);
|
2001-12-18 05:56:48 +08:00
|
|
|
#if 0
|
|
|
|
{
|
|
|
|
char buf[500];
|
|
|
|
type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
|
|
|
|
printf("type = '%s'\n", buf);
|
|
|
|
}
|
|
|
|
#endif
|
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");
|
2002-01-04 05:14:59 +08:00
|
|
|
/* compute text section */
|
|
|
|
cur_text_section = ad.section;
|
|
|
|
if (!cur_text_section)
|
|
|
|
cur_text_section = text_section;
|
|
|
|
ind = (int)cur_text_section->data_ptr;
|
2001-10-30 07:23:01 +08:00
|
|
|
/* patch forward references */
|
2001-12-23 01:05:23 +08:00
|
|
|
if ((sym = sym_find(v)) && (sym->r & VT_FORWARD)) {
|
2001-12-05 08:45:08 +08:00
|
|
|
greloc_patch(sym, ind);
|
2001-12-23 01:05:23 +08:00
|
|
|
sym->t = t;
|
2001-11-01 22:48:10 +08:00
|
|
|
} else {
|
|
|
|
/* put function address */
|
2001-12-23 01:05:23 +08:00
|
|
|
sym = sym_push1(&global_stack, v, t, ind);
|
2001-11-01 22:48:10 +08:00
|
|
|
}
|
2001-12-23 01:05:23 +08:00
|
|
|
sym->r = VT_CONST;
|
2001-12-18 05:56:48 +08:00
|
|
|
funcname = get_tok_str(v, NULL);
|
2002-01-05 04:17:24 +08:00
|
|
|
/* put debug symbol */
|
|
|
|
if (do_debug)
|
|
|
|
put_func_debug(t);
|
2001-11-08 07:13:29 +08:00
|
|
|
/* push a dummy symbol to enable local sym storage */
|
|
|
|
sym_push1(&local_stack, 0, 0, 0);
|
2002-01-04 06:43:10 +08:00
|
|
|
gfunc_prolog(t);
|
2001-10-28 07:48:39 +08:00
|
|
|
loc = 0;
|
|
|
|
rsym = 0;
|
2001-12-18 05:56:48 +08:00
|
|
|
block(NULL, NULL, NULL, NULL, 0);
|
2001-10-28 07:48:39 +08:00
|
|
|
gsym(rsym);
|
2002-01-04 06:43:10 +08:00
|
|
|
gfunc_epilog();
|
2002-01-04 05:14:59 +08:00
|
|
|
cur_text_section->data_ptr = (unsigned char *)ind;
|
2001-12-13 05:16:17 +08:00
|
|
|
sym_pop(&label_stack, NULL); /* reset label stack */
|
|
|
|
sym_pop(&local_stack, NULL); /* reset local stack */
|
2002-01-05 04:17:24 +08:00
|
|
|
/* end of function */
|
|
|
|
if (do_debug) {
|
|
|
|
put_stabn(N_FUN, 0, 0, ind - func_ind);
|
|
|
|
}
|
2001-12-03 05:24:43 +08:00
|
|
|
funcname = ""; /* for safety */
|
|
|
|
func_vt = VT_VOID; /* for safety */
|
2002-01-06 03:55:43 +08:00
|
|
|
ind = 0; /* 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-12-23 01:05:23 +08:00
|
|
|
sym_push(v, t | VT_TYPEDEF, 0, 0);
|
2001-12-09 06:51:04 +08:00
|
|
|
} else if ((t & VT_BTYPE) == VT_FUNC) {
|
2001-10-30 07:23:01 +08:00
|
|
|
/* external function definition */
|
2001-12-23 01:05:23 +08:00
|
|
|
external_sym(v, t, 0);
|
2001-10-28 07:48:39 +08:00
|
|
|
} else {
|
2001-10-30 07:23:01 +08:00
|
|
|
/* not lvalue if array */
|
2001-12-23 01:05:23 +08:00
|
|
|
r = 0;
|
2001-10-30 07:23:01 +08:00
|
|
|
if (!(t & VT_ARRAY))
|
2001-12-23 01:05:23 +08:00
|
|
|
r |= 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-23 01:05:23 +08:00
|
|
|
external_sym(v, t, r);
|
2001-10-30 07:23:01 +08:00
|
|
|
} else {
|
|
|
|
if (t & VT_STATIC)
|
2001-12-23 01:05:23 +08:00
|
|
|
r |= VT_CONST;
|
|
|
|
else
|
|
|
|
r |= l;
|
2001-12-02 22:47:48 +08:00
|
|
|
has_init = (tok == '=');
|
|
|
|
if (has_init)
|
2001-11-18 01:22:38 +08:00
|
|
|
next();
|
2002-01-04 05:14:59 +08:00
|
|
|
addr = decl_initializer_alloc(t, &ad, r,
|
2001-12-23 01:05:23 +08:00
|
|
|
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;
|
2001-12-23 01:05:23 +08:00
|
|
|
if (!is_compatible_types(sym->t, t))
|
2001-12-05 10:02:25 +08:00
|
|
|
error("incompatible types for redefinition of '%s'",
|
2001-12-18 05:56:48 +08:00
|
|
|
get_tok_str(v, NULL));
|
2001-12-23 01:05:23 +08:00
|
|
|
if (!(sym->r & VT_FORWARD))
|
2001-12-18 05:56:48 +08:00
|
|
|
error("redefinition of '%s'", get_tok_str(v, NULL));
|
2001-12-05 10:02:25 +08:00
|
|
|
greloc_patch(sym, addr);
|
|
|
|
} else {
|
|
|
|
do_def:
|
2001-12-23 01:05:23 +08:00
|
|
|
sym_push(v, t, r, addr);
|
2001-12-05 10:02:25 +08:00
|
|
|
}
|
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 */
|
2001-12-23 01:05:23 +08:00
|
|
|
ext_sym = sym_push1(&extern_stack, s->v, s->t, s->c);
|
|
|
|
ext_sym->r = s->r;
|
|
|
|
} else if (ext_sym->r & VT_FORWARD) {
|
2001-12-08 23:04:01 +08:00
|
|
|
/* external symbol already exists, but only as forward
|
|
|
|
definition */
|
2001-12-23 01:05:23 +08:00
|
|
|
if (!(s->r & VT_FORWARD)) {
|
2001-12-08 23:04:01 +08:00
|
|
|
/* 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 */
|
2001-12-23 01:05:23 +08:00
|
|
|
if (!(s->r & VT_FORWARD))
|
2001-12-18 05:56:48 +08:00
|
|
|
error("'%s' defined twice", get_tok_str(s->v, NULL));
|
2001-12-08 23:04:01 +08:00
|
|
|
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;
|
2002-01-05 04:17:24 +08:00
|
|
|
char buf[512];
|
|
|
|
|
2001-12-08 23:04:01 +08:00
|
|
|
funcname = "";
|
2002-01-04 05:14:59 +08:00
|
|
|
|
2002-01-13 00:39:35 +08:00
|
|
|
file = tcc_open(filename1);
|
2001-12-08 23:04:01 +08:00
|
|
|
if (!file)
|
2002-01-13 00:39:35 +08:00
|
|
|
error("file '%s' not found", filename1);
|
2001-12-08 23:04:01 +08:00
|
|
|
include_stack_ptr = include_stack;
|
|
|
|
ifdef_stack_ptr = ifdef_stack;
|
|
|
|
|
2001-12-17 01:58:32 +08:00
|
|
|
vtop = vstack - 1;
|
2001-12-08 23:04:01 +08:00
|
|
|
anon_sym = SYM_FIRST_ANOM;
|
2002-01-05 04:17:24 +08:00
|
|
|
|
|
|
|
/* file info: full path + filename */
|
|
|
|
if (do_debug) {
|
|
|
|
getcwd(buf, sizeof(buf));
|
|
|
|
pstrcat(buf, sizeof(buf), "/");
|
|
|
|
put_stabs(buf, N_SO, 0, 0, (unsigned long)text_section->data_ptr);
|
2002-01-13 00:39:35 +08:00
|
|
|
put_stabs(file->filename, N_SO, 0, 0,
|
|
|
|
(unsigned long)text_section->data_ptr);
|
2002-01-05 04:17:24 +08:00
|
|
|
}
|
|
|
|
/* define common 'char *' type because it is often used internally
|
|
|
|
for arrays and struct dereference */
|
|
|
|
char_pointer_type = mk_pointer(VT_BYTE);
|
|
|
|
|
2001-12-08 23:04:01 +08:00
|
|
|
define_start = define_stack.top;
|
|
|
|
inp();
|
|
|
|
ch = '\n'; /* needed to parse correctly first preprocessor command */
|
|
|
|
next();
|
|
|
|
decl(VT_CONST);
|
|
|
|
if (tok != -1)
|
|
|
|
expect("declaration");
|
2002-01-13 00:39:35 +08:00
|
|
|
tcc_close(file);
|
2001-12-08 23:04:01 +08:00
|
|
|
|
2002-01-05 04:17:24 +08:00
|
|
|
/* end of translation unit info */
|
|
|
|
if (do_debug) {
|
|
|
|
put_stabn(N_SO, 0, 0, (unsigned long)text_section->data_ptr);
|
|
|
|
}
|
|
|
|
|
2001-12-08 23:04:01 +08:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2002-01-04 05:14:59 +08:00
|
|
|
/* define a symbol. A value can also be provided with the '=' operator */
|
|
|
|
/* XXX: currently only handles integers and string defines. should use
|
|
|
|
tcc parser, but would need a custom 'FILE *' */
|
|
|
|
void define_symbol(const char *sym)
|
|
|
|
{
|
2002-01-13 19:07:19 +08:00
|
|
|
char *p;
|
|
|
|
BufferedFile bf1, *bf = &bf1;
|
2002-01-04 05:14:59 +08:00
|
|
|
|
2002-01-13 19:07:19 +08:00
|
|
|
pstrcpy(bf->buffer, IO_BUF_SIZE, sym);
|
|
|
|
p = strchr(bf->buffer, '=');
|
2002-01-04 05:14:59 +08:00
|
|
|
if (!p) {
|
2002-01-13 19:07:19 +08:00
|
|
|
/* default value */
|
|
|
|
pstrcat(bf->buffer, IO_BUF_SIZE, " 1");
|
2002-01-04 05:14:59 +08:00
|
|
|
} else {
|
2002-01-13 19:07:19 +08:00
|
|
|
*p = ' ';
|
2002-01-04 05:14:59 +08:00
|
|
|
}
|
|
|
|
|
2002-01-13 19:07:19 +08:00
|
|
|
/* init file structure */
|
|
|
|
bf->fd = -1;
|
|
|
|
bf->buf_ptr = bf->buffer;
|
|
|
|
bf->buf_end = bf->buffer + strlen(bf->buffer);
|
|
|
|
bf->filename[0] = '\0';
|
|
|
|
bf->line_num = 1;
|
|
|
|
file = bf;
|
|
|
|
|
|
|
|
include_stack_ptr = include_stack;
|
|
|
|
|
|
|
|
/* parse with define parser */
|
|
|
|
inp();
|
|
|
|
ch = '\n'; /* needed to parse correctly first preprocessor command */
|
|
|
|
next_nomacro();
|
|
|
|
parse_define();
|
|
|
|
file = NULL;
|
2002-01-04 05:14:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void undef_symbol(const char *sym)
|
|
|
|
{
|
|
|
|
TokenSym *ts;
|
|
|
|
Sym *s;
|
|
|
|
ts = tok_alloc(sym, 0);
|
|
|
|
s = sym_find1(&define_stack, tok);
|
|
|
|
/* undefine symbol by putting an invalid name */
|
|
|
|
if (s)
|
|
|
|
sym_undef(&define_stack, s);
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2002-01-05 04:17:24 +08:00
|
|
|
static void *resolve_sym(const char *sym)
|
|
|
|
{
|
2002-03-04 06:45:55 +08:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2002-01-05 04:17:24 +08:00
|
|
|
if (do_bounds_check) {
|
2002-03-04 06:45:55 +08:00
|
|
|
void *ptr;
|
2002-01-05 04:17:24 +08:00
|
|
|
ptr = bound_resolve_sym(sym);
|
|
|
|
if (ptr)
|
|
|
|
return ptr;
|
|
|
|
}
|
2002-03-04 06:45:55 +08:00
|
|
|
#endif
|
2002-01-05 04:17:24 +08:00
|
|
|
return dlsym(NULL, sym);
|
|
|
|
}
|
|
|
|
|
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;
|
2001-12-23 01:05:23 +08:00
|
|
|
if (s->r & VT_FORWARD) {
|
2001-12-05 08:45:08 +08:00
|
|
|
/* if there is at least one relocation to do, then find it
|
|
|
|
and patch it */
|
|
|
|
if (s->c) {
|
2001-12-18 05:56:48 +08:00
|
|
|
str = get_tok_str(s->v, NULL);
|
2002-01-05 04:17:24 +08:00
|
|
|
addr = (int)resolve_sym(str);
|
2001-12-05 08:45:08 +08:00
|
|
|
if (!addr)
|
|
|
|
error("unresolved external reference '%s'", str);
|
|
|
|
greloc_patch(s, addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s = s1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-01-05 04:17:24 +08:00
|
|
|
static int put_elf_str(Section *s, const char *sym)
|
|
|
|
{
|
|
|
|
int c, offset;
|
|
|
|
offset = s->data_ptr - s->data;
|
|
|
|
for(;;) {
|
|
|
|
c = *sym++;
|
|
|
|
*s->data_ptr++ = c;
|
|
|
|
if (c == '\0')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void put_elf_sym(Section *s,
|
|
|
|
unsigned long value, unsigned long size,
|
|
|
|
int info, int other, int shndx, const char *name)
|
|
|
|
{
|
|
|
|
int name_offset;
|
|
|
|
Elf32_Sym *sym;
|
|
|
|
|
|
|
|
sym = (Elf32_Sym *)s->data_ptr;
|
|
|
|
if (name)
|
|
|
|
name_offset = put_elf_str(s->link, name);
|
|
|
|
else
|
|
|
|
name_offset = 0;
|
|
|
|
sym->st_name = name_offset;
|
|
|
|
sym->st_value = value;
|
|
|
|
sym->st_size = size;
|
|
|
|
sym->st_info = info;
|
|
|
|
sym->st_other = other;
|
|
|
|
sym->st_shndx = shndx;
|
|
|
|
s->data_ptr += sizeof(Elf32_Sym);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* put stab debug information */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
unsigned long n_strx; /* index into string table of name */
|
|
|
|
unsigned char n_type; /* type of symbol */
|
|
|
|
unsigned char n_other; /* misc info (usually empty) */
|
|
|
|
unsigned short n_desc; /* description field */
|
|
|
|
unsigned long n_value; /* value of symbol */
|
|
|
|
} Stab_Sym;
|
|
|
|
|
|
|
|
static void put_stabs(const char *str, int type, int other, int desc, int value)
|
|
|
|
{
|
|
|
|
Stab_Sym *sym;
|
|
|
|
|
|
|
|
sym = (Stab_Sym *)stab_section->data_ptr;
|
|
|
|
if (str) {
|
|
|
|
sym->n_strx = put_elf_str(stabstr_section, str);
|
|
|
|
} else {
|
|
|
|
sym->n_strx = 0;
|
|
|
|
}
|
|
|
|
sym->n_type = type;
|
|
|
|
sym->n_other = other;
|
|
|
|
sym->n_desc = desc;
|
|
|
|
sym->n_value = value;
|
|
|
|
|
|
|
|
stab_section->data_ptr += sizeof(Stab_Sym);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void put_stabn(int type, int other, int desc, int value)
|
|
|
|
{
|
|
|
|
put_stabs(NULL, type, other, desc, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void put_stabd(int type, int other, int desc)
|
|
|
|
{
|
|
|
|
put_stabs(NULL, type, other, desc, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* output an ELF file (currently, only for testing) */
|
2002-01-05 08:40:47 +08:00
|
|
|
/* XXX: generate dynamic reloc info + DLL tables */
|
|
|
|
/* XXX: generate startup code */
|
2002-01-06 03:55:43 +08:00
|
|
|
/* XXX: better program header generation */
|
|
|
|
/* XXX: handle realloc'ed sections (instead of mmaping them) */
|
2001-11-19 00:33:01 +08:00
|
|
|
void build_exe(char *filename)
|
|
|
|
{
|
2002-01-05 04:17:24 +08:00
|
|
|
Elf32_Ehdr ehdr;
|
2001-11-19 00:33:01 +08:00
|
|
|
FILE *f;
|
2002-01-05 04:17:24 +08:00
|
|
|
int shnum, i, phnum, file_offset, offset, size, j;
|
|
|
|
Section *sec, *strsec;
|
|
|
|
Elf32_Shdr *shdr, *sh;
|
|
|
|
Elf32_Phdr *phdr, *ph;
|
|
|
|
|
|
|
|
memset(&ehdr, 0, sizeof(ehdr));
|
|
|
|
|
|
|
|
/* we add a section for symbols */
|
|
|
|
strsec = new_section(".shstrtab", SHT_STRTAB, 0);
|
|
|
|
put_elf_str(strsec, "");
|
|
|
|
|
|
|
|
/* count number of sections and compute number of program segments */
|
|
|
|
shnum = 1; /* section index zero is reserved */
|
|
|
|
phnum = 0;
|
|
|
|
for(sec = first_section; sec != NULL; sec = sec->next) {
|
|
|
|
shnum++;
|
|
|
|
if (sec->sh_flags & SHF_ALLOC)
|
|
|
|
phnum++;
|
|
|
|
}
|
|
|
|
/* allocate section headers */
|
|
|
|
shdr = malloc(shnum * sizeof(Elf32_Shdr));
|
|
|
|
if (!shdr)
|
|
|
|
error("memory full");
|
|
|
|
memset(shdr, 0, shnum * sizeof(Elf32_Shdr));
|
|
|
|
/* allocate program segment headers */
|
|
|
|
phdr = malloc(phnum * sizeof(Elf32_Phdr));
|
|
|
|
if (!phdr)
|
|
|
|
error("memory full");
|
|
|
|
memset(phdr, 0, phnum * sizeof(Elf32_Phdr));
|
|
|
|
|
|
|
|
/* XXX: find correct load order */
|
|
|
|
file_offset = sizeof(Elf32_Ehdr) + phnum * sizeof(Elf32_Phdr);
|
|
|
|
for(sec = first_section, i = 1; sec != NULL; sec = sec->next, i++) {
|
|
|
|
sh = &shdr[i];
|
|
|
|
sh->sh_name = put_elf_str(strsec, sec->name);
|
|
|
|
sh->sh_type = sec->sh_type;
|
|
|
|
sh->sh_flags = sec->sh_flags;
|
|
|
|
sh->sh_entsize = sec->sh_entsize;
|
|
|
|
if (sec->link)
|
|
|
|
sh->sh_link = sec->link->sh_num;
|
|
|
|
if (sh->sh_type == SHT_STRTAB) {
|
|
|
|
sh->sh_addralign = 1;
|
|
|
|
} else if (sh->sh_type == SHT_SYMTAB ||
|
|
|
|
(sh->sh_flags & SHF_ALLOC) == 0) {
|
|
|
|
sh->sh_addralign = 4;
|
|
|
|
} else {
|
|
|
|
sh->sh_addr = (Elf32_Word)sec->data;
|
|
|
|
sh->sh_addralign = 4096;
|
|
|
|
}
|
|
|
|
sh->sh_size = (Elf32_Word)sec->data_ptr - (Elf32_Word)sec->data;
|
|
|
|
/* align to section start */
|
|
|
|
file_offset = (file_offset + sh->sh_addralign - 1) &
|
|
|
|
~(sh->sh_addralign - 1);
|
|
|
|
sh->sh_offset = file_offset;
|
|
|
|
file_offset += sh->sh_size;
|
|
|
|
}
|
|
|
|
/* build program headers (simplistic - not fully correct) */
|
|
|
|
j = 0;
|
|
|
|
for(i=1;i<shnum;i++) {
|
|
|
|
sh = &shdr[i];
|
|
|
|
if (sh->sh_type == SHT_PROGBITS &&
|
|
|
|
(sh->sh_flags & SHF_ALLOC) != 0) {
|
|
|
|
ph = &phdr[j++];
|
|
|
|
ph->p_type = PT_LOAD;
|
|
|
|
ph->p_offset = sh->sh_offset;
|
|
|
|
ph->p_vaddr = sh->sh_addr;
|
|
|
|
ph->p_paddr = ph->p_vaddr;
|
|
|
|
ph->p_filesz = sh->sh_size;
|
|
|
|
ph->p_memsz = sh->sh_size;
|
|
|
|
ph->p_flags = PF_R;
|
|
|
|
if (sh->sh_flags & SHF_WRITE)
|
|
|
|
ph->p_flags |= PF_W;
|
|
|
|
if (sh->sh_flags & SHF_EXECINSTR)
|
|
|
|
ph->p_flags |= PF_X;
|
|
|
|
ph->p_align = sh->sh_addralign;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* align to 4 */
|
|
|
|
file_offset = (file_offset + 3) & -4;
|
|
|
|
|
|
|
|
/* fill header */
|
|
|
|
ehdr.e_ident[0] = ELFMAG0;
|
|
|
|
ehdr.e_ident[1] = ELFMAG1;
|
|
|
|
ehdr.e_ident[2] = ELFMAG2;
|
|
|
|
ehdr.e_ident[3] = ELFMAG3;
|
|
|
|
ehdr.e_ident[4] = ELFCLASS32;
|
|
|
|
ehdr.e_ident[5] = ELFDATA2LSB;
|
|
|
|
ehdr.e_ident[6] = EV_CURRENT;
|
|
|
|
ehdr.e_type = ET_EXEC;
|
|
|
|
ehdr.e_machine = EM_386;
|
|
|
|
ehdr.e_version = EV_CURRENT;
|
|
|
|
ehdr.e_entry = 0; /* XXX: patch it */
|
|
|
|
ehdr.e_phoff = sizeof(Elf32_Ehdr);
|
|
|
|
ehdr.e_shoff = file_offset;
|
|
|
|
ehdr.e_ehsize = sizeof(Elf32_Ehdr);
|
|
|
|
ehdr.e_phentsize = sizeof(Elf32_Phdr);
|
|
|
|
ehdr.e_phnum = phnum;
|
|
|
|
ehdr.e_shentsize = sizeof(Elf32_Shdr);
|
|
|
|
ehdr.e_shnum = shnum;
|
|
|
|
ehdr.e_shstrndx = shnum - 1;
|
|
|
|
|
|
|
|
/* write elf file */
|
2001-11-19 00:33:01 +08:00
|
|
|
f = fopen(filename, "w");
|
2002-01-05 04:17:24 +08:00
|
|
|
if (!f)
|
|
|
|
error("could not write '%s'", filename);
|
|
|
|
fwrite(&ehdr, 1, sizeof(Elf32_Ehdr), f);
|
|
|
|
fwrite(phdr, 1, phnum * sizeof(Elf32_Phdr), f);
|
|
|
|
offset = sizeof(Elf32_Ehdr) + phnum * sizeof(Elf32_Phdr);
|
|
|
|
for(sec = first_section, i = 1; sec != NULL; sec = sec->next, i++) {
|
|
|
|
sh = &shdr[i];
|
|
|
|
while (offset < sh->sh_offset) {
|
|
|
|
fputc(0, f);
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
size = sec->data_ptr - sec->data;
|
|
|
|
fwrite(sec->data, 1, size, f);
|
|
|
|
offset += size;
|
|
|
|
}
|
|
|
|
while (offset < ehdr.e_shoff) {
|
|
|
|
fputc(0, f);
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
fwrite(shdr, 1, shnum * sizeof(Elf32_Shdr), f);
|
2001-11-19 00:33:01 +08:00
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
2002-01-05 04:17:24 +08:00
|
|
|
/* print the position in the source file of PC value 'pc' by reading
|
|
|
|
the stabs debug information */
|
|
|
|
static void rt_printline(unsigned long wanted_pc)
|
|
|
|
{
|
|
|
|
Stab_Sym *sym, *sym_end;
|
|
|
|
char func_name[128];
|
|
|
|
unsigned long func_addr, last_pc, pc;
|
|
|
|
const char *incl_files[INCLUDE_STACK_SIZE];
|
|
|
|
int incl_index, len, last_line_num, i;
|
|
|
|
const char *str, *p;
|
|
|
|
|
|
|
|
func_name[0] = '\0';
|
|
|
|
func_addr = 0;
|
|
|
|
incl_index = 0;
|
|
|
|
last_pc = 0xffffffff;
|
|
|
|
last_line_num = 1;
|
|
|
|
sym = (Stab_Sym *)stab_section->data + 1;
|
|
|
|
sym_end = (Stab_Sym *)stab_section->data_ptr;
|
|
|
|
while (sym < sym_end) {
|
|
|
|
switch(sym->n_type) {
|
|
|
|
/* function start or end */
|
|
|
|
case N_FUN:
|
|
|
|
if (sym->n_strx == 0) {
|
|
|
|
func_name[0] = '\0';
|
|
|
|
func_addr = 0;
|
|
|
|
} else {
|
|
|
|
str = stabstr_section->data + sym->n_strx;
|
|
|
|
p = strchr(str, ':');
|
|
|
|
if (!p) {
|
|
|
|
pstrcpy(func_name, sizeof(func_name), str);
|
|
|
|
} else {
|
|
|
|
len = p - str;
|
|
|
|
if (len > sizeof(func_name) - 1)
|
|
|
|
len = sizeof(func_name) - 1;
|
|
|
|
memcpy(func_name, str, len);
|
|
|
|
func_name[len] = '\0';
|
|
|
|
}
|
|
|
|
func_addr = sym->n_value;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
/* line number info */
|
|
|
|
case N_SLINE:
|
|
|
|
pc = sym->n_value + func_addr;
|
|
|
|
if (wanted_pc >= last_pc && wanted_pc < pc)
|
|
|
|
goto found;
|
|
|
|
last_pc = pc;
|
|
|
|
last_line_num = sym->n_desc;
|
|
|
|
break;
|
|
|
|
/* include files */
|
|
|
|
case N_BINCL:
|
|
|
|
str = stabstr_section->data + sym->n_strx;
|
|
|
|
add_incl:
|
|
|
|
if (incl_index < INCLUDE_STACK_SIZE) {
|
|
|
|
incl_files[incl_index++] = str;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case N_EINCL:
|
|
|
|
if (incl_index > 1)
|
|
|
|
incl_index--;
|
|
|
|
break;
|
|
|
|
case N_SO:
|
|
|
|
if (sym->n_strx == 0) {
|
|
|
|
incl_index = 0; /* end of translation unit */
|
|
|
|
} else {
|
|
|
|
str = stabstr_section->data + sym->n_strx;
|
|
|
|
/* do not add path */
|
|
|
|
len = strlen(str);
|
|
|
|
if (len > 0 && str[len - 1] != '/')
|
|
|
|
goto add_incl;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sym++;
|
|
|
|
}
|
|
|
|
/* did not find line number info: */
|
2002-01-05 08:40:47 +08:00
|
|
|
fprintf(stderr, "(no debug info, pc=0x%08lx): ", wanted_pc);
|
2002-01-05 04:17:24 +08:00
|
|
|
return;
|
|
|
|
found:
|
|
|
|
for(i = 0; i < incl_index - 1; i++)
|
|
|
|
fprintf(stderr, "In file included from %s\n",
|
|
|
|
incl_files[i]);
|
|
|
|
if (incl_index > 0) {
|
|
|
|
fprintf(stderr, "%s:%d: ",
|
|
|
|
incl_files[incl_index - 1], last_line_num);
|
|
|
|
}
|
|
|
|
if (func_name[0] != '\0') {
|
|
|
|
fprintf(stderr, "in function '%s()': ", func_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-01-06 01:02:38 +08:00
|
|
|
/* emit a run time error at position 'pc' */
|
|
|
|
void rt_error(unsigned long pc, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
|
|
|
|
rt_printline(pc);
|
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
exit(255);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
2002-01-05 04:17:24 +08:00
|
|
|
|
2002-01-13 00:39:35 +08:00
|
|
|
#ifndef WIN32
|
2002-01-05 04:17:24 +08:00
|
|
|
/* signal handler for fatal errors */
|
|
|
|
static void sig_error(int signum, siginfo_t *siginf, void *puc)
|
|
|
|
{
|
|
|
|
struct ucontext *uc = puc;
|
|
|
|
unsigned long pc;
|
|
|
|
|
|
|
|
#ifdef __i386__
|
|
|
|
pc = uc->uc_mcontext.gregs[14];
|
|
|
|
#else
|
|
|
|
#error please put the right sigcontext field
|
|
|
|
#endif
|
|
|
|
|
|
|
|
switch(signum) {
|
|
|
|
case SIGFPE:
|
|
|
|
switch(siginf->si_code) {
|
|
|
|
case FPE_INTDIV:
|
|
|
|
case FPE_FLTDIV:
|
2002-01-06 01:02:38 +08:00
|
|
|
rt_error(pc, "division by zero");
|
2002-01-05 04:17:24 +08:00
|
|
|
break;
|
|
|
|
default:
|
2002-01-06 01:02:38 +08:00
|
|
|
rt_error(pc, "floating point exception");
|
2002-01-05 04:17:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SIGBUS:
|
|
|
|
case SIGSEGV:
|
2002-01-06 01:02:38 +08:00
|
|
|
rt_error(pc, "dereferencing invalid pointer");
|
2002-01-05 04:17:24 +08:00
|
|
|
break;
|
|
|
|
case SIGILL:
|
2002-01-06 01:02:38 +08:00
|
|
|
rt_error(pc, "illegal instruction");
|
2002-01-05 04:17:24 +08:00
|
|
|
break;
|
|
|
|
case SIGABRT:
|
2002-01-06 01:02:38 +08:00
|
|
|
rt_error(pc, "abort() called");
|
2002-01-05 04:17:24 +08:00
|
|
|
break;
|
|
|
|
default:
|
2002-01-06 01:02:38 +08:00
|
|
|
rt_error(pc, "caught signal %d", signum);
|
2002-01-05 04:17:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
exit(255);
|
|
|
|
}
|
2002-01-13 00:39:35 +08:00
|
|
|
#endif
|
2002-01-05 04:17:24 +08:00
|
|
|
|
|
|
|
/* launch the compiled program with the given arguments */
|
|
|
|
int launch_exe(int argc, char **argv)
|
|
|
|
{
|
|
|
|
Sym *s;
|
|
|
|
int (*t)();
|
|
|
|
|
|
|
|
s = sym_find1(&extern_stack, TOK_MAIN);
|
|
|
|
if (!s || (s->r & VT_FORWARD))
|
|
|
|
error("main() not defined");
|
|
|
|
|
|
|
|
if (do_debug) {
|
2002-01-13 00:39:35 +08:00
|
|
|
#ifdef WIN32
|
|
|
|
error("debug mode currently not available for Windows");
|
|
|
|
#else
|
|
|
|
struct sigaction sigact;
|
2002-01-05 04:17:24 +08:00
|
|
|
/* install TCC signal handlers to print debug info on fatal
|
|
|
|
runtime errors */
|
|
|
|
sigact.sa_flags = SA_SIGINFO | SA_ONESHOT;
|
|
|
|
sigact.sa_sigaction = sig_error;
|
|
|
|
sigemptyset(&sigact.sa_mask);
|
|
|
|
sigaction(SIGFPE, &sigact, NULL);
|
|
|
|
sigaction(SIGILL, &sigact, NULL);
|
|
|
|
sigaction(SIGSEGV, &sigact, NULL);
|
|
|
|
sigaction(SIGBUS, &sigact, NULL);
|
|
|
|
sigaction(SIGABRT, &sigact, NULL);
|
2002-01-13 00:39:35 +08:00
|
|
|
#endif
|
2002-01-05 04:17:24 +08:00
|
|
|
}
|
|
|
|
|
2002-03-04 06:45:55 +08:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2002-01-05 04:17:24 +08:00
|
|
|
if (do_bounds_check) {
|
|
|
|
int *p, *p_end;
|
|
|
|
__bound_init();
|
|
|
|
/* add all known static regions */
|
|
|
|
p = (int *)bounds_section->data;
|
|
|
|
p_end = (int *)bounds_section->data_ptr;
|
|
|
|
while (p < p_end) {
|
|
|
|
__bound_new_region((void *)p[0], p[1]);
|
|
|
|
p += 2;
|
|
|
|
}
|
|
|
|
}
|
2002-03-04 06:45:55 +08:00
|
|
|
#endif
|
2002-01-05 04:17:24 +08:00
|
|
|
|
|
|
|
t = (int (*)())s->c;
|
|
|
|
return (*t)(argc, argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-01-04 05:14:59 +08:00
|
|
|
void help(void)
|
|
|
|
{
|
2002-03-17 19:15:05 +08:00
|
|
|
printf("tcc version 0.9.6 - Tiny C Compiler - Copyright (C) 2001, 2002 Fabrice Bellard\n"
|
2002-01-05 04:17:24 +08:00
|
|
|
"usage: tcc [-Idir] [-Dsym[=val]] [-Usym] [-llib] [-g] [-b]\n"
|
|
|
|
" [-i infile] infile [infile_args...]\n"
|
|
|
|
"\n"
|
|
|
|
"-Idir : add include path 'dir'\n"
|
|
|
|
"-Dsym[=val] : define 'sym' with value 'val'\n"
|
|
|
|
"-Usym : undefine 'sym'\n"
|
|
|
|
"-llib : link with dynamic library 'lib'\n"
|
2002-01-05 08:40:47 +08:00
|
|
|
"-g : generate runtime debug info\n"
|
2002-03-04 06:45:55 +08:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2002-01-05 04:17:24 +08:00
|
|
|
"-b : compile with built-in memory and bounds checker (implies -g)\n"
|
2002-03-04 06:45:55 +08:00
|
|
|
#endif
|
2002-01-05 04:17:24 +08:00
|
|
|
"-i infile : compile infile\n"
|
|
|
|
);
|
2002-01-04 05:14:59 +08:00
|
|
|
}
|
|
|
|
|
2001-11-11 10:53:01 +08:00
|
|
|
int main(int argc, char **argv)
|
2001-10-28 07:48:39 +08:00
|
|
|
{
|
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;
|
2002-01-27 02:05:29 +08:00
|
|
|
p = tcc_keywords;
|
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
|
|
|
|
2002-01-04 05:14:59 +08:00
|
|
|
/* create standard sections */
|
2002-01-05 04:17:24 +08:00
|
|
|
text_section = new_section(".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
|
|
|
|
data_section = new_section(".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
|
|
|
|
/* XXX: should change type to SHT_NOBITS */
|
|
|
|
bss_section = new_section(".bss", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
|
2001-12-08 23:04:01 +08:00
|
|
|
|
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:
|
2002-01-04 05:14:59 +08:00
|
|
|
help();
|
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);
|
2002-01-04 05:14:59 +08:00
|
|
|
} else if (r[1] == 'U') {
|
|
|
|
undef_symbol(r + 2);
|
2001-11-12 00:07:43 +08:00
|
|
|
} 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++]);
|
2002-01-27 02:05:29 +08:00
|
|
|
} else if (!strcmp(r + 1, "bench")) {
|
|
|
|
do_bench = 1;
|
2002-03-04 06:45:55 +08:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2002-01-05 04:17:24 +08:00
|
|
|
} else if (r[1] == 'b') {
|
|
|
|
if (!do_bounds_check) {
|
|
|
|
do_bounds_check = 1;
|
2002-01-06 03:55:43 +08:00
|
|
|
/* define symbol */
|
|
|
|
define_symbol("__BOUNDS_CHECKING_ON");
|
2002-01-06 00:15:57 +08:00
|
|
|
/* create bounds sections */
|
2002-01-05 04:17:24 +08:00
|
|
|
bounds_section = new_section(".bounds",
|
|
|
|
SHT_PROGBITS, SHF_ALLOC);
|
2002-01-06 00:15:57 +08:00
|
|
|
lbounds_section = new_section(".lbounds",
|
|
|
|
SHT_PROGBITS, SHF_ALLOC);
|
2002-01-05 04:17:24 +08:00
|
|
|
/* debug is implied */
|
|
|
|
goto debug_opt;
|
|
|
|
}
|
2002-03-04 06:45:55 +08:00
|
|
|
#endif
|
2002-01-05 04:17:24 +08:00
|
|
|
} else if (r[1] == 'g') {
|
2002-03-04 06:45:55 +08:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2002-01-05 04:17:24 +08:00
|
|
|
debug_opt:
|
2002-03-04 06:45:55 +08:00
|
|
|
#endif
|
2002-01-05 04:17:24 +08:00
|
|
|
if (!do_debug) {
|
|
|
|
do_debug = 1;
|
|
|
|
|
|
|
|
/* stab symbols */
|
|
|
|
stab_section = new_section(".stab", SHT_PROGBITS, 0);
|
|
|
|
stab_section->sh_entsize = sizeof(Stab_Sym);
|
|
|
|
stabstr_section = new_section(".stabstr", SHT_STRTAB, 0);
|
|
|
|
put_elf_str(stabstr_section, "");
|
|
|
|
stab_section->link = stabstr_section;
|
|
|
|
/* put first entry */
|
|
|
|
put_stabs("", 0, 0, 0, 0);
|
|
|
|
|
|
|
|
/* elf symbols */
|
|
|
|
symtab_section = new_section(".symtab", SHT_SYMTAB, 0);
|
|
|
|
symtab_section->sh_entsize = sizeof(Elf32_Sym);
|
|
|
|
strtab_section = new_section(".strtab", SHT_STRTAB, 0);
|
|
|
|
put_elf_str(strtab_section, "");
|
|
|
|
symtab_section->link = strtab_section;
|
|
|
|
put_elf_sym(symtab_section, 0, 0, 0, 0, 0, NULL);
|
|
|
|
}
|
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 {
|
2002-03-04 06:45:55 +08:00
|
|
|
error("invalid option -- '%s'", r);
|
2001-11-11 10:53:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-08 23:04:01 +08:00
|
|
|
tcc_compile_file(argv[optind]);
|
|
|
|
|
2002-01-27 02:05:29 +08:00
|
|
|
if (do_bench) {
|
|
|
|
printf("total: %d idents, %d lines, %d bytes\n",
|
|
|
|
tok_ident - TOK_IDENT, total_lines, total_bytes);
|
|
|
|
}
|
|
|
|
|
2001-12-08 23:04:01 +08:00
|
|
|
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 {
|
2002-01-05 04:17:24 +08:00
|
|
|
return launch_exe(argc - optind, argv + optind);
|
2001-11-19 00:33:01 +08:00
|
|
|
}
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|