2001-11-01 17:48:10 +03:00
|
|
|
/*
|
|
|
|
* TCC - Tiny C Compiler
|
|
|
|
*
|
2002-01-04 23:17:24 +03:00
|
|
|
* Copyright (c) 2001, 2002 Fabrice Bellard
|
2001-11-01 17:48:10 +03: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 02:39:24 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2002-01-04 00:14:59 +03:00
|
|
|
#include <stdarg.h>
|
2001-12-07 02:39:24 +03:00
|
|
|
#include <string.h>
|
2001-12-14 01:28:53 +03:00
|
|
|
#include <errno.h>
|
|
|
|
#include <math.h>
|
2002-01-04 23:17:24 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
2002-01-12 19:39:35 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2002-11-02 17:12:32 +03:00
|
|
|
#include <setjmp.h>
|
2002-11-19 00:46:44 +03:00
|
|
|
#ifdef WIN32
|
|
|
|
#include <sys/timeb.h>
|
|
|
|
#endif
|
2002-01-12 19:39:35 +03:00
|
|
|
#ifndef WIN32
|
2002-11-19 00:46:44 +03:00
|
|
|
#include <sys/time.h>
|
2002-01-04 23:17:24 +03:00
|
|
|
#include <sys/ucontext.h>
|
2002-01-12 19:39:35 +03:00
|
|
|
#endif
|
|
|
|
#include "elf.h"
|
|
|
|
#include "stab.h"
|
2001-12-07 02:39:24 +03:00
|
|
|
#ifndef CONFIG_TCC_STATIC
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
2001-10-28 02:48:39 +03:00
|
|
|
|
2002-05-14 03:00:17 +04:00
|
|
|
#include "libtcc.h"
|
|
|
|
|
2002-11-22 21:12:41 +03:00
|
|
|
/* parser debug */
|
|
|
|
//#define PARSE_DEBUG
|
2001-11-11 05:53:01 +03:00
|
|
|
/* preprocessor debug */
|
|
|
|
//#define PP_DEBUG
|
2002-09-08 23:34:32 +04:00
|
|
|
/* include file debug */
|
|
|
|
//#define INC_DEBUG
|
2001-11-06 04:20:20 +03:00
|
|
|
|
2002-08-18 17:22:55 +04:00
|
|
|
//#define MEM_DEBUG
|
|
|
|
|
2002-02-10 19:14:03 +03: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 01:45:55 +03:00
|
|
|
#if !defined(WIN32) && !defined(TCC_UCLIBC) && !defined(TCC_TARGET_IL)
|
|
|
|
#define CONFIG_TCC_BCHECK /* enable bound checking code */
|
|
|
|
#endif
|
|
|
|
|
2002-06-30 21:34:30 +04:00
|
|
|
#ifndef CONFIG_TCC_PREFIX
|
|
|
|
#define CONFIG_TCC_PREFIX "/usr/local"
|
|
|
|
#endif
|
|
|
|
|
2002-07-16 02:17:02 +04:00
|
|
|
/* path to find crt1.o, crti.o and crtn.o. Only needed when generating
|
|
|
|
executables or dlls */
|
|
|
|
#define CONFIG_TCC_CRT_PREFIX "/usr/lib"
|
|
|
|
|
2001-11-06 04:20:20 +03:00
|
|
|
#define INCLUDE_STACK_SIZE 32
|
2001-11-08 04:12:31 +03:00
|
|
|
#define IFDEF_STACK_SIZE 64
|
2001-11-08 04:30:44 +03:00
|
|
|
#define VSTACK_SIZE 64
|
2001-11-11 05:53:01 +03:00
|
|
|
#define STRING_MAX_SIZE 1024
|
2001-11-06 04:20:20 +03:00
|
|
|
|
2002-01-26 21:05:29 +03:00
|
|
|
#define TOK_HASH_SIZE 2048 /* must be a power of two */
|
|
|
|
#define TOK_ALLOC_INCR 512 /* must be a power of two */
|
2002-11-24 01:02:40 +03:00
|
|
|
#define TOK_STR_ALLOC_INCR_BITS 6
|
|
|
|
#define TOK_STR_ALLOC_INCR (1 << TOK_STR_ALLOC_INCR_BITS)
|
|
|
|
#define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
|
2001-11-18 19:33:01 +03:00
|
|
|
|
2001-11-11 05:53:01 +03:00
|
|
|
/* token symbol management */
|
|
|
|
typedef struct TokenSym {
|
2001-11-18 19:33:01 +03:00
|
|
|
struct TokenSym *hash_next;
|
2002-11-19 00:46:44 +03:00
|
|
|
struct Sym *sym_define; /* direct pointer to define */
|
2002-11-20 03:29:04 +03:00
|
|
|
struct Sym *sym_label; /* direct pointer to label */
|
|
|
|
struct Sym *sym_struct; /* direct pointer to structure */
|
|
|
|
struct Sym *sym_identifier; /* direct pointer to identifier */
|
2001-11-11 05:53:01 +03:00
|
|
|
int tok; /* token number */
|
|
|
|
int len;
|
|
|
|
char str[1];
|
|
|
|
} TokenSym;
|
|
|
|
|
2002-08-18 17:22:55 +04:00
|
|
|
typedef struct CString {
|
|
|
|
int size; /* size in bytes */
|
|
|
|
void *data; /* either 'char *' or 'int *' */
|
|
|
|
int size_allocated;
|
|
|
|
void *data_allocated; /* if non NULL, data has been malloced */
|
|
|
|
} CString;
|
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
/* type definition */
|
|
|
|
typedef struct CType {
|
|
|
|
int t;
|
|
|
|
struct Sym *ref;
|
|
|
|
} CType;
|
|
|
|
|
2001-12-14 01:28:53 +03:00
|
|
|
/* constant value */
|
|
|
|
typedef union CValue {
|
|
|
|
long double ld;
|
|
|
|
double d;
|
|
|
|
float f;
|
|
|
|
int i;
|
|
|
|
unsigned int ui;
|
2001-12-18 00:56:48 +03:00
|
|
|
unsigned int ul; /* address (should be unsigned long on 64 bit cpu) */
|
|
|
|
long long ll;
|
|
|
|
unsigned long long ull;
|
2002-08-18 17:22:55 +04:00
|
|
|
struct CString *cstr;
|
2002-07-23 03:37:39 +04:00
|
|
|
void *ptr;
|
2001-12-20 04:05:21 +03:00
|
|
|
int tab[1];
|
2001-12-14 01:28:53 +03:00
|
|
|
} CValue;
|
|
|
|
|
2001-12-16 20:58:32 +03:00
|
|
|
/* value on stack */
|
|
|
|
typedef struct SValue {
|
2002-11-19 00:46:44 +03:00
|
|
|
CType type; /* type */
|
2002-01-04 00:14:59 +03:00
|
|
|
unsigned short r; /* register + flags */
|
|
|
|
unsigned short r2; /* second register, used for 'long long'
|
|
|
|
type. If not used, set to VT_CONST */
|
2002-07-14 18:38:33 +04:00
|
|
|
CValue c; /* constant, if VT_CONST */
|
2002-08-30 03:05:59 +04:00
|
|
|
struct Sym *sym; /* symbol, if (VT_SYM | VT_CONST) */
|
2001-12-16 20:58:32 +03:00
|
|
|
} SValue;
|
|
|
|
|
2001-11-01 17:48:10 +03:00
|
|
|
/* symbol management */
|
|
|
|
typedef struct Sym {
|
|
|
|
int v; /* symbol token */
|
2001-12-22 20:05:23 +03:00
|
|
|
int r; /* associated register */
|
2001-11-01 17:48:10 +03:00
|
|
|
int c; /* associated number */
|
2002-11-19 00:46:44 +03:00
|
|
|
CType type; /* associated type */
|
2001-11-01 17:48:10 +03:00
|
|
|
struct Sym *next; /* next related symbol */
|
|
|
|
struct Sym *prev; /* prev symbol in stack */
|
2002-11-20 03:29:04 +03:00
|
|
|
struct Sym *prev_tok; /* previous symbol for this token */
|
2001-11-01 17:48:10 +03:00
|
|
|
} Sym;
|
|
|
|
|
2002-01-04 00:14:59 +03:00
|
|
|
/* section definition */
|
2002-07-15 03:00:39 +04:00
|
|
|
/* XXX: use directly ELF structure for parameters ? */
|
|
|
|
/* special flag to indicate that the section should not be linked to
|
|
|
|
the other ones */
|
|
|
|
#define SHF_PRIVATE 0x80000000
|
|
|
|
|
2002-01-04 00:14:59 +03:00
|
|
|
typedef struct Section {
|
2002-07-27 18:05:37 +04:00
|
|
|
unsigned long data_offset; /* current data offset */
|
|
|
|
unsigned char *data; /* section data */
|
2002-08-18 17:22:55 +04:00
|
|
|
unsigned long data_allocated; /* used for realloc() handling */
|
2002-07-15 03:00:39 +04:00
|
|
|
int sh_name; /* elf section name (only used during output) */
|
2002-01-04 23:17:24 +03:00
|
|
|
int sh_num; /* elf section number */
|
|
|
|
int sh_type; /* elf section type */
|
|
|
|
int sh_flags; /* elf section flags */
|
2002-07-15 03:00:39 +04:00
|
|
|
int sh_info; /* elf section info */
|
|
|
|
int sh_addralign; /* elf section alignment */
|
2002-01-04 23:17:24 +03:00
|
|
|
int sh_entsize; /* elf entry size */
|
2002-07-15 03:00:39 +04:00
|
|
|
unsigned long sh_size; /* section size (only used during output) */
|
|
|
|
unsigned long sh_addr; /* address at which the section is relocated */
|
|
|
|
unsigned long sh_offset; /* address at which the section is relocated */
|
2002-07-28 03:08:04 +04:00
|
|
|
int nb_hashed_syms; /* used to resize the hash table */
|
2002-01-04 23:17:24 +03:00
|
|
|
struct Section *link; /* link to another section */
|
2002-07-14 18:38:33 +04:00
|
|
|
struct Section *reloc; /* corresponding section for relocation, if any */
|
|
|
|
struct Section *hash; /* hash table for symbols */
|
2002-01-04 00:14:59 +03:00
|
|
|
struct Section *next;
|
2002-07-13 15:17:19 +04:00
|
|
|
char name[64]; /* section name */
|
2002-01-04 00:14:59 +03:00
|
|
|
} Section;
|
|
|
|
|
2002-07-22 04:20:38 +04:00
|
|
|
typedef struct DLLReference {
|
|
|
|
int level;
|
|
|
|
char name[1];
|
|
|
|
} DLLReference;
|
|
|
|
|
2002-01-04 00:14:59 +03:00
|
|
|
/* GNUC attribute definition */
|
|
|
|
typedef struct AttributeDef {
|
|
|
|
int aligned;
|
|
|
|
Section *section;
|
2002-01-26 21:05:29 +03:00
|
|
|
unsigned char func_call; /* FUNC_CDECL or FUNC_STDCALL */
|
2002-01-04 00:14:59 +03:00
|
|
|
} AttributeDef;
|
2001-12-05 03:45:08 +03:00
|
|
|
|
2001-12-08 18:04:01 +03: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 17:48:10 +03:00
|
|
|
|
2002-01-26 21:05:29 +03:00
|
|
|
/* stored in 'Sym.c' field */
|
2001-11-08 02:13:29 +03: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-26 21:05:29 +03:00
|
|
|
/* stored in 'Sym.r' field */
|
|
|
|
#define FUNC_CDECL 0 /* standard c call */
|
|
|
|
#define FUNC_STDCALL 1 /* pascal c call */
|
|
|
|
|
2001-11-11 19:07:43 +03:00
|
|
|
/* field 'Sym.t' for macros */
|
2001-11-11 05:53:01 +03:00
|
|
|
#define MACRO_OBJ 0 /* object like macro */
|
|
|
|
#define MACRO_FUNC 1 /* function like macro */
|
|
|
|
|
2002-11-03 03:43:55 +03:00
|
|
|
/* field 'Sym.r' for labels */
|
2002-07-14 18:38:33 +04:00
|
|
|
#define LABEL_FORWARD 1 /* label is forward defined */
|
|
|
|
|
2001-11-11 19:07:43 +03:00
|
|
|
/* type_decl() types */
|
|
|
|
#define TYPE_ABSTRACT 1 /* type without variable */
|
|
|
|
#define TYPE_DIRECT 2 /* type with variable */
|
|
|
|
|
2002-01-12 19:39:35 +03:00
|
|
|
#define IO_BUF_SIZE 8192
|
|
|
|
|
|
|
|
typedef struct BufferedFile {
|
2002-11-23 02:28:06 +03:00
|
|
|
uint8_t *buf_ptr;
|
|
|
|
uint8_t *buf_end;
|
2002-01-12 19:39:35 +03:00
|
|
|
int fd;
|
|
|
|
int line_num; /* current line number - here to simply code */
|
2002-11-23 01:16:30 +03:00
|
|
|
int ifndef_macro; /* #ifndef macro / #endif search */
|
|
|
|
int ifndef_macro_saved; /* saved ifndef_macro */
|
2002-09-08 20:14:57 +04:00
|
|
|
int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */
|
2002-09-08 23:34:32 +04:00
|
|
|
char inc_type; /* type of include */
|
|
|
|
char inc_filename[512]; /* filename specified by the user */
|
2002-07-27 18:05:37 +04:00
|
|
|
char filename[1024]; /* current filename - here to simplify code */
|
2002-01-12 19:39:35 +03:00
|
|
|
unsigned char buffer[IO_BUF_SIZE + 1]; /* extra size for CH_EOB char */
|
|
|
|
} BufferedFile;
|
|
|
|
|
2002-11-22 17:43:38 +03:00
|
|
|
#define CH_EOB '\\' /* end of buffer or '\0' char in file */
|
2002-01-12 19:39:35 +03:00
|
|
|
#define CH_EOF (-1) /* end of file */
|
2001-11-06 04:20:20 +03:00
|
|
|
|
2002-06-30 21:34:30 +04:00
|
|
|
/* parsing state (used to save parser state to reparse part of the
|
|
|
|
source several times) */
|
|
|
|
typedef struct ParseState {
|
|
|
|
int *macro_ptr;
|
|
|
|
int line_num;
|
|
|
|
int tok;
|
|
|
|
CValue tokc;
|
|
|
|
} ParseState;
|
|
|
|
|
|
|
|
/* used to record tokens */
|
|
|
|
typedef struct TokenString {
|
|
|
|
int *str;
|
|
|
|
int len;
|
2002-11-24 01:02:40 +03:00
|
|
|
int allocated_len;
|
2002-06-30 21:34:30 +04:00
|
|
|
int last_line_num;
|
|
|
|
} TokenString;
|
|
|
|
|
2002-09-08 23:34:32 +04:00
|
|
|
/* include file cache, used to find files faster and also to eliminate
|
|
|
|
inclusion if the include file is protected by #ifndef ... #endif */
|
|
|
|
typedef struct CachedInclude {
|
|
|
|
int ifndef_macro;
|
|
|
|
char type; /* '"' or '>' to give include type */
|
|
|
|
char filename[1]; /* path specified in #include */
|
|
|
|
} CachedInclude;
|
|
|
|
|
2001-12-07 02:39:24 +03:00
|
|
|
/* parser */
|
2002-11-23 01:16:30 +03:00
|
|
|
static struct BufferedFile *file;
|
|
|
|
static int ch, tok, tok1;
|
|
|
|
static CValue tokc, tok1c;
|
|
|
|
static CString tokcstr; /* current parsed string, if any */
|
|
|
|
/* additionnal informations about token */
|
|
|
|
static int tok_flags;
|
|
|
|
#define TOK_FLAG_BOL 0x0001 /* beginning of line before */
|
|
|
|
#define TOK_FLAG_BOF 0x0002 /* beginning of file before */
|
|
|
|
#define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
|
|
|
|
|
2002-09-08 20:14:57 +04:00
|
|
|
/* if true, line feed is returned as a token. line feed is also
|
|
|
|
returned at eof */
|
2002-11-23 01:16:30 +03:00
|
|
|
static int return_linefeed;
|
|
|
|
static Section *text_section, *data_section, *bss_section; /* predefined sections */
|
|
|
|
static Section *cur_text_section; /* current section where function code is
|
2002-01-04 00:14:59 +03:00
|
|
|
generated */
|
2002-01-05 19:15:57 +03:00
|
|
|
/* bound check related sections */
|
2002-11-23 01:16:30 +03:00
|
|
|
static Section *bounds_section; /* contains global data bound description */
|
|
|
|
static Section *lbounds_section; /* contains local data bound description */
|
2002-07-14 18:38:33 +04:00
|
|
|
/* symbol sections */
|
2002-11-23 01:16:30 +03:00
|
|
|
static Section *symtab_section, *strtab_section;
|
2002-07-14 18:38:33 +04:00
|
|
|
|
2002-01-04 23:17:24 +03:00
|
|
|
/* debug sections */
|
2002-11-23 01:16:30 +03:00
|
|
|
static Section *stab_section, *stabstr_section;
|
2002-01-04 00:14:59 +03:00
|
|
|
|
2001-11-01 17:48:10 +03:00
|
|
|
/* loc : local variable index
|
2002-01-04 00:14:59 +03:00
|
|
|
ind : output code index
|
2001-10-28 02:48:39 +03:00
|
|
|
rsym: return symbol
|
2001-11-06 04:20:20 +03:00
|
|
|
anon_sym: anonymous symbol index
|
2001-10-28 02:48:39 +03:00
|
|
|
*/
|
2002-11-23 01:16:30 +03:00
|
|
|
static int rsym, anon_sym, ind, loc;
|
2002-09-08 16:42:36 +04:00
|
|
|
/* expression generation modifiers */
|
2002-11-23 01:16:30 +03:00
|
|
|
static int const_wanted; /* true if constant wanted */
|
|
|
|
static int nocode_wanted; /* true if no code generation wanted for an expression */
|
|
|
|
static int global_expr; /* true if compound literals must be allocated
|
|
|
|
globally (used during initializers parsing */
|
|
|
|
static CType func_vt; /* current function return type (used by return
|
|
|
|
instruction) */
|
|
|
|
static int func_vc;
|
|
|
|
static int last_line_num, last_ind, func_ind; /* debug last line number and pc */
|
|
|
|
static int tok_ident;
|
|
|
|
static TokenSym **table_ident;
|
|
|
|
static TokenSym *hash_ident[TOK_HASH_SIZE];
|
|
|
|
static char token_buf[STRING_MAX_SIZE + 1];
|
|
|
|
static char *funcname;
|
|
|
|
static Sym *global_stack, *local_stack;
|
|
|
|
static Sym *define_stack;
|
|
|
|
static Sym *label_stack;
|
|
|
|
|
|
|
|
static SValue vstack[VSTACK_SIZE], *vtop;
|
|
|
|
static int *macro_ptr, *macro_ptr_allocated;
|
2002-11-19 00:46:44 +03:00
|
|
|
/* some predefined types */
|
2002-11-23 01:16:30 +03:00
|
|
|
static CType char_pointer_type, func_old_type, int_type;
|
2002-01-04 23:17:24 +03:00
|
|
|
|
|
|
|
/* compile with debug symbol (and use them if error during execution) */
|
2002-11-23 01:16:30 +03:00
|
|
|
static int do_debug = 0;
|
2002-01-04 23:17:24 +03:00
|
|
|
|
|
|
|
/* compile with built-in memory and bounds checker */
|
2002-11-23 01:16:30 +03:00
|
|
|
static int do_bounds_check = 0;
|
2001-10-28 02:48:39 +03:00
|
|
|
|
2002-01-26 21:05:29 +03:00
|
|
|
/* display benchmark infos */
|
2002-11-23 01:16:30 +03:00
|
|
|
static int do_bench = 0;
|
|
|
|
static int total_lines;
|
|
|
|
static int total_bytes;
|
2002-01-26 21:05:29 +03:00
|
|
|
|
2001-12-05 05:02:25 +03:00
|
|
|
/* use GNU C extensions */
|
2002-11-23 01:16:30 +03:00
|
|
|
static int gnu_ext = 1;
|
2001-12-05 05:02:25 +03:00
|
|
|
|
2001-12-14 01:28:53 +03:00
|
|
|
/* use Tiny C extensions */
|
2002-11-23 01:16:30 +03:00
|
|
|
static int tcc_ext = 1;
|
2001-12-14 01:28:53 +03:00
|
|
|
|
2002-11-03 03:43:55 +03:00
|
|
|
/* max number of callers shown if error */
|
|
|
|
static int num_callers = 6;
|
|
|
|
static const char **rt_bound_error_msg;
|
|
|
|
|
2002-11-02 17:12:32 +03:00
|
|
|
/* XXX: suppress that ASAP */
|
|
|
|
static struct TCCState *tcc_state;
|
2002-07-15 03:00:39 +04:00
|
|
|
|
2002-07-25 02:10:59 +04:00
|
|
|
/* give the path of the tcc libraries */
|
|
|
|
static const char *tcc_lib_path = CONFIG_TCC_PREFIX "/lib/tcc";
|
|
|
|
|
2002-05-14 03:00:17 +04:00
|
|
|
struct TCCState {
|
2002-07-22 04:20:38 +04:00
|
|
|
int output_type;
|
2002-11-02 17:12:32 +03:00
|
|
|
|
|
|
|
BufferedFile **include_stack_ptr;
|
|
|
|
int *ifdef_stack_ptr;
|
|
|
|
|
|
|
|
/* include file handling */
|
|
|
|
char **include_paths;
|
|
|
|
int nb_include_paths;
|
|
|
|
char **sysinclude_paths;
|
|
|
|
int nb_sysinclude_paths;
|
|
|
|
CachedInclude **cached_includes;
|
|
|
|
int nb_cached_includes;
|
|
|
|
|
|
|
|
char **library_paths;
|
|
|
|
int nb_library_paths;
|
|
|
|
|
|
|
|
/* array of all loaded dlls (including those referenced by loaded
|
|
|
|
dlls) */
|
|
|
|
DLLReference **loaded_dlls;
|
|
|
|
int nb_loaded_dlls;
|
|
|
|
|
|
|
|
/* sections */
|
|
|
|
Section **sections;
|
|
|
|
int nb_sections; /* number of sections, including first dummy section */
|
|
|
|
|
|
|
|
/* got handling */
|
|
|
|
Section *got;
|
|
|
|
unsigned long *got_offsets;
|
|
|
|
int nb_got_offsets;
|
|
|
|
int nb_plt_entries;
|
|
|
|
/* give the correspondance from symtab indexes to dynsym indexes */
|
|
|
|
int *symtab_to_dynsym;
|
|
|
|
|
|
|
|
/* temporary dynamic symbol sections (for dll loading) */
|
|
|
|
Section *dynsymtab_section;
|
|
|
|
/* exported dynamic symbol section */
|
|
|
|
Section *dynsym;
|
|
|
|
|
|
|
|
/* if true, static linking is performed */
|
|
|
|
int static_link;
|
|
|
|
|
|
|
|
/* error handling */
|
|
|
|
void *error_opaque;
|
|
|
|
void (*error_func)(void *opaque, const char *msg);
|
|
|
|
int error_set_jmp_enabled;
|
|
|
|
jmp_buf error_jmp_buf;
|
|
|
|
int nb_errors;
|
|
|
|
|
|
|
|
/* see include_stack_ptr */
|
|
|
|
BufferedFile *include_stack[INCLUDE_STACK_SIZE];
|
|
|
|
|
|
|
|
/* see ifdef_stack_ptr */
|
|
|
|
int ifdef_stack[IFDEF_STACK_SIZE];
|
2002-05-14 03:00:17 +04:00
|
|
|
};
|
|
|
|
|
2001-11-06 04:20:20 +03:00
|
|
|
/* The current value can be: */
|
2002-01-05 19:15:57 +03:00
|
|
|
#define VT_VALMASK 0x00ff
|
|
|
|
#define VT_CONST 0x00f0 /* constant in vc
|
2001-11-06 04:20:20 +03:00
|
|
|
(must be first non register value) */
|
2002-01-05 19:15:57 +03: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 */
|
2002-07-14 18:38:33 +04:00
|
|
|
#define VT_SYM 0x0200 /* a symbol value is added */
|
2002-01-05 19:15:57 +03:00
|
|
|
#define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
|
2002-07-13 15:17:19 +04:00
|
|
|
char/short stored in integer registers) */
|
2002-01-05 19:15:57 +03:00
|
|
|
#define VT_MUSTBOUND 0x0800 /* bound checking must be done before
|
|
|
|
dereferencing value */
|
2002-07-13 19:55:38 +04:00
|
|
|
#define VT_BOUNDED 0x8000 /* value is bounded. The address of the
|
|
|
|
bounding function call point is in vc */
|
2002-06-30 21:34:30 +04:00
|
|
|
#define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
|
|
|
|
#define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
|
|
|
|
#define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
|
2002-07-13 15:17:19 +04:00
|
|
|
#define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
|
2001-12-05 03:45:08 +03:00
|
|
|
|
2001-12-09 01:51:04 +03:00
|
|
|
/* types */
|
2002-07-27 18:05:37 +04:00
|
|
|
#define VT_STRUCT_SHIFT 12 /* structure/enum name shift (20 bits left) */
|
2001-12-09 01:51:04 +03:00
|
|
|
|
2001-12-22 20:05:23 +03: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 */
|
2002-06-30 21:34:30 +04:00
|
|
|
#define VT_PTR 4 /* pointer */
|
2001-12-22 20:05:23 +03:00
|
|
|
#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 04:20:20 +03:00
|
|
|
|
2001-10-28 02:48:39 +03:00
|
|
|
/* token values */
|
|
|
|
|
2001-11-08 02:13:29 +03: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 02:48:39 +03:00
|
|
|
|
2001-11-06 04:20:20 +03:00
|
|
|
#define TOK_LAND 0xa0
|
|
|
|
#define TOK_LOR 0xa1
|
2001-10-28 02:48:39 +03:00
|
|
|
|
2001-11-01 17:48:10 +03:00
|
|
|
#define TOK_DEC 0xa2
|
|
|
|
#define TOK_MID 0xa3 /* inc/dec, to void constant */
|
|
|
|
#define TOK_INC 0xa4
|
2001-11-06 04:20:20 +03: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 00:14:59 +03:00
|
|
|
#define TOK_CINT 0xb3 /* number in tokc */
|
2001-11-11 05:53:01 +03:00
|
|
|
#define TOK_CCHAR 0xb4 /* char constant in tokc */
|
|
|
|
#define TOK_STR 0xb5 /* pointer to string in tokc */
|
|
|
|
#define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
|
2002-06-30 21:34:30 +04:00
|
|
|
#define TOK_LCHAR 0xb7
|
|
|
|
#define TOK_LSTR 0xb8
|
2001-12-14 01:28:53 +03:00
|
|
|
#define TOK_CFLOAT 0xb9 /* float constant */
|
2002-06-30 21:34:30 +04:00
|
|
|
#define TOK_LINENUM 0xba /* line number info */
|
2001-12-14 01:28:53 +03:00
|
|
|
#define TOK_CDOUBLE 0xc0 /* double constant */
|
|
|
|
#define TOK_CLDOUBLE 0xc1 /* long double constant */
|
2002-01-04 00:14:59 +03: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 */
|
2002-11-02 20:02:31 +03:00
|
|
|
#define TOK_PPNUM 0xce /* preprocessor number */
|
2002-01-04 00:14:59 +03:00
|
|
|
|
|
|
|
#define TOK_SHL 0x01 /* shift left */
|
2001-11-06 04:20:20 +03:00
|
|
|
#define TOK_SAR 0x02 /* signed shift right */
|
2001-10-28 02:48:39 +03:00
|
|
|
|
2001-10-29 01:24:16 +03: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 04:20:20 +03:00
|
|
|
#define TOK_A_SAR 0x82
|
2001-10-29 01:24:16 +03:00
|
|
|
|
2002-08-18 17:22:55 +04:00
|
|
|
/* WARNING: the content of this string encodes token numbers */
|
|
|
|
static char tok_two_chars[] = "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
|
|
|
|
|
2002-07-27 02:59:19 +04:00
|
|
|
#define TOK_EOF (-1) /* end of file */
|
|
|
|
#define TOK_LINEFEED 10 /* line feed */
|
2002-01-12 19:39:35 +03:00
|
|
|
|
2001-11-11 05:53:01 +03:00
|
|
|
/* all identificators and strings have token above that */
|
|
|
|
#define TOK_IDENT 256
|
|
|
|
|
2001-11-08 04:12:31 +03:00
|
|
|
enum {
|
2002-11-02 23:45:52 +03:00
|
|
|
TOK_LAST = TOK_IDENT - 1,
|
2002-07-23 03:37:39 +04:00
|
|
|
#define DEF(id, str) id,
|
|
|
|
#include "tcctok.h"
|
|
|
|
#undef DEF
|
2001-11-08 04:12:31 +03:00
|
|
|
};
|
|
|
|
|
2002-11-02 23:45:52 +03:00
|
|
|
static const char *tcc_keywords =
|
2002-07-23 03:37:39 +04:00
|
|
|
#define DEF(id, str) str "\0"
|
|
|
|
#include "tcctok.h"
|
|
|
|
#undef DEF
|
2002-01-26 21:05:29 +03:00
|
|
|
;
|
|
|
|
|
2002-11-02 23:45:52 +03:00
|
|
|
#define TOK_UIDENT TOK_DEFINE
|
|
|
|
|
2002-01-12 19:39:35 +03:00
|
|
|
#ifdef WIN32
|
|
|
|
#define snprintf _snprintf
|
2002-11-19 00:46:44 +03:00
|
|
|
#define vsnprintf _vsnprintf
|
2002-03-04 01:45:55 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(WIN32) || defined(TCC_UCLIBC)
|
2002-01-12 19:39:35 +03: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 02:20:55 +03: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-12 19:39:35 +03:00
|
|
|
#endif
|
2001-12-14 02:20:55 +03:00
|
|
|
|
2002-07-22 04:20:38 +04:00
|
|
|
static char *pstrcpy(char *buf, int buf_size, const char *s);
|
|
|
|
static char *pstrcat(char *buf, int buf_size, const char *s);
|
2002-02-10 19:14:03 +03:00
|
|
|
|
2002-11-02 23:45:52 +03:00
|
|
|
static void next(void);
|
|
|
|
static void next_nomacro(void);
|
2002-11-19 00:46:44 +03:00
|
|
|
static void parse_expr_type(CType *type);
|
|
|
|
static void expr_type(CType *type);
|
|
|
|
static void unary_type(CType *type);
|
2002-08-30 03:05:59 +04:00
|
|
|
static int expr_const(void);
|
2002-11-02 23:45:52 +03:00
|
|
|
static void expr_eq(void);
|
|
|
|
static void gexpr(void);
|
|
|
|
static void decl(int l);
|
2002-11-19 00:46:44 +03:00
|
|
|
static void decl_initializer(CType *type, Section *sec, unsigned long c,
|
2002-08-18 18:34:57 +04:00
|
|
|
int first, int size_only);
|
2002-11-19 00:46:44 +03:00
|
|
|
static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
|
2002-08-18 18:34:57 +04:00
|
|
|
int has_init, int v, int scope);
|
2001-12-23 03:34:26 +03:00
|
|
|
int gv(int rc);
|
2002-01-05 19:15:57 +03:00
|
|
|
void gv2(int rc1, int rc2);
|
2001-12-13 00:16:17 +03:00
|
|
|
void move_reg(int r, int s);
|
2002-05-14 03:00:17 +04:00
|
|
|
void save_regs(int n);
|
2001-12-13 00:16:17 +03:00
|
|
|
void save_reg(int r);
|
2001-12-16 20:58:32 +03:00
|
|
|
void vpop(void);
|
2001-12-13 00:16:17 +03:00
|
|
|
void vswap(void);
|
2001-12-16 20:58:32 +03:00
|
|
|
void vdup(void);
|
2001-12-13 00:16:17 +03:00
|
|
|
int get_reg(int rc);
|
|
|
|
|
2002-09-09 01:55:47 +04:00
|
|
|
static void macro_subst(TokenString *tok_str,
|
|
|
|
Sym **nested_list, int *macro_str);
|
2001-11-11 05:53:01 +03:00
|
|
|
int save_reg_forced(int r);
|
2001-12-10 01:04:15 +03:00
|
|
|
void gen_op(int op);
|
2002-01-04 00:14:59 +03:00
|
|
|
void force_charshort_cast(int t);
|
2002-11-19 00:46:44 +03:00
|
|
|
static void gen_cast(CType *type);
|
2001-12-03 00:24:43 +03:00
|
|
|
void vstore(void);
|
2002-11-20 03:29:04 +03:00
|
|
|
static Sym *sym_find(int v);
|
|
|
|
static Sym *sym_push(int v, CType *type, int r, int c);
|
2002-01-04 00:14:59 +03:00
|
|
|
|
|
|
|
/* type handling */
|
2002-11-24 17:18:17 +03:00
|
|
|
static int type_size(CType *type, int *a);
|
2002-11-19 00:46:44 +03:00
|
|
|
static inline CType *pointed_type(CType *type);
|
|
|
|
static int pointed_size(CType *type);
|
2002-08-18 17:22:55 +04:00
|
|
|
static int lvalue_type(int t);
|
2002-11-19 00:46:44 +03:00
|
|
|
static int is_compatible_types(CType *type1, CType *type2);
|
|
|
|
static int parse_btype(CType *type, AttributeDef *ad);
|
|
|
|
static void type_decl(CType *type, AttributeDef *ad, int *v, int td);
|
2002-01-04 00:14:59 +03:00
|
|
|
|
2001-12-14 01:28:53 +03:00
|
|
|
void error(const char *fmt, ...);
|
2001-12-23 03:34:26 +03:00
|
|
|
void vpushi(int v);
|
2002-11-19 00:46:44 +03:00
|
|
|
void vset(CType *type, int r, int v);
|
2002-01-04 00:14:59 +03:00
|
|
|
void type_to_str(char *buf, int buf_size,
|
2002-11-19 00:46:44 +03:00
|
|
|
CType *type, const char *varstr);
|
2002-07-14 18:38:33 +04:00
|
|
|
char *get_tok_str(int v, CValue *cv);
|
2002-11-19 00:46:44 +03:00
|
|
|
static Sym *get_sym_ref(CType *type, Section *sec,
|
2002-07-25 02:10:59 +04:00
|
|
|
unsigned long offset, unsigned long size);
|
2002-11-19 00:46:44 +03:00
|
|
|
static Sym *external_global_sym(int v, CType *type, int r);
|
2001-12-14 01:28:53 +03:00
|
|
|
|
2002-01-04 23:17:24 +03:00
|
|
|
/* section generation */
|
2002-08-18 17:22:55 +04:00
|
|
|
static void section_realloc(Section *sec, unsigned long new_size);
|
2002-07-27 18:05:37 +04:00
|
|
|
static void *section_ptr_add(Section *sec, unsigned long size);
|
2002-07-28 03:08:04 +04:00
|
|
|
static void put_extern_sym(Sym *sym, Section *section,
|
|
|
|
unsigned long value, unsigned long size);
|
|
|
|
static void greloc(Section *s, Sym *sym, unsigned long addr, int type);
|
2002-01-04 23:17:24 +03:00
|
|
|
static int put_elf_str(Section *s, const char *sym);
|
2002-07-14 18:38:33 +04:00
|
|
|
static int put_elf_sym(Section *s,
|
|
|
|
unsigned long value, unsigned long size,
|
|
|
|
int info, int other, int shndx, const char *name);
|
2002-08-18 17:22:55 +04:00
|
|
|
static int add_elf_sym(Section *s, unsigned long value, unsigned long size,
|
|
|
|
int info, int sh_num, const char *name);
|
2002-07-15 03:00:39 +04:00
|
|
|
static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
|
2002-07-14 18:38:33 +04:00
|
|
|
int type, int symbol);
|
2002-07-27 18:05:37 +04:00
|
|
|
static void put_stabs(const char *str, int type, int other, int desc,
|
|
|
|
unsigned long value);
|
|
|
|
static void put_stabs_r(const char *str, int type, int other, int desc,
|
|
|
|
unsigned long value, Section *sec, int sym_index);
|
2002-01-04 23:17:24 +03:00
|
|
|
static void put_stabn(int type, int other, int desc, int value);
|
|
|
|
static void put_stabd(int type, int other, int desc);
|
2002-07-22 04:20:38 +04:00
|
|
|
static int tcc_add_dll(TCCState *s, const char *filename, int flags);
|
|
|
|
|
|
|
|
#define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
|
|
|
|
#define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
|
|
|
|
static int tcc_add_file_internal(TCCState *s, const char *filename, int flags);
|
2002-01-04 23:17:24 +03:00
|
|
|
|
2001-12-18 00:56:48 +03: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-02-10 19:14:03 +03:00
|
|
|
#ifdef TCC_TARGET_I386
|
2001-12-14 01:28:53 +03:00
|
|
|
#include "i386-gen.c"
|
2002-02-10 19:14:03 +03:00
|
|
|
#endif
|
|
|
|
#ifdef TCC_TARGET_IL
|
|
|
|
#include "il-gen.c"
|
|
|
|
#endif
|
2001-11-11 19:07:43 +03:00
|
|
|
|
2001-12-07 02:39:24 +03:00
|
|
|
#ifdef CONFIG_TCC_STATIC
|
|
|
|
|
|
|
|
#define RTLD_LAZY 0x001
|
|
|
|
#define RTLD_NOW 0x002
|
|
|
|
#define RTLD_GLOBAL 0x100
|
2002-11-19 00:46:44 +03:00
|
|
|
#define RTLD_DEFAULT NULL
|
2001-12-07 02:39:24 +03:00
|
|
|
|
2001-11-11 19:07:43 +03:00
|
|
|
/* dummy function for profiling */
|
|
|
|
void *dlopen(const char *filename, int flag)
|
|
|
|
{
|
2001-12-07 02:39:24 +03:00
|
|
|
return NULL;
|
2001-11-11 19:07:43 +03:00
|
|
|
}
|
2001-12-07 02:39:24 +03:00
|
|
|
|
2001-11-11 19:07:43 +03:00
|
|
|
const char *dlerror(void)
|
|
|
|
{
|
|
|
|
return "error";
|
|
|
|
}
|
|
|
|
|
2001-12-07 02:39:24 +03: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-12 19:39:35 +03:00
|
|
|
void *dlsym(void *handle, const char *symbol)
|
2001-11-11 19:07:43 +03:00
|
|
|
{
|
2001-12-07 02:39:24 +03:00
|
|
|
TCCSyms *p;
|
|
|
|
p = tcc_syms;
|
|
|
|
while (p->str != NULL) {
|
|
|
|
if (!strcmp(p->str, symbol))
|
|
|
|
return p->ptr;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
return NULL;
|
2001-11-11 19:07:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2001-10-28 02:48:39 +03:00
|
|
|
|
2002-01-04 00:14:59 +03:00
|
|
|
/********************************************************/
|
|
|
|
|
2002-03-17 14:32:48 +03: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-12 19:39:35 +03:00
|
|
|
/* copy a string and truncate it. */
|
2002-07-22 04:20:38 +04:00
|
|
|
static char *pstrcpy(char *buf, int buf_size, const char *s)
|
2002-01-04 00:14:59 +03:00
|
|
|
{
|
|
|
|
char *q, *q_end;
|
|
|
|
int c;
|
|
|
|
|
2002-01-12 19:39:35 +03: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 00:14:59 +03:00
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2002-01-12 19:39:35 +03:00
|
|
|
/* strcat and truncate. */
|
2002-07-22 04:20:38 +04:00
|
|
|
static char *pstrcat(char *buf, int buf_size, const char *s)
|
2002-01-04 01:43:10 +03:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
len = strlen(buf);
|
|
|
|
if (len < buf_size)
|
|
|
|
pstrcpy(buf + len, buf_size - len, s);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2002-08-18 17:22:55 +04:00
|
|
|
/* memory management */
|
|
|
|
#ifdef MEM_DEBUG
|
|
|
|
int mem_cur_size;
|
|
|
|
int mem_max_size;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static inline void tcc_free(void *ptr)
|
|
|
|
{
|
|
|
|
#ifdef MEM_DEBUG
|
|
|
|
mem_cur_size -= malloc_usable_size(ptr);
|
|
|
|
#endif
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *tcc_malloc(unsigned long size)
|
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
ptr = malloc(size);
|
2002-11-02 17:12:32 +03:00
|
|
|
if (!ptr && size)
|
2002-08-18 17:22:55 +04:00
|
|
|
error("memory full");
|
|
|
|
#ifdef MEM_DEBUG
|
|
|
|
mem_cur_size += malloc_usable_size(ptr);
|
|
|
|
if (mem_cur_size > mem_max_size)
|
|
|
|
mem_max_size = mem_cur_size;
|
|
|
|
#endif
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *tcc_mallocz(unsigned long size)
|
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
ptr = tcc_malloc(size);
|
|
|
|
memset(ptr, 0, size);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void *tcc_realloc(void *ptr, unsigned long size)
|
|
|
|
{
|
|
|
|
void *ptr1;
|
|
|
|
#ifdef MEM_DEBUG
|
|
|
|
mem_cur_size -= malloc_usable_size(ptr);
|
|
|
|
#endif
|
|
|
|
ptr1 = realloc(ptr, size);
|
|
|
|
#ifdef MEM_DEBUG
|
|
|
|
/* NOTE: count not correct if alloc error, but not critical */
|
|
|
|
mem_cur_size += malloc_usable_size(ptr1);
|
|
|
|
if (mem_cur_size > mem_max_size)
|
|
|
|
mem_max_size = mem_cur_size;
|
|
|
|
#endif
|
|
|
|
return ptr1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *tcc_strdup(const char *str)
|
|
|
|
{
|
|
|
|
char *ptr;
|
|
|
|
ptr = tcc_malloc(strlen(str) + 1);
|
|
|
|
strcpy(ptr, str);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define free(p) use_tcc_free(p)
|
|
|
|
#define malloc(s) use_tcc_malloc(s)
|
|
|
|
#define realloc(p, s) use_tcc_realloc(p, s)
|
|
|
|
|
2002-07-22 04:20:38 +04:00
|
|
|
static void dynarray_add(void ***ptab, int *nb_ptr, void *data)
|
|
|
|
{
|
|
|
|
int nb, nb_alloc;
|
|
|
|
void **pp;
|
|
|
|
|
|
|
|
nb = *nb_ptr;
|
|
|
|
pp = *ptab;
|
|
|
|
/* every power of two we double array size */
|
|
|
|
if ((nb & (nb - 1)) == 0) {
|
|
|
|
if (!nb)
|
|
|
|
nb_alloc = 1;
|
|
|
|
else
|
|
|
|
nb_alloc = nb * 2;
|
2002-08-18 17:22:55 +04:00
|
|
|
pp = tcc_realloc(pp, nb_alloc * sizeof(void *));
|
2002-07-22 04:20:38 +04:00
|
|
|
if (!pp)
|
|
|
|
error("memory full");
|
|
|
|
*ptab = pp;
|
|
|
|
}
|
|
|
|
pp[nb++] = data;
|
|
|
|
*nb_ptr = nb;
|
|
|
|
}
|
|
|
|
|
2002-11-02 17:12:32 +03:00
|
|
|
Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
|
2002-01-04 00:14:59 +03:00
|
|
|
{
|
2002-07-22 04:20:38 +04:00
|
|
|
Section *sec;
|
2002-01-04 00:14:59 +03:00
|
|
|
|
2002-08-18 17:22:55 +04:00
|
|
|
sec = tcc_mallocz(sizeof(Section));
|
2002-01-04 00:14:59 +03:00
|
|
|
pstrcpy(sec->name, sizeof(sec->name), name);
|
2002-01-04 23:17:24 +03:00
|
|
|
sec->sh_type = sh_type;
|
|
|
|
sec->sh_flags = sh_flags;
|
2002-07-15 03:00:39 +04:00
|
|
|
switch(sh_type) {
|
|
|
|
case SHT_HASH:
|
|
|
|
case SHT_REL:
|
|
|
|
case SHT_DYNSYM:
|
|
|
|
case SHT_SYMTAB:
|
|
|
|
case SHT_DYNAMIC:
|
|
|
|
sec->sh_addralign = 4;
|
|
|
|
break;
|
|
|
|
case SHT_STRTAB:
|
|
|
|
sec->sh_addralign = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sec->sh_addralign = 32; /* default conservative alignment */
|
|
|
|
break;
|
|
|
|
}
|
2002-07-14 18:38:33 +04:00
|
|
|
|
2002-08-18 17:22:55 +04:00
|
|
|
/* only add section if not private */
|
2002-07-22 04:20:38 +04:00
|
|
|
if (!(sh_flags & SHF_PRIVATE)) {
|
2002-11-02 17:12:32 +03:00
|
|
|
sec->sh_num = s1->nb_sections;
|
|
|
|
dynarray_add((void ***)&s1->sections, &s1->nb_sections, sec);
|
2002-07-14 18:38:33 +04:00
|
|
|
}
|
2002-01-04 00:14:59 +03:00
|
|
|
return sec;
|
|
|
|
}
|
|
|
|
|
2002-11-02 17:12:32 +03:00
|
|
|
static void free_section(Section *s)
|
|
|
|
{
|
|
|
|
tcc_free(s->data);
|
|
|
|
tcc_free(s);
|
|
|
|
}
|
|
|
|
|
2002-08-18 17:22:55 +04:00
|
|
|
/* realloc section and set its content to zero */
|
|
|
|
static void section_realloc(Section *sec, unsigned long new_size)
|
2002-07-27 18:05:37 +04:00
|
|
|
{
|
2002-08-18 17:22:55 +04:00
|
|
|
unsigned long size;
|
|
|
|
unsigned char *data;
|
|
|
|
|
|
|
|
size = sec->data_allocated;
|
|
|
|
if (size == 0)
|
|
|
|
size = 1;
|
|
|
|
while (size < new_size)
|
|
|
|
size = size * 2;
|
|
|
|
data = tcc_realloc(sec->data, size);
|
|
|
|
if (!data)
|
|
|
|
error("memory full");
|
|
|
|
memset(data + sec->data_allocated, 0, size - sec->data_allocated);
|
|
|
|
sec->data = data;
|
|
|
|
sec->data_allocated = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reserve at least 'size' bytes in section 'sec' from
|
2002-08-18 18:34:57 +04:00
|
|
|
sec->data_offset. */
|
2002-07-27 18:05:37 +04:00
|
|
|
static void *section_ptr_add(Section *sec, unsigned long size)
|
|
|
|
{
|
2002-08-18 17:22:55 +04:00
|
|
|
unsigned long offset, offset1;
|
|
|
|
|
|
|
|
offset = sec->data_offset;
|
|
|
|
offset1 = offset + size;
|
|
|
|
if (offset1 > sec->data_allocated)
|
|
|
|
section_realloc(sec, offset1);
|
|
|
|
sec->data_offset = offset1;
|
|
|
|
return sec->data + offset;
|
2002-07-27 18:05:37 +04:00
|
|
|
}
|
|
|
|
|
2002-01-04 00:14:59 +03:00
|
|
|
/* return a reference to a section, and create it if it does not
|
|
|
|
exists */
|
2002-11-02 17:12:32 +03:00
|
|
|
Section *find_section(TCCState *s1, const char *name)
|
2002-01-04 00:14:59 +03:00
|
|
|
{
|
|
|
|
Section *sec;
|
2002-07-22 04:20:38 +04:00
|
|
|
int i;
|
2002-11-02 17:12:32 +03:00
|
|
|
for(i = 1; i < s1->nb_sections; i++) {
|
|
|
|
sec = s1->sections[i];
|
2002-01-04 00:14:59 +03:00
|
|
|
if (!strcmp(name, sec->name))
|
|
|
|
return sec;
|
|
|
|
}
|
2002-01-04 23:17:24 +03:00
|
|
|
/* sections are created as PROGBITS */
|
2002-11-02 17:12:32 +03:00
|
|
|
return new_section(s1, name, SHT_PROGBITS, SHF_ALLOC);
|
2002-01-04 00:14:59 +03:00
|
|
|
}
|
|
|
|
|
2002-08-18 17:22:55 +04:00
|
|
|
/* update sym->c so that it points to an external symbol in section
|
|
|
|
'section' with value 'value' */
|
|
|
|
static void put_extern_sym(Sym *sym, Section *section,
|
|
|
|
unsigned long value, unsigned long size)
|
|
|
|
{
|
|
|
|
int sym_type, sym_bind, sh_num, info;
|
|
|
|
Elf32_Sym *esym;
|
|
|
|
const char *name;
|
|
|
|
|
|
|
|
if (section)
|
|
|
|
sh_num = section->sh_num;
|
|
|
|
else
|
|
|
|
sh_num = SHN_UNDEF;
|
|
|
|
if (!sym->c) {
|
2002-11-19 00:46:44 +03:00
|
|
|
if ((sym->type.t & VT_BTYPE) == VT_FUNC)
|
2002-08-18 17:22:55 +04:00
|
|
|
sym_type = STT_FUNC;
|
|
|
|
else
|
|
|
|
sym_type = STT_OBJECT;
|
2002-11-19 00:46:44 +03:00
|
|
|
if (sym->type.t & VT_STATIC)
|
2002-08-18 17:22:55 +04:00
|
|
|
sym_bind = STB_LOCAL;
|
|
|
|
else
|
|
|
|
sym_bind = STB_GLOBAL;
|
|
|
|
|
|
|
|
name = get_tok_str(sym->v, NULL);
|
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
|
|
|
if (do_bounds_check) {
|
2002-11-19 00:46:44 +03:00
|
|
|
char buf[32];
|
|
|
|
|
2002-11-03 03:43:55 +03:00
|
|
|
/* XXX: avoid doing that for statics ? */
|
2002-08-18 17:22:55 +04:00
|
|
|
/* if bound checking is activated, we change some function
|
|
|
|
names by adding the "__bound" prefix */
|
|
|
|
switch(sym->v) {
|
2002-09-08 16:42:36 +04:00
|
|
|
#if 0
|
|
|
|
/* XXX: we rely only on malloc hooks */
|
2002-08-18 17:22:55 +04:00
|
|
|
case TOK_malloc:
|
|
|
|
case TOK_free:
|
|
|
|
case TOK_realloc:
|
|
|
|
case TOK_memalign:
|
|
|
|
case TOK_calloc:
|
2002-09-08 16:42:36 +04:00
|
|
|
#endif
|
2002-08-18 17:22:55 +04:00
|
|
|
case TOK_memcpy:
|
|
|
|
case TOK_memmove:
|
|
|
|
case TOK_memset:
|
|
|
|
case TOK_strlen:
|
|
|
|
case TOK_strcpy:
|
|
|
|
strcpy(buf, "__bound_");
|
|
|
|
strcat(buf, name);
|
|
|
|
name = buf;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
info = ELF32_ST_INFO(sym_bind, sym_type);
|
|
|
|
sym->c = add_elf_sym(symtab_section, value, size, info, sh_num, name);
|
|
|
|
} else {
|
|
|
|
esym = &((Elf32_Sym *)symtab_section->data)[sym->c];
|
|
|
|
esym->st_value = value;
|
|
|
|
esym->st_size = size;
|
|
|
|
esym->st_shndx = sh_num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-14 18:38:33 +04:00
|
|
|
/* add a new relocation entry to symbol 'sym' in section 's' */
|
2002-07-28 03:08:04 +04:00
|
|
|
static void greloc(Section *s, Sym *sym, unsigned long offset, int type)
|
2001-12-23 03:34:26 +03:00
|
|
|
{
|
2002-07-14 18:38:33 +04:00
|
|
|
if (!sym->c)
|
2002-07-28 03:08:04 +04:00
|
|
|
put_extern_sym(sym, NULL, 0, 0);
|
2002-07-14 18:38:33 +04:00
|
|
|
/* now we can add ELF relocation info */
|
2002-07-15 03:00:39 +04:00
|
|
|
put_elf_reloc(symtab_section, s, offset, type, sym->c);
|
2001-12-23 03:34:26 +03:00
|
|
|
}
|
|
|
|
|
2001-12-14 01:28:53 +03:00
|
|
|
static inline int isid(int c)
|
2001-10-28 02:48:39 +03:00
|
|
|
{
|
2001-11-18 19:33:01 +03:00
|
|
|
return (c >= 'a' && c <= 'z') ||
|
|
|
|
(c >= 'A' && c <= 'Z') ||
|
2001-10-28 02:48:39 +03:00
|
|
|
c == '_';
|
|
|
|
}
|
|
|
|
|
2001-12-14 01:28:53 +03:00
|
|
|
static inline int isnum(int c)
|
2001-10-28 02:48:39 +03:00
|
|
|
{
|
2002-01-04 00:14:59 +03:00
|
|
|
return c >= '0' && c <= '9';
|
2001-10-28 02:48:39 +03:00
|
|
|
}
|
|
|
|
|
2002-08-31 16:43:39 +04:00
|
|
|
static inline int isoct(int c)
|
|
|
|
{
|
|
|
|
return c >= '0' && c <= '7';
|
|
|
|
}
|
|
|
|
|
2001-12-14 01:28:53 +03:00
|
|
|
static inline int toup(int c)
|
|
|
|
{
|
2002-11-02 20:02:31 +03:00
|
|
|
if (c >= 'a' && c <= 'z')
|
|
|
|
return c - 'a' + 'A';
|
2001-12-14 01:28:53 +03:00
|
|
|
else
|
2002-11-02 20:02:31 +03:00
|
|
|
return c;
|
2001-12-14 01:28:53 +03:00
|
|
|
}
|
|
|
|
|
2002-11-02 17:12:32 +03:00
|
|
|
static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
|
2001-11-08 02:13:29 +03:00
|
|
|
{
|
2002-11-02 17:12:32 +03:00
|
|
|
int len;
|
|
|
|
len = strlen(buf);
|
|
|
|
vsnprintf(buf + len, buf_size - len, fmt, ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
strcat_vprintf(buf, buf_size, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
2002-07-22 04:20:38 +04:00
|
|
|
|
2002-11-02 17:12:32 +03:00
|
|
|
void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
char buf[2048];
|
|
|
|
BufferedFile **f;
|
|
|
|
|
|
|
|
buf[0] = '\0';
|
2002-03-04 01:45:55 +03:00
|
|
|
if (file) {
|
2002-11-02 17:12:32 +03:00
|
|
|
for(f = s1->include_stack; f < s1->include_stack_ptr; f++)
|
|
|
|
strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
|
|
|
|
(*f)->filename, (*f)->line_num);
|
2002-07-22 04:20:38 +04:00
|
|
|
if (file->line_num > 0) {
|
2002-11-02 17:12:32 +03:00
|
|
|
strcat_printf(buf, sizeof(buf),
|
|
|
|
"%s:%d: ", file->filename, file->line_num);
|
2002-07-22 04:20:38 +04:00
|
|
|
} else {
|
2002-11-02 17:12:32 +03:00
|
|
|
strcat_printf(buf, sizeof(buf),
|
|
|
|
"%s: ", file->filename);
|
2002-07-22 04:20:38 +04:00
|
|
|
}
|
2002-03-04 01:45:55 +03:00
|
|
|
} else {
|
2002-11-02 17:12:32 +03:00
|
|
|
strcat_printf(buf, sizeof(buf),
|
|
|
|
"tcc: ");
|
2002-03-04 01:45:55 +03:00
|
|
|
}
|
2002-11-02 17:12:32 +03:00
|
|
|
if (is_warning)
|
|
|
|
strcat_printf(buf, sizeof(buf), "warning: ");
|
|
|
|
strcat_vprintf(buf, sizeof(buf), fmt, ap);
|
|
|
|
|
|
|
|
if (!s1->error_func) {
|
|
|
|
/* default case: stderr */
|
|
|
|
fprintf(stderr, "%s\n", buf);
|
|
|
|
} else {
|
|
|
|
s1->error_func(s1->error_opaque, buf);
|
|
|
|
}
|
2002-11-02 20:02:31 +03:00
|
|
|
if (!is_warning)
|
|
|
|
s1->nb_errors++;
|
2002-11-02 17:12:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef LIBTCC
|
|
|
|
void tcc_set_error_func(TCCState *s, void *error_opaque,
|
|
|
|
void (*error_func)(void *opaque, const char *msg))
|
|
|
|
{
|
|
|
|
s->error_opaque = error_opaque;
|
|
|
|
s->error_func = error_func;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* error without aborting current compilation */
|
|
|
|
void error_noabort(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
TCCState *s1 = tcc_state;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
error1(s1, 0, fmt, ap);
|
|
|
|
va_end(ap);
|
2001-11-08 02:13:29 +03:00
|
|
|
}
|
|
|
|
|
2001-11-11 19:07:43 +03:00
|
|
|
void error(const char *fmt, ...)
|
2001-10-28 18:20:12 +03:00
|
|
|
{
|
2002-11-02 17:12:32 +03:00
|
|
|
TCCState *s1 = tcc_state;
|
2001-11-11 19:07:43 +03:00
|
|
|
va_list ap;
|
2002-11-02 17:12:32 +03:00
|
|
|
|
2001-11-11 19:07:43 +03:00
|
|
|
va_start(ap, fmt);
|
2002-11-02 17:12:32 +03:00
|
|
|
error1(s1, 0, fmt, ap);
|
2001-11-11 19:07:43 +03:00
|
|
|
va_end(ap);
|
2002-11-02 17:12:32 +03:00
|
|
|
/* better than nothing: in some cases, we accept to handle errors */
|
|
|
|
if (s1->error_set_jmp_enabled) {
|
|
|
|
longjmp(s1->error_jmp_buf, 1);
|
|
|
|
} else {
|
|
|
|
/* XXX: suppress it someday */
|
|
|
|
exit(1);
|
|
|
|
}
|
2001-10-28 18:20:12 +03:00
|
|
|
}
|
|
|
|
|
2001-11-11 19:07:43 +03:00
|
|
|
void expect(const char *msg)
|
2001-11-01 17:48:10 +03:00
|
|
|
{
|
2001-11-11 19:07:43 +03:00
|
|
|
error("%s expected", msg);
|
2001-11-01 17:48:10 +03:00
|
|
|
}
|
|
|
|
|
2001-12-20 02:01:51 +03:00
|
|
|
void warning(const char *fmt, ...)
|
2001-10-28 18:20:12 +03:00
|
|
|
{
|
2002-11-02 17:12:32 +03:00
|
|
|
TCCState *s1 = tcc_state;
|
2001-12-20 02:01:51 +03:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2002-11-02 17:12:32 +03:00
|
|
|
error1(s1, 1, fmt, ap);
|
2001-12-20 02:01:51 +03:00
|
|
|
va_end(ap);
|
2001-10-28 18:20:12 +03:00
|
|
|
}
|
|
|
|
|
2001-12-09 01:51:04 +03:00
|
|
|
void skip(int c)
|
2001-10-28 02:48:39 +03:00
|
|
|
{
|
2001-11-11 19:07:43 +03:00
|
|
|
if (tok != c)
|
|
|
|
error("'%c' expected", c);
|
2001-10-28 02:48:39 +03:00
|
|
|
next();
|
|
|
|
}
|
2001-10-28 18:20:12 +03:00
|
|
|
|
2002-09-08 23:34:32 +04:00
|
|
|
void test_lvalue(void)
|
2001-10-28 18:20:12 +03:00
|
|
|
{
|
2001-12-20 04:05:21 +03:00
|
|
|
if (!(vtop->r & VT_LVAL))
|
2001-11-01 17:48:10 +03:00
|
|
|
expect("lvalue");
|
2001-10-28 18:20:12 +03:00
|
|
|
}
|
|
|
|
|
2002-11-24 01:02:40 +03:00
|
|
|
/* allocate a new token */
|
|
|
|
static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
|
2001-11-11 05:53:01 +03:00
|
|
|
{
|
2002-11-24 01:02:40 +03:00
|
|
|
TokenSym *ts, **ptable;
|
2002-11-23 21:15:17 +03:00
|
|
|
int i;
|
2001-12-09 03:27:44 +03:00
|
|
|
|
|
|
|
if (tok_ident >= SYM_FIRST_ANOM)
|
|
|
|
error("memory full");
|
|
|
|
|
2001-11-18 19:33:01 +03:00
|
|
|
/* expand token table if needed */
|
|
|
|
i = tok_ident - TOK_IDENT;
|
|
|
|
if ((i % TOK_ALLOC_INCR) == 0) {
|
2002-08-18 17:22:55 +04:00
|
|
|
ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
|
2001-11-18 19:33:01 +03:00
|
|
|
if (!ptable)
|
|
|
|
error("memory full");
|
|
|
|
table_ident = ptable;
|
2001-11-11 05:53:01 +03:00
|
|
|
}
|
2001-12-09 03:27:44 +03:00
|
|
|
|
2002-08-18 17:22:55 +04:00
|
|
|
ts = tcc_malloc(sizeof(TokenSym) + len);
|
2001-11-18 19:33:01 +03:00
|
|
|
table_ident[i] = ts;
|
|
|
|
ts->tok = tok_ident++;
|
2002-11-19 00:46:44 +03:00
|
|
|
ts->sym_define = NULL;
|
2002-11-20 03:29:04 +03:00
|
|
|
ts->sym_label = NULL;
|
|
|
|
ts->sym_struct = NULL;
|
|
|
|
ts->sym_identifier = NULL;
|
2001-11-11 05:53:01 +03:00
|
|
|
ts->len = len;
|
2001-11-18 19:33:01 +03:00
|
|
|
ts->hash_next = NULL;
|
2002-11-22 17:12:28 +03:00
|
|
|
memcpy(ts->str, str, len);
|
|
|
|
ts->str[len] = '\0';
|
2001-11-11 05:53:01 +03:00
|
|
|
*pts = ts;
|
|
|
|
return ts;
|
|
|
|
}
|
2001-10-28 18:20:12 +03:00
|
|
|
|
2002-11-24 01:02:40 +03:00
|
|
|
#define TOK_HASH_INIT 1
|
|
|
|
#define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
|
|
|
|
|
|
|
|
/* find a token and add it if not found */
|
|
|
|
static TokenSym *tok_alloc(const char *str, int len)
|
|
|
|
{
|
|
|
|
TokenSym *ts, **pts;
|
|
|
|
int i;
|
|
|
|
unsigned int h;
|
|
|
|
|
|
|
|
h = TOK_HASH_INIT;
|
|
|
|
for(i=0;i<len;i++)
|
|
|
|
h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
|
|
|
|
h &= (TOK_HASH_SIZE - 1);
|
|
|
|
|
|
|
|
pts = &hash_ident[h];
|
|
|
|
for(;;) {
|
|
|
|
ts = *pts;
|
|
|
|
if (!ts)
|
|
|
|
break;
|
|
|
|
if (ts->len == len && !memcmp(ts->str, str, len))
|
|
|
|
return ts;
|
|
|
|
pts = &(ts->hash_next);
|
|
|
|
}
|
|
|
|
return tok_alloc_new(pts, str, len);
|
|
|
|
}
|
|
|
|
|
2002-08-18 17:22:55 +04:00
|
|
|
/* CString handling */
|
|
|
|
|
|
|
|
static void cstr_realloc(CString *cstr, int new_size)
|
|
|
|
{
|
|
|
|
int size;
|
|
|
|
void *data;
|
|
|
|
|
|
|
|
size = cstr->size_allocated;
|
|
|
|
if (size == 0)
|
|
|
|
size = 8; /* no need to allocate a too small first string */
|
|
|
|
while (size < new_size)
|
|
|
|
size = size * 2;
|
|
|
|
data = tcc_realloc(cstr->data_allocated, size);
|
|
|
|
if (!data)
|
|
|
|
error("memory full");
|
|
|
|
cstr->data_allocated = data;
|
|
|
|
cstr->size_allocated = size;
|
|
|
|
cstr->data = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add a byte */
|
|
|
|
static void cstr_ccat(CString *cstr, int ch)
|
|
|
|
{
|
|
|
|
int size;
|
|
|
|
size = cstr->size + 1;
|
|
|
|
if (size > cstr->size_allocated)
|
|
|
|
cstr_realloc(cstr, size);
|
|
|
|
((unsigned char *)cstr->data)[size - 1] = ch;
|
|
|
|
cstr->size = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cstr_cat(CString *cstr, const char *str)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
for(;;) {
|
|
|
|
c = *str;
|
|
|
|
if (c == '\0')
|
|
|
|
break;
|
|
|
|
cstr_ccat(cstr, c);
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add a wide char */
|
|
|
|
static void cstr_wccat(CString *cstr, int ch)
|
|
|
|
{
|
|
|
|
int size;
|
|
|
|
size = cstr->size + sizeof(int);
|
|
|
|
if (size > cstr->size_allocated)
|
|
|
|
cstr_realloc(cstr, size);
|
|
|
|
*(int *)(((unsigned char *)cstr->data) + size - sizeof(int)) = ch;
|
|
|
|
cstr->size = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cstr_new(CString *cstr)
|
|
|
|
{
|
|
|
|
memset(cstr, 0, sizeof(CString));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* free string and reset it to NULL */
|
|
|
|
static void cstr_free(CString *cstr)
|
|
|
|
{
|
|
|
|
tcc_free(cstr->data_allocated);
|
|
|
|
cstr_new(cstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define cstr_reset(cstr) cstr_free(cstr)
|
|
|
|
|
2002-11-23 01:16:30 +03:00
|
|
|
static CString *cstr_dup(CString *cstr1)
|
|
|
|
{
|
|
|
|
CString *cstr;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
cstr = tcc_malloc(sizeof(CString));
|
|
|
|
size = cstr1->size;
|
|
|
|
cstr->size = size;
|
|
|
|
cstr->size_allocated = size;
|
|
|
|
cstr->data_allocated = tcc_malloc(size);
|
|
|
|
cstr->data = cstr->data_allocated;
|
|
|
|
memcpy(cstr->data_allocated, cstr1->data_allocated, size);
|
|
|
|
return cstr;
|
|
|
|
}
|
|
|
|
|
2002-08-18 17:22:55 +04:00
|
|
|
/* XXX: unicode ? */
|
|
|
|
static void add_char(CString *cstr, int c)
|
2001-11-11 05:53:01 +03:00
|
|
|
{
|
|
|
|
if (c == '\'' || c == '\"' || c == '\\') {
|
|
|
|
/* XXX: could be more precise if char or string */
|
2002-08-18 17:22:55 +04:00
|
|
|
cstr_ccat(cstr, '\\');
|
2001-11-11 05:53:01 +03:00
|
|
|
}
|
|
|
|
if (c >= 32 && c <= 126) {
|
2002-08-18 17:22:55 +04:00
|
|
|
cstr_ccat(cstr, c);
|
2001-11-11 05:53:01 +03:00
|
|
|
} else {
|
2002-08-18 17:22:55 +04:00
|
|
|
cstr_ccat(cstr, '\\');
|
2001-11-11 05:53:01 +03:00
|
|
|
if (c == '\n') {
|
2002-08-18 17:22:55 +04:00
|
|
|
cstr_ccat(cstr, 'n');
|
2001-11-11 05:53:01 +03:00
|
|
|
} else {
|
2002-08-18 17:22:55 +04:00
|
|
|
cstr_ccat(cstr, '0' + ((c >> 6) & 7));
|
|
|
|
cstr_ccat(cstr, '0' + ((c >> 3) & 7));
|
|
|
|
cstr_ccat(cstr, '0' + (c & 7));
|
2001-11-11 05:53:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-10-28 02:48:39 +03:00
|
|
|
|
2001-11-11 05:53:01 +03:00
|
|
|
/* XXX: buffer overflow */
|
2002-08-18 17:22:55 +04:00
|
|
|
/* XXX: float tokens */
|
2001-12-14 01:28:53 +03:00
|
|
|
char *get_tok_str(int v, CValue *cv)
|
2001-10-30 02:23:01 +03:00
|
|
|
{
|
2001-11-11 05:53:01 +03:00
|
|
|
static char buf[STRING_MAX_SIZE + 1];
|
2002-08-18 17:22:55 +04:00
|
|
|
static CString cstr_buf;
|
|
|
|
CString *cstr;
|
|
|
|
unsigned char *q;
|
2001-10-30 02:23:01 +03:00
|
|
|
char *p;
|
2002-08-18 17:22:55 +04:00
|
|
|
int i, len;
|
2001-11-11 05:53:01 +03:00
|
|
|
|
2002-08-18 17:22:55 +04:00
|
|
|
/* NOTE: to go faster, we give a fixed buffer for small strings */
|
|
|
|
cstr_reset(&cstr_buf);
|
|
|
|
cstr_buf.data = buf;
|
|
|
|
cstr_buf.size_allocated = sizeof(buf);
|
|
|
|
p = buf;
|
|
|
|
|
|
|
|
switch(v) {
|
|
|
|
case TOK_CINT:
|
|
|
|
case TOK_CUINT:
|
2002-11-02 23:45:52 +03:00
|
|
|
/* XXX: not quite exact, but only useful for testing */
|
2002-08-18 17:22:55 +04:00
|
|
|
sprintf(p, "%u", cv->ui);
|
|
|
|
break;
|
2002-11-02 17:12:32 +03:00
|
|
|
case TOK_CLLONG:
|
|
|
|
case TOK_CULLONG:
|
2002-11-02 23:45:52 +03:00
|
|
|
/* XXX: not quite exact, but only useful for testing */
|
2002-11-02 17:12:32 +03:00
|
|
|
sprintf(p, "%Lu", cv->ull);
|
|
|
|
break;
|
2002-08-18 17:22:55 +04:00
|
|
|
case TOK_CCHAR:
|
|
|
|
case TOK_LCHAR:
|
|
|
|
cstr_ccat(&cstr_buf, '\'');
|
|
|
|
add_char(&cstr_buf, cv->i);
|
|
|
|
cstr_ccat(&cstr_buf, '\'');
|
|
|
|
cstr_ccat(&cstr_buf, '\0');
|
|
|
|
break;
|
2002-11-02 20:02:31 +03:00
|
|
|
case TOK_PPNUM:
|
|
|
|
cstr = cv->cstr;
|
|
|
|
len = cstr->size - 1;
|
|
|
|
for(i=0;i<len;i++)
|
|
|
|
add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
|
2002-11-02 23:45:52 +03:00
|
|
|
cstr_ccat(&cstr_buf, '\0');
|
2002-11-02 20:02:31 +03:00
|
|
|
break;
|
2002-08-18 17:22:55 +04:00
|
|
|
case TOK_STR:
|
|
|
|
case TOK_LSTR:
|
|
|
|
cstr = cv->cstr;
|
|
|
|
cstr_ccat(&cstr_buf, '\"');
|
|
|
|
if (v == TOK_STR) {
|
|
|
|
len = cstr->size - 1;
|
|
|
|
for(i=0;i<len;i++)
|
|
|
|
add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
|
|
|
|
} else {
|
|
|
|
len = (cstr->size / sizeof(int)) - 1;
|
|
|
|
for(i=0;i<len;i++)
|
|
|
|
add_char(&cstr_buf, ((int *)cstr->data)[i]);
|
|
|
|
}
|
|
|
|
cstr_ccat(&cstr_buf, '\"');
|
|
|
|
cstr_ccat(&cstr_buf, '\0');
|
|
|
|
break;
|
|
|
|
case TOK_LT:
|
|
|
|
v = '<';
|
|
|
|
goto addv;
|
|
|
|
case TOK_GT:
|
|
|
|
v = '>';
|
|
|
|
goto addv;
|
|
|
|
case TOK_A_SHL:
|
|
|
|
return strcpy(p, "<<=");
|
|
|
|
case TOK_A_SAR:
|
|
|
|
return strcpy(p, ">>=");
|
|
|
|
default:
|
|
|
|
if (v < TOK_IDENT) {
|
|
|
|
/* search in two bytes table */
|
|
|
|
q = tok_two_chars;
|
|
|
|
while (*q) {
|
|
|
|
if (q[2] == v) {
|
|
|
|
*p++ = q[0];
|
|
|
|
*p++ = q[1];
|
|
|
|
*p = '\0';
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
q += 3;
|
|
|
|
}
|
|
|
|
addv:
|
|
|
|
*p++ = v;
|
|
|
|
*p = '\0';
|
|
|
|
} else if (v < tok_ident) {
|
|
|
|
return table_ident[v - TOK_IDENT]->str;
|
|
|
|
} else if (v >= SYM_FIRST_ANOM) {
|
|
|
|
/* special name for anonymous symbol */
|
|
|
|
sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
|
|
|
|
} else {
|
|
|
|
/* should never happen */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
break;
|
2001-10-30 02:23:01 +03:00
|
|
|
}
|
2002-08-18 17:22:55 +04:00
|
|
|
return cstr_buf.data;
|
2001-10-30 02:23:01 +03:00
|
|
|
}
|
|
|
|
|
2001-11-18 19:33:01 +03:00
|
|
|
/* push, without hashing */
|
2002-11-20 03:29:04 +03:00
|
|
|
static Sym *sym_push2(Sym **ps, int v, int t, int c)
|
2001-11-18 19:33:01 +03:00
|
|
|
{
|
|
|
|
Sym *s;
|
2002-08-18 17:22:55 +04:00
|
|
|
s = tcc_malloc(sizeof(Sym));
|
2001-11-18 19:33:01 +03:00
|
|
|
s->v = v;
|
2002-11-19 00:46:44 +03:00
|
|
|
s->type.t = t;
|
2001-11-18 19:33:01 +03:00
|
|
|
s->c = c;
|
|
|
|
s->next = NULL;
|
|
|
|
/* add in stack */
|
|
|
|
s->prev = *ps;
|
|
|
|
*ps = s;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2001-11-01 17:48:10 +03:00
|
|
|
/* find a symbol and return its associated structure. 's' is the top
|
|
|
|
of the symbol stack */
|
2002-11-20 03:29:04 +03:00
|
|
|
static Sym *sym_find2(Sym *s, int v)
|
2001-11-01 17:48:10 +03:00
|
|
|
{
|
|
|
|
while (s) {
|
|
|
|
if (s->v == v)
|
|
|
|
return s;
|
|
|
|
s = s->prev;
|
|
|
|
}
|
2001-11-18 19:33:01 +03:00
|
|
|
return NULL;
|
2001-11-01 17:48:10 +03:00
|
|
|
}
|
|
|
|
|
2002-11-20 03:29:04 +03:00
|
|
|
/* structure lookup */
|
|
|
|
static Sym *struct_find(int v)
|
2001-11-01 17:48:10 +03:00
|
|
|
{
|
2002-11-20 03:29:04 +03:00
|
|
|
v -= TOK_IDENT;
|
|
|
|
if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
|
|
|
|
return NULL;
|
|
|
|
return table_ident[v]->sym_struct;
|
|
|
|
}
|
2001-11-18 19:33:01 +03:00
|
|
|
|
2002-11-20 03:29:04 +03:00
|
|
|
/* find an identifier */
|
|
|
|
static inline Sym *sym_find(int v)
|
|
|
|
{
|
|
|
|
v -= TOK_IDENT;
|
|
|
|
if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
|
|
|
|
return NULL;
|
|
|
|
return table_ident[v]->sym_identifier;
|
2001-11-18 19:33:01 +03:00
|
|
|
}
|
|
|
|
|
2002-11-20 03:29:04 +03:00
|
|
|
/* push a given symbol on the symbol stack */
|
|
|
|
static Sym *sym_push(int v, CType *type, int r, int c)
|
2001-11-18 19:33:01 +03:00
|
|
|
{
|
|
|
|
Sym *s, **ps;
|
2002-11-20 03:29:04 +03:00
|
|
|
TokenSym *ts;
|
|
|
|
|
|
|
|
if (local_stack)
|
|
|
|
ps = &local_stack;
|
|
|
|
else
|
|
|
|
ps = &global_stack;
|
|
|
|
s = sym_push2(ps, v, type->t, c);
|
|
|
|
s->type.ref = type->ref;
|
|
|
|
s->r = r;
|
|
|
|
/* don't record fields or anonymous symbols */
|
|
|
|
/* XXX: simplify */
|
|
|
|
if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
|
|
|
|
/* record symbol in token array */
|
|
|
|
ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
|
|
|
|
if (v & SYM_STRUCT)
|
|
|
|
ps = &ts->sym_struct;
|
|
|
|
else
|
|
|
|
ps = &ts->sym_identifier;
|
|
|
|
s->prev_tok = *ps;
|
2001-12-13 00:16:17 +03:00
|
|
|
*ps = s;
|
|
|
|
}
|
2001-11-01 17:48:10 +03:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2002-11-20 03:29:04 +03:00
|
|
|
/* push a global identifier */
|
|
|
|
static Sym *global_identifier_push(int v, int t, int c)
|
2001-11-01 17:48:10 +03:00
|
|
|
{
|
2002-11-20 03:29:04 +03:00
|
|
|
Sym *s, **ps;
|
|
|
|
s = sym_push2(&global_stack, v, t, c);
|
|
|
|
/* don't record anonymous symbol */
|
|
|
|
if (v < SYM_FIRST_ANOM) {
|
|
|
|
ps = &table_ident[v - TOK_IDENT]->sym_identifier;
|
|
|
|
/* modify the top most local identifier, so that
|
|
|
|
sym_identifier will point to 's' when popped */
|
|
|
|
while (*ps != NULL)
|
|
|
|
ps = &(*ps)->prev_tok;
|
|
|
|
s->prev_tok = NULL;
|
|
|
|
*ps = s;
|
|
|
|
}
|
2001-12-22 20:05:23 +03:00
|
|
|
return s;
|
2001-11-01 17:48:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* pop symbols until top reaches 'b' */
|
2002-11-20 03:29:04 +03:00
|
|
|
static void sym_pop(Sym **ptop, Sym *b)
|
2001-11-01 17:48:10 +03:00
|
|
|
{
|
2002-11-20 03:29:04 +03:00
|
|
|
Sym *s, *ss, **ps;
|
|
|
|
TokenSym *ts;
|
|
|
|
int v;
|
2001-11-01 17:48:10 +03:00
|
|
|
|
2002-11-20 03:29:04 +03:00
|
|
|
s = *ptop;
|
2001-11-01 17:48:10 +03:00
|
|
|
while(s != b) {
|
|
|
|
ss = s->prev;
|
2002-11-20 03:29:04 +03:00
|
|
|
v = s->v;
|
|
|
|
/* remove symbol in token array */
|
|
|
|
/* XXX: simplify */
|
|
|
|
if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
|
|
|
|
ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
|
|
|
|
if (v & SYM_STRUCT)
|
|
|
|
ps = &ts->sym_struct;
|
|
|
|
else
|
|
|
|
ps = &ts->sym_identifier;
|
|
|
|
*ps = s->prev_tok;
|
|
|
|
}
|
2002-08-18 17:22:55 +04:00
|
|
|
tcc_free(s);
|
2001-11-01 17:48:10 +03:00
|
|
|
s = ss;
|
|
|
|
}
|
2002-11-20 03:29:04 +03:00
|
|
|
*ptop = b;
|
2001-11-01 17:48:10 +03:00
|
|
|
}
|
|
|
|
|
2002-01-12 19:39:35 +03:00
|
|
|
/* I/O layer */
|
|
|
|
|
2002-11-02 17:12:32 +03:00
|
|
|
BufferedFile *tcc_open(TCCState *s1, const char *filename)
|
2002-01-12 19:39:35 +03:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
BufferedFile *bf;
|
|
|
|
|
|
|
|
fd = open(filename, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return NULL;
|
2002-08-18 17:22:55 +04:00
|
|
|
bf = tcc_malloc(sizeof(BufferedFile));
|
2002-01-12 19:39:35 +03:00
|
|
|
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);
|
2002-11-23 02:28:06 +03:00
|
|
|
bf->line_num = 1;
|
2002-09-08 23:34:32 +04:00
|
|
|
bf->ifndef_macro = 0;
|
2002-11-02 17:12:32 +03:00
|
|
|
bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
|
2002-01-26 21:05:29 +03:00
|
|
|
// printf("opening '%s'\n", filename);
|
2002-01-12 19:39:35 +03:00
|
|
|
return bf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tcc_close(BufferedFile *bf)
|
|
|
|
{
|
2002-01-26 21:05:29 +03:00
|
|
|
total_lines += bf->line_num;
|
2002-01-12 19:39:35 +03:00
|
|
|
close(bf->fd);
|
2002-08-18 17:22:55 +04:00
|
|
|
tcc_free(bf);
|
2002-01-12 19:39:35 +03:00
|
|
|
}
|
|
|
|
|
2002-11-23 02:28:06 +03:00
|
|
|
/* fill input buffer and peek next char */
|
|
|
|
static int tcc_peekc_slow(BufferedFile *bf)
|
2002-01-12 19:39:35 +03:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
/* only tries to read if really end of buffer */
|
|
|
|
if (bf->buf_ptr >= bf->buf_end) {
|
|
|
|
if (bf->fd != -1) {
|
2002-11-23 21:15:17 +03:00
|
|
|
#if defined(PARSE_DEBUG)
|
|
|
|
len = 8;
|
|
|
|
#else
|
|
|
|
len = IO_BUF_SIZE;
|
|
|
|
#endif
|
|
|
|
len = read(bf->fd, bf->buffer, len);
|
2002-01-12 19:39:35 +03:00
|
|
|
if (len < 0)
|
|
|
|
len = 0;
|
|
|
|
} else {
|
|
|
|
len = 0;
|
|
|
|
}
|
2002-01-26 21:05:29 +03:00
|
|
|
total_bytes += len;
|
2002-01-12 19:39:35 +03:00
|
|
|
bf->buf_ptr = bf->buffer;
|
|
|
|
bf->buf_end = bf->buffer + len;
|
|
|
|
*bf->buf_end = CH_EOB;
|
|
|
|
}
|
|
|
|
if (bf->buf_ptr < bf->buf_end) {
|
2002-11-23 02:28:06 +03:00
|
|
|
return bf->buf_ptr[0];
|
2002-01-12 19:39:35 +03:00
|
|
|
} else {
|
|
|
|
bf->buf_ptr = bf->buf_end;
|
|
|
|
return CH_EOF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-23 21:15:17 +03:00
|
|
|
/* return the current character, handling end of block if necessary
|
|
|
|
(but not stray) */
|
|
|
|
static int handle_eob(void)
|
2001-12-07 02:57:36 +03:00
|
|
|
{
|
2002-11-23 21:15:17 +03:00
|
|
|
return tcc_peekc_slow(file);
|
2001-12-07 02:57:36 +03:00
|
|
|
}
|
|
|
|
|
2002-11-22 21:12:41 +03:00
|
|
|
/* read next char from current input file and handle end of input buffer */
|
2001-12-07 02:57:36 +03:00
|
|
|
static inline void inp(void)
|
2001-11-06 04:20:20 +03:00
|
|
|
{
|
2002-11-23 02:28:06 +03:00
|
|
|
ch = *(++(file->buf_ptr));
|
2002-01-12 19:39:35 +03:00
|
|
|
/* end of buffer/file handling */
|
2002-11-22 21:12:41 +03:00
|
|
|
if (ch == CH_EOB)
|
2002-11-23 21:15:17 +03:00
|
|
|
ch = handle_eob();
|
2001-11-06 04:20:20 +03:00
|
|
|
}
|
|
|
|
|
2002-11-22 21:12:41 +03:00
|
|
|
/* handle '\[\r]\n' */
|
2002-08-30 01:16:24 +04:00
|
|
|
static void handle_stray(void)
|
|
|
|
{
|
2002-11-22 21:12:41 +03:00
|
|
|
while (ch == '\\') {
|
|
|
|
inp();
|
|
|
|
if (ch == '\n') {
|
|
|
|
file->line_num++;
|
2002-08-30 01:16:24 +04:00
|
|
|
inp();
|
2002-11-22 21:12:41 +03:00
|
|
|
} else if (ch == '\r') {
|
2002-08-30 01:16:24 +04:00
|
|
|
inp();
|
2002-11-22 21:12:41 +03:00
|
|
|
if (ch != '\n')
|
|
|
|
goto fail;
|
|
|
|
file->line_num++;
|
2002-08-30 01:16:24 +04:00
|
|
|
inp();
|
|
|
|
} else {
|
2002-11-22 21:12:41 +03:00
|
|
|
fail:
|
|
|
|
error("stray '\\' in program");
|
2002-08-30 01:16:24 +04:00
|
|
|
}
|
2002-11-22 21:12:41 +03:00
|
|
|
}
|
2002-08-30 01:16:24 +04:00
|
|
|
}
|
|
|
|
|
2002-11-23 21:15:17 +03:00
|
|
|
/* skip the stray and handle the \\n case. Output an error if
|
|
|
|
incorrect char after the stray */
|
|
|
|
static int handle_stray1(uint8_t *p)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
if (p >= file->buf_end) {
|
|
|
|
file->buf_ptr = p;
|
|
|
|
c = handle_eob();
|
|
|
|
p = file->buf_ptr;
|
|
|
|
if (c == '\\')
|
|
|
|
goto parse_stray;
|
|
|
|
} else {
|
|
|
|
parse_stray:
|
|
|
|
file->buf_ptr = p;
|
|
|
|
ch = *p;
|
|
|
|
handle_stray();
|
|
|
|
p = file->buf_ptr;
|
|
|
|
c = *p;
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handle the complicated stray case */
|
|
|
|
#define PEEKC(c, p)\
|
|
|
|
{\
|
|
|
|
p++;\
|
|
|
|
c = *p;\
|
|
|
|
if (c == '\\') {\
|
|
|
|
c = handle_stray1(p);\
|
|
|
|
p = file->buf_ptr;\
|
|
|
|
}\
|
|
|
|
}
|
|
|
|
|
2002-11-22 21:12:41 +03:00
|
|
|
/* input with '\[\r]\n' handling. Note that this function cannot
|
|
|
|
handle other characters after '\', so you cannot call it inside
|
|
|
|
strings or comments */
|
|
|
|
static void minp(void)
|
2001-11-06 04:20:20 +03:00
|
|
|
{
|
2001-11-11 05:53:01 +03:00
|
|
|
inp();
|
2002-08-30 01:16:24 +04:00
|
|
|
if (ch == '\\')
|
|
|
|
handle_stray();
|
2001-11-06 04:20:20 +03:00
|
|
|
}
|
|
|
|
|
2001-12-07 02:57:36 +03:00
|
|
|
|
2002-11-22 21:12:41 +03:00
|
|
|
static void parse_line_comment(void)
|
2001-11-06 04:20:20 +03:00
|
|
|
{
|
2002-11-22 21:12:41 +03:00
|
|
|
/* single line C++ comments */
|
|
|
|
/* XXX: accept '\\\n' ? */
|
|
|
|
inp();
|
2002-11-23 02:28:06 +03:00
|
|
|
while (ch != '\n' && ch != CH_EOF)
|
2001-11-06 04:20:20 +03:00
|
|
|
inp();
|
2002-11-22 21:12:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_comment(void)
|
|
|
|
{
|
2002-11-23 02:28:06 +03:00
|
|
|
uint8_t *p;
|
|
|
|
int c;
|
|
|
|
|
2002-11-22 21:12:41 +03:00
|
|
|
/* C comments */
|
2002-11-23 02:28:06 +03:00
|
|
|
p = file->buf_ptr;
|
2002-11-23 21:15:17 +03:00
|
|
|
p++;
|
2002-11-22 21:12:41 +03:00
|
|
|
for(;;) {
|
|
|
|
/* fast skip loop */
|
2002-11-23 02:28:06 +03:00
|
|
|
for(;;) {
|
|
|
|
c = *p;
|
|
|
|
if (c == '\n' || c == '*' || c == '\\')
|
|
|
|
break;
|
|
|
|
p++;
|
|
|
|
c = *p;
|
|
|
|
if (c == '\n' || c == '*' || c == '\\')
|
|
|
|
break;
|
|
|
|
p++;
|
|
|
|
}
|
2002-11-22 21:12:41 +03:00
|
|
|
/* now we can handle all the cases */
|
2002-11-23 02:28:06 +03:00
|
|
|
if (c == '\n') {
|
2002-11-22 21:12:41 +03:00
|
|
|
file->line_num++;
|
2002-11-23 02:28:06 +03:00
|
|
|
p++;
|
|
|
|
} else if (c == '*') {
|
|
|
|
p++;
|
2002-11-22 21:12:41 +03:00
|
|
|
for(;;) {
|
2002-11-23 02:28:06 +03:00
|
|
|
c = *p;
|
2002-11-23 21:15:17 +03:00
|
|
|
if (c == '*') {
|
|
|
|
p++;
|
|
|
|
} else if (c == '/') {
|
2002-11-22 21:12:41 +03:00
|
|
|
goto end_of_comment;
|
2002-11-23 02:28:06 +03:00
|
|
|
} else if (c == '\\') {
|
2002-11-23 21:15:17 +03:00
|
|
|
file->buf_ptr = p;
|
|
|
|
c = handle_eob();
|
|
|
|
if (c == '\\') {
|
|
|
|
/* skip '\\n', but if '\' followed but another
|
|
|
|
char, behave asif a stray was parsed */
|
|
|
|
ch = file->buf_ptr[0];
|
|
|
|
while (ch == '\\') {
|
|
|
|
inp();
|
|
|
|
if (ch == '\n') {
|
|
|
|
file->line_num++;
|
|
|
|
inp();
|
|
|
|
} else if (ch == '\r') {
|
|
|
|
inp();
|
|
|
|
if (ch == '\n') {
|
|
|
|
file->line_num++;
|
|
|
|
inp();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p = file->buf_ptr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2002-11-22 21:12:41 +03:00
|
|
|
}
|
2002-11-23 21:15:17 +03:00
|
|
|
p = file->buf_ptr;
|
2002-11-23 02:28:06 +03:00
|
|
|
} else {
|
2002-11-22 21:12:41 +03:00
|
|
|
break;
|
|
|
|
}
|
2001-11-06 04:20:20 +03:00
|
|
|
}
|
2002-11-23 21:15:17 +03:00
|
|
|
} else {
|
|
|
|
/* stray, eob or eof */
|
2002-11-23 02:28:06 +03:00
|
|
|
file->buf_ptr = p;
|
2002-11-23 21:15:17 +03:00
|
|
|
c = handle_eob();
|
2002-11-23 02:28:06 +03:00
|
|
|
p = file->buf_ptr;
|
2002-11-23 21:15:17 +03:00
|
|
|
if (c == CH_EOF) {
|
2002-11-23 02:28:06 +03:00
|
|
|
error("unexpected end of file in comment");
|
2002-11-23 21:15:17 +03:00
|
|
|
} else if (c == '\\') {
|
|
|
|
p++;
|
2002-11-23 02:28:06 +03:00
|
|
|
}
|
2001-11-06 04:20:20 +03:00
|
|
|
}
|
|
|
|
}
|
2002-11-22 21:12:41 +03:00
|
|
|
end_of_comment:
|
2002-11-23 02:28:06 +03:00
|
|
|
p++;
|
|
|
|
file->buf_ptr = p;
|
|
|
|
ch = *p;
|
2001-11-06 04:20:20 +03:00
|
|
|
}
|
|
|
|
|
2002-11-22 21:12:41 +03:00
|
|
|
#define cinp minp
|
|
|
|
|
2002-08-30 01:16:24 +04:00
|
|
|
/* space exlcuding newline */
|
|
|
|
static inline int is_space(int ch)
|
2001-11-06 04:20:20 +03:00
|
|
|
{
|
2002-08-30 01:16:24 +04:00
|
|
|
return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r';
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void skip_spaces(void)
|
|
|
|
{
|
|
|
|
while (is_space(ch))
|
2001-11-06 04:20:20 +03:00
|
|
|
cinp();
|
|
|
|
}
|
|
|
|
|
2001-11-08 04:12:31 +03:00
|
|
|
/* skip block of text until #else, #elif or #endif. skip also pairs of
|
|
|
|
#if/#endif */
|
2002-01-06 00:13:17 +03:00
|
|
|
void preprocess_skip(void)
|
2001-11-08 04:12:31 +03:00
|
|
|
{
|
2002-11-23 21:15:17 +03:00
|
|
|
int a, start_of_line, sep, c;
|
|
|
|
uint8_t *p;
|
|
|
|
|
|
|
|
p = file->buf_ptr;
|
2002-11-22 21:12:41 +03:00
|
|
|
start_of_line = 1;
|
2001-11-08 04:12:31 +03:00
|
|
|
a = 0;
|
2002-11-23 01:16:30 +03:00
|
|
|
for(;;) {
|
|
|
|
redo_no_start:
|
2002-11-23 21:15:17 +03:00
|
|
|
c = *p;
|
|
|
|
switch(c) {
|
2002-11-22 21:12:41 +03:00
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\f':
|
|
|
|
case '\v':
|
|
|
|
case '\r':
|
2002-11-23 21:15:17 +03:00
|
|
|
p++;
|
2002-11-23 01:16:30 +03:00
|
|
|
goto redo_no_start;
|
2002-11-22 21:12:41 +03:00
|
|
|
case '\n':
|
|
|
|
start_of_line = 1;
|
|
|
|
file->line_num++;
|
2002-11-23 21:15:17 +03:00
|
|
|
p++;
|
2002-11-23 01:16:30 +03:00
|
|
|
goto redo_no_start;
|
2002-11-22 21:12:41 +03:00
|
|
|
case '\\':
|
2002-11-23 21:15:17 +03:00
|
|
|
file->buf_ptr = p;
|
|
|
|
c = handle_eob();
|
|
|
|
if (c == CH_EOF) {
|
|
|
|
expect("#endif");
|
|
|
|
} else if (c == '\\') {
|
|
|
|
/* XXX: incorrect: should not give an error */
|
|
|
|
ch = file->buf_ptr[0];
|
|
|
|
handle_stray();
|
|
|
|
}
|
|
|
|
p = file->buf_ptr;
|
2002-11-23 01:16:30 +03:00
|
|
|
goto redo_no_start;
|
2002-11-22 21:12:41 +03:00
|
|
|
/* skip strings */
|
|
|
|
case '\"':
|
|
|
|
case '\'':
|
2002-11-23 21:15:17 +03:00
|
|
|
sep = c;
|
|
|
|
p++;
|
|
|
|
for(;;) {
|
|
|
|
c = *p;
|
|
|
|
if (c == sep) {
|
|
|
|
break;
|
|
|
|
} else if (c == '\\') {
|
|
|
|
file->buf_ptr = p;
|
|
|
|
c = handle_eob();
|
|
|
|
p = file->buf_ptr;
|
|
|
|
if (c == CH_EOF) {
|
|
|
|
/* XXX: better error message */
|
|
|
|
error("unterminated string");
|
|
|
|
} else if (c == '\\') {
|
|
|
|
/* ignore next char */
|
|
|
|
p++;
|
|
|
|
c = *p;
|
|
|
|
if (c == '\\') {
|
|
|
|
file->buf_ptr = p;
|
|
|
|
c = handle_eob();
|
|
|
|
p = file->buf_ptr;
|
|
|
|
}
|
|
|
|
if (c == '\n')
|
|
|
|
file->line_num++;
|
|
|
|
else if (c != CH_EOF)
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
} else if (c == '\n') {
|
2002-11-22 21:12:41 +03:00
|
|
|
file->line_num++;
|
2002-11-23 21:15:17 +03:00
|
|
|
p++;
|
|
|
|
} else {
|
|
|
|
p++;
|
2002-11-22 21:12:41 +03:00
|
|
|
}
|
|
|
|
}
|
2002-11-23 21:15:17 +03:00
|
|
|
p++;
|
2002-11-22 21:12:41 +03:00
|
|
|
break;
|
|
|
|
/* skip comments */
|
|
|
|
case '/':
|
2002-11-23 21:15:17 +03:00
|
|
|
file->buf_ptr = p;
|
|
|
|
ch = *p;
|
2002-11-22 21:12:41 +03:00
|
|
|
minp();
|
|
|
|
if (ch == '*') {
|
|
|
|
parse_comment();
|
|
|
|
} else if (ch == '/') {
|
|
|
|
parse_line_comment();
|
|
|
|
}
|
2002-11-23 21:15:17 +03:00
|
|
|
p = file->buf_ptr;
|
2002-11-22 21:12:41 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '#':
|
2002-11-23 21:15:17 +03:00
|
|
|
p++;
|
2002-11-22 21:12:41 +03:00
|
|
|
if (start_of_line) {
|
2002-11-23 21:15:17 +03:00
|
|
|
file->buf_ptr = p;
|
2002-11-22 21:12:41 +03:00
|
|
|
next_nomacro();
|
2002-11-23 21:15:17 +03:00
|
|
|
p = file->buf_ptr;
|
2002-11-22 21:12:41 +03:00
|
|
|
if (a == 0 &&
|
|
|
|
(tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
|
|
|
|
goto the_end;
|
|
|
|
if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
|
|
|
|
a++;
|
|
|
|
else if (tok == TOK_ENDIF)
|
|
|
|
a--;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2002-11-23 21:15:17 +03:00
|
|
|
p++;
|
2002-11-22 21:12:41 +03:00
|
|
|
break;
|
2001-11-08 04:12:31 +03:00
|
|
|
}
|
2002-11-23 01:16:30 +03:00
|
|
|
start_of_line = 0;
|
2001-11-08 04:12:31 +03:00
|
|
|
}
|
2002-11-22 21:12:41 +03:00
|
|
|
the_end: ;
|
2002-11-23 21:15:17 +03:00
|
|
|
file->buf_ptr = p;
|
2001-11-08 04:12:31 +03:00
|
|
|
}
|
|
|
|
|
2002-06-30 21:34:30 +04:00
|
|
|
/* ParseState handling */
|
|
|
|
|
|
|
|
/* XXX: currently, no include file info is stored. Thus, we cannot display
|
|
|
|
accurate messages if the function or data definition spans multiple
|
|
|
|
files */
|
|
|
|
|
|
|
|
/* save current parse state in 's' */
|
|
|
|
void save_parse_state(ParseState *s)
|
|
|
|
{
|
|
|
|
s->line_num = file->line_num;
|
|
|
|
s->macro_ptr = macro_ptr;
|
|
|
|
s->tok = tok;
|
|
|
|
s->tokc = tokc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* restore parse state from 's' */
|
|
|
|
void restore_parse_state(ParseState *s)
|
|
|
|
{
|
|
|
|
file->line_num = s->line_num;
|
|
|
|
macro_ptr = s->macro_ptr;
|
|
|
|
tok = s->tok;
|
|
|
|
tokc = s->tokc;
|
|
|
|
}
|
|
|
|
|
2001-12-14 01:28:53 +03:00
|
|
|
/* return the number of additionnal 'ints' necessary to store the
|
|
|
|
token */
|
|
|
|
static inline int tok_ext_size(int t)
|
2001-11-29 01:48:43 +03:00
|
|
|
{
|
2001-12-14 01:28:53 +03:00
|
|
|
switch(t) {
|
|
|
|
/* 4 bytes */
|
2002-01-04 00:14:59 +03:00
|
|
|
case TOK_CINT:
|
|
|
|
case TOK_CUINT:
|
2001-12-14 01:28:53 +03:00
|
|
|
case TOK_CCHAR:
|
|
|
|
case TOK_LCHAR:
|
|
|
|
case TOK_STR:
|
|
|
|
case TOK_LSTR:
|
|
|
|
case TOK_CFLOAT:
|
2002-06-30 21:34:30 +04:00
|
|
|
case TOK_LINENUM:
|
2002-11-02 20:02:31 +03:00
|
|
|
case TOK_PPNUM:
|
2001-12-14 01:28:53 +03:00
|
|
|
return 1;
|
|
|
|
case TOK_CDOUBLE:
|
2002-01-04 00:14:59 +03:00
|
|
|
case TOK_CLLONG:
|
|
|
|
case TOK_CULLONG:
|
2001-12-14 01:28:53 +03:00
|
|
|
return 2;
|
|
|
|
case TOK_CLDOUBLE:
|
|
|
|
return LDOUBLE_SIZE / 4;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
2001-11-29 01:48:43 +03:00
|
|
|
}
|
|
|
|
|
2002-06-30 21:34:30 +04:00
|
|
|
/* token string handling */
|
|
|
|
|
|
|
|
static inline void tok_str_new(TokenString *s)
|
|
|
|
{
|
|
|
|
s->str = NULL;
|
|
|
|
s->len = 0;
|
2002-11-24 01:02:40 +03:00
|
|
|
s->allocated_len = 0;
|
2002-06-30 21:34:30 +04:00
|
|
|
s->last_line_num = -1;
|
|
|
|
}
|
|
|
|
|
2002-08-18 17:22:55 +04:00
|
|
|
static void tok_str_free(int *str)
|
|
|
|
{
|
|
|
|
const int *p;
|
|
|
|
CString *cstr;
|
|
|
|
int t;
|
|
|
|
|
|
|
|
p = str;
|
|
|
|
for(;;) {
|
2002-11-24 01:02:40 +03:00
|
|
|
t = *p;
|
|
|
|
/* NOTE: we test zero separately so that GCC can generate a
|
|
|
|
table for the following switch */
|
2002-08-18 17:22:55 +04:00
|
|
|
if (t == 0)
|
|
|
|
break;
|
2002-11-24 01:02:40 +03:00
|
|
|
switch(t) {
|
|
|
|
case TOK_CINT:
|
|
|
|
case TOK_CUINT:
|
|
|
|
case TOK_CCHAR:
|
|
|
|
case TOK_LCHAR:
|
|
|
|
case TOK_CFLOAT:
|
|
|
|
case TOK_LINENUM:
|
|
|
|
p += 2;
|
|
|
|
break;
|
|
|
|
case TOK_PPNUM:
|
|
|
|
case TOK_STR:
|
|
|
|
case TOK_LSTR:
|
2002-08-18 17:22:55 +04:00
|
|
|
/* XXX: use a macro to be portable on 64 bit ? */
|
2002-11-24 01:02:40 +03:00
|
|
|
cstr = (CString *)p[1];
|
2002-08-18 17:22:55 +04:00
|
|
|
cstr_free(cstr);
|
|
|
|
tcc_free(cstr);
|
2002-11-24 01:02:40 +03:00
|
|
|
p += 2;
|
|
|
|
break;
|
|
|
|
case TOK_CDOUBLE:
|
|
|
|
case TOK_CLLONG:
|
|
|
|
case TOK_CULLONG:
|
|
|
|
p += 3;
|
|
|
|
break;
|
|
|
|
case TOK_CLDOUBLE:
|
|
|
|
p += 1 + (LDOUBLE_SIZE / 4);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
p++;
|
|
|
|
break;
|
2002-08-18 17:22:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tcc_free(str);
|
|
|
|
}
|
|
|
|
|
2002-11-24 01:02:40 +03:00
|
|
|
static int *tok_str_realloc(TokenString *s)
|
|
|
|
{
|
|
|
|
int *str, len;
|
|
|
|
|
|
|
|
len = s->allocated_len + TOK_STR_ALLOC_INCR;
|
|
|
|
str = tcc_realloc(s->str, len * sizeof(int));
|
|
|
|
if (!str)
|
|
|
|
error("memory full");
|
|
|
|
s->allocated_len = len;
|
|
|
|
s->str = str;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2002-06-30 21:34:30 +04:00
|
|
|
static void tok_str_add(TokenString *s, int t)
|
2001-11-08 04:12:31 +03:00
|
|
|
{
|
2001-11-11 05:53:01 +03:00
|
|
|
int len, *str;
|
2002-06-30 21:34:30 +04:00
|
|
|
|
|
|
|
len = s->len;
|
|
|
|
str = s->str;
|
2002-11-24 01:02:40 +03:00
|
|
|
if (len >= s->allocated_len)
|
|
|
|
str = tok_str_realloc(s);
|
2001-11-11 05:53:01 +03:00
|
|
|
str[len++] = t;
|
2002-06-30 21:34:30 +04:00
|
|
|
s->len = len;
|
2001-11-08 04:12:31 +03:00
|
|
|
}
|
|
|
|
|
2002-06-30 21:34:30 +04:00
|
|
|
static void tok_str_add2(TokenString *s, int t, CValue *cv)
|
2001-11-08 04:12:31 +03:00
|
|
|
{
|
2002-11-24 01:02:40 +03:00
|
|
|
int len, *str;
|
2002-08-18 17:22:55 +04:00
|
|
|
|
2002-11-24 01:02:40 +03:00
|
|
|
len = s->len;
|
|
|
|
str = s->str;
|
|
|
|
|
|
|
|
/* allocate space for worst case */
|
|
|
|
if (len + TOK_MAX_SIZE > s->allocated_len)
|
|
|
|
str = tok_str_realloc(s);
|
|
|
|
str[len++] = t;
|
|
|
|
switch(t) {
|
|
|
|
case TOK_CINT:
|
|
|
|
case TOK_CUINT:
|
|
|
|
case TOK_CCHAR:
|
|
|
|
case TOK_LCHAR:
|
|
|
|
case TOK_CFLOAT:
|
|
|
|
case TOK_LINENUM:
|
|
|
|
str[len++] = cv->tab[0];
|
|
|
|
break;
|
|
|
|
case TOK_PPNUM:
|
|
|
|
case TOK_STR:
|
|
|
|
case TOK_LSTR:
|
|
|
|
str[len++] = (int)cstr_dup(cv->cstr);
|
|
|
|
break;
|
|
|
|
case TOK_CDOUBLE:
|
|
|
|
case TOK_CLLONG:
|
|
|
|
case TOK_CULLONG:
|
|
|
|
str[len++] = cv->tab[0];
|
|
|
|
str[len++] = cv->tab[1];
|
|
|
|
break;
|
|
|
|
case TOK_CLDOUBLE:
|
|
|
|
#if LDOUBLE_SIZE == 12
|
|
|
|
str[len++] = cv->tab[0];
|
|
|
|
str[len++] = cv->tab[1];
|
|
|
|
str[len++] = cv->tab[2];
|
|
|
|
#else
|
|
|
|
#error add long double size support
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2002-08-18 17:22:55 +04:00
|
|
|
}
|
2002-11-24 01:02:40 +03:00
|
|
|
s->len = len;
|
2002-06-30 21:34:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add the current parse token in token string 's' */
|
|
|
|
static void tok_str_add_tok(TokenString *s)
|
|
|
|
{
|
|
|
|
CValue cval;
|
|
|
|
|
|
|
|
/* save line number info */
|
|
|
|
if (file->line_num != s->last_line_num) {
|
|
|
|
s->last_line_num = file->line_num;
|
|
|
|
cval.i = s->last_line_num;
|
|
|
|
tok_str_add2(s, TOK_LINENUM, &cval);
|
|
|
|
}
|
|
|
|
tok_str_add2(s, tok, &tokc);
|
2001-12-14 01:28:53 +03:00
|
|
|
}
|
|
|
|
|
2002-11-24 01:02:40 +03:00
|
|
|
#if LDOUBLE_SIZE == 12
|
|
|
|
#define LDOUBLE_GET(p, cv) \
|
|
|
|
cv.tab[0] = p[0]; \
|
|
|
|
cv.tab[1] = p[1]; \
|
|
|
|
cv.tab[2] = p[2];
|
|
|
|
#else
|
|
|
|
#error add long double size support
|
|
|
|
#endif
|
|
|
|
|
2001-12-14 01:28:53 +03:00
|
|
|
|
2002-11-24 01:02:40 +03:00
|
|
|
/* get a token from an integer array and increment pointer
|
|
|
|
accordingly. we code it as a macro to avoid pointer aliasing. */
|
|
|
|
#define TOK_GET(t, p, cv) \
|
|
|
|
{ \
|
|
|
|
t = *p++; \
|
|
|
|
switch(t) { \
|
|
|
|
case TOK_CINT: \
|
|
|
|
case TOK_CUINT: \
|
|
|
|
case TOK_CCHAR: \
|
|
|
|
case TOK_LCHAR: \
|
|
|
|
case TOK_CFLOAT: \
|
|
|
|
case TOK_LINENUM: \
|
|
|
|
case TOK_STR: \
|
|
|
|
case TOK_LSTR: \
|
|
|
|
case TOK_PPNUM: \
|
|
|
|
cv.tab[0] = *p++; \
|
|
|
|
break; \
|
|
|
|
case TOK_CDOUBLE: \
|
|
|
|
case TOK_CLLONG: \
|
|
|
|
case TOK_CULLONG: \
|
|
|
|
cv.tab[0] = p[0]; \
|
|
|
|
cv.tab[1] = p[1]; \
|
|
|
|
p += 2; \
|
|
|
|
break; \
|
|
|
|
case TOK_CLDOUBLE: \
|
|
|
|
LDOUBLE_GET(p, cv); \
|
|
|
|
p += LDOUBLE_SIZE / 4; \
|
|
|
|
break; \
|
|
|
|
default: \
|
|
|
|
break; \
|
|
|
|
} \
|
2001-11-11 05:53:01 +03:00
|
|
|
}
|
2001-11-08 04:12:31 +03:00
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
/* defines handling */
|
|
|
|
static inline void define_push(int v, int macro_type, int *str, Sym *first_arg)
|
|
|
|
{
|
|
|
|
Sym *s;
|
|
|
|
|
|
|
|
s = sym_push2(&define_stack, v, macro_type, (int)str);
|
|
|
|
s->next = first_arg;
|
|
|
|
table_ident[v - TOK_IDENT]->sym_define = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* undefined a define symbol. Its name is just set to zero */
|
|
|
|
static void define_undef(Sym *s)
|
|
|
|
{
|
|
|
|
int v;
|
|
|
|
v = s->v;
|
|
|
|
if (v >= TOK_IDENT && v < tok_ident)
|
|
|
|
table_ident[v - TOK_IDENT]->sym_define = NULL;
|
|
|
|
s->v = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Sym *define_find(int v)
|
|
|
|
{
|
|
|
|
v -= TOK_IDENT;
|
|
|
|
if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
|
|
|
|
return NULL;
|
|
|
|
return table_ident[v]->sym_define;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* free define stack until top reaches 'b' */
|
|
|
|
static void free_defines(Sym *b)
|
|
|
|
{
|
|
|
|
Sym *top, *top1;
|
|
|
|
int v;
|
|
|
|
|
|
|
|
top = define_stack;
|
|
|
|
while (top != b) {
|
|
|
|
top1 = top->prev;
|
|
|
|
/* do not free args or predefined defines */
|
|
|
|
if (top->c)
|
|
|
|
tok_str_free((int *)top->c);
|
|
|
|
v = top->v;
|
|
|
|
if (v >= TOK_IDENT && v < tok_ident)
|
|
|
|
table_ident[v - TOK_IDENT]->sym_define = NULL;
|
|
|
|
tcc_free(top);
|
|
|
|
top = top1;
|
|
|
|
}
|
|
|
|
define_stack = b;
|
|
|
|
}
|
|
|
|
|
2002-11-20 03:29:04 +03:00
|
|
|
/* label lookup */
|
|
|
|
static Sym *label_find(int v)
|
|
|
|
{
|
|
|
|
v -= TOK_IDENT;
|
|
|
|
if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
|
|
|
|
return NULL;
|
|
|
|
return table_ident[v]->sym_label;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Sym *label_push(int v, int flags)
|
|
|
|
{
|
|
|
|
Sym *s;
|
|
|
|
s = sym_push2(&label_stack, v, 0, 0);
|
|
|
|
s->r = flags;
|
|
|
|
table_ident[v - TOK_IDENT]->sym_label = s;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2001-11-11 05:53:01 +03:00
|
|
|
/* eval an expression for #if/#elif */
|
2002-11-24 01:02:40 +03:00
|
|
|
static int expr_preprocess(void)
|
2001-11-11 05:53:01 +03:00
|
|
|
{
|
2002-06-30 21:34:30 +04:00
|
|
|
int c, t;
|
|
|
|
TokenString str;
|
2001-11-11 05:53:01 +03:00
|
|
|
|
2002-06-30 21:34:30 +04:00
|
|
|
tok_str_new(&str);
|
2002-09-08 23:34:32 +04:00
|
|
|
while (tok != TOK_LINEFEED && tok != TOK_EOF) {
|
2001-11-11 05:53:01 +03:00
|
|
|
next(); /* do macro subst */
|
|
|
|
if (tok == TOK_DEFINED) {
|
|
|
|
next_nomacro();
|
|
|
|
t = tok;
|
|
|
|
if (t == '(')
|
|
|
|
next_nomacro();
|
2002-11-19 00:46:44 +03:00
|
|
|
c = define_find(tok) != 0;
|
2001-11-11 05:53:01 +03:00
|
|
|
if (t == '(')
|
|
|
|
next_nomacro();
|
2002-01-04 00:14:59 +03:00
|
|
|
tok = TOK_CINT;
|
2001-12-14 01:28:53 +03:00
|
|
|
tokc.i = c;
|
2001-11-11 05:53:01 +03:00
|
|
|
} else if (tok >= TOK_IDENT) {
|
|
|
|
/* if undefined macro */
|
2002-01-04 00:14:59 +03:00
|
|
|
tok = TOK_CINT;
|
2001-12-14 01:28:53 +03:00
|
|
|
tokc.i = 0;
|
2001-11-11 05:53:01 +03:00
|
|
|
}
|
2002-06-30 21:34:30 +04:00
|
|
|
tok_str_add_tok(&str);
|
2001-11-11 05:53:01 +03:00
|
|
|
}
|
2002-06-30 21:34:30 +04:00
|
|
|
tok_str_add(&str, -1); /* simulate end of file */
|
|
|
|
tok_str_add(&str, 0);
|
2001-11-11 05:53:01 +03:00
|
|
|
/* now evaluate C constant expression */
|
2002-06-30 21:34:30 +04:00
|
|
|
macro_ptr = str.str;
|
2001-11-08 04:12:31 +03:00
|
|
|
next();
|
|
|
|
c = expr_const();
|
2001-11-11 19:07:43 +03:00
|
|
|
macro_ptr = NULL;
|
2002-08-18 17:22:55 +04:00
|
|
|
tok_str_free(str.str);
|
2001-11-08 04:12:31 +03:00
|
|
|
return c != 0;
|
|
|
|
}
|
|
|
|
|
2002-11-22 21:12:41 +03:00
|
|
|
#if defined(PARSE_DEBUG) || defined(PP_DEBUG)
|
2002-11-24 01:02:40 +03:00
|
|
|
static void tok_print(int *str)
|
2001-11-11 05:53:01 +03:00
|
|
|
{
|
2001-12-14 01:28:53 +03:00
|
|
|
int t;
|
|
|
|
CValue cval;
|
2001-11-11 05:53:01 +03:00
|
|
|
|
|
|
|
while (1) {
|
2002-11-24 01:02:40 +03:00
|
|
|
TOK_GET(t, str, cval);
|
2001-11-11 05:53:01 +03:00
|
|
|
if (!t)
|
|
|
|
break;
|
2001-12-14 01:28:53 +03:00
|
|
|
printf(" %s", get_tok_str(t, &cval));
|
2001-11-11 05:53:01 +03:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-01-13 14:07:19 +03:00
|
|
|
/* parse after #define */
|
2002-11-23 21:15:17 +03:00
|
|
|
static void parse_define(void)
|
2002-01-13 14:07:19 +03:00
|
|
|
{
|
|
|
|
Sym *s, *first, **ps;
|
2002-11-23 21:15:17 +03:00
|
|
|
int v, t, varg, is_vaargs, c;
|
2002-06-30 21:34:30 +04:00
|
|
|
TokenString str;
|
|
|
|
|
2002-01-13 14:07:19 +03:00
|
|
|
v = tok;
|
2002-11-19 00:46:44 +03:00
|
|
|
if (v < TOK_IDENT)
|
|
|
|
error("invalid macro name '%s'", get_tok_str(tok, &tokc));
|
2002-01-13 14:07:19 +03:00
|
|
|
/* XXX: should check if same macro (ANSI) */
|
|
|
|
first = NULL;
|
|
|
|
t = MACRO_OBJ;
|
|
|
|
/* '(' must be just after macro definition for MACRO_FUNC */
|
2002-11-23 21:15:17 +03:00
|
|
|
c = file->buf_ptr[0];
|
|
|
|
if (c == '\\')
|
|
|
|
c = handle_stray1(file->buf_ptr);
|
|
|
|
if (c == '(') {
|
2002-01-13 14:07:19 +03:00
|
|
|
next_nomacro();
|
|
|
|
next_nomacro();
|
|
|
|
ps = &first;
|
|
|
|
while (tok != ')') {
|
2002-07-13 15:17:19 +04:00
|
|
|
varg = tok;
|
|
|
|
next_nomacro();
|
|
|
|
is_vaargs = 0;
|
|
|
|
if (varg == TOK_DOTS) {
|
|
|
|
varg = TOK___VA_ARGS__;
|
|
|
|
is_vaargs = 1;
|
|
|
|
} else if (tok == TOK_DOTS && gnu_ext) {
|
|
|
|
is_vaargs = 1;
|
|
|
|
next_nomacro();
|
|
|
|
}
|
|
|
|
if (varg < TOK_IDENT)
|
|
|
|
error("badly punctuated parameter list");
|
2002-11-19 00:46:44 +03:00
|
|
|
s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
|
2002-01-13 14:07:19 +03:00
|
|
|
*ps = s;
|
|
|
|
ps = &s->next;
|
|
|
|
if (tok != ',')
|
|
|
|
break;
|
|
|
|
next_nomacro();
|
|
|
|
}
|
|
|
|
t = MACRO_FUNC;
|
|
|
|
}
|
2002-06-30 21:34:30 +04:00
|
|
|
tok_str_new(&str);
|
2002-09-08 20:14:57 +04:00
|
|
|
next_nomacro();
|
|
|
|
/* EOF testing necessary for '-D' handling */
|
|
|
|
while (tok != TOK_LINEFEED && tok != TOK_EOF) {
|
2002-06-30 21:34:30 +04:00
|
|
|
tok_str_add2(&str, tok, &tokc);
|
2002-09-08 20:14:57 +04:00
|
|
|
next_nomacro();
|
2002-01-13 14:07:19 +03:00
|
|
|
}
|
2002-06-30 21:34:30 +04:00
|
|
|
tok_str_add(&str, 0);
|
2002-01-13 14:07:19 +03:00
|
|
|
#ifdef PP_DEBUG
|
|
|
|
printf("define %s %d: ", get_tok_str(v, NULL), t);
|
2002-07-27 02:59:19 +04:00
|
|
|
tok_print(str.str);
|
2002-01-13 14:07:19 +03:00
|
|
|
#endif
|
2002-11-19 00:46:44 +03:00
|
|
|
define_push(v, t, str.str, first);
|
2002-01-13 14:07:19 +03:00
|
|
|
}
|
|
|
|
|
2002-09-08 23:34:32 +04:00
|
|
|
/* XXX: use a token or a hash table to accelerate matching ? */
|
2002-11-02 17:12:32 +03:00
|
|
|
static CachedInclude *search_cached_include(TCCState *s1,
|
|
|
|
int type, const char *filename)
|
2002-09-08 23:34:32 +04:00
|
|
|
{
|
|
|
|
CachedInclude *e;
|
|
|
|
int i;
|
|
|
|
|
2002-11-02 17:12:32 +03:00
|
|
|
for(i = 0;i < s1->nb_cached_includes; i++) {
|
|
|
|
e = s1->cached_includes[i];
|
2002-09-08 23:34:32 +04:00
|
|
|
if (e->type == type && !strcmp(e->filename, filename))
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-11-02 17:12:32 +03:00
|
|
|
static inline void add_cached_include(TCCState *s1, int type,
|
2002-09-08 23:34:32 +04:00
|
|
|
const char *filename, int ifndef_macro)
|
|
|
|
{
|
|
|
|
CachedInclude *e;
|
|
|
|
|
2002-11-02 17:12:32 +03:00
|
|
|
if (search_cached_include(s1, type, filename))
|
2002-09-08 23:34:32 +04:00
|
|
|
return;
|
|
|
|
#ifdef INC_DEBUG
|
|
|
|
printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
|
|
|
|
#endif
|
|
|
|
e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
|
|
|
|
if (!e)
|
|
|
|
return;
|
|
|
|
e->type = type;
|
|
|
|
strcpy(e->filename, filename);
|
|
|
|
e->ifndef_macro = ifndef_macro;
|
2002-11-02 17:12:32 +03:00
|
|
|
dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
|
2002-09-08 23:34:32 +04:00
|
|
|
}
|
|
|
|
|
2002-11-23 01:16:30 +03:00
|
|
|
/* is_bof is true if first non space token at beginning of file */
|
|
|
|
static void preprocess(int is_bof)
|
2001-11-06 04:20:20 +03:00
|
|
|
{
|
2002-11-02 17:12:32 +03:00
|
|
|
TCCState *s1 = tcc_state;
|
2002-11-24 01:35:28 +03:00
|
|
|
int size, i, c, n;
|
2001-11-06 04:20:20 +03:00
|
|
|
char buf[1024], *q, *p;
|
|
|
|
char buf1[1024];
|
2002-01-12 19:39:35 +03:00
|
|
|
BufferedFile *f;
|
2002-01-13 14:07:19 +03:00
|
|
|
Sym *s;
|
2002-09-08 23:34:32 +04:00
|
|
|
CachedInclude *e;
|
2002-11-02 17:12:32 +03:00
|
|
|
|
2002-09-08 23:34:32 +04:00
|
|
|
return_linefeed = 1; /* linefeed will be returned as a
|
|
|
|
token. EOF is also returned as line feed */
|
2001-11-08 04:26:37 +03:00
|
|
|
next_nomacro();
|
2001-11-08 04:12:31 +03:00
|
|
|
redo:
|
2002-11-20 03:29:04 +03:00
|
|
|
switch(tok) {
|
|
|
|
case TOK_DEFINE:
|
2001-11-11 05:53:01 +03:00
|
|
|
next_nomacro();
|
2002-01-13 14:07:19 +03:00
|
|
|
parse_define();
|
2002-11-20 03:29:04 +03:00
|
|
|
break;
|
|
|
|
case TOK_UNDEF:
|
2001-11-11 05:53:01 +03:00
|
|
|
next_nomacro();
|
2002-11-19 00:46:44 +03:00
|
|
|
s = define_find(tok);
|
2001-11-11 05:53:01 +03:00
|
|
|
/* undefine symbol by putting an invalid name */
|
|
|
|
if (s)
|
2002-11-19 00:46:44 +03:00
|
|
|
define_undef(s);
|
2002-11-20 03:29:04 +03:00
|
|
|
break;
|
|
|
|
case TOK_INCLUDE:
|
2002-11-23 21:15:17 +03:00
|
|
|
ch = file->buf_ptr[0];
|
|
|
|
/* XXX: incorrect if comments : use next_nomacro with a special mode */
|
2001-11-06 04:20:20 +03:00
|
|
|
skip_spaces();
|
|
|
|
if (ch == '<') {
|
|
|
|
c = '>';
|
|
|
|
goto read_name;
|
|
|
|
} else if (ch == '\"') {
|
|
|
|
c = ch;
|
|
|
|
read_name:
|
2002-11-23 02:28:06 +03:00
|
|
|
/* XXX: better stray handling */
|
2001-11-06 04:20:20 +03:00
|
|
|
minp();
|
|
|
|
q = buf;
|
2002-08-18 17:22:55 +04:00
|
|
|
while (ch != c && ch != '\n' && ch != CH_EOF) {
|
2001-11-06 04:20:20 +03:00
|
|
|
if ((q - buf) < sizeof(buf) - 1)
|
|
|
|
*q++ = ch;
|
|
|
|
minp();
|
|
|
|
}
|
|
|
|
*q = '\0';
|
2002-11-23 02:28:06 +03:00
|
|
|
minp();
|
2002-11-22 21:12:41 +03:00
|
|
|
#if 0
|
2002-08-18 17:22:55 +04:00
|
|
|
/* eat all spaces and comments after include */
|
|
|
|
/* XXX: slightly incorrect */
|
|
|
|
while (ch1 != '\n' && ch1 != CH_EOF)
|
|
|
|
inp();
|
2002-11-22 21:12:41 +03:00
|
|
|
#endif
|
2001-11-17 20:22:38 +03:00
|
|
|
} else {
|
2002-08-18 17:22:55 +04:00
|
|
|
/* computed #include : either we have only strings or
|
|
|
|
we have anything enclosed in '<>' */
|
2001-11-17 20:22:38 +03:00
|
|
|
next();
|
2002-08-18 17:22:55 +04:00
|
|
|
buf[0] = '\0';
|
|
|
|
if (tok == TOK_STR) {
|
|
|
|
while (tok != TOK_LINEFEED) {
|
|
|
|
if (tok != TOK_STR) {
|
|
|
|
include_syntax:
|
|
|
|
error("'#include' expects \"FILENAME\" or <FILENAME>");
|
|
|
|
}
|
|
|
|
pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
c = '\"';
|
|
|
|
} else {
|
|
|
|
int len;
|
|
|
|
while (tok != TOK_LINEFEED) {
|
|
|
|
pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
len = strlen(buf);
|
|
|
|
/* check syntax and remove '<>' */
|
|
|
|
if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
|
|
|
|
goto include_syntax;
|
|
|
|
memmove(buf, buf + 1, len - 2);
|
|
|
|
buf[len - 2] = '\0';
|
|
|
|
c = '>';
|
|
|
|
}
|
2001-11-06 04:20:20 +03:00
|
|
|
}
|
2001-12-14 02:20:55 +03:00
|
|
|
|
2002-11-02 17:12:32 +03:00
|
|
|
e = search_cached_include(s1, c, buf);
|
2002-11-19 00:46:44 +03:00
|
|
|
if (e && define_find(e->ifndef_macro)) {
|
2002-09-08 23:34:32 +04:00
|
|
|
/* no need to parse the include because the 'ifndef macro'
|
|
|
|
is defined */
|
|
|
|
#ifdef INC_DEBUG
|
|
|
|
printf("%s: skipping %s\n", file->filename, buf);
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
if (c == '\"') {
|
|
|
|
/* first search in current dir if "header.h" */
|
|
|
|
size = 0;
|
|
|
|
p = strrchr(file->filename, '/');
|
|
|
|
if (p)
|
|
|
|
size = p + 1 - file->filename;
|
|
|
|
if (size > sizeof(buf1) - 1)
|
|
|
|
size = sizeof(buf1) - 1;
|
|
|
|
memcpy(buf1, file->filename, size);
|
|
|
|
buf1[size] = '\0';
|
|
|
|
pstrcat(buf1, sizeof(buf1), buf);
|
2002-11-02 17:12:32 +03:00
|
|
|
f = tcc_open(s1, buf1);
|
2002-09-08 23:34:32 +04:00
|
|
|
if (f)
|
|
|
|
goto found;
|
|
|
|
}
|
2002-11-02 17:12:32 +03:00
|
|
|
if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
|
2002-09-08 23:34:32 +04:00
|
|
|
error("#include recursion too deep");
|
|
|
|
/* now search in all the include paths */
|
2002-11-02 17:12:32 +03:00
|
|
|
n = s1->nb_include_paths + s1->nb_sysinclude_paths;
|
2002-09-08 23:34:32 +04:00
|
|
|
for(i = 0; i < n; i++) {
|
|
|
|
const char *path;
|
2002-11-02 17:12:32 +03:00
|
|
|
if (i < s1->nb_include_paths)
|
|
|
|
path = s1->include_paths[i];
|
2002-09-08 23:34:32 +04:00
|
|
|
else
|
2002-11-02 17:12:32 +03:00
|
|
|
path = s1->sysinclude_paths[i - s1->nb_include_paths];
|
2002-09-08 23:34:32 +04:00
|
|
|
pstrcpy(buf1, sizeof(buf1), path);
|
|
|
|
pstrcat(buf1, sizeof(buf1), "/");
|
|
|
|
pstrcat(buf1, sizeof(buf1), buf);
|
2002-11-02 17:12:32 +03:00
|
|
|
f = tcc_open(s1, buf1);
|
2002-09-08 23:34:32 +04:00
|
|
|
if (f)
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
error("include file '%s' not found", buf);
|
|
|
|
f = NULL;
|
|
|
|
found:
|
|
|
|
#ifdef INC_DEBUG
|
|
|
|
printf("%s: including %s\n", file->filename, buf1);
|
|
|
|
#endif
|
|
|
|
f->inc_type = c;
|
|
|
|
pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf);
|
|
|
|
/* push current file in stack */
|
|
|
|
/* XXX: fix current line init */
|
2002-11-02 17:12:32 +03:00
|
|
|
*s1->include_stack_ptr++ = file;
|
2002-09-08 23:34:32 +04:00
|
|
|
file = f;
|
|
|
|
/* add include file debug info */
|
|
|
|
if (do_debug) {
|
|
|
|
put_stabs(file->filename, N_BINCL, 0, 0, 0);
|
|
|
|
}
|
2002-11-23 02:28:06 +03:00
|
|
|
tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
|
|
|
|
ch = file->buf_ptr[0];
|
2002-11-23 01:16:30 +03:00
|
|
|
goto the_end;
|
2002-09-08 23:34:32 +04:00
|
|
|
}
|
2002-11-20 03:29:04 +03:00
|
|
|
break;
|
|
|
|
case TOK_IFNDEF:
|
2001-11-08 04:26:37 +03:00
|
|
|
c = 1;
|
2001-11-08 04:12:31 +03:00
|
|
|
goto do_ifdef;
|
2002-11-20 03:29:04 +03:00
|
|
|
case TOK_IF:
|
2001-11-08 04:12:31 +03:00
|
|
|
c = expr_preprocess();
|
2001-11-08 04:26:37 +03:00
|
|
|
goto do_if;
|
2002-11-20 03:29:04 +03:00
|
|
|
case TOK_IFDEF:
|
2001-11-08 04:26:37 +03:00
|
|
|
c = 0;
|
2001-11-08 04:12:31 +03:00
|
|
|
do_ifdef:
|
2001-11-08 04:26:37 +03:00
|
|
|
next_nomacro();
|
2002-09-08 23:34:32 +04:00
|
|
|
if (tok < TOK_IDENT)
|
|
|
|
error("invalid argument for '#if%sdef'", c ? "n" : "");
|
2002-11-23 01:16:30 +03:00
|
|
|
if (is_bof) {
|
2002-09-08 23:34:32 +04:00
|
|
|
if (c) {
|
2002-11-23 01:16:30 +03:00
|
|
|
#ifdef INC_DEBUG
|
|
|
|
printf("#ifndef %s\n", get_tok_str(tok, NULL));
|
|
|
|
#endif
|
2002-09-08 23:34:32 +04:00
|
|
|
file->ifndef_macro = tok;
|
|
|
|
}
|
|
|
|
}
|
2002-11-19 00:46:44 +03:00
|
|
|
c = (define_find(tok) != 0) ^ c;
|
2001-11-08 04:26:37 +03:00
|
|
|
do_if:
|
2002-11-02 17:12:32 +03:00
|
|
|
if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
|
2001-11-08 04:12:31 +03:00
|
|
|
error("memory full");
|
2002-11-02 17:12:32 +03:00
|
|
|
*s1->ifdef_stack_ptr++ = c;
|
2001-11-08 04:12:31 +03:00
|
|
|
goto test_skip;
|
2002-11-20 03:29:04 +03:00
|
|
|
case TOK_ELSE:
|
2002-11-02 17:12:32 +03:00
|
|
|
if (s1->ifdef_stack_ptr == s1->ifdef_stack)
|
2002-01-26 21:05:29 +03:00
|
|
|
error("#else without matching #if");
|
2002-11-02 17:12:32 +03:00
|
|
|
if (s1->ifdef_stack_ptr[-1] & 2)
|
2001-11-08 04:12:31 +03:00
|
|
|
error("#else after #else");
|
2002-11-02 17:12:32 +03:00
|
|
|
c = (s1->ifdef_stack_ptr[-1] ^= 3);
|
2001-11-08 04:12:31 +03:00
|
|
|
goto test_skip;
|
2002-11-20 03:29:04 +03:00
|
|
|
case TOK_ELIF:
|
2002-11-02 17:12:32 +03:00
|
|
|
if (s1->ifdef_stack_ptr == s1->ifdef_stack)
|
2002-01-26 21:05:29 +03:00
|
|
|
error("#elif without matching #if");
|
2002-11-02 17:12:32 +03:00
|
|
|
c = s1->ifdef_stack_ptr[-1];
|
2002-01-26 21:05:29 +03:00
|
|
|
if (c > 1)
|
2001-11-08 04:12:31 +03:00
|
|
|
error("#elif after #else");
|
2002-01-26 21:05:29 +03:00
|
|
|
/* last #if/#elif expression was true: we skip */
|
|
|
|
if (c == 1)
|
|
|
|
goto skip;
|
2001-11-08 04:12:31 +03:00
|
|
|
c = expr_preprocess();
|
2002-11-02 17:12:32 +03:00
|
|
|
s1->ifdef_stack_ptr[-1] = c;
|
2001-11-08 04:12:31 +03:00
|
|
|
test_skip:
|
|
|
|
if (!(c & 1)) {
|
2002-01-26 21:05:29 +03:00
|
|
|
skip:
|
2001-11-08 04:12:31 +03:00
|
|
|
preprocess_skip();
|
2002-11-23 01:16:30 +03:00
|
|
|
is_bof = 0;
|
2001-11-08 04:12:31 +03:00
|
|
|
goto redo;
|
|
|
|
}
|
2002-11-20 03:29:04 +03:00
|
|
|
break;
|
|
|
|
case TOK_ENDIF:
|
2002-11-02 17:12:32 +03:00
|
|
|
if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
|
2002-01-26 21:05:29 +03:00
|
|
|
error("#endif without matching #if");
|
2002-11-23 01:16:30 +03:00
|
|
|
s1->ifdef_stack_ptr--;
|
|
|
|
/* '#ifndef macro' was at the start of file. Now we check if
|
|
|
|
an '#endif' is exactly at the end of file */
|
2002-09-08 23:34:32 +04:00
|
|
|
if (file->ifndef_macro &&
|
2002-11-23 01:16:30 +03:00
|
|
|
s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
|
|
|
|
file->ifndef_macro_saved = file->ifndef_macro;
|
|
|
|
/* need to set to zero to avoid false matches if another
|
|
|
|
#ifndef at middle of file */
|
|
|
|
file->ifndef_macro = 0;
|
2002-09-08 23:34:32 +04:00
|
|
|
while (tok != TOK_LINEFEED)
|
|
|
|
next_nomacro();
|
2002-11-23 01:16:30 +03:00
|
|
|
tok_flags |= TOK_FLAG_ENDIF;
|
|
|
|
goto the_end;
|
2002-09-08 23:34:32 +04:00
|
|
|
}
|
2002-11-20 03:29:04 +03:00
|
|
|
break;
|
|
|
|
case TOK_LINE:
|
2001-11-17 20:22:38 +03:00
|
|
|
next();
|
2002-01-04 00:14:59 +03:00
|
|
|
if (tok != TOK_CINT)
|
2001-11-17 20:22:38 +03:00
|
|
|
error("#line");
|
2002-11-24 01:35:28 +03:00
|
|
|
file->line_num = tokc.i - 1; /* the line number will be incremented after */
|
2002-08-18 17:22:55 +04:00
|
|
|
next();
|
|
|
|
if (tok != TOK_LINEFEED) {
|
2001-11-17 20:22:38 +03:00
|
|
|
if (tok != TOK_STR)
|
|
|
|
error("#line");
|
2002-01-12 19:39:35 +03:00
|
|
|
pstrcpy(file->filename, sizeof(file->filename),
|
2002-08-18 17:22:55 +04:00
|
|
|
(char *)tokc.cstr->data);
|
2001-11-17 20:22:38 +03:00
|
|
|
}
|
2002-11-20 03:29:04 +03:00
|
|
|
break;
|
|
|
|
case TOK_ERROR:
|
|
|
|
case TOK_WARNING:
|
|
|
|
c = tok;
|
|
|
|
skip_spaces();
|
|
|
|
q = buf;
|
|
|
|
while (ch != '\n' && ch != CH_EOF) {
|
|
|
|
if ((q - buf) < sizeof(buf) - 1)
|
|
|
|
*q++ = ch;
|
|
|
|
minp();
|
|
|
|
}
|
|
|
|
*q = '\0';
|
|
|
|
if (c == TOK_ERROR)
|
|
|
|
error("#error %s", buf);
|
|
|
|
else
|
|
|
|
warning("#warning %s", buf);
|
|
|
|
break;
|
2002-11-24 17:18:17 +03:00
|
|
|
case TOK_PRAGMA:
|
|
|
|
/* ignored */
|
|
|
|
break;
|
2002-11-20 03:29:04 +03:00
|
|
|
default:
|
|
|
|
if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_CINT) {
|
|
|
|
/* '!' is ignored to allow C scripts. numbers are ignored
|
|
|
|
to emulate cpp behaviour */
|
|
|
|
} else {
|
|
|
|
error("invalid preprocessing directive #%s", get_tok_str(tok, &tokc));
|
|
|
|
}
|
|
|
|
break;
|
2001-11-06 04:20:20 +03:00
|
|
|
}
|
|
|
|
/* ignore other preprocess commands or #! for C scripts */
|
2002-11-23 01:16:30 +03:00
|
|
|
while (tok != TOK_LINEFEED)
|
2002-07-27 02:59:19 +04:00
|
|
|
next_nomacro();
|
|
|
|
the_end:
|
|
|
|
return_linefeed = 0;
|
2001-11-06 04:20:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* read a number in base b */
|
2002-07-27 18:05:37 +04:00
|
|
|
static int getn(int b)
|
2001-11-06 04:20:20 +03:00
|
|
|
{
|
|
|
|
int n, t;
|
|
|
|
n = 0;
|
|
|
|
while (1) {
|
2002-01-04 00:14:59 +03:00
|
|
|
if (ch >= 'a' && ch <= 'f')
|
2001-11-06 04:20:20 +03:00
|
|
|
t = ch - 'a' + 10;
|
2002-01-04 00:14:59 +03:00
|
|
|
else if (ch >= 'A' && ch <= 'F')
|
2001-11-06 04:20:20 +03:00
|
|
|
t = ch - 'A' + 10;
|
|
|
|
else if (isnum(ch))
|
|
|
|
t = ch - '0';
|
|
|
|
else
|
|
|
|
break;
|
2002-01-04 00:14:59 +03:00
|
|
|
if (t < 0 || t >= b)
|
2001-11-06 04:20:20 +03:00
|
|
|
break;
|
|
|
|
n = n * b + t;
|
2002-11-22 21:12:41 +03:00
|
|
|
inp();
|
2001-11-06 04:20:20 +03:00
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
2001-11-01 17:48:10 +03:00
|
|
|
|
2001-11-11 05:53:01 +03:00
|
|
|
/* read a character for string or char constant and eval escape codes */
|
2002-07-27 18:05:37 +04:00
|
|
|
static int getq(void)
|
2001-10-28 02:48:39 +03:00
|
|
|
{
|
2001-11-11 05:53:01 +03:00
|
|
|
int c;
|
2001-10-28 02:48:39 +03:00
|
|
|
|
2002-11-22 21:12:41 +03:00
|
|
|
redo:
|
2001-11-11 05:53:01 +03:00
|
|
|
c = ch;
|
2002-11-22 21:12:41 +03:00
|
|
|
inp();
|
2001-11-11 05:53:01 +03:00
|
|
|
if (c == '\\') {
|
2002-11-22 21:12:41 +03:00
|
|
|
switch(ch) {
|
|
|
|
case '0': case '1': case '2': case '3':
|
|
|
|
case '4': case '5': case '6': case '7':
|
2001-11-17 20:22:38 +03:00
|
|
|
/* at most three octal digits */
|
|
|
|
c = ch - '0';
|
2002-11-22 21:12:41 +03:00
|
|
|
inp();
|
2002-08-31 16:43:39 +04:00
|
|
|
if (isoct(ch)) {
|
2001-11-17 20:22:38 +03:00
|
|
|
c = c * 8 + ch - '0';
|
2002-11-22 21:12:41 +03:00
|
|
|
inp();
|
2002-08-31 16:43:39 +04:00
|
|
|
if (isoct(ch)) {
|
2001-11-17 20:22:38 +03:00
|
|
|
c = c * 8 + ch - '0';
|
2002-11-22 21:12:41 +03:00
|
|
|
inp();
|
2001-11-17 20:22:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return c;
|
2002-11-22 21:12:41 +03:00
|
|
|
case 'x':
|
|
|
|
inp();
|
2001-11-17 20:22:38 +03:00
|
|
|
return getn(16);
|
2002-11-22 21:12:41 +03:00
|
|
|
case 'a':
|
|
|
|
c = '\a';
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
c = '\b';
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
c = '\f';
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
c = '\n';
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
c = '\r';
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
c = '\t';
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
c = '\v';
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
if (!gnu_ext)
|
|
|
|
goto invalid_escape;
|
|
|
|
c = 27;
|
|
|
|
break;
|
|
|
|
case '\'':
|
|
|
|
case '\"':
|
|
|
|
case '\\':
|
|
|
|
case '?':
|
|
|
|
c = ch;
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
inp();
|
|
|
|
goto redo;
|
|
|
|
case '\r':
|
|
|
|
inp();
|
|
|
|
if (ch != '\n')
|
|
|
|
goto invalid_escape;
|
|
|
|
inp();
|
|
|
|
goto redo;
|
|
|
|
default:
|
|
|
|
invalid_escape:
|
|
|
|
error("invalid escaped char");
|
2001-11-11 05:53:01 +03:00
|
|
|
}
|
2002-11-22 21:12:41 +03:00
|
|
|
inp();
|
2002-08-30 01:16:24 +04:00
|
|
|
} else if (c == '\r' && ch == '\n') {
|
2002-11-22 21:12:41 +03:00
|
|
|
inp();
|
2002-08-30 01:16:24 +04:00
|
|
|
c = '\n';
|
2001-10-31 01:43:19 +03:00
|
|
|
}
|
2001-11-11 05:53:01 +03:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2001-12-14 01:28:53 +03: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-02 20:02:31 +03:00
|
|
|
/* parse number in null terminated string 'p' and return it in the
|
|
|
|
current token */
|
|
|
|
void parse_number(const char *p)
|
2001-12-14 01:28:53 +03:00
|
|
|
{
|
2002-11-02 20:02:31 +03:00
|
|
|
int b, t, shift, frac_bits, s, exp_val, ch;
|
2001-12-14 01:28:53 +03:00
|
|
|
char *q;
|
|
|
|
unsigned int bn[BN_SIZE];
|
|
|
|
double d;
|
|
|
|
|
|
|
|
/* number */
|
|
|
|
q = token_buf;
|
2002-11-02 20:02:31 +03:00
|
|
|
ch = *p++;
|
2001-12-14 01:28:53 +03:00
|
|
|
t = ch;
|
2002-11-02 20:02:31 +03:00
|
|
|
ch = *p++;
|
2001-12-14 01:28:53 +03:00
|
|
|
*q++ = t;
|
|
|
|
b = 10;
|
|
|
|
if (t == '.') {
|
2002-11-02 20:02:31 +03:00
|
|
|
goto float_frac_parse;
|
2001-12-14 01:28:53 +03:00
|
|
|
} else if (t == '0') {
|
|
|
|
if (ch == 'x' || ch == 'X') {
|
|
|
|
q--;
|
2002-11-02 20:02:31 +03:00
|
|
|
ch = *p++;
|
2001-12-14 01:28:53 +03:00
|
|
|
b = 16;
|
|
|
|
} else if (tcc_ext && (ch == 'b' || ch == 'B')) {
|
|
|
|
q--;
|
2002-11-02 20:02:31 +03:00
|
|
|
ch = *p++;
|
2001-12-14 01:28:53 +03:00
|
|
|
b = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* parse all digits. cannot check octal numbers at this stage
|
|
|
|
because of floating point constants */
|
|
|
|
while (1) {
|
2002-01-04 00:14:59 +03:00
|
|
|
if (ch >= 'a' && ch <= 'f')
|
2001-12-14 01:28:53 +03:00
|
|
|
t = ch - 'a' + 10;
|
2002-01-04 00:14:59 +03:00
|
|
|
else if (ch >= 'A' && ch <= 'F')
|
2001-12-14 01:28:53 +03: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;
|
2002-11-02 20:02:31 +03:00
|
|
|
ch = *p++;
|
2001-12-14 01:28:53 +03:00
|
|
|
}
|
2002-01-04 00:14:59 +03:00
|
|
|
if (ch == '.' ||
|
2001-12-14 01:28:53 +03: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 == '.') {
|
2002-11-02 20:02:31 +03:00
|
|
|
ch = *p++;
|
2001-12-14 01:28:53 +03:00
|
|
|
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;
|
2002-11-02 20:02:31 +03:00
|
|
|
ch = *p++;
|
2001-12-14 01:28:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ch != 'p' && ch != 'P')
|
2002-11-22 17:43:38 +03:00
|
|
|
expect("exponent");
|
2002-11-02 20:02:31 +03:00
|
|
|
ch = *p++;
|
2001-12-14 01:28:53 +03:00
|
|
|
s = 1;
|
|
|
|
exp_val = 0;
|
|
|
|
if (ch == '+') {
|
2002-11-02 20:02:31 +03:00
|
|
|
ch = *p++;
|
2001-12-14 01:28:53 +03:00
|
|
|
} else if (ch == '-') {
|
|
|
|
s = -1;
|
2002-11-02 20:02:31 +03:00
|
|
|
ch = *p++;
|
2001-12-14 01:28:53 +03:00
|
|
|
}
|
|
|
|
if (ch < '0' || ch > '9')
|
2002-11-22 17:43:38 +03:00
|
|
|
expect("exponent digits");
|
2001-12-14 01:28:53 +03:00
|
|
|
while (ch >= '0' && ch <= '9') {
|
|
|
|
exp_val = exp_val * 10 + ch - '0';
|
2002-11-02 20:02:31 +03:00
|
|
|
ch = *p++;
|
2001-12-14 01:28:53 +03:00
|
|
|
}
|
|
|
|
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') {
|
2002-11-02 20:02:31 +03:00
|
|
|
ch = *p++;
|
2001-12-14 01:28:53 +03:00
|
|
|
tok = TOK_CFLOAT;
|
|
|
|
/* float : should handle overflow */
|
2001-12-18 00:56:48 +03:00
|
|
|
tokc.f = (float)d;
|
2001-12-14 01:28:53 +03:00
|
|
|
} else if (t == 'L') {
|
2002-11-02 20:02:31 +03:00
|
|
|
ch = *p++;
|
2001-12-14 01:28:53 +03:00
|
|
|
tok = TOK_CLDOUBLE;
|
|
|
|
/* XXX: not large enough */
|
2001-12-18 00:56:48 +03:00
|
|
|
tokc.ld = (long double)d;
|
2001-12-14 01:28:53 +03:00
|
|
|
} else {
|
|
|
|
tok = TOK_CDOUBLE;
|
2001-12-18 00:56:48 +03:00
|
|
|
tokc.d = d;
|
2001-12-14 01:28:53 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* decimal floats */
|
|
|
|
if (ch == '.') {
|
|
|
|
if (q >= token_buf + STRING_MAX_SIZE)
|
|
|
|
goto num_too_long;
|
|
|
|
*q++ = ch;
|
2002-11-02 20:02:31 +03:00
|
|
|
ch = *p++;
|
2001-12-14 01:28:53 +03:00
|
|
|
float_frac_parse:
|
|
|
|
while (ch >= '0' && ch <= '9') {
|
|
|
|
if (q >= token_buf + STRING_MAX_SIZE)
|
|
|
|
goto num_too_long;
|
|
|
|
*q++ = ch;
|
2002-11-02 20:02:31 +03:00
|
|
|
ch = *p++;
|
2001-12-14 01:28:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ch == 'e' || ch == 'E') {
|
|
|
|
if (q >= token_buf + STRING_MAX_SIZE)
|
|
|
|
goto num_too_long;
|
|
|
|
*q++ = ch;
|
2002-11-02 20:02:31 +03:00
|
|
|
ch = *p++;
|
2001-12-14 01:28:53 +03:00
|
|
|
if (ch == '-' || ch == '+') {
|
|
|
|
if (q >= token_buf + STRING_MAX_SIZE)
|
|
|
|
goto num_too_long;
|
|
|
|
*q++ = ch;
|
2002-11-02 20:02:31 +03:00
|
|
|
ch = *p++;
|
2001-12-14 01:28:53 +03:00
|
|
|
}
|
|
|
|
if (ch < '0' || ch > '9')
|
2002-11-22 17:43:38 +03:00
|
|
|
expect("exponent digits");
|
2001-12-14 01:28:53 +03:00
|
|
|
while (ch >= '0' && ch <= '9') {
|
|
|
|
if (q >= token_buf + STRING_MAX_SIZE)
|
|
|
|
goto num_too_long;
|
|
|
|
*q++ = ch;
|
2002-11-02 20:02:31 +03:00
|
|
|
ch = *p++;
|
2001-12-14 01:28:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*q = '\0';
|
|
|
|
t = toup(ch);
|
|
|
|
errno = 0;
|
|
|
|
if (t == 'F') {
|
2002-11-02 20:02:31 +03:00
|
|
|
ch = *p++;
|
2001-12-14 01:28:53 +03:00
|
|
|
tok = TOK_CFLOAT;
|
2001-12-18 00:56:48 +03:00
|
|
|
tokc.f = strtof(token_buf, NULL);
|
2001-12-14 01:28:53 +03:00
|
|
|
} else if (t == 'L') {
|
2002-11-02 20:02:31 +03:00
|
|
|
ch = *p++;
|
2001-12-14 01:28:53 +03:00
|
|
|
tok = TOK_CLDOUBLE;
|
2001-12-18 00:56:48 +03:00
|
|
|
tokc.ld = strtold(token_buf, NULL);
|
2001-12-14 01:28:53 +03:00
|
|
|
} else {
|
|
|
|
tok = TOK_CDOUBLE;
|
2001-12-18 00:56:48 +03:00
|
|
|
tokc.d = strtod(token_buf, NULL);
|
2001-12-14 01:28:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2002-01-04 00:14:59 +03:00
|
|
|
unsigned long long n, n1;
|
2002-11-02 20:02:31 +03:00
|
|
|
int lcount, ucount;
|
2002-01-04 00:14:59 +03:00
|
|
|
|
2001-12-14 01:28:53 +03: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 */
|
2002-11-02 20:02:31 +03:00
|
|
|
/* XXX: this test is not reliable */
|
2001-12-14 01:28:53 +03:00
|
|
|
if (n < n1)
|
|
|
|
error("integer constant overflow");
|
|
|
|
}
|
2002-01-04 00:14:59 +03: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;
|
2002-11-02 20:02:31 +03:00
|
|
|
ucount = 0;
|
2002-01-04 00:14:59 +03:00
|
|
|
for(;;) {
|
|
|
|
t = toup(ch);
|
|
|
|
if (t == 'L') {
|
|
|
|
if (lcount >= 2)
|
2002-11-02 20:02:31 +03:00
|
|
|
error("three 'l's in integer constant");
|
2002-01-04 00:14:59 +03:00
|
|
|
lcount++;
|
|
|
|
if (lcount == 2) {
|
|
|
|
if (tok == TOK_CINT)
|
|
|
|
tok = TOK_CLLONG;
|
|
|
|
else if (tok == TOK_CUINT)
|
|
|
|
tok = TOK_CULLONG;
|
|
|
|
}
|
2002-11-02 20:02:31 +03:00
|
|
|
ch = *p++;
|
2002-01-04 00:14:59 +03:00
|
|
|
} else if (t == 'U') {
|
2002-11-02 20:02:31 +03:00
|
|
|
if (ucount >= 1)
|
|
|
|
error("two 'u's in integer constant");
|
|
|
|
ucount++;
|
2002-01-04 00:14:59 +03:00
|
|
|
if (tok == TOK_CINT)
|
|
|
|
tok = TOK_CUINT;
|
|
|
|
else if (tok == TOK_CLLONG)
|
|
|
|
tok = TOK_CULLONG;
|
2002-11-02 20:02:31 +03:00
|
|
|
ch = *p++;
|
2002-01-04 00:14:59 +03:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tok == TOK_CINT || tok == TOK_CUINT)
|
|
|
|
tokc.ui = n;
|
|
|
|
else
|
|
|
|
tokc.ull = n;
|
2001-12-14 01:28:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-23 21:15:17 +03:00
|
|
|
|
|
|
|
#define PARSE2(c1, tok1, c2, tok2) \
|
|
|
|
case c1: \
|
|
|
|
PEEKC(c, p); \
|
|
|
|
if (c == c2) { \
|
|
|
|
p++; \
|
|
|
|
tok = tok2; \
|
|
|
|
} else { \
|
|
|
|
tok = tok1; \
|
|
|
|
} \
|
|
|
|
break;
|
|
|
|
|
2001-11-11 05:53:01 +03:00
|
|
|
/* return next token without macro substitution */
|
2002-09-08 23:34:32 +04:00
|
|
|
static inline void next_nomacro1(void)
|
2001-11-11 05:53:01 +03:00
|
|
|
{
|
2002-11-23 21:15:17 +03:00
|
|
|
int b, t, c;
|
2001-11-11 05:53:01 +03:00
|
|
|
TokenSym *ts;
|
2002-11-23 21:15:17 +03:00
|
|
|
uint8_t *p, *p1;
|
2002-11-24 01:02:40 +03:00
|
|
|
unsigned int h;
|
2001-11-11 05:53:01 +03:00
|
|
|
|
2002-11-23 21:15:17 +03:00
|
|
|
p = file->buf_ptr;
|
2002-11-22 16:37:52 +03:00
|
|
|
redo_no_start:
|
2002-11-23 21:15:17 +03:00
|
|
|
c = *p;
|
|
|
|
switch(c) {
|
2002-11-22 16:37:52 +03:00
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\f':
|
|
|
|
case '\v':
|
|
|
|
case '\r':
|
2002-11-23 21:15:17 +03:00
|
|
|
p++;
|
2002-11-22 16:37:52 +03:00
|
|
|
goto redo_no_start;
|
|
|
|
|
2002-11-22 21:12:41 +03:00
|
|
|
case '\\':
|
2002-11-23 02:28:06 +03:00
|
|
|
/* first look if it is in fact an end of buffer */
|
2002-11-23 21:15:17 +03:00
|
|
|
if (p >= file->buf_end) {
|
|
|
|
file->buf_ptr = p;
|
|
|
|
handle_eob();
|
|
|
|
p = file->buf_ptr;
|
|
|
|
if (p >= file->buf_end)
|
|
|
|
goto parse_eof;
|
|
|
|
else
|
|
|
|
goto redo_no_start;
|
|
|
|
} else {
|
|
|
|
file->buf_ptr = p;
|
|
|
|
ch = *p;
|
|
|
|
handle_stray();
|
|
|
|
p = file->buf_ptr;
|
2002-11-23 02:28:06 +03:00
|
|
|
goto redo_no_start;
|
2002-11-23 21:15:17 +03:00
|
|
|
}
|
|
|
|
parse_eof:
|
2002-11-23 01:16:30 +03:00
|
|
|
{
|
|
|
|
TCCState *s1 = tcc_state;
|
|
|
|
|
|
|
|
if (return_linefeed) {
|
|
|
|
tok = TOK_LINEFEED;
|
|
|
|
} else if (s1->include_stack_ptr == s1->include_stack) {
|
|
|
|
/* no include left : end of file */
|
|
|
|
tok = TOK_EOF;
|
|
|
|
} else {
|
|
|
|
/* pop include file */
|
|
|
|
|
|
|
|
/* test if previous '#endif' was after a #ifdef at
|
|
|
|
start of file */
|
|
|
|
if (tok_flags & TOK_FLAG_ENDIF) {
|
|
|
|
#ifdef INC_DEBUG
|
|
|
|
printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
|
|
|
|
#endif
|
|
|
|
add_cached_include(s1, file->inc_type, file->inc_filename,
|
|
|
|
file->ifndef_macro_saved);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add end of include file debug info */
|
|
|
|
if (do_debug) {
|
|
|
|
put_stabd(N_EINCL, 0, 0);
|
|
|
|
}
|
|
|
|
/* pop include stack */
|
|
|
|
tcc_close(file);
|
|
|
|
s1->include_stack_ptr--;
|
|
|
|
file = *s1->include_stack_ptr;
|
2002-11-23 21:15:17 +03:00
|
|
|
p = file->buf_ptr;
|
2002-11-23 01:16:30 +03:00
|
|
|
goto redo_no_start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2002-11-22 16:37:52 +03:00
|
|
|
case '\n':
|
|
|
|
if (return_linefeed) {
|
|
|
|
tok = TOK_LINEFEED;
|
|
|
|
} else {
|
2002-11-24 01:02:40 +03:00
|
|
|
file->line_num++;
|
2002-11-23 01:16:30 +03:00
|
|
|
tok_flags |= TOK_FLAG_BOL;
|
2002-11-23 21:15:17 +03:00
|
|
|
p++;
|
2002-11-22 16:37:52 +03:00
|
|
|
goto redo_no_start;
|
2001-10-28 18:20:12 +03:00
|
|
|
}
|
2002-11-22 16:37:52 +03:00
|
|
|
break;
|
2002-11-20 03:29:04 +03:00
|
|
|
|
|
|
|
case '#':
|
2002-11-23 21:15:17 +03:00
|
|
|
/* XXX: simplify */
|
|
|
|
PEEKC(c, p);
|
2002-11-23 01:16:30 +03:00
|
|
|
if (tok_flags & TOK_FLAG_BOL) {
|
2002-11-23 21:15:17 +03:00
|
|
|
file->buf_ptr = p;
|
2002-11-23 01:16:30 +03:00
|
|
|
preprocess(tok_flags & TOK_FLAG_BOF);
|
2002-11-23 21:15:17 +03:00
|
|
|
p = file->buf_ptr;
|
2002-11-20 03:29:04 +03:00
|
|
|
goto redo_no_start;
|
2002-11-22 21:12:41 +03:00
|
|
|
} else {
|
2002-11-23 21:15:17 +03:00
|
|
|
if (c == '#') {
|
|
|
|
p++;
|
2002-11-20 03:29:04 +03:00
|
|
|
tok = TOK_TWOSHARPS;
|
2002-11-22 21:12:41 +03:00
|
|
|
} else {
|
|
|
|
tok = '#';
|
2001-11-29 01:48:43 +03:00
|
|
|
}
|
2001-11-17 20:22:38 +03:00
|
|
|
}
|
2002-11-20 03:29:04 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'a': case 'b': case 'c': case 'd':
|
|
|
|
case 'e': case 'f': case 'g': case 'h':
|
|
|
|
case 'i': case 'j': case 'k': case 'l':
|
|
|
|
case 'm': case 'n': case 'o': case 'p':
|
|
|
|
case 'q': case 'r': case 's': case 't':
|
|
|
|
case 'u': case 'v': case 'w': case 'x':
|
|
|
|
case 'y': case 'z':
|
|
|
|
case 'A': case 'B': case 'C': case 'D':
|
|
|
|
case 'E': case 'F': case 'G': case 'H':
|
|
|
|
case 'I': case 'J': case 'K':
|
|
|
|
case 'M': case 'N': case 'O': case 'P':
|
|
|
|
case 'Q': case 'R': case 'S': case 'T':
|
|
|
|
case 'U': case 'V': case 'W': case 'X':
|
|
|
|
case 'Y': case 'Z':
|
|
|
|
case '_':
|
2002-11-23 21:15:17 +03:00
|
|
|
parse_ident_fast:
|
|
|
|
p1 = p;
|
2002-11-24 01:02:40 +03:00
|
|
|
h = TOK_HASH_INIT;
|
|
|
|
h = TOK_HASH_FUNC(h, c);
|
2002-11-23 21:15:17 +03:00
|
|
|
p++;
|
|
|
|
for(;;) {
|
|
|
|
c = *p;
|
|
|
|
if (!isid(c) && !isnum(c))
|
|
|
|
break;
|
2002-11-24 01:02:40 +03:00
|
|
|
h = TOK_HASH_FUNC(h, c);
|
2002-11-23 21:15:17 +03:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
if (c != '\\') {
|
2002-11-24 01:02:40 +03:00
|
|
|
TokenSym **pts;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
/* fast case : no stray found, so we have the full token
|
|
|
|
and we have already hashed it */
|
|
|
|
len = p - p1;
|
|
|
|
h &= (TOK_HASH_SIZE - 1);
|
|
|
|
pts = &hash_ident[h];
|
|
|
|
for(;;) {
|
|
|
|
ts = *pts;
|
|
|
|
if (!ts)
|
|
|
|
break;
|
|
|
|
if (ts->len == len && !memcmp(ts->str, p1, len))
|
|
|
|
goto token_found;
|
|
|
|
pts = &(ts->hash_next);
|
|
|
|
}
|
|
|
|
ts = tok_alloc_new(pts, p1, len);
|
|
|
|
token_found: ;
|
2002-11-23 21:15:17 +03:00
|
|
|
} else {
|
|
|
|
/* slower case */
|
|
|
|
cstr_reset(&tokcstr);
|
|
|
|
|
|
|
|
while (p1 < p) {
|
|
|
|
cstr_ccat(&tokcstr, *p1);
|
|
|
|
p1++;
|
|
|
|
}
|
|
|
|
p--;
|
|
|
|
PEEKC(c, p);
|
|
|
|
parse_ident_slow:
|
|
|
|
while (isid(c) || isnum(c)) {
|
|
|
|
cstr_ccat(&tokcstr, c);
|
|
|
|
PEEKC(c, p);
|
|
|
|
}
|
|
|
|
ts = tok_alloc(tokcstr.data, tokcstr.size);
|
2001-10-28 02:48:39 +03:00
|
|
|
}
|
2001-11-11 05:53:01 +03:00
|
|
|
tok = ts->tok;
|
2002-11-20 03:29:04 +03:00
|
|
|
break;
|
|
|
|
case 'L':
|
2002-11-24 01:02:40 +03:00
|
|
|
t = p[1];
|
|
|
|
if (t != '\\' && t != '\'' && t != '\"') {
|
2002-11-23 21:15:17 +03:00
|
|
|
/* fast case */
|
|
|
|
goto parse_ident_fast;
|
|
|
|
} else {
|
|
|
|
PEEKC(c, p);
|
|
|
|
if (c == '\'') {
|
|
|
|
tok = TOK_LCHAR;
|
|
|
|
goto char_const;
|
|
|
|
} else if (c == '\"') {
|
|
|
|
tok = TOK_LSTR;
|
|
|
|
goto str_const;
|
|
|
|
} else {
|
|
|
|
cstr_reset(&tokcstr);
|
|
|
|
cstr_ccat(&tokcstr, 'L');
|
|
|
|
goto parse_ident_slow;
|
|
|
|
}
|
2002-11-20 03:29:04 +03:00
|
|
|
}
|
2002-11-23 21:15:17 +03:00
|
|
|
break;
|
2002-11-20 03:29:04 +03:00
|
|
|
case '0': case '1': case '2': case '3':
|
|
|
|
case '4': case '5': case '6': case '7':
|
|
|
|
case '8': case '9':
|
|
|
|
|
2002-11-02 20:02:31 +03:00
|
|
|
cstr_reset(&tokcstr);
|
|
|
|
/* after the first digit, accept digits, alpha, '.' or sign if
|
|
|
|
prefixed by 'eEpP' */
|
|
|
|
parse_num:
|
|
|
|
for(;;) {
|
2002-11-23 21:15:17 +03:00
|
|
|
t = c;
|
|
|
|
cstr_ccat(&tokcstr, c);
|
|
|
|
PEEKC(c, p);
|
|
|
|
if (!(isnum(c) || isid(c) || c == '.' ||
|
|
|
|
((c == '+' || c == '-') &&
|
2002-11-02 20:02:31 +03:00
|
|
|
(t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* We add a trailing '\0' to ease parsing */
|
|
|
|
cstr_ccat(&tokcstr, '\0');
|
|
|
|
tokc.cstr = &tokcstr;
|
|
|
|
tok = TOK_PPNUM;
|
2002-11-20 03:29:04 +03:00
|
|
|
break;
|
|
|
|
case '.':
|
2002-11-02 20:02:31 +03:00
|
|
|
/* special dot handling because it can also start a number */
|
2002-11-23 21:15:17 +03:00
|
|
|
PEEKC(c, p);
|
|
|
|
if (isnum(c)) {
|
2002-11-02 20:02:31 +03:00
|
|
|
cstr_reset(&tokcstr);
|
|
|
|
cstr_ccat(&tokcstr, '.');
|
|
|
|
goto parse_num;
|
2002-11-23 21:15:17 +03:00
|
|
|
} else if (c == '.') {
|
|
|
|
PEEKC(c, p);
|
|
|
|
if (c != '.')
|
2002-11-02 20:02:31 +03:00
|
|
|
expect("'.'");
|
2002-11-23 21:15:17 +03:00
|
|
|
PEEKC(c, p);
|
2002-11-02 20:02:31 +03:00
|
|
|
tok = TOK_DOTS;
|
|
|
|
} else {
|
|
|
|
tok = '.';
|
|
|
|
}
|
2002-11-20 03:29:04 +03:00
|
|
|
break;
|
|
|
|
case '\'':
|
2001-11-29 01:48:43 +03:00
|
|
|
tok = TOK_CCHAR;
|
2001-11-17 20:22:38 +03:00
|
|
|
char_const:
|
2002-11-23 21:15:17 +03:00
|
|
|
file->buf_ptr = p;
|
2002-11-22 21:12:41 +03:00
|
|
|
inp();
|
2002-07-27 18:05:37 +04:00
|
|
|
b = getq();
|
|
|
|
/* this cast is needed if >= 128 */
|
|
|
|
if (tok == TOK_CCHAR)
|
|
|
|
b = (char)b;
|
|
|
|
tokc.i = b;
|
2001-11-11 05:53:01 +03:00
|
|
|
if (ch != '\'')
|
2002-11-22 17:43:38 +03:00
|
|
|
error("unterminated character constant");
|
2002-11-23 21:15:17 +03:00
|
|
|
p = file->buf_ptr;
|
|
|
|
p++;
|
2002-11-20 03:29:04 +03:00
|
|
|
break;
|
|
|
|
case '\"':
|
2001-11-29 01:48:43 +03:00
|
|
|
tok = TOK_STR;
|
2001-11-17 20:22:38 +03:00
|
|
|
str_const:
|
2002-11-23 21:15:17 +03:00
|
|
|
file->buf_ptr = p;
|
2002-11-22 21:12:41 +03:00
|
|
|
inp();
|
2002-08-18 17:22:55 +04:00
|
|
|
cstr_reset(&tokcstr);
|
2001-11-11 05:53:01 +03:00
|
|
|
while (ch != '\"') {
|
|
|
|
b = getq();
|
2002-08-18 17:22:55 +04:00
|
|
|
if (ch == CH_EOF)
|
2001-11-11 05:53:01 +03:00
|
|
|
error("unterminated string");
|
2002-08-18 17:22:55 +04:00
|
|
|
if (tok == TOK_STR)
|
|
|
|
cstr_ccat(&tokcstr, b);
|
|
|
|
else
|
|
|
|
cstr_wccat(&tokcstr, b);
|
2001-11-11 05:53:01 +03:00
|
|
|
}
|
2002-08-18 17:22:55 +04:00
|
|
|
if (tok == TOK_STR)
|
|
|
|
cstr_ccat(&tokcstr, '\0');
|
|
|
|
else
|
|
|
|
cstr_wccat(&tokcstr, '\0');
|
|
|
|
tokc.cstr = &tokcstr;
|
2002-11-23 21:15:17 +03:00
|
|
|
p = file->buf_ptr;
|
|
|
|
p++;
|
2002-11-20 03:29:04 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '<':
|
2002-11-23 21:15:17 +03:00
|
|
|
PEEKC(c, p);
|
|
|
|
if (c == '=') {
|
|
|
|
p++;
|
2002-11-20 03:29:04 +03:00
|
|
|
tok = TOK_LE;
|
2002-11-23 21:15:17 +03:00
|
|
|
} else if (c == '<') {
|
|
|
|
PEEKC(c, p);
|
|
|
|
if (c == '=') {
|
|
|
|
p++;
|
2002-11-20 03:29:04 +03:00
|
|
|
tok = TOK_A_SHL;
|
|
|
|
} else {
|
|
|
|
tok = TOK_SHL;
|
2001-10-28 02:48:39 +03:00
|
|
|
}
|
2002-11-20 03:29:04 +03:00
|
|
|
} else {
|
2001-10-28 02:48:39 +03:00
|
|
|
tok = TOK_LT;
|
2002-11-20 03:29:04 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '>':
|
2002-11-23 21:15:17 +03:00
|
|
|
PEEKC(c, p);
|
|
|
|
if (c == '=') {
|
|
|
|
p++;
|
2002-11-20 03:29:04 +03:00
|
|
|
tok = TOK_GE;
|
2002-11-23 21:15:17 +03:00
|
|
|
} else if (c == '>') {
|
|
|
|
PEEKC(c, p);
|
|
|
|
if (c == '=') {
|
|
|
|
p++;
|
2002-11-20 03:29:04 +03:00
|
|
|
tok = TOK_A_SAR;
|
|
|
|
} else {
|
|
|
|
tok = TOK_SAR;
|
|
|
|
}
|
|
|
|
} else {
|
2001-10-28 02:48:39 +03:00
|
|
|
tok = TOK_GT;
|
2002-11-20 03:29:04 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '&':
|
2002-11-23 21:15:17 +03:00
|
|
|
PEEKC(c, p);
|
|
|
|
if (c == '&') {
|
|
|
|
p++;
|
2002-11-20 03:29:04 +03:00
|
|
|
tok = TOK_LAND;
|
2002-11-23 21:15:17 +03:00
|
|
|
} else if (c == '=') {
|
|
|
|
p++;
|
2002-11-20 03:29:04 +03:00
|
|
|
tok = TOK_A_AND;
|
2002-11-23 21:15:17 +03:00
|
|
|
} else {
|
|
|
|
tok = '&';
|
2002-11-20 03:29:04 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '|':
|
2002-11-23 21:15:17 +03:00
|
|
|
PEEKC(c, p);
|
|
|
|
if (c == '|') {
|
|
|
|
p++;
|
2002-11-20 03:29:04 +03:00
|
|
|
tok = TOK_LOR;
|
2002-11-23 21:15:17 +03:00
|
|
|
} else if (c == '=') {
|
|
|
|
p++;
|
2002-11-20 03:29:04 +03:00
|
|
|
tok = TOK_A_OR;
|
2002-11-23 21:15:17 +03:00
|
|
|
} else {
|
|
|
|
tok = '|';
|
2002-11-20 03:29:04 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '+':
|
2002-11-23 21:15:17 +03:00
|
|
|
PEEKC(c, p);
|
|
|
|
if (c == '+') {
|
|
|
|
p++;
|
2002-11-20 03:29:04 +03:00
|
|
|
tok = TOK_INC;
|
2002-11-23 21:15:17 +03:00
|
|
|
} else if (c == '=') {
|
|
|
|
p++;
|
2002-11-20 03:29:04 +03:00
|
|
|
tok = TOK_A_ADD;
|
2002-11-23 21:15:17 +03:00
|
|
|
} else {
|
|
|
|
tok = '+';
|
2002-11-20 03:29:04 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '-':
|
2002-11-23 21:15:17 +03:00
|
|
|
PEEKC(c, p);
|
|
|
|
if (c == '-') {
|
|
|
|
p++;
|
2002-11-20 03:29:04 +03:00
|
|
|
tok = TOK_DEC;
|
2002-11-23 21:15:17 +03:00
|
|
|
} else if (c == '=') {
|
|
|
|
p++;
|
2002-11-20 03:29:04 +03:00
|
|
|
tok = TOK_A_SUB;
|
2002-11-23 21:15:17 +03:00
|
|
|
} else if (c == '>') {
|
|
|
|
p++;
|
2002-11-20 03:29:04 +03:00
|
|
|
tok = TOK_ARROW;
|
2002-11-23 21:15:17 +03:00
|
|
|
} else {
|
|
|
|
tok = '-';
|
2002-11-20 03:29:04 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2002-11-23 21:15:17 +03:00
|
|
|
PARSE2('!', '!', '=', TOK_NE)
|
|
|
|
PARSE2('=', '=', '=', TOK_EQ)
|
|
|
|
PARSE2('*', '*', '=', TOK_A_MUL)
|
|
|
|
PARSE2('%', '%', '=', TOK_A_MOD)
|
|
|
|
PARSE2('^', '^', '=', TOK_A_XOR)
|
2002-11-20 03:29:04 +03:00
|
|
|
|
|
|
|
/* comments or operator */
|
|
|
|
case '/':
|
2002-11-23 21:15:17 +03:00
|
|
|
PEEKC(c, p);
|
|
|
|
if (c == '*') {
|
|
|
|
file->buf_ptr = p;
|
2002-11-22 21:12:41 +03:00
|
|
|
parse_comment();
|
2002-11-23 21:15:17 +03:00
|
|
|
p = file->buf_ptr;
|
2002-11-22 21:12:41 +03:00
|
|
|
goto redo_no_start;
|
2002-11-23 21:15:17 +03:00
|
|
|
} else if (c == '/') {
|
|
|
|
file->buf_ptr = p;
|
2002-11-22 21:12:41 +03:00
|
|
|
parse_line_comment();
|
2002-11-23 21:15:17 +03:00
|
|
|
p = file->buf_ptr;
|
2002-11-22 21:12:41 +03:00
|
|
|
goto redo_no_start;
|
2002-11-23 21:15:17 +03:00
|
|
|
} else if (c == '=') {
|
|
|
|
p++;
|
2002-11-20 03:29:04 +03:00
|
|
|
tok = TOK_A_DIV;
|
2002-11-22 21:12:41 +03:00
|
|
|
} else {
|
|
|
|
tok = '/';
|
2002-11-20 03:29:04 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* simple tokens */
|
|
|
|
case '(':
|
|
|
|
case ')':
|
|
|
|
case '[':
|
|
|
|
case ']':
|
|
|
|
case '{':
|
|
|
|
case '}':
|
|
|
|
case ',':
|
|
|
|
case ';':
|
|
|
|
case ':':
|
|
|
|
case '?':
|
|
|
|
case '~':
|
2002-11-23 21:15:17 +03:00
|
|
|
tok = c;
|
|
|
|
p++;
|
2002-11-20 03:29:04 +03:00
|
|
|
break;
|
|
|
|
default:
|
2002-11-23 21:15:17 +03:00
|
|
|
error("unrecognized character \\x%02x", c);
|
2002-11-20 03:29:04 +03:00
|
|
|
break;
|
2001-10-28 02:48:39 +03:00
|
|
|
}
|
2002-11-23 21:15:17 +03:00
|
|
|
file->buf_ptr = p;
|
2002-11-23 01:16:30 +03:00
|
|
|
tok_flags = 0;
|
2002-11-22 21:12:41 +03:00
|
|
|
#if defined(PARSE_DEBUG)
|
|
|
|
printf("token = %s\n", get_tok_str(tok, &tokc));
|
|
|
|
#endif
|
2001-11-11 05:53:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* return next token without macro substitution. Can read input from
|
|
|
|
macro_ptr buffer */
|
2002-11-02 23:45:52 +03:00
|
|
|
static void next_nomacro(void)
|
2001-11-11 05:53:01 +03:00
|
|
|
{
|
|
|
|
if (macro_ptr) {
|
2002-06-30 21:34:30 +04:00
|
|
|
redo:
|
2001-11-11 05:53:01 +03:00
|
|
|
tok = *macro_ptr;
|
2002-06-30 21:34:30 +04:00
|
|
|
if (tok) {
|
2002-11-24 01:02:40 +03:00
|
|
|
TOK_GET(tok, macro_ptr, tokc);
|
2002-06-30 21:34:30 +04:00
|
|
|
if (tok == TOK_LINENUM) {
|
|
|
|
file->line_num = tokc.i;
|
|
|
|
goto redo;
|
|
|
|
}
|
|
|
|
}
|
2001-11-11 05:53:01 +03:00
|
|
|
} else {
|
|
|
|
next_nomacro1();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* substitute args in macro_str and return allocated string */
|
2002-11-02 23:45:52 +03:00
|
|
|
static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args)
|
2001-11-11 05:53:01 +03:00
|
|
|
{
|
2002-06-30 21:34:30 +04:00
|
|
|
int *st, last_tok, t, notfirst;
|
2001-11-11 05:53:01 +03:00
|
|
|
Sym *s;
|
2001-12-14 01:28:53 +03:00
|
|
|
CValue cval;
|
2002-06-30 21:34:30 +04:00
|
|
|
TokenString str;
|
2002-08-18 17:22:55 +04:00
|
|
|
CString cstr;
|
|
|
|
|
2002-06-30 21:34:30 +04:00
|
|
|
tok_str_new(&str);
|
2001-11-11 05:53:01 +03:00
|
|
|
last_tok = 0;
|
|
|
|
while(1) {
|
2002-11-24 01:02:40 +03:00
|
|
|
TOK_GET(t, macro_str, cval);
|
2001-11-11 05:53:01 +03:00
|
|
|
if (!t)
|
|
|
|
break;
|
|
|
|
if (t == '#') {
|
|
|
|
/* stringize */
|
2002-11-24 01:02:40 +03:00
|
|
|
TOK_GET(t, macro_str, cval);
|
2001-11-11 05:53:01 +03:00
|
|
|
if (!t)
|
|
|
|
break;
|
2001-11-18 19:33:01 +03:00
|
|
|
s = sym_find2(args, t);
|
2001-11-11 05:53:01 +03:00
|
|
|
if (s) {
|
2002-08-18 17:22:55 +04:00
|
|
|
cstr_new(&cstr);
|
2001-11-11 05:53:01 +03:00
|
|
|
st = (int *)s->c;
|
|
|
|
notfirst = 0;
|
|
|
|
while (*st) {
|
|
|
|
if (notfirst)
|
2002-08-18 17:22:55 +04:00
|
|
|
cstr_ccat(&cstr, ' ');
|
2002-11-24 01:02:40 +03:00
|
|
|
TOK_GET(t, st, cval);
|
2002-08-18 17:22:55 +04:00
|
|
|
cstr_cat(&cstr, get_tok_str(t, &cval));
|
2001-11-11 05:53:01 +03:00
|
|
|
notfirst = 1;
|
|
|
|
}
|
2002-08-18 17:22:55 +04:00
|
|
|
cstr_ccat(&cstr, '\0');
|
2001-11-11 05:53:01 +03:00
|
|
|
#ifdef PP_DEBUG
|
2002-08-18 17:22:55 +04:00
|
|
|
printf("stringize: %s\n", (char *)cstr.data);
|
2001-11-11 05:53:01 +03:00
|
|
|
#endif
|
|
|
|
/* add string */
|
2002-08-18 17:22:55 +04:00
|
|
|
cval.cstr = &cstr;
|
2002-06-30 21:34:30 +04:00
|
|
|
tok_str_add2(&str, TOK_STR, &cval);
|
2002-08-18 17:22:55 +04:00
|
|
|
cstr_free(&cstr);
|
2001-11-11 05:53:01 +03:00
|
|
|
} else {
|
2002-06-30 21:34:30 +04:00
|
|
|
tok_str_add2(&str, t, &cval);
|
2001-11-11 05:53:01 +03:00
|
|
|
}
|
2001-12-14 01:28:53 +03:00
|
|
|
} else if (t >= TOK_IDENT) {
|
2001-11-18 19:33:01 +03:00
|
|
|
s = sym_find2(args, t);
|
2001-11-11 05:53:01 +03:00
|
|
|
if (s) {
|
|
|
|
st = (int *)s->c;
|
2002-07-13 15:17:19 +04:00
|
|
|
/* if '##' is present before or after, no arg substitution */
|
2001-11-11 05:53:01 +03:00
|
|
|
if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
|
2002-07-13 15:17:19 +04:00
|
|
|
/* special case for var arg macros : ## eats the
|
2002-11-02 17:12:32 +03:00
|
|
|
',' if empty VA_ARGS variable. */
|
2002-07-13 15:17:19 +04:00
|
|
|
/* XXX: test of the ',' is not 100%
|
|
|
|
reliable. should fix it to avoid security
|
|
|
|
problems */
|
2002-11-19 00:46:44 +03:00
|
|
|
if (gnu_ext && s->type.t &&
|
2002-07-13 15:17:19 +04:00
|
|
|
last_tok == TOK_TWOSHARPS &&
|
2002-08-18 17:22:55 +04:00
|
|
|
str.len >= 2 && str.str[str.len - 2] == ',') {
|
2002-11-02 17:12:32 +03:00
|
|
|
if (*st == 0) {
|
|
|
|
/* suppress ',' '##' */
|
|
|
|
str.len -= 2;
|
|
|
|
} else {
|
|
|
|
/* suppress '##' and add variable */
|
|
|
|
str.len--;
|
|
|
|
goto add_var;
|
|
|
|
}
|
2002-07-13 15:17:19 +04:00
|
|
|
} else {
|
2002-08-18 17:22:55 +04:00
|
|
|
int t1;
|
2002-11-02 17:12:32 +03:00
|
|
|
add_var:
|
2002-08-18 17:22:55 +04:00
|
|
|
for(;;) {
|
2002-11-24 01:02:40 +03:00
|
|
|
TOK_GET(t1, st, cval);
|
2002-08-18 17:22:55 +04:00
|
|
|
if (!t1)
|
|
|
|
break;
|
|
|
|
tok_str_add2(&str, t1, &cval);
|
|
|
|
}
|
2002-07-13 15:17:19 +04:00
|
|
|
}
|
2001-11-11 05:53:01 +03:00
|
|
|
} else {
|
2002-06-30 21:34:30 +04:00
|
|
|
macro_subst(&str, nested_list, st);
|
2001-11-11 05:53:01 +03:00
|
|
|
}
|
|
|
|
} else {
|
2002-06-30 21:34:30 +04:00
|
|
|
tok_str_add(&str, t);
|
2001-11-11 05:53:01 +03:00
|
|
|
}
|
2001-12-14 01:28:53 +03:00
|
|
|
} else {
|
2002-06-30 21:34:30 +04:00
|
|
|
tok_str_add2(&str, t, &cval);
|
2001-11-11 05:53:01 +03:00
|
|
|
}
|
|
|
|
last_tok = t;
|
|
|
|
}
|
2002-06-30 21:34:30 +04:00
|
|
|
tok_str_add(&str, 0);
|
|
|
|
return str.str;
|
2001-11-11 05:53:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* handle the '##' operator */
|
2002-09-09 01:55:47 +04:00
|
|
|
static int *macro_twosharps(void)
|
2001-11-11 05:53:01 +03:00
|
|
|
{
|
|
|
|
TokenSym *ts;
|
2002-06-30 21:34:30 +04:00
|
|
|
int *macro_ptr1;
|
2001-12-14 01:28:53 +03:00
|
|
|
int t;
|
2002-11-02 17:12:32 +03:00
|
|
|
const char *p1, *p2;
|
2001-12-14 01:28:53 +03:00
|
|
|
CValue cval;
|
2002-06-30 21:34:30 +04:00
|
|
|
TokenString macro_str1;
|
2002-11-02 20:02:31 +03:00
|
|
|
CString cstr;
|
|
|
|
|
|
|
|
cstr_new(&cstr);
|
2002-06-30 21:34:30 +04:00
|
|
|
tok_str_new(¯o_str1);
|
2001-11-11 05:53:01 +03:00
|
|
|
tok = 0;
|
|
|
|
while (1) {
|
|
|
|
next_nomacro();
|
|
|
|
if (tok == 0)
|
|
|
|
break;
|
2001-12-14 02:20:55 +03:00
|
|
|
while (*macro_ptr == TOK_TWOSHARPS) {
|
2001-11-11 05:53:01 +03:00
|
|
|
macro_ptr++;
|
|
|
|
macro_ptr1 = macro_ptr;
|
|
|
|
t = *macro_ptr;
|
|
|
|
if (t) {
|
2002-11-24 01:02:40 +03:00
|
|
|
TOK_GET(t, macro_ptr, cval);
|
2002-11-02 20:02:31 +03:00
|
|
|
|
|
|
|
/* We concatenate the two tokens if we have an
|
|
|
|
identifier or a preprocessing number */
|
|
|
|
cstr_reset(&cstr);
|
2002-11-02 17:12:32 +03:00
|
|
|
p1 = get_tok_str(tok, &tokc);
|
2002-11-02 20:02:31 +03:00
|
|
|
cstr_cat(&cstr, p1);
|
2002-11-02 17:12:32 +03:00
|
|
|
p2 = get_tok_str(t, &cval);
|
2002-11-02 20:02:31 +03:00
|
|
|
cstr_cat(&cstr, p2);
|
|
|
|
cstr_ccat(&cstr, '\0');
|
2002-11-02 23:45:52 +03:00
|
|
|
|
2002-11-02 20:02:31 +03:00
|
|
|
if ((tok >= TOK_IDENT || tok == TOK_PPNUM) &&
|
|
|
|
(t >= TOK_IDENT || t == TOK_PPNUM)) {
|
|
|
|
if (tok == TOK_PPNUM) {
|
|
|
|
/* if number, then create a number token */
|
|
|
|
/* NOTE: no need to allocate because
|
|
|
|
tok_str_add2() does it */
|
|
|
|
tokc.cstr = &cstr;
|
2002-11-02 17:12:32 +03:00
|
|
|
} else {
|
2002-11-02 20:02:31 +03:00
|
|
|
/* if identifier, we must do a test to
|
|
|
|
validate we have a correct identifier */
|
|
|
|
if (t == TOK_PPNUM) {
|
|
|
|
const char *p;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
p = p2;
|
|
|
|
for(;;) {
|
|
|
|
c = *p;
|
|
|
|
if (c == '\0')
|
|
|
|
break;
|
|
|
|
p++;
|
|
|
|
if (!isnum(c) && !isid(c))
|
|
|
|
goto error_pasting;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ts = tok_alloc(cstr.data, strlen(cstr.data));
|
|
|
|
tok = ts->tok; /* modify current token */
|
2002-11-02 17:12:32 +03:00
|
|
|
}
|
2001-11-11 05:53:01 +03:00
|
|
|
} else {
|
2002-11-02 20:02:31 +03:00
|
|
|
const char *str = cstr.data;
|
|
|
|
const unsigned char *q;
|
|
|
|
|
|
|
|
/* we look for a valid token */
|
|
|
|
/* XXX: do more extensive checks */
|
|
|
|
if (!strcmp(str, ">>=")) {
|
|
|
|
tok = TOK_A_SAR;
|
|
|
|
} else if (!strcmp(str, "<<=")) {
|
|
|
|
tok = TOK_A_SHL;
|
|
|
|
} else if (strlen(str) == 2) {
|
|
|
|
/* search in two bytes table */
|
|
|
|
q = tok_two_chars;
|
|
|
|
for(;;) {
|
|
|
|
if (!*q)
|
|
|
|
goto error_pasting;
|
|
|
|
if (q[0] == str[0] && q[1] == str[1])
|
|
|
|
break;
|
|
|
|
q += 3;
|
|
|
|
}
|
|
|
|
tok = q[2];
|
|
|
|
} else {
|
|
|
|
error_pasting:
|
|
|
|
/* NOTE: because get_tok_str use a static buffer,
|
|
|
|
we must save it */
|
|
|
|
cstr_reset(&cstr);
|
|
|
|
p1 = get_tok_str(tok, &tokc);
|
|
|
|
cstr_cat(&cstr, p1);
|
|
|
|
cstr_ccat(&cstr, '\0');
|
|
|
|
p2 = get_tok_str(t, &cval);
|
|
|
|
warning("pasting \"%s\" and \"%s\" does not give a valid preprocessing token", cstr.data, p2);
|
|
|
|
/* cannot merge tokens: just add them separately */
|
|
|
|
tok_str_add2(¯o_str1, tok, &tokc);
|
|
|
|
/* XXX: free associated memory ? */
|
|
|
|
tok = t;
|
|
|
|
tokc = cval;
|
|
|
|
}
|
2001-11-11 05:53:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-06-30 21:34:30 +04:00
|
|
|
tok_str_add2(¯o_str1, tok, &tokc);
|
2001-11-11 05:53:01 +03:00
|
|
|
}
|
2002-11-02 20:02:31 +03:00
|
|
|
cstr_free(&cstr);
|
2002-06-30 21:34:30 +04:00
|
|
|
tok_str_add(¯o_str1, 0);
|
|
|
|
return macro_str1.str;
|
2001-11-11 05:53:01 +03:00
|
|
|
}
|
|
|
|
|
2002-09-09 01:55:47 +04:00
|
|
|
|
|
|
|
/* do macro substitution of current token with macro 's' and add
|
|
|
|
result to (tok_str,tok_len). 'nested_list' is the list of all
|
|
|
|
macros we got inside to avoid recursing. Return non zero if no
|
|
|
|
substitution needs to be done */
|
|
|
|
static int macro_subst_tok(TokenString *tok_str,
|
|
|
|
Sym **nested_list, Sym *s)
|
|
|
|
{
|
|
|
|
Sym *args, *sa, *sa1;
|
|
|
|
int mstr_allocated, parlevel, *mstr, t;
|
2002-06-30 21:34:30 +04:00
|
|
|
TokenString str;
|
2002-08-18 17:22:55 +04:00
|
|
|
char *cstrval;
|
2002-09-09 01:55:47 +04:00
|
|
|
CValue cval;
|
|
|
|
CString cstr;
|
|
|
|
|
|
|
|
/* if symbol is a macro, prepare substitution */
|
|
|
|
/* if nested substitution, do nothing */
|
|
|
|
if (sym_find2(*nested_list, tok))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* special macros */
|
|
|
|
if (tok == TOK___LINE__) {
|
|
|
|
cval.i = file->line_num;
|
|
|
|
tok_str_add2(tok_str, TOK_CINT, &cval);
|
|
|
|
} else if (tok == TOK___FILE__) {
|
|
|
|
cstrval = file->filename;
|
|
|
|
goto add_cstr;
|
|
|
|
tok_str_add2(tok_str, TOK_STR, &cval);
|
|
|
|
} else if (tok == TOK___DATE__) {
|
|
|
|
cstrval = "Jan 1 2002";
|
|
|
|
goto add_cstr;
|
|
|
|
} else if (tok == TOK___TIME__) {
|
|
|
|
cstrval = "00:00:00";
|
|
|
|
add_cstr:
|
|
|
|
cstr_new(&cstr);
|
|
|
|
cstr_cat(&cstr, cstrval);
|
|
|
|
cstr_ccat(&cstr, '\0');
|
|
|
|
cval.cstr = &cstr;
|
|
|
|
tok_str_add2(tok_str, TOK_STR, &cval);
|
|
|
|
cstr_free(&cstr);
|
|
|
|
} else {
|
|
|
|
mstr = (int *)s->c;
|
|
|
|
mstr_allocated = 0;
|
2002-11-19 00:46:44 +03:00
|
|
|
if (s->type.t == MACRO_FUNC) {
|
2002-09-09 01:55:47 +04:00
|
|
|
/* NOTE: we do not use next_nomacro to avoid eating the
|
|
|
|
next token. XXX: find better solution */
|
|
|
|
if (macro_ptr) {
|
|
|
|
t = *macro_ptr;
|
|
|
|
} else {
|
2002-11-23 01:16:30 +03:00
|
|
|
/* XXX: incorrect with comments */
|
2002-11-23 21:15:17 +03:00
|
|
|
ch = file->buf_ptr[0];
|
2002-09-09 01:55:47 +04:00
|
|
|
while (is_space(ch) || ch == '\n')
|
|
|
|
cinp();
|
|
|
|
t = ch;
|
|
|
|
}
|
|
|
|
if (t != '(') /* no macro subst */
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* argument macro */
|
|
|
|
next_nomacro();
|
|
|
|
next_nomacro();
|
|
|
|
args = NULL;
|
|
|
|
sa = s->next;
|
|
|
|
/* 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));
|
|
|
|
tok_str_new(&str);
|
|
|
|
parlevel = 0;
|
|
|
|
/* NOTE: non zero sa->t indicates VA_ARGS */
|
|
|
|
while ((parlevel > 0 ||
|
|
|
|
(tok != ')' &&
|
2002-11-19 00:46:44 +03:00
|
|
|
(tok != ',' || sa->type.t))) &&
|
2002-09-09 01:55:47 +04:00
|
|
|
tok != -1) {
|
|
|
|
if (tok == '(')
|
|
|
|
parlevel++;
|
|
|
|
else if (tok == ')')
|
|
|
|
parlevel--;
|
|
|
|
tok_str_add2(&str, tok, &tokc);
|
|
|
|
next_nomacro();
|
|
|
|
}
|
|
|
|
tok_str_add(&str, 0);
|
2002-11-19 00:46:44 +03:00
|
|
|
sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, (int)str.str);
|
2002-09-09 01:55:47 +04:00
|
|
|
sa = sa->next;
|
|
|
|
if (tok == ')') {
|
|
|
|
/* special case for gcc var args: add an empty
|
|
|
|
var arg argument if it is omitted */
|
2002-11-19 00:46:44 +03:00
|
|
|
if (sa && sa->type.t && gnu_ext)
|
2002-09-09 01:55:47 +04:00
|
|
|
continue;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (tok != ',')
|
|
|
|
expect(",");
|
|
|
|
next_nomacro();
|
|
|
|
}
|
|
|
|
if (sa) {
|
|
|
|
error("macro '%s' used with too few args",
|
|
|
|
get_tok_str(s->v, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now subst each arg */
|
|
|
|
mstr = macro_arg_subst(nested_list, mstr, args);
|
|
|
|
/* free memory */
|
|
|
|
sa = args;
|
|
|
|
while (sa) {
|
|
|
|
sa1 = sa->prev;
|
|
|
|
tok_str_free((int *)sa->c);
|
|
|
|
tcc_free(sa);
|
|
|
|
sa = sa1;
|
|
|
|
}
|
|
|
|
mstr_allocated = 1;
|
|
|
|
}
|
|
|
|
sym_push2(nested_list, s->v, 0, 0);
|
|
|
|
macro_subst(tok_str, nested_list, mstr);
|
|
|
|
/* pop nested defined symbol */
|
|
|
|
sa1 = *nested_list;
|
|
|
|
*nested_list = sa1->prev;
|
|
|
|
tcc_free(sa1);
|
|
|
|
if (mstr_allocated)
|
|
|
|
tok_str_free(mstr);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* do macro substitution of macro_str and add result to
|
|
|
|
(tok_str,tok_len). 'nested_list' is the list of all macros we got
|
|
|
|
inside to avoid recursing. */
|
|
|
|
static void macro_subst(TokenString *tok_str,
|
|
|
|
Sym **nested_list, int *macro_str)
|
|
|
|
{
|
|
|
|
Sym *s;
|
|
|
|
int *saved_macro_ptr;
|
|
|
|
int *macro_str1;
|
2002-08-18 17:22:55 +04:00
|
|
|
|
2001-11-11 05:53:01 +03:00
|
|
|
saved_macro_ptr = macro_ptr;
|
|
|
|
macro_ptr = macro_str;
|
2002-09-09 01:55:47 +04:00
|
|
|
/* first scan for '##' operator handling */
|
|
|
|
macro_str1 = macro_twosharps();
|
|
|
|
macro_ptr = macro_str1;
|
2001-11-11 05:53:01 +03:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
next_nomacro();
|
|
|
|
if (tok == 0)
|
|
|
|
break;
|
2002-11-19 00:46:44 +03:00
|
|
|
s = define_find(tok);
|
2002-09-09 01:55:47 +04:00
|
|
|
if (s != NULL) {
|
|
|
|
if (macro_subst_tok(tok_str, nested_list, s) != 0)
|
2001-11-11 05:53:01 +03:00
|
|
|
goto no_subst;
|
|
|
|
} else {
|
|
|
|
no_subst:
|
2002-06-30 21:34:30 +04:00
|
|
|
tok_str_add2(tok_str, tok, &tokc);
|
2001-11-11 05:53:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
macro_ptr = saved_macro_ptr;
|
2002-09-09 01:55:47 +04:00
|
|
|
tok_str_free(macro_str1);
|
2001-11-11 05:53:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* return next token with macro substitution */
|
2002-11-02 23:45:52 +03:00
|
|
|
static void next(void)
|
2001-11-11 05:53:01 +03:00
|
|
|
{
|
2002-09-09 01:55:47 +04:00
|
|
|
Sym *nested_list, *s;
|
2002-06-30 21:34:30 +04:00
|
|
|
TokenString str;
|
2001-11-11 05:53:01 +03:00
|
|
|
|
|
|
|
/* special 'ungettok' case for label parsing */
|
|
|
|
if (tok1) {
|
|
|
|
tok = tok1;
|
2001-12-05 05:02:25 +03:00
|
|
|
tokc = tok1c;
|
2001-11-11 05:53:01 +03:00
|
|
|
tok1 = 0;
|
|
|
|
} else {
|
|
|
|
redo:
|
2002-09-09 01:55:47 +04:00
|
|
|
next_nomacro();
|
2001-11-11 05:53:01 +03:00
|
|
|
if (!macro_ptr) {
|
2002-06-30 21:34:30 +04:00
|
|
|
/* if not reading from macro substituted string, then try
|
2002-09-09 01:55:47 +04:00
|
|
|
to substitute macros */
|
|
|
|
if (tok >= TOK_IDENT) {
|
2002-11-19 00:46:44 +03:00
|
|
|
s = define_find(tok);
|
2002-09-09 01:55:47 +04:00
|
|
|
if (s) {
|
|
|
|
/* we have a macro: we try to substitute */
|
|
|
|
tok_str_new(&str);
|
|
|
|
nested_list = NULL;
|
|
|
|
if (macro_subst_tok(&str, &nested_list, s) == 0) {
|
|
|
|
/* substitution done, NOTE: maybe empty */
|
|
|
|
tok_str_add(&str, 0);
|
|
|
|
macro_ptr = str.str;
|
|
|
|
macro_ptr_allocated = str.str;
|
|
|
|
goto redo;
|
|
|
|
}
|
|
|
|
}
|
2001-11-11 05:53:01 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (tok == 0) {
|
|
|
|
/* end of macro string: free it */
|
2002-08-18 17:22:55 +04:00
|
|
|
tok_str_free(macro_ptr_allocated);
|
2001-11-11 05:53:01 +03:00
|
|
|
macro_ptr = NULL;
|
|
|
|
goto redo;
|
|
|
|
}
|
|
|
|
}
|
2002-11-02 20:02:31 +03:00
|
|
|
|
|
|
|
/* convert preprocessor tokens into C tokens */
|
|
|
|
if (tok == TOK_PPNUM) {
|
|
|
|
parse_number((char *)tokc.cstr->data);
|
|
|
|
}
|
2001-11-11 05:53:01 +03:00
|
|
|
}
|
2001-10-28 02:48:39 +03:00
|
|
|
}
|
|
|
|
|
2001-11-06 04:20:20 +03:00
|
|
|
void swap(int *p, int *q)
|
|
|
|
{
|
|
|
|
int t;
|
|
|
|
t = *p;
|
|
|
|
*p = *q;
|
|
|
|
*q = t;
|
|
|
|
}
|
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
void vsetc(CType *type, int r, CValue *vc)
|
2001-11-06 04:20:20 +03:00
|
|
|
{
|
2002-09-08 16:42:36 +04:00
|
|
|
int v;
|
|
|
|
|
2001-12-16 20:58:32 +03:00
|
|
|
if (vtop >= vstack + VSTACK_SIZE)
|
|
|
|
error("memory full");
|
2002-09-08 16:42:36 +04:00
|
|
|
/* cannot let cpu flags if other instruction are generated. Also
|
|
|
|
avoid leaving VT_JMP anywhere except on the top of the stack
|
|
|
|
because it would complicate the code generator. */
|
|
|
|
if (vtop >= vstack) {
|
|
|
|
v = vtop->r & VT_VALMASK;
|
|
|
|
if (v == VT_CMP || (v & ~1) == VT_JMP)
|
|
|
|
gv(RC_INT);
|
|
|
|
}
|
2001-12-16 20:58:32 +03:00
|
|
|
vtop++;
|
2002-11-19 00:46:44 +03:00
|
|
|
vtop->type = *type;
|
2001-12-20 04:05:21 +03:00
|
|
|
vtop->r = r;
|
2002-01-04 00:14:59 +03:00
|
|
|
vtop->r2 = VT_CONST;
|
2001-12-18 00:56:48 +03:00
|
|
|
vtop->c = *vc;
|
2001-12-16 20:58:32 +03:00
|
|
|
}
|
|
|
|
|
2001-12-20 04:05:21 +03:00
|
|
|
/* push integer constant */
|
|
|
|
void vpushi(int v)
|
|
|
|
{
|
|
|
|
CValue cval;
|
|
|
|
cval.i = v;
|
2002-11-19 00:46:44 +03:00
|
|
|
vsetc(&int_type, VT_CONST, &cval);
|
2001-12-20 04:05:21 +03:00
|
|
|
}
|
|
|
|
|
2002-07-25 02:10:59 +04:00
|
|
|
/* Return a static symbol pointing to a section */
|
2002-11-19 00:46:44 +03:00
|
|
|
static Sym *get_sym_ref(CType *type, Section *sec,
|
2002-07-25 02:10:59 +04:00
|
|
|
unsigned long offset, unsigned long size)
|
2002-07-14 18:38:33 +04:00
|
|
|
{
|
|
|
|
int v;
|
|
|
|
Sym *sym;
|
|
|
|
|
|
|
|
v = anon_sym++;
|
2002-11-20 03:29:04 +03:00
|
|
|
sym = global_identifier_push(v, type->t | VT_STATIC, 0);
|
2002-11-19 00:46:44 +03:00
|
|
|
sym->type.ref = type->ref;
|
2002-07-14 18:38:33 +04:00
|
|
|
sym->r = VT_CONST | VT_SYM;
|
2002-07-28 03:08:04 +04:00
|
|
|
put_extern_sym(sym, sec, offset, size);
|
2002-07-25 02:10:59 +04:00
|
|
|
return sym;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* push a reference to a section offset by adding a dummy symbol */
|
2002-11-19 00:46:44 +03:00
|
|
|
static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
|
2002-07-25 02:10:59 +04:00
|
|
|
{
|
|
|
|
CValue cval;
|
|
|
|
|
2002-08-30 03:05:59 +04:00
|
|
|
cval.ul = 0;
|
2002-11-19 00:46:44 +03:00
|
|
|
vsetc(type, VT_CONST | VT_SYM, &cval);
|
|
|
|
vtop->sym = get_sym_ref(type, sec, offset, size);
|
2002-07-14 18:38:33 +04:00
|
|
|
}
|
|
|
|
|
2002-08-30 01:57:41 +04:00
|
|
|
/* define a new external reference to a symbol 'v' of type 'u' */
|
2002-11-19 00:46:44 +03:00
|
|
|
static Sym *external_global_sym(int v, CType *type, int r)
|
2002-08-30 01:57:41 +04:00
|
|
|
{
|
|
|
|
Sym *s;
|
|
|
|
|
|
|
|
s = sym_find(v);
|
|
|
|
if (!s) {
|
|
|
|
/* push forward reference */
|
2002-11-20 03:29:04 +03:00
|
|
|
s = global_identifier_push(v, type->t | VT_EXTERN, 0);
|
2002-11-19 00:46:44 +03:00
|
|
|
s->type.ref = type->ref;
|
2002-08-30 01:57:41 +04:00
|
|
|
s->r = r | VT_CONST | VT_SYM;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* define a new external reference to a symbol 'v' of type 'u' */
|
2002-11-19 00:46:44 +03:00
|
|
|
static Sym *external_sym(int v, CType *type, int r)
|
2002-08-30 01:57:41 +04:00
|
|
|
{
|
|
|
|
Sym *s;
|
|
|
|
|
|
|
|
s = sym_find(v);
|
|
|
|
if (!s) {
|
|
|
|
/* push forward reference */
|
2002-11-19 00:46:44 +03:00
|
|
|
s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
|
|
|
|
s->type.t |= VT_EXTERN;
|
2002-08-30 01:57:41 +04:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2002-08-30 01:16:24 +04:00
|
|
|
/* push a reference to global symbol v */
|
2002-11-19 00:46:44 +03:00
|
|
|
static void vpush_global_sym(CType *type, int v)
|
2002-07-23 03:37:39 +04:00
|
|
|
{
|
|
|
|
Sym *sym;
|
|
|
|
CValue cval;
|
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
sym = external_global_sym(v, type, 0);
|
2002-08-30 03:05:59 +04:00
|
|
|
cval.ul = 0;
|
2002-11-19 00:46:44 +03:00
|
|
|
vsetc(type, VT_CONST | VT_SYM, &cval);
|
2002-08-30 03:05:59 +04:00
|
|
|
vtop->sym = sym;
|
2002-07-23 03:37:39 +04:00
|
|
|
}
|
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
void vset(CType *type, int r, int v)
|
2001-12-16 20:58:32 +03:00
|
|
|
{
|
2001-12-18 00:56:48 +03:00
|
|
|
CValue cval;
|
|
|
|
|
|
|
|
cval.i = v;
|
2002-11-19 00:46:44 +03:00
|
|
|
vsetc(type, r, &cval);
|
|
|
|
}
|
|
|
|
|
|
|
|
void vseti(int r, int v)
|
|
|
|
{
|
|
|
|
CType type;
|
|
|
|
type.t = VT_INT;
|
|
|
|
vset(&type, r, v);
|
2001-12-16 20:58:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void vswap(void)
|
|
|
|
{
|
|
|
|
SValue tmp;
|
|
|
|
|
|
|
|
tmp = vtop[0];
|
|
|
|
vtop[0] = vtop[-1];
|
|
|
|
vtop[-1] = tmp;
|
|
|
|
}
|
|
|
|
|
2002-01-04 00:14:59 +03:00
|
|
|
void vpushv(SValue *v)
|
2001-12-16 20:58:32 +03:00
|
|
|
{
|
|
|
|
if (vtop >= vstack + VSTACK_SIZE)
|
|
|
|
error("memory full");
|
|
|
|
vtop++;
|
2002-01-04 00:14:59 +03:00
|
|
|
*vtop = *v;
|
2001-11-06 04:20:20 +03:00
|
|
|
}
|
|
|
|
|
2002-01-04 00:14:59 +03:00
|
|
|
void vdup(void)
|
2001-11-06 04:20:20 +03:00
|
|
|
{
|
2002-01-04 00:14:59 +03:00
|
|
|
vpushv(vtop);
|
|
|
|
}
|
2001-12-13 00:16:17 +03:00
|
|
|
|
2002-01-04 00:14:59 +03:00
|
|
|
/* save r to the memory stack, and mark it as being free */
|
|
|
|
void save_reg(int r)
|
|
|
|
{
|
2002-11-19 00:46:44 +03:00
|
|
|
int l, saved, size, align;
|
2002-01-04 00:14:59 +03:00
|
|
|
SValue *p, sv;
|
2002-11-19 00:46:44 +03:00
|
|
|
CType *type;
|
2001-11-06 04:20:20 +03:00
|
|
|
|
|
|
|
/* modify all stack values */
|
2002-01-04 00:14:59 +03:00
|
|
|
saved = 0;
|
|
|
|
l = 0;
|
2001-12-16 20:58:32 +03:00
|
|
|
for(p=vstack;p<=vtop;p++) {
|
2002-01-04 00:14:59 +03:00
|
|
|
if ((p->r & VT_VALMASK) == r ||
|
|
|
|
(p->r2 & VT_VALMASK) == r) {
|
|
|
|
/* must save value on stack if not already done */
|
|
|
|
if (!saved) {
|
2002-11-02 17:12:32 +03:00
|
|
|
/* NOTE: must reload 'r' because r might be equal to r2 */
|
|
|
|
r = p->r & VT_VALMASK;
|
2002-01-04 00:14:59 +03:00
|
|
|
/* store register in the stack */
|
2002-11-19 00:46:44 +03:00
|
|
|
type = &p->type;
|
2002-01-26 21:05:29 +03:00
|
|
|
if ((p->r & VT_LVAL) ||
|
2002-11-19 00:46:44 +03:00
|
|
|
(!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
|
|
|
|
type = &int_type;
|
|
|
|
size = type_size(type, &align);
|
2002-01-04 00:14:59 +03:00
|
|
|
loc = (loc - size) & -align;
|
2002-11-19 00:46:44 +03:00
|
|
|
sv.type.t = type->t;
|
2002-01-04 00:14:59 +03:00
|
|
|
sv.r = VT_LOCAL | VT_LVAL;
|
|
|
|
sv.c.ul = loc;
|
|
|
|
store(r, &sv);
|
2002-02-10 19:14:03 +03:00
|
|
|
#ifdef TCC_TARGET_I386
|
|
|
|
/* x86 specific: need to pop fp register ST0 if saved */
|
2002-01-04 00:14:59 +03:00
|
|
|
if (r == REG_ST0) {
|
|
|
|
o(0xd9dd); /* fstp %st(1) */
|
|
|
|
}
|
2002-02-10 19:14:03 +03:00
|
|
|
#endif
|
2002-01-04 00:14:59 +03:00
|
|
|
/* special long long case */
|
2002-11-19 00:46:44 +03:00
|
|
|
if ((type->t & VT_BTYPE) == VT_LLONG) {
|
2002-01-04 00:14:59 +03:00
|
|
|
sv.c.ul += 4;
|
|
|
|
store(p->r2, &sv);
|
|
|
|
}
|
|
|
|
l = loc;
|
|
|
|
saved = 1;
|
|
|
|
}
|
|
|
|
/* mark that stack entry as being saved on the stack */
|
2002-08-18 17:22:55 +04:00
|
|
|
if (p->r & VT_LVAL) {
|
|
|
|
/* also suppress the bounded flag because the
|
|
|
|
relocation address of the function was stored in
|
|
|
|
p->c.ul */
|
|
|
|
p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
|
|
|
|
} else {
|
2002-11-19 00:46:44 +03:00
|
|
|
p->r = lvalue_type(p->type.t) | VT_LOCAL;
|
2002-08-18 17:22:55 +04:00
|
|
|
}
|
2002-01-04 00:14:59 +03:00
|
|
|
p->r2 = VT_CONST;
|
2001-12-18 00:56:48 +03:00
|
|
|
p->c.ul = l;
|
2001-11-06 04:20:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-13 00:16:17 +03:00
|
|
|
/* find a free register of class 'rc'. If none, save one register */
|
|
|
|
int get_reg(int rc)
|
2001-11-06 04:20:20 +03:00
|
|
|
{
|
2002-01-04 00:14:59 +03:00
|
|
|
int r;
|
2001-12-16 20:58:32 +03:00
|
|
|
SValue *p;
|
2001-11-06 04:20:20 +03:00
|
|
|
|
|
|
|
/* find a free register */
|
|
|
|
for(r=0;r<NB_REGS;r++) {
|
2001-12-13 00:16:17 +03:00
|
|
|
if (reg_classes[r] & rc) {
|
2001-12-16 20:58:32 +03:00
|
|
|
for(p=vstack;p<=vtop;p++) {
|
2002-01-04 00:14:59 +03:00
|
|
|
if ((p->r & VT_VALMASK) == r ||
|
|
|
|
(p->r2 & VT_VALMASK) == r)
|
2001-12-13 00:16:17 +03:00
|
|
|
goto notfound;
|
|
|
|
}
|
|
|
|
return r;
|
2001-11-06 04:20:20 +03:00
|
|
|
}
|
|
|
|
notfound: ;
|
|
|
|
}
|
|
|
|
|
2002-01-04 00:14:59 +03: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-16 20:58:32 +03:00
|
|
|
for(p=vstack;p<=vtop;p++) {
|
2001-12-20 04:05:21 +03:00
|
|
|
r = p->r & VT_VALMASK;
|
2002-11-02 17:12:32 +03:00
|
|
|
if (r < VT_CONST && (reg_classes[r] & rc))
|
|
|
|
goto save_found;
|
|
|
|
/* also look at second register (if long long) */
|
|
|
|
r = p->r2 & VT_VALMASK;
|
2001-12-13 00:16:17 +03:00
|
|
|
if (r < VT_CONST && (reg_classes[r] & rc)) {
|
2002-11-02 17:12:32 +03:00
|
|
|
save_found:
|
2001-11-06 04:20:20 +03:00
|
|
|
save_reg(r);
|
2002-11-02 17:12:32 +03:00
|
|
|
return r;
|
2001-11-06 04:20:20 +03:00
|
|
|
}
|
|
|
|
}
|
2002-11-02 17:12:32 +03:00
|
|
|
/* Should never comes here */
|
|
|
|
return -1;
|
2001-11-06 04:20:20 +03:00
|
|
|
}
|
|
|
|
|
2002-05-14 03:00:17 +04:00
|
|
|
/* save registers up to (vtop - n) stack entry */
|
|
|
|
void save_regs(int n)
|
2001-11-06 04:20:20 +03:00
|
|
|
{
|
2001-12-16 20:58:32 +03:00
|
|
|
int r;
|
2002-05-14 03:00:17 +04:00
|
|
|
SValue *p, *p1;
|
|
|
|
p1 = vtop - n;
|
|
|
|
for(p = vstack;p <= p1; p++) {
|
2001-12-20 04:05:21 +03:00
|
|
|
r = p->r & VT_VALMASK;
|
2001-11-06 04:20:20 +03: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 00:16:17 +03:00
|
|
|
void move_reg(int r, int s)
|
2001-11-06 04:20:20 +03:00
|
|
|
{
|
2001-12-20 04:05:21 +03:00
|
|
|
SValue sv;
|
|
|
|
|
2001-11-06 04:20:20 +03:00
|
|
|
if (r != s) {
|
|
|
|
save_reg(r);
|
2002-11-19 00:46:44 +03:00
|
|
|
sv.type.t = VT_INT;
|
2001-12-20 04:05:21 +03:00
|
|
|
sv.r = s;
|
|
|
|
sv.c.ul = 0;
|
|
|
|
load(r, &sv);
|
2001-11-06 04:20:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-01-05 03:40:47 +03: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)
|
2002-08-18 17:22:55 +04:00
|
|
|
vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
|
2002-01-05 03:40:47 +03:00
|
|
|
}
|
|
|
|
|
2002-03-04 01:45:55 +03:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2002-01-05 19:15:57 +03:00
|
|
|
/* generate lvalue bound code */
|
|
|
|
void gbound(void)
|
|
|
|
{
|
2002-11-19 00:46:44 +03:00
|
|
|
int lval_type;
|
|
|
|
CType type1;
|
2002-07-13 15:17:19 +04:00
|
|
|
|
2002-01-05 19:15:57 +03:00
|
|
|
vtop->r &= ~VT_MUSTBOUND;
|
|
|
|
/* if lvalue, then use checking code before dereferencing */
|
|
|
|
if (vtop->r & VT_LVAL) {
|
2002-07-13 19:55:38 +04:00
|
|
|
/* if not VT_BOUNDED value, then make one */
|
|
|
|
if (!(vtop->r & VT_BOUNDED)) {
|
|
|
|
lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
|
2002-08-18 17:22:55 +04:00
|
|
|
/* must save type because we must set it to int to get pointer */
|
2002-11-19 00:46:44 +03:00
|
|
|
type1 = vtop->type;
|
|
|
|
vtop->type.t = VT_INT;
|
2002-07-13 19:55:38 +04:00
|
|
|
gaddrof();
|
|
|
|
vpushi(0);
|
|
|
|
gen_bounded_ptr_add();
|
|
|
|
vtop->r |= lval_type;
|
2002-11-19 00:46:44 +03:00
|
|
|
vtop->type = type1;
|
2002-07-13 19:55:38 +04:00
|
|
|
}
|
|
|
|
/* then check for dereferencing */
|
|
|
|
gen_bounded_ptr_deref();
|
2002-01-05 19:15:57 +03:00
|
|
|
}
|
|
|
|
}
|
2002-03-04 01:45:55 +03:00
|
|
|
#endif
|
2002-01-05 19:15:57 +03:00
|
|
|
|
2001-12-23 03:34:26 +03:00
|
|
|
/* store vtop a register belonging to class 'rc'. lvalues are
|
2002-01-04 00:14:59 +03:00
|
|
|
converted to values. Cannot be used if cannot be converted to
|
2001-12-23 03:34:26 +03:00
|
|
|
register value (such as structures). */
|
|
|
|
int gv(int rc)
|
2001-11-06 04:20:20 +03:00
|
|
|
{
|
2002-07-27 18:05:37 +04:00
|
|
|
int r, r2, rc2, bit_pos, bit_size, size, align, i;
|
2002-01-04 00:14:59 +03:00
|
|
|
unsigned long long ll;
|
2001-12-10 01:04:15 +03:00
|
|
|
|
|
|
|
/* NOTE: get_reg can modify vstack[] */
|
2002-11-19 00:46:44 +03:00
|
|
|
if (vtop->type.t & VT_BITFIELD) {
|
|
|
|
bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
|
|
|
|
bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
|
2001-12-10 01:04:15 +03:00
|
|
|
/* remove bit field info to avoid loops */
|
2002-11-19 00:46:44 +03:00
|
|
|
vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
|
2001-12-10 01:04:15 +03:00
|
|
|
/* generate shifts */
|
2001-12-20 04:05:21 +03:00
|
|
|
vpushi(32 - (bit_pos + bit_size));
|
2001-12-10 01:04:15 +03:00
|
|
|
gen_op(TOK_SHL);
|
2001-12-20 04:05:21 +03:00
|
|
|
vpushi(32 - bit_size);
|
2001-12-10 01:04:15 +03:00
|
|
|
/* NOTE: transformed to SHR if unsigned */
|
|
|
|
gen_op(TOK_SAR);
|
2001-12-23 03:34:26 +03:00
|
|
|
r = gv(rc);
|
2001-12-10 01:04:15 +03:00
|
|
|
} else {
|
2002-11-19 00:46:44 +03:00
|
|
|
if (is_float(vtop->type.t) &&
|
2001-12-20 04:05:21 +03:00
|
|
|
(vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
|
2002-07-15 03:00:39 +04:00
|
|
|
Sym *sym;
|
2002-07-27 18:05:37 +04:00
|
|
|
int *ptr;
|
2002-07-28 03:08:04 +04:00
|
|
|
unsigned long offset;
|
|
|
|
|
2002-07-15 03:00:39 +04:00
|
|
|
/* XXX: unify with initializers handling ? */
|
2001-12-18 00:56:48 +03:00
|
|
|
/* CPUs usually cannot use float constants, so we store them
|
|
|
|
generically in data segment */
|
2002-11-19 00:46:44 +03:00
|
|
|
size = type_size(&vtop->type, &align);
|
2002-07-28 03:08:04 +04:00
|
|
|
offset = (data_section->data_offset + align - 1) & -align;
|
|
|
|
data_section->data_offset = offset;
|
2001-12-18 00:56:48 +03:00
|
|
|
/* XXX: not portable yet */
|
2002-07-28 03:08:04 +04:00
|
|
|
ptr = section_ptr_add(data_section, size);
|
2001-12-18 00:56:48 +03:00
|
|
|
size = size >> 2;
|
|
|
|
for(i=0;i<size;i++)
|
2002-07-27 18:05:37 +04:00
|
|
|
ptr[i] = vtop->c.tab[i];
|
2002-11-19 00:46:44 +03:00
|
|
|
sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
|
2002-07-15 03:00:39 +04:00
|
|
|
vtop->r |= VT_LVAL | VT_SYM;
|
2002-08-30 03:05:59 +04:00
|
|
|
vtop->sym = sym;
|
|
|
|
vtop->c.ul = 0;
|
2001-12-18 00:56:48 +03:00
|
|
|
}
|
2002-03-04 01:45:55 +03:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2002-01-05 19:15:57 +03:00
|
|
|
if (vtop->r & VT_MUSTBOUND)
|
|
|
|
gbound();
|
2002-03-04 01:45:55 +03:00
|
|
|
#endif
|
2002-01-05 19:15:57 +03:00
|
|
|
|
2001-12-20 04:05:21 +03:00
|
|
|
r = vtop->r & VT_VALMASK;
|
2001-12-23 03:34:26 +03: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 00:14:59 +03:00
|
|
|
!(reg_classes[r] & rc) ||
|
2002-11-19 00:46:44 +03:00
|
|
|
((vtop->type.t & VT_BTYPE) == VT_LLONG &&
|
2002-01-04 00:14:59 +03:00
|
|
|
!(reg_classes[vtop->r2] & rc))) {
|
2001-12-13 00:16:17 +03:00
|
|
|
r = get_reg(rc);
|
2002-11-19 00:46:44 +03:00
|
|
|
if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
|
2002-01-04 00:14:59 +03:00
|
|
|
/* 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 */
|
2002-11-19 00:46:44 +03:00
|
|
|
vtop->type.t = VT_INT;
|
2002-01-05 03:40:47 +03:00
|
|
|
gaddrof();
|
2002-01-04 00:14:59 +03: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;
|
2002-11-19 00:46:44 +03:00
|
|
|
} else if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
|
2002-07-13 15:17:19 +04:00
|
|
|
int t1, t;
|
|
|
|
/* lvalue of scalar type : need to use lvalue type
|
|
|
|
because of possible cast */
|
2002-11-19 00:46:44 +03:00
|
|
|
t = vtop->type.t;
|
2002-07-13 15:17:19 +04:00
|
|
|
t1 = t;
|
|
|
|
/* compute memory access type */
|
|
|
|
if (vtop->r & VT_LVAL_BYTE)
|
|
|
|
t = VT_BYTE;
|
|
|
|
else if (vtop->r & VT_LVAL_SHORT)
|
|
|
|
t = VT_SHORT;
|
|
|
|
if (vtop->r & VT_LVAL_UNSIGNED)
|
|
|
|
t |= VT_UNSIGNED;
|
2002-11-19 00:46:44 +03:00
|
|
|
vtop->type.t = t;
|
2002-07-13 15:17:19 +04:00
|
|
|
load(r, vtop);
|
|
|
|
/* restore wanted type */
|
2002-11-19 00:46:44 +03:00
|
|
|
vtop->type.t = t1;
|
2002-01-04 00:14:59 +03:00
|
|
|
} else {
|
|
|
|
/* one register type load */
|
|
|
|
load(r, vtop);
|
|
|
|
}
|
2001-12-13 00:16:17 +03:00
|
|
|
}
|
2001-12-20 04:05:21 +03:00
|
|
|
vtop->r = r;
|
2001-12-10 01:04:15 +03:00
|
|
|
}
|
2001-11-06 04:20:20 +03:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2002-01-05 19:15:57 +03:00
|
|
|
/* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
|
|
|
|
void gv2(int rc1, int rc2)
|
|
|
|
{
|
2002-09-08 16:42:36 +04:00
|
|
|
int v;
|
|
|
|
|
|
|
|
/* generate more generic register first. But VT_JMP or VT_CMP
|
|
|
|
values must be generated first in all cases to avoid possible
|
|
|
|
reload errors */
|
|
|
|
v = vtop[0].r & VT_VALMASK;
|
|
|
|
if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
|
2002-01-05 19:15:57 +03:00
|
|
|
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 00:14:59 +03:00
|
|
|
/* expand long long on stack in two int registers */
|
|
|
|
void lexpand(void)
|
|
|
|
{
|
|
|
|
int u;
|
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
u = vtop->type.t & VT_UNSIGNED;
|
2002-01-04 00:14:59 +03:00
|
|
|
gv(RC_INT);
|
|
|
|
vdup();
|
|
|
|
vtop[0].r = vtop[-1].r2;
|
|
|
|
vtop[0].r2 = VT_CONST;
|
|
|
|
vtop[-1].r2 = VT_CONST;
|
2002-11-19 00:46:44 +03:00
|
|
|
vtop[0].type.t = VT_INT | u;
|
|
|
|
vtop[-1].type.t = VT_INT | u;
|
2002-01-04 00:14:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* build a long long from two ints */
|
|
|
|
void lbuild(int t)
|
|
|
|
{
|
2002-01-05 19:15:57 +03:00
|
|
|
gv2(RC_INT, RC_INT);
|
2002-01-04 00:14:59 +03:00
|
|
|
vtop[-1].r2 = vtop[0].r;
|
2002-11-19 00:46:44 +03:00
|
|
|
vtop[-1].type.t = t;
|
2002-01-04 00:14:59 +03:00
|
|
|
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-07-13 15:17:19 +04:00
|
|
|
int v;
|
|
|
|
v = vtop->r & VT_VALMASK;
|
2002-02-10 19:14:03 +03:00
|
|
|
#ifdef TCC_TARGET_I386
|
2002-01-04 00:14:59 +03:00
|
|
|
/* for x86, we need to pop the FP stack */
|
2002-11-02 23:45:52 +03:00
|
|
|
if (v == REG_ST0 && !nocode_wanted) {
|
2002-01-04 00:14:59 +03:00
|
|
|
o(0xd9dd); /* fstp %st(1) */
|
2002-07-13 15:17:19 +04:00
|
|
|
} else
|
2002-02-10 19:14:03 +03:00
|
|
|
#endif
|
2002-07-13 15:17:19 +04:00
|
|
|
if (v == VT_JMP || v == VT_JMPI) {
|
|
|
|
/* need to put correct jump if && or || without test */
|
|
|
|
gsym(vtop->c.ul);
|
|
|
|
}
|
2002-01-04 00:14:59 +03: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;
|
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
t = vtop->type.t;
|
2002-01-04 00:14:59 +03:00
|
|
|
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;
|
2002-11-19 00:46:44 +03:00
|
|
|
sv.type.t = VT_INT;
|
2002-01-04 00:14:59 +03:00
|
|
|
if (is_float(t)) {
|
|
|
|
rc = RC_FLOAT;
|
2002-11-19 00:46:44 +03:00
|
|
|
sv.type.t = t;
|
2002-01-04 00:14:59 +03:00
|
|
|
}
|
|
|
|
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;
|
2002-07-23 03:37:39 +04:00
|
|
|
int func;
|
2002-01-04 00:14:59 +03:00
|
|
|
GFuncContext gf;
|
|
|
|
SValue tmp;
|
|
|
|
|
|
|
|
switch(op) {
|
|
|
|
case '/':
|
|
|
|
case TOK_PDIV:
|
2002-07-23 03:37:39 +04:00
|
|
|
func = TOK___divdi3;
|
2002-01-04 00:14:59 +03:00
|
|
|
goto gen_func;
|
|
|
|
case TOK_UDIV:
|
2002-07-23 03:37:39 +04:00
|
|
|
func = TOK___udivdi3;
|
2002-01-04 00:14:59 +03:00
|
|
|
goto gen_func;
|
|
|
|
case '%':
|
2002-07-23 03:37:39 +04:00
|
|
|
func = TOK___moddi3;
|
2002-01-04 00:14:59 +03:00
|
|
|
goto gen_func;
|
|
|
|
case TOK_UMOD:
|
2002-07-23 03:37:39 +04:00
|
|
|
func = TOK___umoddi3;
|
2002-01-04 00:14:59 +03:00
|
|
|
gen_func:
|
|
|
|
/* call generic long long function */
|
2002-01-26 21:05:29 +03:00
|
|
|
gfunc_start(&gf, FUNC_CDECL);
|
2002-01-04 00:14:59 +03:00
|
|
|
gfunc_param(&gf);
|
|
|
|
gfunc_param(&gf);
|
2002-11-19 00:46:44 +03:00
|
|
|
vpush_global_sym(&func_old_type, func);
|
2002-01-04 00:14:59 +03:00
|
|
|
gfunc_call(&gf);
|
|
|
|
vpushi(0);
|
|
|
|
vtop->r = REG_IRET;
|
|
|
|
vtop->r2 = REG_LRET;
|
|
|
|
break;
|
|
|
|
case '^':
|
|
|
|
case '&':
|
|
|
|
case '|':
|
|
|
|
case '*':
|
|
|
|
case '+':
|
|
|
|
case '-':
|
2002-11-19 00:46:44 +03:00
|
|
|
t = vtop->type.t;
|
2002-01-04 00:14:59 +03:00
|
|
|
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-05 22:55:43 +03:00
|
|
|
/* XXX: add non carry method too (for MIPS or alpha) */
|
2002-01-04 00:14:59 +03: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:
|
2002-07-14 18:38:33 +04:00
|
|
|
if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
|
2002-11-19 00:46:44 +03:00
|
|
|
t = vtop[-1].type.t;
|
2002-01-04 00:14:59 +03:00
|
|
|
vswap();
|
|
|
|
lexpand();
|
|
|
|
vrotb(3);
|
2002-11-02 17:12:32 +03:00
|
|
|
/* stack: L H shift */
|
2002-01-04 00:14:59 +03:00
|
|
|
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);
|
2002-11-02 17:12:32 +03:00
|
|
|
if (op == TOK_SHL)
|
|
|
|
gen_op(TOK_SHL);
|
|
|
|
else
|
|
|
|
gen_op(TOK_SHR);
|
2002-01-04 00:14:59 +03:00
|
|
|
gen_op('|');
|
|
|
|
}
|
|
|
|
if (op != TOK_SHL)
|
|
|
|
vswap();
|
|
|
|
lbuild(t);
|
|
|
|
} else {
|
|
|
|
/* XXX: should provide a faster fallback on x86 ? */
|
|
|
|
switch(op) {
|
|
|
|
case TOK_SAR:
|
2002-07-23 03:37:39 +04:00
|
|
|
func = TOK___sardi3;
|
2002-01-04 00:14:59 +03:00
|
|
|
goto gen_func;
|
|
|
|
case TOK_SHR:
|
2002-07-23 03:37:39 +04:00
|
|
|
func = TOK___shrdi3;
|
2002-01-04 00:14:59 +03:00
|
|
|
goto gen_func;
|
|
|
|
case TOK_SHL:
|
2002-07-23 03:37:39 +04:00
|
|
|
func = TOK___shldi3;
|
2002-01-04 00:14:59 +03:00
|
|
|
goto gen_func;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* compare operations */
|
2002-11-19 00:46:44 +03:00
|
|
|
t = vtop->type.t;
|
2002-01-04 00:14:59 +03:00
|
|
|
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-10 19:14:03 +03:00
|
|
|
if (a == 0) {
|
2002-01-04 00:14:59 +03:00
|
|
|
b = gtst(0, 0);
|
2002-02-10 19:14:03 +03:00
|
|
|
} else {
|
|
|
|
#ifdef TCC_TARGET_I386
|
2002-01-04 00:14:59 +03:00
|
|
|
b = psym(0x850f, 0);
|
2002-02-10 19:14:03 +03:00
|
|
|
#else
|
|
|
|
error("not implemented");
|
|
|
|
#endif
|
|
|
|
}
|
2002-01-04 00:14:59 +03:00
|
|
|
}
|
2002-11-24 02:12:26 +03:00
|
|
|
/* compare low. Always unsigned */
|
|
|
|
op1 = op;
|
|
|
|
if (op1 == TOK_LT)
|
|
|
|
op1 = TOK_ULT;
|
|
|
|
else if (op1 == TOK_LE)
|
|
|
|
op1 = TOK_ULE;
|
|
|
|
else if (op1 == TOK_GT)
|
|
|
|
op1 = TOK_UGT;
|
|
|
|
else if (op1 == TOK_GE)
|
|
|
|
op1 = TOK_UGE;
|
|
|
|
gen_op(op1);
|
2002-01-04 00:14:59 +03:00
|
|
|
a = gtst(1, a);
|
|
|
|
gsym(b);
|
2002-11-19 00:46:44 +03:00
|
|
|
vseti(VT_JMPI, a);
|
2002-01-04 00:14:59 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-17 14:15:05 +03:00
|
|
|
/* handle integer constant optimizations and various machine
|
|
|
|
independant opt */
|
|
|
|
void gen_opic(int op)
|
2001-11-06 04:20:20 +03:00
|
|
|
{
|
2001-12-16 20:58:32 +03:00
|
|
|
int fc, c1, c2, n;
|
|
|
|
SValue *v1, *v2;
|
2001-11-06 04:20:20 +03:00
|
|
|
|
2001-12-16 20:58:32 +03:00
|
|
|
v1 = vtop - 1;
|
|
|
|
v2 = vtop;
|
2001-12-13 00:16:17 +03:00
|
|
|
/* currently, we cannot do computations with forward symbols */
|
2002-07-14 18:38:33 +04:00
|
|
|
c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
|
|
|
|
c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
|
2001-11-06 04:20:20 +03:00
|
|
|
if (c1 && c2) {
|
2001-12-18 00:56:48 +03:00
|
|
|
fc = v2->c.i;
|
2001-11-06 04:20:20 +03:00
|
|
|
switch(op) {
|
2001-12-18 00:56:48 +03: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-05 22:55:43 +03:00
|
|
|
|
2001-11-06 04:20:20 +03:00
|
|
|
case TOK_PDIV:
|
2002-01-05 22:55:43 +03: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 00:56:48 +03: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 05:53:01 +03:00
|
|
|
/* tests */
|
2001-12-18 00:56:48 +03: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 05:53:01 +03:00
|
|
|
/* logical */
|
2001-12-18 00:56:48 +03: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 04:20:20 +03:00
|
|
|
default:
|
|
|
|
goto general_case;
|
|
|
|
}
|
2001-12-16 20:58:32 +03:00
|
|
|
vtop--;
|
2001-11-06 04:20:20 +03:00
|
|
|
} else {
|
|
|
|
/* if commutative ops, put c2 as constant */
|
|
|
|
if (c1 && (op == '+' || op == '&' || op == '^' ||
|
|
|
|
op == '|' || op == '*')) {
|
2001-12-16 20:58:32 +03:00
|
|
|
vswap();
|
2001-11-06 04:20:20 +03:00
|
|
|
swap(&c1, &c2);
|
|
|
|
}
|
2001-12-18 00:56:48 +03:00
|
|
|
fc = vtop->c.i;
|
2001-11-06 04:20:20 +03: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-16 20:58:32 +03:00
|
|
|
/* nothing to do */
|
|
|
|
vtop--;
|
2001-11-06 04:20:20 +03: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 00:56:48 +03:00
|
|
|
vtop->c.i = n;
|
2001-11-06 04:20:20 +03:00
|
|
|
if (op == '*')
|
|
|
|
op = TOK_SHL;
|
|
|
|
else if (op == TOK_PDIV)
|
|
|
|
op = TOK_SAR;
|
|
|
|
else
|
|
|
|
op = TOK_SHR;
|
|
|
|
}
|
|
|
|
goto general_case;
|
2002-08-30 03:05:59 +04:00
|
|
|
} else if (c2 && (op == '+' || op == '-') &&
|
|
|
|
(vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) ==
|
|
|
|
(VT_CONST | VT_SYM)) {
|
|
|
|
/* symbol + constant case */
|
|
|
|
if (op == '-')
|
|
|
|
fc = -fc;
|
|
|
|
vtop--;
|
|
|
|
vtop->c.i += fc;
|
2001-11-06 04:20:20 +03:00
|
|
|
} else {
|
|
|
|
general_case:
|
2002-11-02 23:45:52 +03:00
|
|
|
if (!nocode_wanted) {
|
|
|
|
/* call low level op generator */
|
|
|
|
gen_opi(op);
|
|
|
|
} else {
|
|
|
|
vtop--;
|
|
|
|
}
|
2001-11-06 04:20:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-17 14:15:05 +03: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 */
|
2002-07-14 18:38:33 +04:00
|
|
|
c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
|
|
|
|
c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
|
2002-03-17 14:15:05 +03:00
|
|
|
if (c1 && c2) {
|
2002-11-19 00:46:44 +03:00
|
|
|
if (v1->type.t == VT_FLOAT) {
|
2002-03-17 14:15:05 +03:00
|
|
|
f1 = v1->c.f;
|
|
|
|
f2 = v2->c.f;
|
2002-11-19 00:46:44 +03:00
|
|
|
} else if (v1->type.t == VT_DOUBLE) {
|
2002-03-17 14:15:05 +03:00
|
|
|
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 14:32:48 +03:00
|
|
|
if (!ieee_finite(f1) || !ieee_finite(f2))
|
2002-03-17 14:15:05 +03: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 ? */
|
2002-11-19 00:46:44 +03:00
|
|
|
if (v1->type.t == VT_FLOAT) {
|
2002-03-17 14:15:05 +03:00
|
|
|
v1->c.f = f1;
|
2002-11-19 00:46:44 +03:00
|
|
|
} else if (v1->type.t == VT_DOUBLE) {
|
2002-03-17 14:15:05 +03:00
|
|
|
v1->c.d = f1;
|
|
|
|
} else {
|
|
|
|
v1->c.ld = f1;
|
|
|
|
}
|
|
|
|
vtop--;
|
|
|
|
} else {
|
|
|
|
general_case:
|
2002-11-02 23:45:52 +03:00
|
|
|
if (!nocode_wanted) {
|
|
|
|
gen_opf(op);
|
|
|
|
} else {
|
|
|
|
vtop--;
|
|
|
|
}
|
2002-03-17 14:15:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
static int pointed_size(CType *type)
|
2001-11-06 04:20:20 +03:00
|
|
|
{
|
2002-11-19 00:46:44 +03:00
|
|
|
int align;
|
|
|
|
return type_size(pointed_type(type), &align);
|
2001-11-06 04:20:20 +03:00
|
|
|
}
|
|
|
|
|
2002-01-04 00:14:59 +03: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 04:20:20 +03:00
|
|
|
/* generic gen_op: handles types problems */
|
2001-11-11 05:53:01 +03:00
|
|
|
void gen_op(int op)
|
2001-11-06 04:20:20 +03:00
|
|
|
{
|
2001-12-13 00:16:17 +03:00
|
|
|
int u, t1, t2, bt1, bt2, t;
|
2002-11-19 00:46:44 +03:00
|
|
|
CType type1;
|
2001-11-06 04:20:20 +03:00
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
t1 = vtop[-1].type.t;
|
|
|
|
t2 = vtop[0].type.t;
|
2001-12-13 00:16:17 +03:00
|
|
|
bt1 = t1 & VT_BTYPE;
|
|
|
|
bt2 = t2 & VT_BTYPE;
|
|
|
|
|
2002-01-04 00:14:59 +03: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 00:16:17 +03:00
|
|
|
}
|
2002-01-04 00:14:59 +03:00
|
|
|
/* if both pointers, then it must be the '-' op */
|
2002-11-19 00:46:44 +03:00
|
|
|
if (bt1 == VT_PTR && bt2 == VT_PTR) {
|
2001-11-06 04:20:20 +03:00
|
|
|
if (op != '-')
|
2002-01-04 00:14:59 +03:00
|
|
|
error("cannot use pointers here");
|
|
|
|
// check_pointer_types(vtop - 1, vtop);
|
2001-11-06 04:20:20 +03:00
|
|
|
/* XXX: check that types are compatible */
|
2002-11-19 00:46:44 +03:00
|
|
|
u = pointed_size(&vtop[-1].type);
|
2002-03-17 14:15:05 +03:00
|
|
|
gen_opic(op);
|
2001-12-16 20:58:32 +03:00
|
|
|
/* set to integer type */
|
2002-11-19 00:46:44 +03:00
|
|
|
vtop->type.t = VT_INT;
|
2001-12-20 04:05:21 +03:00
|
|
|
vpushi(u);
|
2001-11-06 04:20:20 +03:00
|
|
|
gen_op(TOK_PDIV);
|
2002-01-04 00:14:59 +03:00
|
|
|
} else {
|
|
|
|
/* exactly one pointer : must be '+' or '-'. */
|
|
|
|
if (op != '-' && op != '+')
|
|
|
|
error("cannot use pointers here");
|
|
|
|
/* Put pointer as first operand */
|
2002-11-19 00:46:44 +03:00
|
|
|
if (bt2 == VT_PTR) {
|
2001-12-16 20:58:32 +03:00
|
|
|
vswap();
|
2001-11-06 04:20:20 +03:00
|
|
|
swap(&t1, &t2);
|
|
|
|
}
|
2002-11-19 00:46:44 +03:00
|
|
|
type1 = vtop[-1].type;
|
2002-01-04 00:14:59 +03:00
|
|
|
/* XXX: cast to int ? (long long case) */
|
2002-11-19 00:46:44 +03:00
|
|
|
vpushi(pointed_size(&vtop[-1].type));
|
2001-11-06 04:20:20 +03:00
|
|
|
gen_op('*');
|
2002-03-04 01:45:55 +03:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2002-01-26 22:13:31 +03:00
|
|
|
/* if evaluating constant expression, no code should be
|
|
|
|
generated, so no bound check */
|
|
|
|
if (do_bounds_check && !const_wanted) {
|
2002-01-05 19:15:57 +03:00
|
|
|
/* if bounded pointers, we generate a special code to
|
|
|
|
test bounds */
|
2002-01-04 23:17:24 +03:00
|
|
|
if (op == '-') {
|
|
|
|
vpushi(0);
|
|
|
|
vswap();
|
|
|
|
gen_op('-');
|
|
|
|
}
|
2002-07-13 19:55:38 +04:00
|
|
|
gen_bounded_ptr_add();
|
|
|
|
} else
|
2002-03-04 01:45:55 +03:00
|
|
|
#endif
|
|
|
|
{
|
2002-03-17 14:15:05 +03:00
|
|
|
gen_opic(op);
|
2002-01-04 23:17:24 +03:00
|
|
|
}
|
2002-03-17 14:15:05 +03:00
|
|
|
/* put again type if gen_opic() swaped operands */
|
2002-11-19 00:46:44 +03:00
|
|
|
vtop->type = type1;
|
2002-01-04 00:14:59 +03: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 04:20:20 +03:00
|
|
|
} else {
|
2002-01-04 00:14:59 +03:00
|
|
|
t = VT_FLOAT;
|
2001-11-06 04:20:20 +03:00
|
|
|
}
|
2002-01-04 00:14:59 +03: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 04:20:20 +03:00
|
|
|
} else {
|
2002-01-04 00:14:59 +03: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 04:20:20 +03:00
|
|
|
if (op == TOK_SAR)
|
|
|
|
op = TOK_SHR;
|
|
|
|
else if (op == '/')
|
|
|
|
op = TOK_UDIV;
|
|
|
|
else if (op == '%')
|
|
|
|
op = TOK_UMOD;
|
2001-11-08 02:13:29 +03: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 04:20:20 +03:00
|
|
|
}
|
2002-01-04 00:14:59 +03:00
|
|
|
vswap();
|
2002-11-19 00:46:44 +03:00
|
|
|
type1.t = t;
|
|
|
|
gen_cast(&type1);
|
2002-01-04 00:14:59 +03:00
|
|
|
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)
|
2002-11-19 00:46:44 +03:00
|
|
|
type1.t = VT_INT;
|
|
|
|
gen_cast(&type1);
|
2002-01-04 00:14:59 +03:00
|
|
|
if (is_float(t))
|
2002-03-17 14:15:05 +03:00
|
|
|
gen_opif(op);
|
2002-01-04 00:14:59 +03:00
|
|
|
else if ((t & VT_BTYPE) == VT_LLONG)
|
|
|
|
gen_opl(op);
|
|
|
|
else
|
2002-03-17 14:15:05 +03:00
|
|
|
gen_opic(op);
|
2002-01-04 00:14:59 +03:00
|
|
|
if (op >= TOK_ULT && op <= TOK_GT) {
|
|
|
|
/* relationnal op: the result is an int */
|
2002-11-19 00:46:44 +03:00
|
|
|
vtop->type.t = VT_INT;
|
2002-01-04 00:14:59 +03:00
|
|
|
} else {
|
2002-11-19 00:46:44 +03:00
|
|
|
vtop->type.t = t;
|
2002-01-04 00:14:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generic itof for unsigned long long case */
|
|
|
|
void gen_cvt_itof1(int t)
|
|
|
|
{
|
|
|
|
GFuncContext gf;
|
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
|
2002-01-04 00:14:59 +03:00
|
|
|
(VT_LLONG | VT_UNSIGNED)) {
|
|
|
|
|
2002-01-26 21:05:29 +03:00
|
|
|
gfunc_start(&gf, FUNC_CDECL);
|
2002-01-04 00:14:59 +03:00
|
|
|
gfunc_param(&gf);
|
|
|
|
if (t == VT_FLOAT)
|
2002-11-19 00:46:44 +03:00
|
|
|
vpush_global_sym(&func_old_type, TOK___ulltof);
|
2002-01-04 00:14:59 +03:00
|
|
|
else if (t == VT_DOUBLE)
|
2002-11-19 00:46:44 +03:00
|
|
|
vpush_global_sym(&func_old_type, TOK___ulltod);
|
2002-01-04 00:14:59 +03:00
|
|
|
else
|
2002-11-19 00:46:44 +03:00
|
|
|
vpush_global_sym(&func_old_type, TOK___ulltold);
|
2002-01-04 00:14:59 +03:00
|
|
|
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-26 21:05:29 +03:00
|
|
|
gfunc_start(&gf, FUNC_CDECL);
|
2002-11-19 00:46:44 +03:00
|
|
|
st = vtop->type.t & VT_BTYPE;
|
2002-01-04 00:14:59 +03:00
|
|
|
gfunc_param(&gf);
|
|
|
|
if (st == VT_FLOAT)
|
2002-11-19 00:46:44 +03:00
|
|
|
vpush_global_sym(&func_old_type, TOK___fixunssfdi);
|
2002-01-04 00:14:59 +03:00
|
|
|
else if (st == VT_DOUBLE)
|
2002-11-19 00:46:44 +03:00
|
|
|
vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
|
2002-01-04 00:14:59 +03:00
|
|
|
else
|
2002-11-19 00:46:44 +03:00
|
|
|
vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
|
2002-01-04 00:14:59 +03:00
|
|
|
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 04:20:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
/* cast 'vtop' to 'type' */
|
|
|
|
static void gen_cast(CType *type)
|
2001-12-03 00:24:43 +03:00
|
|
|
{
|
2002-07-13 15:17:19 +04:00
|
|
|
int sbt, dbt, sf, df, c;
|
2002-01-04 00:14:59 +03:00
|
|
|
|
|
|
|
/* 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;
|
2002-11-19 00:46:44 +03:00
|
|
|
force_charshort_cast(vtop->type.t);
|
2002-01-04 00:14:59 +03:00
|
|
|
}
|
2002-11-19 00:46:44 +03:00
|
|
|
|
|
|
|
dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
|
|
|
|
sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
|
2002-01-04 00:14:59 +03:00
|
|
|
|
2002-11-02 23:45:52 +03:00
|
|
|
if (sbt != dbt && !nocode_wanted) {
|
2001-12-20 04:05:21 +03:00
|
|
|
sf = is_float(sbt);
|
|
|
|
df = is_float(dbt);
|
2002-07-14 18:38:33 +04:00
|
|
|
c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
|
2001-12-20 04:05:21 +03:00
|
|
|
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 */
|
|
|
|
if (c) {
|
2002-07-13 15:17:19 +04:00
|
|
|
switch(sbt) {
|
2001-12-20 04:05:21 +03:00
|
|
|
case VT_LLONG | VT_UNSIGNED:
|
|
|
|
case VT_LLONG:
|
2002-01-04 00:14:59 +03:00
|
|
|
/* XXX: add const cases for long long */
|
2001-12-20 04:05:21 +03: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 00:56:48 +03:00
|
|
|
}
|
2001-12-20 04:05:21 +03: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 00:56:48 +03:00
|
|
|
}
|
2001-12-20 04:05:21 +03:00
|
|
|
break;
|
2001-12-18 00:56:48 +03:00
|
|
|
}
|
2001-12-20 04:05:21 +03:00
|
|
|
} else {
|
|
|
|
do_itof:
|
2002-01-04 00:14:59 +03:00
|
|
|
gen_cvt_itof1(dbt);
|
2001-12-20 04:05:21 +03:00
|
|
|
}
|
|
|
|
} else if (sf) {
|
|
|
|
/* convert fp to int */
|
|
|
|
/* we handle char/short/etc... with generic code */
|
2002-07-13 15:17:19 +04:00
|
|
|
if (dbt != (VT_INT | VT_UNSIGNED) &&
|
|
|
|
dbt != (VT_LLONG | VT_UNSIGNED) &&
|
|
|
|
dbt != VT_LLONG)
|
|
|
|
dbt = VT_INT;
|
2001-12-20 04:05:21 +03:00
|
|
|
if (c) {
|
2002-07-13 15:17:19 +04:00
|
|
|
switch(dbt) {
|
2001-12-20 04:05:21 +03:00
|
|
|
case VT_LLONG | VT_UNSIGNED:
|
|
|
|
case VT_LLONG:
|
2002-01-04 00:14:59 +03:00
|
|
|
/* XXX: add const cases for long long */
|
2001-12-20 04:05:21 +03: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 01:28:53 +03:00
|
|
|
}
|
2001-12-20 04:05:21 +03:00
|
|
|
} else {
|
|
|
|
do_ftoi:
|
2002-07-13 15:17:19 +04:00
|
|
|
gen_cvt_ftoi1(dbt);
|
2001-12-20 04:05:21 +03:00
|
|
|
}
|
2002-11-19 00:46:44 +03:00
|
|
|
if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
|
2001-12-20 04:05:21 +03:00
|
|
|
/* additionnal cast for char/short/bool... */
|
2002-11-19 00:46:44 +03:00
|
|
|
vtop->type.t = dbt;
|
|
|
|
gen_cast(type);
|
2001-12-20 04:05:21 +03:00
|
|
|
}
|
2002-07-13 15:17:19 +04:00
|
|
|
} else if ((dbt & VT_BTYPE) == VT_LLONG) {
|
|
|
|
if ((sbt & VT_BTYPE) != VT_LLONG) {
|
|
|
|
/* scalar to long long */
|
|
|
|
if (c) {
|
|
|
|
if (sbt == (VT_INT | VT_UNSIGNED))
|
|
|
|
vtop->c.ll = vtop->c.ui;
|
|
|
|
else
|
|
|
|
vtop->c.ll = vtop->c.i;
|
2002-01-04 00:14:59 +03:00
|
|
|
} else {
|
2002-07-13 15:17:19 +04:00
|
|
|
/* machine independant conversion */
|
|
|
|
gv(RC_INT);
|
|
|
|
/* generate high word */
|
|
|
|
if (sbt == (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();
|
2002-01-04 00:14:59 +03:00
|
|
|
}
|
|
|
|
}
|
2001-12-20 04:05:21 +03:00
|
|
|
} else if (dbt == VT_BOOL) {
|
2002-01-04 00:14:59 +03:00
|
|
|
/* scalar to bool */
|
2001-12-20 04:05:21 +03:00
|
|
|
vpushi(0);
|
|
|
|
gen_op(TOK_NE);
|
2002-07-13 15:17:19 +04:00
|
|
|
} else if ((dbt & VT_BTYPE) == VT_BYTE ||
|
|
|
|
(dbt & VT_BTYPE) == VT_SHORT) {
|
2002-11-19 00:46:44 +03:00
|
|
|
force_charshort_cast(dbt);
|
2002-07-13 15:17:19 +04:00
|
|
|
} else if ((dbt & VT_BTYPE) == VT_INT) {
|
2002-01-04 00:14:59 +03:00
|
|
|
/* scalar to int */
|
|
|
|
if (sbt == VT_LLONG) {
|
|
|
|
/* from long long: just take low order word */
|
|
|
|
lexpand();
|
|
|
|
vpop();
|
2002-07-13 15:17:19 +04:00
|
|
|
}
|
|
|
|
/* if lvalue and single word type, nothing to do because
|
|
|
|
the lvalue already contains the real type size (see
|
|
|
|
VT_LVAL_xxx constants) */
|
2001-12-03 00:24:43 +03:00
|
|
|
}
|
|
|
|
}
|
2002-11-19 00:46:44 +03:00
|
|
|
vtop->type = *type;
|
2001-12-03 00:24:43 +03:00
|
|
|
}
|
|
|
|
|
2001-11-01 17:48:10 +03:00
|
|
|
/* return type size. Put alignment at 'a' */
|
2002-11-24 17:18:17 +03:00
|
|
|
static int type_size(CType *type, int *a)
|
2001-10-28 02:48:39 +03:00
|
|
|
{
|
2001-11-01 17:48:10 +03:00
|
|
|
Sym *s;
|
2001-12-09 01:51:04 +03:00
|
|
|
int bt;
|
2001-11-01 17:48:10 +03:00
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
bt = type->t & VT_BTYPE;
|
2001-12-09 01:51:04 +03:00
|
|
|
if (bt == VT_STRUCT) {
|
2001-11-01 17:48:10 +03:00
|
|
|
/* struct/union */
|
2002-11-19 00:46:44 +03:00
|
|
|
s = type->ref;
|
2002-11-24 17:18:17 +03:00
|
|
|
*a = s->r;
|
2001-11-01 17:48:10 +03:00
|
|
|
return s->c;
|
2001-12-10 01:04:15 +03:00
|
|
|
} else if (bt == VT_PTR) {
|
2002-11-19 00:46:44 +03:00
|
|
|
if (type->t & VT_ARRAY) {
|
|
|
|
s = type->ref;
|
|
|
|
return type_size(&s->type, a) * s->c;
|
2001-12-10 01:04:15 +03:00
|
|
|
} else {
|
2001-12-14 01:28:53 +03:00
|
|
|
*a = PTR_SIZE;
|
|
|
|
return PTR_SIZE;
|
2001-12-10 01:04:15 +03:00
|
|
|
}
|
2001-12-14 01:28:53 +03:00
|
|
|
} else if (bt == VT_LDOUBLE) {
|
|
|
|
*a = LDOUBLE_ALIGN;
|
|
|
|
return LDOUBLE_SIZE;
|
2001-12-16 20:58:32 +03:00
|
|
|
} else if (bt == VT_DOUBLE || bt == VT_LLONG) {
|
2002-11-24 17:18:17 +03:00
|
|
|
*a = 4; /* XXX: i386 specific */
|
2001-12-13 00:16:17 +03:00
|
|
|
return 8;
|
|
|
|
} else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
|
2001-11-06 04:20:20 +03:00
|
|
|
*a = 4;
|
|
|
|
return 4;
|
2001-12-09 01:51:04 +03:00
|
|
|
} else if (bt == VT_SHORT) {
|
2001-11-11 21:00:58 +03:00
|
|
|
*a = 2;
|
|
|
|
return 2;
|
2001-11-01 17:48:10 +03:00
|
|
|
} else {
|
2001-12-13 00:16:17 +03:00
|
|
|
/* char, void, function, _Bool */
|
2001-11-01 17:48:10 +03:00
|
|
|
*a = 1;
|
2001-10-28 02:48:39 +03:00
|
|
|
return 1;
|
2001-11-01 17:48:10 +03:00
|
|
|
}
|
2001-10-28 02:48:39 +03:00
|
|
|
}
|
|
|
|
|
2001-11-06 04:20:20 +03:00
|
|
|
/* return the pointed type of t */
|
2002-11-19 00:46:44 +03:00
|
|
|
static inline CType *pointed_type(CType *type)
|
2001-10-30 02:23:01 +03:00
|
|
|
{
|
2002-11-19 00:46:44 +03:00
|
|
|
return &type->ref->type;
|
2001-10-30 02:23:01 +03:00
|
|
|
}
|
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
/* modify type so that its it is a pointer to type. */
|
|
|
|
static void mk_pointer(CType *type)
|
2001-10-28 02:48:39 +03:00
|
|
|
{
|
2002-11-19 00:46:44 +03:00
|
|
|
Sym *s;
|
2002-11-20 03:29:04 +03:00
|
|
|
s = sym_push(SYM_FIELD, type, 0, -1);
|
2002-11-19 00:46:44 +03:00
|
|
|
type->t = VT_PTR | (type->t & ~VT_TYPE);
|
|
|
|
type->ref = s;
|
2001-10-28 02:48:39 +03:00
|
|
|
}
|
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
static int is_compatible_types(CType *type1, CType *type2)
|
2001-12-18 00:56:48 +03:00
|
|
|
{
|
|
|
|
Sym *s1, *s2;
|
2002-11-19 00:46:44 +03:00
|
|
|
int bt1, bt2, t1, t2;
|
2001-12-18 00:56:48 +03:00
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
t1 = type1->t & VT_TYPE;
|
|
|
|
t2 = type2->t & VT_TYPE;
|
2001-12-18 00:56:48 +03:00
|
|
|
bt1 = t1 & VT_BTYPE;
|
|
|
|
bt2 = t2 & VT_BTYPE;
|
|
|
|
if (bt1 == VT_PTR) {
|
2002-11-19 00:46:44 +03:00
|
|
|
type1 = pointed_type(type1);
|
2001-12-18 00:56:48 +03:00
|
|
|
/* if function, then convert implicitely to function pointer */
|
|
|
|
if (bt2 != VT_FUNC) {
|
|
|
|
if (bt2 != VT_PTR)
|
|
|
|
return 0;
|
2002-11-19 00:46:44 +03:00
|
|
|
type2 = pointed_type(type2);
|
2001-12-18 00:56:48 +03:00
|
|
|
}
|
|
|
|
/* void matches everything */
|
2002-11-19 00:46:44 +03:00
|
|
|
/* XXX: not fully compliant */
|
|
|
|
if ((type1->t & VT_TYPE) == VT_VOID || (type2->t & VT_TYPE) == VT_VOID)
|
2001-12-18 00:56:48 +03:00
|
|
|
return 1;
|
2002-11-19 00:46:44 +03:00
|
|
|
return is_compatible_types(type1, type2);
|
2002-07-13 15:17:19 +04:00
|
|
|
} else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
|
2001-12-18 00:56:48 +03:00
|
|
|
return (t2 == t1);
|
|
|
|
} else if (bt1 == VT_FUNC) {
|
|
|
|
if (bt2 != VT_FUNC)
|
|
|
|
return 0;
|
2002-11-19 00:46:44 +03:00
|
|
|
s1 = type1->ref;
|
|
|
|
s2 = type2->ref;
|
|
|
|
if (!is_compatible_types(&s1->type, &s2->type))
|
2001-12-18 00:56:48 +03:00
|
|
|
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;
|
2002-11-19 00:46:44 +03:00
|
|
|
if (!is_compatible_types(&s1->type, &s2->type))
|
2001-12-18 00:56:48 +03:00
|
|
|
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 00:14:59 +03:00
|
|
|
/* XXX: union */
|
2001-12-18 00:56:48 +03:00
|
|
|
/* XXX: add array and function pointers */
|
|
|
|
void type_to_str(char *buf, int buf_size,
|
2002-11-19 00:46:44 +03:00
|
|
|
CType *type, const char *varstr)
|
2001-12-18 00:56:48 +03:00
|
|
|
{
|
2002-11-19 00:46:44 +03:00
|
|
|
int bt, v, t;
|
2001-12-18 00:56:48 +03:00
|
|
|
Sym *s, *sa;
|
|
|
|
char buf1[256];
|
2002-01-04 01:43:10 +03:00
|
|
|
const char *tstr;
|
2001-12-18 00:56:48 +03:00
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
t = type->t & VT_TYPE;
|
2001-12-18 00:56:48 +03:00
|
|
|
bt = t & VT_BTYPE;
|
|
|
|
buf[0] = '\0';
|
|
|
|
if (t & VT_UNSIGNED)
|
2002-01-04 01:43:10 +03:00
|
|
|
pstrcat(buf, buf_size, "unsigned ");
|
2001-12-18 00:56:48 +03:00
|
|
|
switch(bt) {
|
|
|
|
case VT_VOID:
|
2002-01-04 01:43:10 +03:00
|
|
|
tstr = "void";
|
|
|
|
goto add_tstr;
|
2001-12-18 00:56:48 +03:00
|
|
|
case VT_BOOL:
|
2002-01-04 01:43:10 +03:00
|
|
|
tstr = "_Bool";
|
|
|
|
goto add_tstr;
|
2001-12-18 00:56:48 +03:00
|
|
|
case VT_BYTE:
|
2002-01-04 01:43:10 +03:00
|
|
|
tstr = "char";
|
|
|
|
goto add_tstr;
|
2001-12-18 00:56:48 +03:00
|
|
|
case VT_SHORT:
|
2002-01-04 01:43:10 +03:00
|
|
|
tstr = "short";
|
|
|
|
goto add_tstr;
|
2001-12-18 00:56:48 +03:00
|
|
|
case VT_INT:
|
2002-01-04 01:43:10 +03:00
|
|
|
tstr = "int";
|
|
|
|
goto add_tstr;
|
2001-12-18 00:56:48 +03:00
|
|
|
case VT_LONG:
|
2002-01-04 01:43:10 +03:00
|
|
|
tstr = "long";
|
|
|
|
goto add_tstr;
|
2001-12-18 00:56:48 +03:00
|
|
|
case VT_LLONG:
|
2002-01-04 01:43:10 +03:00
|
|
|
tstr = "long long";
|
|
|
|
goto add_tstr;
|
2001-12-18 00:56:48 +03:00
|
|
|
case VT_FLOAT:
|
2002-01-04 01:43:10 +03:00
|
|
|
tstr = "float";
|
|
|
|
goto add_tstr;
|
2001-12-18 00:56:48 +03:00
|
|
|
case VT_DOUBLE:
|
2002-01-04 01:43:10 +03:00
|
|
|
tstr = "double";
|
|
|
|
goto add_tstr;
|
2001-12-18 00:56:48 +03:00
|
|
|
case VT_LDOUBLE:
|
2002-01-04 01:43:10 +03:00
|
|
|
tstr = "long double";
|
|
|
|
add_tstr:
|
|
|
|
pstrcat(buf, buf_size, tstr);
|
2001-12-18 00:56:48 +03:00
|
|
|
break;
|
|
|
|
case VT_ENUM:
|
|
|
|
case VT_STRUCT:
|
|
|
|
if (bt == VT_STRUCT)
|
2002-01-04 01:43:10 +03:00
|
|
|
tstr = "struct ";
|
2001-12-18 00:56:48 +03:00
|
|
|
else
|
2002-01-04 01:43:10 +03:00
|
|
|
tstr = "enum ";
|
|
|
|
pstrcat(buf, buf_size, tstr);
|
2002-11-24 02:56:12 +03:00
|
|
|
v = type->ref->v & ~SYM_STRUCT;
|
2001-12-18 00:56:48 +03:00
|
|
|
if (v >= SYM_FIRST_ANOM)
|
2002-01-04 01:43:10 +03:00
|
|
|
pstrcat(buf, buf_size, "<anonymous>");
|
2001-12-18 00:56:48 +03:00
|
|
|
else
|
2002-01-04 01:43:10 +03:00
|
|
|
pstrcat(buf, buf_size, get_tok_str(v, NULL));
|
2001-12-18 00:56:48 +03:00
|
|
|
break;
|
|
|
|
case VT_FUNC:
|
2002-11-19 00:46:44 +03:00
|
|
|
s = type->ref;
|
|
|
|
type_to_str(buf, buf_size, &s->type, varstr);
|
2002-01-04 01:43:10 +03:00
|
|
|
pstrcat(buf, buf_size, "(");
|
2001-12-18 00:56:48 +03:00
|
|
|
sa = s->next;
|
|
|
|
while (sa != NULL) {
|
2002-11-19 00:46:44 +03:00
|
|
|
type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
|
2002-01-04 01:43:10 +03:00
|
|
|
pstrcat(buf, buf_size, buf1);
|
2001-12-18 00:56:48 +03:00
|
|
|
sa = sa->next;
|
|
|
|
if (sa)
|
2002-01-04 01:43:10 +03:00
|
|
|
pstrcat(buf, buf_size, ", ");
|
2001-12-18 00:56:48 +03:00
|
|
|
}
|
2002-01-04 01:43:10 +03:00
|
|
|
pstrcat(buf, buf_size, ")");
|
2001-12-18 00:56:48 +03:00
|
|
|
goto no_var;
|
|
|
|
case VT_PTR:
|
2002-11-19 00:46:44 +03:00
|
|
|
s = type->ref;
|
2002-01-04 01:43:10 +03:00
|
|
|
pstrcpy(buf1, sizeof(buf1), "*");
|
2001-12-18 00:56:48 +03:00
|
|
|
if (varstr)
|
2002-01-04 01:43:10 +03:00
|
|
|
pstrcat(buf1, sizeof(buf1), varstr);
|
2002-11-19 00:46:44 +03:00
|
|
|
type_to_str(buf, buf_size, &s->type, buf1);
|
2001-12-18 00:56:48 +03:00
|
|
|
goto no_var;
|
|
|
|
}
|
|
|
|
if (varstr) {
|
2002-01-04 01:43:10 +03:00
|
|
|
pstrcat(buf, buf_size, " ");
|
|
|
|
pstrcat(buf, buf_size, varstr);
|
2001-12-18 00:56:48 +03:00
|
|
|
}
|
|
|
|
no_var: ;
|
|
|
|
}
|
|
|
|
|
2002-01-04 00:29:54 +03:00
|
|
|
/* verify type compatibility to store vtop in 'dt' type, and generate
|
2002-01-04 00:14:59 +03:00
|
|
|
casts if needed. */
|
2002-11-19 00:46:44 +03:00
|
|
|
void gen_assign_cast(CType *dt)
|
2001-12-18 00:56:48 +03:00
|
|
|
{
|
2002-11-19 00:46:44 +03:00
|
|
|
CType *st;
|
2001-12-18 00:56:48 +03:00
|
|
|
char buf1[256], buf2[256];
|
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
st = &vtop->type; /* source type */
|
|
|
|
if ((dt->t & VT_BTYPE) == VT_PTR) {
|
2002-01-04 00:29:54 +03:00
|
|
|
/* special cases for pointers */
|
|
|
|
/* a function is implicitely a function pointer */
|
2002-11-19 00:46:44 +03:00
|
|
|
if ((st->t & VT_BTYPE) == VT_FUNC) {
|
2002-01-04 00:29:54 +03:00
|
|
|
if (!is_compatible_types(pointed_type(dt), st))
|
|
|
|
goto error;
|
|
|
|
else
|
|
|
|
goto type_ok;
|
|
|
|
}
|
|
|
|
/* '0' can also be a pointer */
|
2002-11-19 00:46:44 +03:00
|
|
|
if ((st->t & VT_BTYPE) == VT_INT &&
|
2002-07-14 18:38:33 +04:00
|
|
|
((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) &&
|
2002-01-04 00:29:54 +03:00
|
|
|
vtop->c.i == 0)
|
|
|
|
goto type_ok;
|
|
|
|
}
|
|
|
|
if (!is_compatible_types(dt, st)) {
|
|
|
|
error:
|
2001-12-18 00:56:48 +03: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 00:29:54 +03:00
|
|
|
type_ok:
|
2002-01-04 00:14:59 +03:00
|
|
|
gen_cast(dt);
|
2001-12-18 00:56:48 +03:00
|
|
|
}
|
|
|
|
|
2001-12-16 20:58:32 +03:00
|
|
|
/* store vtop in lvalue pushed on stack */
|
2001-12-03 00:24:43 +03:00
|
|
|
void vstore(void)
|
2001-10-28 02:48:39 +03:00
|
|
|
{
|
2002-01-04 00:14:59 +03:00
|
|
|
int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
|
2001-12-02 19:25:49 +03:00
|
|
|
GFuncContext gf;
|
2001-11-06 04:20:20 +03:00
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
ft = vtop[-1].type.t;
|
|
|
|
sbt = vtop->type.t & VT_BTYPE;
|
2002-01-04 00:14:59 +03:00
|
|
|
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;
|
2002-11-19 00:46:44 +03:00
|
|
|
vtop->type.t = ft & VT_TYPE;
|
2002-01-04 00:14:59 +03:00
|
|
|
} else {
|
|
|
|
delayed_cast = 0;
|
2002-11-19 00:46:44 +03:00
|
|
|
gen_assign_cast(&vtop[-1].type);
|
2002-01-04 00:14:59 +03:00
|
|
|
}
|
2001-12-18 00:56:48 +03:00
|
|
|
|
2002-01-04 00:14:59 +03:00
|
|
|
if (sbt == VT_STRUCT) {
|
2001-12-02 19:25:49 +03:00
|
|
|
/* if structure, only generate pointer */
|
|
|
|
/* structure assignment : generate memcpy */
|
|
|
|
/* XXX: optimize if small size */
|
2002-11-02 23:45:52 +03:00
|
|
|
if (!nocode_wanted) {
|
|
|
|
vdup();
|
|
|
|
gfunc_start(&gf, FUNC_CDECL);
|
|
|
|
/* type size */
|
2002-11-19 00:46:44 +03:00
|
|
|
size = type_size(&vtop->type, &align);
|
2002-11-02 23:45:52 +03:00
|
|
|
vpushi(size);
|
|
|
|
gfunc_param(&gf);
|
|
|
|
/* source */
|
2002-11-19 00:46:44 +03:00
|
|
|
vtop->type.t = VT_INT;
|
2002-11-02 23:45:52 +03:00
|
|
|
gaddrof();
|
|
|
|
gfunc_param(&gf);
|
|
|
|
/* destination */
|
|
|
|
vswap();
|
2002-11-19 00:46:44 +03:00
|
|
|
vtop->type.t = VT_INT;
|
2002-11-02 23:45:52 +03:00
|
|
|
gaddrof();
|
|
|
|
gfunc_param(&gf);
|
|
|
|
|
|
|
|
save_regs(0);
|
2002-11-19 00:46:44 +03:00
|
|
|
vpush_global_sym(&func_old_type, TOK_memcpy);
|
2002-11-02 23:45:52 +03:00
|
|
|
gfunc_call(&gf);
|
|
|
|
} else {
|
|
|
|
vswap();
|
|
|
|
vpop();
|
|
|
|
}
|
2001-12-16 20:58:32 +03:00
|
|
|
/* leave source on stack */
|
|
|
|
} else if (ft & VT_BITFIELD) {
|
2001-12-10 01:04:15 +03: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 */
|
2002-11-19 00:46:44 +03:00
|
|
|
vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
|
2001-12-16 20:58:32 +03:00
|
|
|
|
|
|
|
/* duplicate destination */
|
|
|
|
vdup();
|
|
|
|
vtop[-1] = vtop[-2];
|
2001-12-10 01:04:15 +03:00
|
|
|
|
|
|
|
/* mask and shift source */
|
2001-12-20 04:05:21 +03:00
|
|
|
vpushi((1 << bit_size) - 1);
|
2001-12-10 01:04:15 +03:00
|
|
|
gen_op('&');
|
2001-12-20 04:05:21 +03:00
|
|
|
vpushi(bit_pos);
|
2001-12-10 01:04:15 +03:00
|
|
|
gen_op(TOK_SHL);
|
|
|
|
/* load destination, mask and or with source */
|
2001-12-16 20:58:32 +03:00
|
|
|
vswap();
|
2001-12-20 04:05:21 +03:00
|
|
|
vpushi(~(((1 << bit_size) - 1) << bit_pos));
|
2001-12-10 01:04:15 +03:00
|
|
|
gen_op('&');
|
|
|
|
gen_op('|');
|
|
|
|
/* store result */
|
|
|
|
vstore();
|
2001-12-02 19:25:49 +03:00
|
|
|
} else {
|
2002-03-04 01:45:55 +03:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2002-01-05 19:15:57 +03:00
|
|
|
/* bound check case */
|
|
|
|
if (vtop[-1].r & VT_MUSTBOUND) {
|
|
|
|
vswap();
|
|
|
|
gbound();
|
|
|
|
vswap();
|
|
|
|
}
|
2002-03-04 01:45:55 +03:00
|
|
|
#endif
|
2002-11-02 23:45:52 +03:00
|
|
|
if (!nocode_wanted) {
|
|
|
|
rc = RC_INT;
|
|
|
|
if (is_float(ft))
|
|
|
|
rc = RC_FLOAT;
|
|
|
|
r = gv(rc); /* generate value */
|
|
|
|
/* if lvalue was saved on stack, must read it */
|
|
|
|
if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
|
|
|
|
SValue sv;
|
|
|
|
t = get_reg(RC_INT);
|
2002-11-19 00:46:44 +03:00
|
|
|
sv.type.t = VT_INT;
|
2002-11-02 23:45:52 +03:00
|
|
|
sv.r = VT_LOCAL | VT_LVAL;
|
|
|
|
sv.c.ul = vtop[-1].c.ul;
|
|
|
|
load(t, &sv);
|
|
|
|
vtop[-1].r = t | VT_LVAL;
|
|
|
|
}
|
|
|
|
store(r, vtop - 1);
|
|
|
|
/* two word case handling : store second register at word + 4 */
|
|
|
|
if ((ft & VT_BTYPE) == VT_LLONG) {
|
|
|
|
vswap();
|
|
|
|
/* convert to int to increment easily */
|
2002-11-19 00:46:44 +03:00
|
|
|
vtop->type.t = VT_INT;
|
2002-11-02 23:45:52 +03:00
|
|
|
gaddrof();
|
|
|
|
vpushi(4);
|
|
|
|
gen_op('+');
|
|
|
|
vtop->r |= VT_LVAL;
|
|
|
|
vswap();
|
|
|
|
/* XXX: it works because r2 is spilled last ! */
|
|
|
|
store(vtop->r2, vtop - 1);
|
|
|
|
}
|
2002-01-04 00:14:59 +03:00
|
|
|
}
|
|
|
|
vswap();
|
|
|
|
vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
|
|
|
|
vtop->r |= delayed_cast;
|
2001-10-28 04:15:41 +03:00
|
|
|
}
|
2001-11-06 04:20:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* post defines POST/PRE add. c is the token ++ or -- */
|
2001-12-10 01:04:15 +03:00
|
|
|
void inc(int post, int c)
|
2001-11-06 04:20:20 +03:00
|
|
|
{
|
|
|
|
test_lvalue();
|
2001-12-16 20:58:32 +03:00
|
|
|
vdup(); /* save lvalue */
|
2001-11-06 04:20:20 +03:00
|
|
|
if (post) {
|
2002-01-04 00:14:59 +03:00
|
|
|
gv_dup(); /* duplicate value */
|
|
|
|
vrotb(3);
|
|
|
|
vrotb(3);
|
2001-10-28 02:48:39 +03:00
|
|
|
}
|
2001-11-06 04:20:20 +03:00
|
|
|
/* add constant */
|
2001-12-20 04:05:21 +03:00
|
|
|
vpushi(c - TOK_MID);
|
2001-11-06 04:20:20 +03:00
|
|
|
gen_op('+');
|
|
|
|
vstore(); /* store value */
|
|
|
|
if (post)
|
2001-12-16 20:58:32 +03:00
|
|
|
vpop(); /* if post op, return saved value */
|
2001-10-28 02:48:39 +03:00
|
|
|
}
|
|
|
|
|
2002-01-04 00:14:59 +03: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");
|
2002-11-02 17:12:32 +03:00
|
|
|
ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
|
2002-01-04 00:14:59 +03:00
|
|
|
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-26 21:05:29 +03: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 00:14:59 +03: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(')');
|
|
|
|
}
|
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
/* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
|
|
|
|
static void struct_decl(CType *type, int u)
|
2001-11-01 17:48:10 +03:00
|
|
|
{
|
2002-11-19 00:46:44 +03:00
|
|
|
int a, v, size, align, maxalign, c, offset;
|
2001-12-10 01:04:15 +03:00
|
|
|
int bit_size, bit_pos, bsize, bt, lbit_pos;
|
2001-11-17 20:22:38 +03:00
|
|
|
Sym *s, *ss, **ps;
|
2002-01-04 00:14:59 +03:00
|
|
|
AttributeDef ad;
|
2002-11-19 00:46:44 +03:00
|
|
|
CType type1, btype;
|
2001-11-01 17:48:10 +03:00
|
|
|
|
|
|
|
a = tok; /* save decl type */
|
|
|
|
next();
|
|
|
|
if (tok != '{') {
|
|
|
|
v = tok;
|
|
|
|
next();
|
|
|
|
/* struct already defined ? return it */
|
|
|
|
/* XXX: check consistency */
|
2002-11-20 03:29:04 +03:00
|
|
|
s = struct_find(v);
|
2002-01-04 00:14:59 +03:00
|
|
|
if (s) {
|
2002-11-19 00:46:44 +03:00
|
|
|
if (s->type.t != a)
|
2001-11-01 17:48:10 +03:00
|
|
|
error("invalid type");
|
2001-11-11 19:07:43 +03:00
|
|
|
goto do_decl;
|
2001-11-01 17:48:10 +03:00
|
|
|
}
|
2001-11-06 04:20:20 +03:00
|
|
|
} else {
|
|
|
|
v = anon_sym++;
|
2001-11-01 17:48:10 +03:00
|
|
|
}
|
2002-11-19 00:46:44 +03:00
|
|
|
type1.t = a;
|
|
|
|
s = sym_push(v | SYM_STRUCT, &type1, 0, 0);
|
2001-11-01 17:48:10 +03:00
|
|
|
/* put struct/union/enum name in type */
|
2001-11-11 19:07:43 +03:00
|
|
|
do_decl:
|
2002-11-19 00:46:44 +03:00
|
|
|
type->t = u;
|
|
|
|
type->ref = s;
|
2001-11-01 17:48:10 +03:00
|
|
|
|
|
|
|
if (tok == '{') {
|
|
|
|
next();
|
2001-11-11 19:07:43 +03:00
|
|
|
if (s->c)
|
|
|
|
error("struct/union/enum already defined");
|
2001-11-01 17:48:10 +03:00
|
|
|
/* cannot be empty */
|
|
|
|
c = 0;
|
2002-11-24 17:18:17 +03:00
|
|
|
/* non empty enums are not allowed */
|
|
|
|
if (a == TOK_ENUM) {
|
|
|
|
for(;;) {
|
2001-11-01 17:48:10 +03:00
|
|
|
v = tok;
|
|
|
|
next();
|
|
|
|
if (tok == '=') {
|
|
|
|
next();
|
|
|
|
c = expr_const();
|
|
|
|
}
|
2001-12-08 18:04:01 +03:00
|
|
|
/* enum symbols have static storage */
|
2002-11-19 00:46:44 +03:00
|
|
|
ss = sym_push(v, &int_type, VT_CONST, c);
|
|
|
|
ss->type.t |= VT_STATIC;
|
2001-11-01 17:48:10 +03:00
|
|
|
if (tok == ',')
|
|
|
|
next();
|
|
|
|
c++;
|
2002-11-24 17:18:17 +03:00
|
|
|
if (tok == '}')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
skip('}');
|
|
|
|
} else {
|
|
|
|
maxalign = 1;
|
|
|
|
ps = &s->next;
|
|
|
|
bit_pos = 0;
|
|
|
|
offset = 0;
|
|
|
|
while (tok != '}') {
|
2002-11-19 00:46:44 +03:00
|
|
|
parse_btype(&btype, &ad);
|
2001-11-01 17:48:10 +03:00
|
|
|
while (1) {
|
2001-12-10 01:04:15 +03:00
|
|
|
bit_size = -1;
|
|
|
|
v = 0;
|
2002-11-19 00:46:44 +03:00
|
|
|
type1 = btype;
|
2001-12-10 01:04:15 +03:00
|
|
|
if (tok != ':') {
|
2002-11-19 00:46:44 +03:00
|
|
|
type_decl(&type1, &ad, &v, TYPE_DIRECT);
|
|
|
|
if ((type1.t & VT_BTYPE) == VT_FUNC ||
|
|
|
|
(type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN)))
|
2001-12-18 00:56:48 +03:00
|
|
|
error("invalid type for '%s'",
|
|
|
|
get_tok_str(v, NULL));
|
2001-12-10 01:04:15 +03:00
|
|
|
}
|
|
|
|
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 00:56:48 +03:00
|
|
|
get_tok_str(v, NULL));
|
2001-12-10 01:04:15 +03:00
|
|
|
if (v && bit_size == 0)
|
|
|
|
error("zero width for bit-field '%s'",
|
2001-12-18 00:56:48 +03:00
|
|
|
get_tok_str(v, NULL));
|
2001-12-10 01:04:15 +03:00
|
|
|
}
|
2002-11-19 00:46:44 +03:00
|
|
|
size = type_size(&type1, &align);
|
2001-12-10 01:04:15 +03:00
|
|
|
lbit_pos = 0;
|
|
|
|
if (bit_size >= 0) {
|
2002-11-19 00:46:44 +03:00
|
|
|
bt = type1.t & VT_BTYPE;
|
2001-12-10 01:04:15 +03:00
|
|
|
if (bt != VT_INT &&
|
|
|
|
bt != VT_BYTE &&
|
2002-11-24 17:18:17 +03:00
|
|
|
bt != VT_SHORT &&
|
|
|
|
bt != VT_ENUM)
|
2001-12-10 01:04:15 +03:00
|
|
|
error("bitfields must have scalar type");
|
|
|
|
bsize = size * 8;
|
|
|
|
if (bit_size > bsize) {
|
|
|
|
error("width of '%s' exceeds its type",
|
2001-12-18 00:56:48 +03:00
|
|
|
get_tok_str(v, NULL));
|
2001-12-10 01:04:15 +03: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 */
|
2002-11-19 00:46:44 +03:00
|
|
|
type1.t |= VT_BITFIELD |
|
2001-12-10 01:04:15 +03:00
|
|
|
(bit_pos << VT_STRUCT_SHIFT) |
|
|
|
|
(bit_size << (VT_STRUCT_SHIFT + 6));
|
|
|
|
bit_pos += bit_size;
|
|
|
|
}
|
2001-11-01 17:48:10 +03:00
|
|
|
} else {
|
2001-12-10 01:04:15 +03: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 00:56:48 +03:00
|
|
|
get_tok_str(v, NULL), offset);
|
2002-11-19 00:46:44 +03:00
|
|
|
if (type1.t & VT_BITFIELD) {
|
2001-12-10 01:04:15 +03:00
|
|
|
printf(" pos=%d size=%d",
|
2002-11-19 00:46:44 +03:00
|
|
|
(type1.t >> VT_STRUCT_SHIFT) & 0x3f,
|
|
|
|
(type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
|
2001-12-10 01:04:15 +03:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
#endif
|
2002-11-19 00:46:44 +03:00
|
|
|
ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
|
2001-12-10 01:04:15 +03:00
|
|
|
*ps = ss;
|
|
|
|
ps = &ss->next;
|
2001-11-01 17:48:10 +03:00
|
|
|
}
|
2002-11-24 17:18:17 +03:00
|
|
|
if (tok == ';' || tok == TOK_EOF)
|
2001-11-01 17:48:10 +03:00
|
|
|
break;
|
|
|
|
skip(',');
|
|
|
|
}
|
|
|
|
skip(';');
|
|
|
|
}
|
2002-11-24 17:18:17 +03:00
|
|
|
skip('}');
|
|
|
|
/* store size and alignment */
|
|
|
|
s->c = (c + maxalign - 1) & -maxalign;
|
|
|
|
s->r = maxalign;
|
2001-11-01 17:48:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-28 02:48:39 +03:00
|
|
|
/* return 0 if no type declaration. otherwise, return the basic type
|
|
|
|
and skip it.
|
|
|
|
*/
|
2002-11-19 00:46:44 +03:00
|
|
|
static int parse_btype(CType *type, AttributeDef *ad)
|
2001-10-28 02:48:39 +03:00
|
|
|
{
|
2001-12-22 20:05:23 +03:00
|
|
|
int t, u, type_found;
|
2001-11-01 17:48:10 +03:00
|
|
|
Sym *s;
|
2002-11-19 00:46:44 +03:00
|
|
|
CType type1;
|
2001-10-31 01:43:19 +03:00
|
|
|
|
2002-01-04 00:14:59 +03:00
|
|
|
memset(ad, 0, sizeof(AttributeDef));
|
2001-12-22 20:05:23 +03:00
|
|
|
type_found = 0;
|
2001-10-30 02:23:01 +03:00
|
|
|
t = 0;
|
|
|
|
while(1) {
|
2001-12-09 03:27:44 +03:00
|
|
|
switch(tok) {
|
|
|
|
/* basic types */
|
|
|
|
case TOK_CHAR:
|
|
|
|
u = VT_BYTE;
|
|
|
|
basic_type:
|
|
|
|
next();
|
|
|
|
basic_type1:
|
|
|
|
if ((t & VT_BTYPE) != 0)
|
2001-12-20 02:01:51 +03:00
|
|
|
error("too many basic types");
|
2001-12-09 03:27:44 +03: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 01:28:53 +03: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 00:16:17 +03:00
|
|
|
case TOK_BOOL:
|
|
|
|
u = VT_BOOL;
|
|
|
|
goto basic_type;
|
2001-12-09 03:27:44 +03:00
|
|
|
case TOK_FLOAT:
|
2001-12-13 00:16:17 +03:00
|
|
|
u = VT_FLOAT;
|
|
|
|
goto basic_type;
|
2001-12-09 03:27:44 +03:00
|
|
|
case TOK_DOUBLE:
|
2001-12-14 01:28:53 +03: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 03:27:44 +03:00
|
|
|
case TOK_ENUM:
|
2002-11-19 00:46:44 +03:00
|
|
|
struct_decl(&type1, VT_ENUM);
|
|
|
|
basic_type2:
|
|
|
|
u = type1.t;
|
|
|
|
type->ref = type1.ref;
|
2001-12-09 03:27:44 +03:00
|
|
|
goto basic_type1;
|
|
|
|
case TOK_STRUCT:
|
|
|
|
case TOK_UNION:
|
2002-11-19 00:46:44 +03:00
|
|
|
struct_decl(&type1, VT_STRUCT);
|
|
|
|
goto basic_type2;
|
2001-12-09 03:27:44 +03:00
|
|
|
|
|
|
|
/* type modifiers */
|
|
|
|
case TOK_CONST:
|
|
|
|
case TOK_VOLATILE:
|
|
|
|
case TOK_REGISTER:
|
|
|
|
case TOK_SIGNED:
|
2002-01-12 19:39:35 +03:00
|
|
|
case TOK___SIGNED__:
|
2001-12-09 03:27:44 +03:00
|
|
|
case TOK_AUTO:
|
|
|
|
case TOK_INLINE:
|
2002-01-12 19:39:35 +03:00
|
|
|
case TOK___INLINE__:
|
2001-12-09 03:27:44 +03: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 00:14:59 +03:00
|
|
|
/* GNUC attribute */
|
|
|
|
case TOK___ATTRIBUTE__:
|
|
|
|
parse_attribute(ad);
|
|
|
|
break;
|
2002-11-02 23:45:52 +03:00
|
|
|
/* GNUC typeof */
|
|
|
|
case TOK_TYPEOF:
|
|
|
|
next();
|
2002-11-19 00:46:44 +03:00
|
|
|
parse_expr_type(&type1);
|
|
|
|
goto basic_type2;
|
2001-12-09 03:27:44 +03:00
|
|
|
default:
|
|
|
|
s = sym_find(tok);
|
2002-11-19 00:46:44 +03:00
|
|
|
if (!s || !(s->type.t & VT_TYPEDEF))
|
2001-12-09 03:27:44 +03:00
|
|
|
goto the_end;
|
2002-11-19 00:46:44 +03:00
|
|
|
t |= (s->type.t & ~VT_TYPEDEF);
|
|
|
|
type->ref = s->type.ref;
|
2001-10-31 01:43:19 +03:00
|
|
|
next();
|
2001-12-09 03:27:44 +03:00
|
|
|
break;
|
2001-10-30 02:23:01 +03:00
|
|
|
}
|
2001-12-22 20:05:23 +03:00
|
|
|
type_found = 1;
|
2001-10-28 02:48:39 +03:00
|
|
|
}
|
2001-12-09 03:27:44 +03:00
|
|
|
the_end:
|
2001-12-14 01:28:53 +03:00
|
|
|
/* long is never used as type */
|
|
|
|
if ((t & VT_BTYPE) == VT_LONG)
|
|
|
|
t = (t & ~VT_BTYPE) | VT_INT;
|
2002-11-19 00:46:44 +03:00
|
|
|
type->t = t;
|
2001-12-22 20:05:23 +03:00
|
|
|
return type_found;
|
2001-10-28 02:48:39 +03:00
|
|
|
}
|
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
static void post_type(CType *type, AttributeDef *ad)
|
2001-11-06 04:20:20 +03:00
|
|
|
{
|
2002-11-19 00:46:44 +03:00
|
|
|
int n, l, t1;
|
2001-12-03 00:24:43 +03:00
|
|
|
Sym **plast, *s, *first;
|
2002-01-26 21:05:29 +03:00
|
|
|
AttributeDef ad1;
|
2002-11-19 00:46:44 +03:00
|
|
|
CType pt;
|
2001-11-06 04:20:20 +03:00
|
|
|
|
|
|
|
if (tok == '(') {
|
|
|
|
/* function declaration */
|
|
|
|
next();
|
|
|
|
l = 0;
|
2001-12-03 00:24:43 +03:00
|
|
|
first = NULL;
|
|
|
|
plast = &first;
|
2001-11-06 04:20:20 +03:00
|
|
|
while (tok != ')') {
|
|
|
|
/* read param name and compute offset */
|
2001-11-08 02:13:29 +03:00
|
|
|
if (l != FUNC_OLD) {
|
2002-01-26 21:05:29 +03:00
|
|
|
if (!parse_btype(&pt, &ad1)) {
|
2001-11-06 04:20:20 +03:00
|
|
|
if (l) {
|
|
|
|
error("invalid type");
|
|
|
|
} else {
|
2001-11-08 02:13:29 +03:00
|
|
|
l = FUNC_OLD;
|
2001-11-06 04:20:20 +03:00
|
|
|
goto old_proto;
|
|
|
|
}
|
|
|
|
}
|
2001-12-18 00:56:48 +03:00
|
|
|
l = FUNC_NEW;
|
2002-11-19 00:46:44 +03:00
|
|
|
if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
|
2001-11-06 04:20:20 +03:00
|
|
|
break;
|
2002-11-19 00:46:44 +03:00
|
|
|
type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
|
|
|
|
if ((pt.t & VT_BTYPE) == VT_VOID)
|
2001-12-18 00:56:48 +03:00
|
|
|
error("parameter declared as void");
|
2001-11-06 04:20:20 +03:00
|
|
|
} else {
|
|
|
|
old_proto:
|
|
|
|
n = tok;
|
2002-11-19 00:46:44 +03:00
|
|
|
pt.t = VT_INT;
|
2001-11-06 04:20:20 +03:00
|
|
|
next();
|
|
|
|
}
|
|
|
|
/* array must be transformed to pointer according to ANSI C */
|
2002-11-19 00:46:44 +03:00
|
|
|
pt.t &= ~VT_ARRAY;
|
|
|
|
s = sym_push(n | SYM_FIELD, &pt, 0, 0);
|
2001-12-03 00:24:43 +03:00
|
|
|
*plast = s;
|
|
|
|
plast = &s->next;
|
2001-11-06 04:20:20 +03:00
|
|
|
if (tok == ',') {
|
|
|
|
next();
|
2001-11-08 02:13:29 +03:00
|
|
|
if (l == FUNC_NEW && tok == TOK_DOTS) {
|
|
|
|
l = FUNC_ELLIPSIS;
|
2001-11-06 04:20:20 +03:00
|
|
|
next();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-12-18 00:56:48 +03:00
|
|
|
/* if no parameters, then old type prototype */
|
|
|
|
if (l == 0)
|
|
|
|
l = FUNC_OLD;
|
2001-11-06 04:20:20 +03:00
|
|
|
skip(')');
|
2002-11-19 00:46:44 +03:00
|
|
|
t1 = type->t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN);
|
|
|
|
type->t &= ~(VT_TYPEDEF | VT_STATIC | VT_EXTERN);
|
|
|
|
post_type(type, ad);
|
2001-11-08 02:13:29 +03:00
|
|
|
/* we push a anonymous symbol which will contain the function prototype */
|
2002-11-20 03:29:04 +03:00
|
|
|
s = sym_push(SYM_FIELD, type, ad->func_call, l);
|
2001-12-03 00:24:43 +03:00
|
|
|
s->next = first;
|
2002-11-19 00:46:44 +03:00
|
|
|
type->t = t1 | VT_FUNC;
|
|
|
|
type->ref = s;
|
2001-11-06 04:20:20 +03: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 */
|
2002-11-19 00:46:44 +03:00
|
|
|
t1 = type->t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN);
|
|
|
|
type->t &= ~(VT_TYPEDEF | VT_STATIC | VT_EXTERN);
|
|
|
|
post_type(type, ad);
|
2001-11-06 04:20:20 +03:00
|
|
|
|
|
|
|
/* we push a anonymous symbol which will contain the array
|
|
|
|
element type */
|
2002-11-20 03:29:04 +03:00
|
|
|
s = sym_push(SYM_FIELD, type, 0, n);
|
2002-11-19 00:46:44 +03:00
|
|
|
type->t = t1 | VT_ARRAY | VT_PTR;
|
|
|
|
type->ref = s;
|
2001-11-06 04:20:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
/* Parse a type declaration (except basic type), and return the type
|
|
|
|
in 'type'. 'td' is a bitmask indicating which kind of type decl is
|
|
|
|
expected. 'type' should contain the basic type. 'ad' is the
|
|
|
|
attribute definition of the basic type. It can be modified by
|
|
|
|
type_decl().
|
|
|
|
*/
|
|
|
|
static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
|
2001-10-28 02:48:39 +03:00
|
|
|
{
|
2001-11-08 02:13:29 +03:00
|
|
|
Sym *s;
|
2002-11-19 00:46:44 +03:00
|
|
|
CType type1, *type2;
|
2001-10-28 02:48:39 +03:00
|
|
|
|
|
|
|
while (tok == '*') {
|
|
|
|
next();
|
2001-11-14 01:54:35 +03:00
|
|
|
while (tok == TOK_CONST || tok == TOK_VOLATILE || tok == TOK_RESTRICT)
|
2001-11-11 19:07:43 +03:00
|
|
|
next();
|
2002-11-19 00:46:44 +03:00
|
|
|
mk_pointer(type);
|
2001-11-06 04:20:20 +03:00
|
|
|
}
|
2001-10-28 02:48:39 +03:00
|
|
|
|
|
|
|
/* recursive type */
|
|
|
|
/* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
|
2002-11-19 00:46:44 +03:00
|
|
|
type1.t = 0; /* XXX: same as int */
|
2001-10-28 02:48:39 +03:00
|
|
|
if (tok == '(') {
|
|
|
|
next();
|
2002-01-26 21:05:29 +03: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);
|
2002-11-19 00:46:44 +03:00
|
|
|
type_decl(&type1, ad, v, td);
|
2001-10-28 02:48:39 +03:00
|
|
|
skip(')');
|
|
|
|
} else {
|
|
|
|
/* type identifier */
|
2001-11-11 19:07:43 +03:00
|
|
|
if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
|
2001-10-30 02:23:01 +03:00
|
|
|
*v = tok;
|
2001-10-28 02:48:39 +03:00
|
|
|
next();
|
2001-11-11 19:07:43 +03:00
|
|
|
} else {
|
|
|
|
if (!(td & TYPE_ABSTRACT))
|
|
|
|
expect("identifier");
|
|
|
|
*v = 0;
|
2001-10-28 02:48:39 +03:00
|
|
|
}
|
|
|
|
}
|
2002-11-19 00:46:44 +03:00
|
|
|
post_type(type, ad);
|
2002-01-26 21:05:29 +03:00
|
|
|
if (tok == TOK___ATTRIBUTE__)
|
|
|
|
parse_attribute(ad);
|
2002-11-19 00:46:44 +03:00
|
|
|
if (!type1.t)
|
|
|
|
return;
|
|
|
|
/* append type at the end of type1 */
|
|
|
|
type2 = &type1;
|
|
|
|
for(;;) {
|
|
|
|
s = type2->ref;
|
|
|
|
type2 = &s->type;
|
|
|
|
if (!type2->t) {
|
|
|
|
*type2 = *type;
|
2001-11-08 02:13:29 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2002-11-19 00:46:44 +03:00
|
|
|
*type = type1;
|
2001-10-28 02:48:39 +03:00
|
|
|
}
|
|
|
|
|
2002-07-13 15:17:19 +04:00
|
|
|
/* compute the lvalue VT_LVAL_xxx needed to match type t. */
|
|
|
|
static int lvalue_type(int t)
|
|
|
|
{
|
|
|
|
int bt, r;
|
|
|
|
r = VT_LVAL;
|
|
|
|
bt = t & VT_BTYPE;
|
|
|
|
if (bt == VT_BYTE)
|
|
|
|
r |= VT_LVAL_BYTE;
|
|
|
|
else if (bt == VT_SHORT)
|
|
|
|
r |= VT_LVAL_SHORT;
|
|
|
|
else
|
|
|
|
return r;
|
|
|
|
if (t & VT_UNSIGNED)
|
|
|
|
r |= VT_LVAL_UNSIGNED;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* indirection with full error checking and bound check */
|
|
|
|
static void indir(void)
|
2001-10-28 02:48:39 +03:00
|
|
|
{
|
2002-11-19 00:46:44 +03:00
|
|
|
if ((vtop->type.t & VT_BTYPE) != VT_PTR)
|
2001-11-06 04:20:20 +03:00
|
|
|
expect("pointer");
|
2002-11-02 23:45:52 +03:00
|
|
|
if ((vtop->r & VT_LVAL) && !nocode_wanted)
|
2001-12-23 03:34:26 +03:00
|
|
|
gv(RC_INT);
|
2002-11-19 00:46:44 +03:00
|
|
|
vtop->type = *pointed_type(&vtop->type);
|
2002-01-05 19:15:57 +03:00
|
|
|
/* an array is never an lvalue */
|
2002-11-19 00:46:44 +03:00
|
|
|
if (!(vtop->type.t & VT_ARRAY)) {
|
|
|
|
vtop->r |= lvalue_type(vtop->type.t);
|
2002-01-05 19:15:57 +03:00
|
|
|
/* if bound checking, the referenced pointer must be checked */
|
|
|
|
if (do_bounds_check)
|
|
|
|
vtop->r |= VT_MUSTBOUND;
|
|
|
|
}
|
2001-10-28 02:48:39 +03:00
|
|
|
}
|
|
|
|
|
2001-12-18 00:56:48 +03: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;
|
2002-11-19 00:46:44 +03:00
|
|
|
CType type;
|
|
|
|
|
2001-12-18 00:56:48 +03:00
|
|
|
func_type = func->c;
|
|
|
|
if (func_type == FUNC_OLD ||
|
|
|
|
(func_type == FUNC_ELLIPSIS && arg == NULL)) {
|
|
|
|
/* default casting : only need to convert float to double */
|
2002-11-19 00:46:44 +03:00
|
|
|
if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
|
|
|
|
type.t = VT_DOUBLE;
|
|
|
|
gen_cast(&type);
|
|
|
|
}
|
2001-12-18 00:56:48 +03:00
|
|
|
} else if (arg == NULL) {
|
|
|
|
error("too many arguments to function");
|
|
|
|
} else {
|
2002-11-19 00:46:44 +03:00
|
|
|
gen_assign_cast(&arg->type);
|
2001-12-18 00:56:48 +03:00
|
|
|
}
|
2002-11-02 23:45:52 +03:00
|
|
|
if (!nocode_wanted) {
|
|
|
|
gfunc_param(gf);
|
|
|
|
} else {
|
|
|
|
vpop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse an expression of the form '(type)' or '(expr)' and return its
|
|
|
|
type */
|
2002-11-19 00:46:44 +03:00
|
|
|
static void parse_expr_type(CType *type)
|
2002-11-02 23:45:52 +03:00
|
|
|
{
|
2002-11-19 00:46:44 +03:00
|
|
|
int n;
|
2002-11-02 23:45:52 +03:00
|
|
|
AttributeDef ad;
|
|
|
|
|
|
|
|
skip('(');
|
2002-11-19 00:46:44 +03:00
|
|
|
if (parse_btype(type, &ad)) {
|
|
|
|
type_decl(type, &ad, &n, TYPE_ABSTRACT);
|
2002-11-02 23:45:52 +03:00
|
|
|
} else {
|
2002-11-19 00:46:44 +03:00
|
|
|
expr_type(type);
|
2002-11-02 23:45:52 +03:00
|
|
|
}
|
|
|
|
skip(')');
|
2002-11-19 00:46:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void vpush_tokc(int t)
|
|
|
|
{
|
|
|
|
CType type;
|
|
|
|
type.t = t;
|
|
|
|
vsetc(&type, VT_CONST, &tokc);
|
2001-12-18 00:56:48 +03:00
|
|
|
}
|
|
|
|
|
2002-11-02 23:45:52 +03:00
|
|
|
static void unary(void)
|
2001-10-28 02:48:39 +03:00
|
|
|
{
|
2002-11-19 00:46:44 +03:00
|
|
|
int n, t, align, size, r;
|
|
|
|
CType type;
|
2001-11-01 17:48:10 +03:00
|
|
|
Sym *s;
|
2001-12-02 19:25:49 +03:00
|
|
|
GFuncContext gf;
|
2002-01-04 00:14:59 +03:00
|
|
|
AttributeDef ad;
|
2001-10-28 02:48:39 +03:00
|
|
|
|
2002-11-24 02:12:26 +03:00
|
|
|
/* XXX: GCC 2.95.3 does not generate a table although it should be
|
|
|
|
better here */
|
|
|
|
switch(tok) {
|
|
|
|
case TOK_CINT:
|
|
|
|
case TOK_CCHAR:
|
|
|
|
case TOK_LCHAR:
|
2001-12-20 04:05:21 +03:00
|
|
|
vpushi(tokc.i);
|
2001-12-14 02:20:55 +03:00
|
|
|
next();
|
2002-11-24 02:12:26 +03:00
|
|
|
break;
|
|
|
|
case TOK_CUINT:
|
2002-11-19 00:46:44 +03:00
|
|
|
vpush_tokc(VT_INT | VT_UNSIGNED);
|
2002-01-04 00:14:59 +03:00
|
|
|
next();
|
2002-11-24 02:12:26 +03:00
|
|
|
break;
|
|
|
|
case TOK_CLLONG:
|
2002-11-19 00:46:44 +03:00
|
|
|
vpush_tokc(VT_LLONG);
|
2002-01-04 00:14:59 +03:00
|
|
|
next();
|
2002-11-24 02:12:26 +03:00
|
|
|
break;
|
|
|
|
case TOK_CULLONG:
|
2002-11-19 00:46:44 +03:00
|
|
|
vpush_tokc(VT_LLONG | VT_UNSIGNED);
|
2002-01-04 00:14:59 +03:00
|
|
|
next();
|
2002-11-24 02:12:26 +03:00
|
|
|
break;
|
|
|
|
case TOK_CFLOAT:
|
2002-11-19 00:46:44 +03:00
|
|
|
vpush_tokc(VT_FLOAT);
|
2001-12-14 02:20:55 +03:00
|
|
|
next();
|
2002-11-24 02:12:26 +03:00
|
|
|
break;
|
|
|
|
case TOK_CDOUBLE:
|
2002-11-19 00:46:44 +03:00
|
|
|
vpush_tokc(VT_DOUBLE);
|
2001-12-14 02:20:55 +03:00
|
|
|
next();
|
2002-11-24 02:12:26 +03:00
|
|
|
break;
|
|
|
|
case TOK_CLDOUBLE:
|
2002-11-19 00:46:44 +03:00
|
|
|
vpush_tokc(VT_LDOUBLE);
|
2001-10-28 04:15:41 +03:00
|
|
|
next();
|
2002-11-24 02:12:26 +03:00
|
|
|
break;
|
|
|
|
case TOK___FUNCTION__:
|
|
|
|
if (!gnu_ext)
|
|
|
|
goto tok_identifier;
|
|
|
|
/* fall thru */
|
|
|
|
case TOK___FUNC__:
|
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
int len;
|
|
|
|
/* special function name identifier */
|
|
|
|
len = strlen(funcname) + 1;
|
|
|
|
/* generate char[len] type */
|
|
|
|
type.t = VT_BYTE;
|
|
|
|
mk_pointer(&type);
|
|
|
|
type.t |= VT_ARRAY;
|
|
|
|
type.ref->c = len;
|
|
|
|
vpush_ref(&type, data_section, data_section->data_offset, len);
|
|
|
|
ptr = section_ptr_add(data_section, len);
|
|
|
|
memcpy(ptr, funcname, len);
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TOK_LSTR:
|
2001-11-29 01:48:43 +03:00
|
|
|
t = VT_INT;
|
|
|
|
goto str_init;
|
2002-11-24 02:12:26 +03:00
|
|
|
case TOK_STR:
|
2001-11-29 01:48:43 +03:00
|
|
|
/* string parsing */
|
|
|
|
t = VT_BYTE;
|
|
|
|
str_init:
|
2002-11-19 00:46:44 +03:00
|
|
|
type.t = t;
|
|
|
|
mk_pointer(&type);
|
|
|
|
type.t |= VT_ARRAY;
|
2002-08-18 18:34:57 +04:00
|
|
|
memset(&ad, 0, sizeof(AttributeDef));
|
2002-11-19 00:46:44 +03:00
|
|
|
decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
|
2002-11-24 02:12:26 +03:00
|
|
|
break;
|
|
|
|
case '(':
|
2001-10-28 02:48:39 +03:00
|
|
|
next();
|
2002-11-24 02:12:26 +03:00
|
|
|
/* cast ? */
|
|
|
|
if (parse_btype(&type, &ad)) {
|
|
|
|
type_decl(&type, &ad, &n, TYPE_ABSTRACT);
|
|
|
|
skip(')');
|
|
|
|
/* check ISOC99 compound literal */
|
|
|
|
if (tok == '{') {
|
2001-12-02 17:47:48 +03:00
|
|
|
/* data is allocated locally by default */
|
2002-11-24 02:12:26 +03:00
|
|
|
if (global_expr)
|
|
|
|
r = VT_CONST;
|
|
|
|
else
|
|
|
|
r = VT_LOCAL;
|
|
|
|
/* all except arrays are lvalues */
|
|
|
|
if (!(type.t & VT_ARRAY))
|
|
|
|
r |= lvalue_type(type.t);
|
|
|
|
memset(&ad, 0, sizeof(AttributeDef));
|
|
|
|
decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
|
2001-11-06 04:20:20 +03:00
|
|
|
} else {
|
2002-11-24 02:12:26 +03:00
|
|
|
unary();
|
|
|
|
gen_cast(&type);
|
2002-11-19 00:46:44 +03:00
|
|
|
}
|
2002-11-03 03:43:55 +03:00
|
|
|
} else {
|
2002-11-24 02:12:26 +03:00
|
|
|
gexpr();
|
|
|
|
skip(')');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
next();
|
|
|
|
unary();
|
|
|
|
indir();
|
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
next();
|
|
|
|
unary();
|
|
|
|
/* 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. */
|
|
|
|
/* arrays can also be used although they are not lvalues */
|
|
|
|
if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
|
|
|
|
!(vtop->type.t & VT_ARRAY))
|
|
|
|
test_lvalue();
|
|
|
|
mk_pointer(&vtop->type);
|
|
|
|
gaddrof();
|
|
|
|
break;
|
|
|
|
case '!':
|
|
|
|
next();
|
|
|
|
unary();
|
|
|
|
if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST)
|
|
|
|
vtop->c.i = !vtop->c.i;
|
|
|
|
else if ((vtop->r & VT_VALMASK) == VT_CMP)
|
|
|
|
vtop->c.i = vtop->c.i ^ 1;
|
|
|
|
else
|
|
|
|
vseti(VT_JMP, gtst(1, 0));
|
|
|
|
break;
|
|
|
|
case '~':
|
|
|
|
next();
|
|
|
|
unary();
|
|
|
|
vpushi(-1);
|
|
|
|
gen_op('^');
|
|
|
|
break;
|
|
|
|
case '+':
|
|
|
|
next();
|
|
|
|
/* in order to force cast, we add zero */
|
|
|
|
unary();
|
|
|
|
if ((vtop->type.t & VT_BTYPE) == VT_PTR)
|
|
|
|
error("pointer not accepted for unary plus");
|
|
|
|
vpushi(0);
|
|
|
|
gen_op('+');
|
|
|
|
break;
|
|
|
|
case TOK_SIZEOF:
|
|
|
|
case TOK_ALIGNOF:
|
|
|
|
t = tok;
|
|
|
|
next();
|
|
|
|
if (tok == '(') {
|
|
|
|
parse_expr_type(&type);
|
|
|
|
} else {
|
|
|
|
unary_type(&type);
|
|
|
|
}
|
|
|
|
size = type_size(&type, &align);
|
|
|
|
if (t == TOK_SIZEOF)
|
|
|
|
vpushi(size);
|
|
|
|
else
|
|
|
|
vpushi(align);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TOK_INC:
|
|
|
|
case TOK_DEC:
|
|
|
|
t = tok;
|
|
|
|
next();
|
|
|
|
unary();
|
|
|
|
inc(0, t);
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
next();
|
|
|
|
vpushi(0);
|
|
|
|
unary();
|
|
|
|
gen_op('-');
|
|
|
|
break;
|
|
|
|
case TOK_LAND:
|
|
|
|
if (!gnu_ext)
|
|
|
|
goto tok_identifier;
|
|
|
|
next();
|
|
|
|
/* allow to take the address of a label */
|
|
|
|
if (tok < TOK_UIDENT)
|
|
|
|
expect("label identifier");
|
|
|
|
s = label_find(tok);
|
|
|
|
if (!s) {
|
|
|
|
s = label_push(tok, LABEL_FORWARD);
|
2001-10-28 02:48:39 +03:00
|
|
|
}
|
2002-11-24 02:12:26 +03:00
|
|
|
if (!s->type.t) {
|
|
|
|
s->type.t = VT_VOID;
|
|
|
|
mk_pointer(&s->type);
|
|
|
|
s->type.t |= VT_STATIC;
|
|
|
|
}
|
|
|
|
vset(&s->type, VT_CONST | VT_SYM, 0);
|
|
|
|
vtop->sym = s;
|
|
|
|
next();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
tok_identifier:
|
|
|
|
t = tok;
|
|
|
|
next();
|
|
|
|
if (t < TOK_UIDENT)
|
|
|
|
expect("identifier");
|
|
|
|
s = sym_find(t);
|
|
|
|
if (!s) {
|
|
|
|
if (tok != '(')
|
|
|
|
error("'%s' undeclared", get_tok_str(t, NULL));
|
|
|
|
/* for simple function calls, we tolerate undeclared
|
|
|
|
external reference to int() function */
|
|
|
|
s = external_global_sym(t, &func_old_type, 0);
|
|
|
|
}
|
|
|
|
vset(&s->type, s->r, s->c);
|
|
|
|
/* if forward reference, we must point to s */
|
|
|
|
if (vtop->r & VT_SYM) {
|
|
|
|
vtop->sym = s;
|
|
|
|
vtop->c.ul = 0;
|
|
|
|
}
|
|
|
|
break;
|
2001-10-28 02:48:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* post operations */
|
2001-11-01 17:48:10 +03:00
|
|
|
while (1) {
|
2002-01-04 00:14:59 +03:00
|
|
|
if (tok == TOK_INC || tok == TOK_DEC) {
|
2001-11-06 04:20:20 +03:00
|
|
|
inc(1, tok);
|
2001-11-01 17:48:10 +03:00
|
|
|
next();
|
2002-01-04 00:14:59 +03:00
|
|
|
} else if (tok == '.' || tok == TOK_ARROW) {
|
2001-11-01 17:48:10 +03:00
|
|
|
/* field */
|
2001-11-06 04:20:20 +03:00
|
|
|
if (tok == TOK_ARROW)
|
|
|
|
indir();
|
|
|
|
test_lvalue();
|
2002-01-05 03:40:47 +03:00
|
|
|
gaddrof();
|
2001-11-01 17:48:10 +03:00
|
|
|
next();
|
|
|
|
/* expect pointer on structure */
|
2002-11-19 00:46:44 +03:00
|
|
|
if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
|
2001-11-01 17:48:10 +03:00
|
|
|
expect("struct or union");
|
2002-11-19 00:46:44 +03:00
|
|
|
s = vtop->type.ref;
|
2001-11-01 17:48:10 +03:00
|
|
|
/* find field */
|
|
|
|
tok |= SYM_FIELD;
|
2002-01-04 00:14:59 +03:00
|
|
|
while ((s = s->next) != NULL) {
|
2001-11-01 17:48:10 +03:00
|
|
|
if (s->v == tok)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!s)
|
|
|
|
error("field not found");
|
|
|
|
/* add field offset to pointer */
|
2002-11-19 00:46:44 +03:00
|
|
|
vtop->type = char_pointer_type; /* change type to 'char *' */
|
2001-12-20 04:05:21 +03:00
|
|
|
vpushi(s->c);
|
2001-11-06 04:20:20 +03:00
|
|
|
gen_op('+');
|
2001-11-01 17:48:10 +03:00
|
|
|
/* change type to field type, and set to lvalue */
|
2002-11-19 00:46:44 +03:00
|
|
|
vtop->type = s->type;
|
2001-11-11 05:53:01 +03:00
|
|
|
/* an array is never an lvalue */
|
2002-11-19 00:46:44 +03:00
|
|
|
if (!(vtop->type.t & VT_ARRAY)) {
|
|
|
|
vtop->r |= lvalue_type(vtop->type.t);
|
2002-09-08 16:42:36 +04:00
|
|
|
/* if bound checking, the referenced pointer must be checked */
|
|
|
|
if (do_bounds_check)
|
|
|
|
vtop->r |= VT_MUSTBOUND;
|
|
|
|
}
|
2001-11-01 17:48:10 +03:00
|
|
|
next();
|
|
|
|
} else if (tok == '[') {
|
2001-11-06 04:20:20 +03:00
|
|
|
next();
|
2001-12-18 00:56:48 +03:00
|
|
|
gexpr();
|
2001-11-06 04:20:20 +03:00
|
|
|
gen_op('+');
|
|
|
|
indir();
|
2001-11-01 17:48:10 +03:00
|
|
|
skip(']');
|
|
|
|
} else if (tok == '(') {
|
2001-12-20 04:05:21 +03:00
|
|
|
SValue ret;
|
2001-12-18 00:56:48 +03:00
|
|
|
Sym *sa;
|
2001-12-03 00:24:43 +03:00
|
|
|
|
2001-11-01 17:48:10 +03:00
|
|
|
/* function call */
|
2002-11-19 00:46:44 +03:00
|
|
|
if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
|
2001-12-09 01:51:04 +03:00
|
|
|
/* pointer test (no array accepted) */
|
2002-11-19 00:46:44 +03:00
|
|
|
if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
|
|
|
|
vtop->type = *pointed_type(&vtop->type);
|
|
|
|
if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
|
2001-12-05 03:45:08 +03:00
|
|
|
goto error_func;
|
|
|
|
} else {
|
|
|
|
error_func:
|
2001-12-08 23:24:33 +03:00
|
|
|
expect("function pointer");
|
2001-12-05 03:45:08 +03:00
|
|
|
}
|
2001-12-08 23:24:33 +03:00
|
|
|
} else {
|
2001-12-20 04:05:21 +03:00
|
|
|
vtop->r &= ~VT_LVAL; /* no lvalue */
|
2001-12-05 03:45:08 +03:00
|
|
|
}
|
2001-12-03 00:24:43 +03:00
|
|
|
/* get return type */
|
2002-11-19 00:46:44 +03:00
|
|
|
s = vtop->type.ref;
|
2002-11-02 23:45:52 +03:00
|
|
|
if (!nocode_wanted) {
|
|
|
|
save_regs(0); /* save used temporary registers */
|
|
|
|
gfunc_start(&gf, s->r);
|
|
|
|
}
|
2001-11-01 17:48:10 +03:00
|
|
|
next();
|
2001-12-18 00:56:48 +03:00
|
|
|
sa = s->next; /* first parameter */
|
2001-11-11 21:00:58 +03:00
|
|
|
#ifdef INVERT_FUNC_PARAMS
|
|
|
|
{
|
2002-06-30 21:34:30 +04:00
|
|
|
int parlevel;
|
2001-11-18 19:33:01 +03:00
|
|
|
Sym *args, *s1;
|
2002-06-30 21:34:30 +04:00
|
|
|
ParseState saved_parse_state;
|
|
|
|
TokenString str;
|
|
|
|
|
2001-11-11 21:00:58 +03:00
|
|
|
/* read each argument and store it on a stack */
|
|
|
|
args = NULL;
|
2002-07-13 15:17:19 +04:00
|
|
|
if (tok != ')') {
|
|
|
|
for(;;) {
|
|
|
|
tok_str_new(&str);
|
|
|
|
parlevel = 0;
|
|
|
|
while ((parlevel > 0 || (tok != ')' && tok != ',')) &&
|
2002-08-30 03:05:59 +04:00
|
|
|
tok != TOK_EOF) {
|
2002-07-13 15:17:19 +04:00
|
|
|
if (tok == '(')
|
|
|
|
parlevel++;
|
|
|
|
else if (tok == ')')
|
|
|
|
parlevel--;
|
|
|
|
tok_str_add_tok(&str);
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
tok_str_add(&str, -1); /* end of file added */
|
|
|
|
tok_str_add(&str, 0);
|
|
|
|
s1 = sym_push2(&args, 0, 0, (int)str.str);
|
|
|
|
s1->next = sa; /* add reference to argument */
|
|
|
|
if (sa)
|
|
|
|
sa = sa->next;
|
|
|
|
if (tok == ')')
|
|
|
|
break;
|
|
|
|
skip(',');
|
2001-11-11 21:00:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now generate code in reverse order by reading the stack */
|
2002-06-30 21:34:30 +04:00
|
|
|
save_parse_state(&saved_parse_state);
|
2001-11-11 21:00:58 +03:00
|
|
|
while (args) {
|
|
|
|
macro_ptr = (int *)args->c;
|
|
|
|
next();
|
|
|
|
expr_eq();
|
2001-12-08 18:04:01 +03:00
|
|
|
if (tok != -1)
|
|
|
|
expect("',' or ')'");
|
2001-12-18 00:56:48 +03:00
|
|
|
gfunc_param_typed(&gf, s, args->next);
|
2001-11-18 19:33:01 +03:00
|
|
|
s1 = args->prev;
|
2002-08-18 17:22:55 +04:00
|
|
|
tok_str_free((int *)args->c);
|
|
|
|
tcc_free(args);
|
2001-11-18 19:33:01 +03:00
|
|
|
args = s1;
|
2001-11-11 21:00:58 +03:00
|
|
|
}
|
2002-06-30 21:34:30 +04:00
|
|
|
restore_parse_state(&saved_parse_state);
|
2001-11-11 21:00:58 +03:00
|
|
|
}
|
2001-12-03 00:24:43 +03:00
|
|
|
#endif
|
|
|
|
/* compute first implicit argument if a structure is returned */
|
2002-11-19 00:46:44 +03:00
|
|
|
if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
|
2001-12-03 00:24:43 +03:00
|
|
|
/* get some space for the returned structure */
|
2002-11-19 00:46:44 +03:00
|
|
|
size = type_size(&s->type, &align);
|
2001-12-03 00:24:43 +03:00
|
|
|
loc = (loc - size) & -align;
|
2002-11-19 00:46:44 +03:00
|
|
|
ret.type = s->type;
|
2001-12-20 04:05:21 +03:00
|
|
|
ret.r = VT_LOCAL | VT_LVAL;
|
2001-12-03 00:24:43 +03:00
|
|
|
/* pass it as 'int' to avoid structure arg passing
|
|
|
|
problems */
|
2002-11-19 00:46:44 +03:00
|
|
|
vseti(VT_LOCAL, loc);
|
2001-12-20 04:05:21 +03:00
|
|
|
ret.c = vtop->c;
|
2002-11-02 23:45:52 +03:00
|
|
|
if (!nocode_wanted)
|
|
|
|
gfunc_param(&gf);
|
|
|
|
else
|
|
|
|
vtop--;
|
2001-12-03 00:24:43 +03:00
|
|
|
} else {
|
2002-11-19 00:46:44 +03:00
|
|
|
ret.type = s->type;
|
2002-01-04 00:14:59 +03:00
|
|
|
ret.r2 = VT_CONST;
|
2001-12-23 03:34:26 +03:00
|
|
|
/* return in register */
|
2002-11-19 00:46:44 +03:00
|
|
|
if (is_float(ret.type.t)) {
|
2001-12-23 03:34:26 +03:00
|
|
|
ret.r = REG_FRET;
|
2002-01-04 00:14:59 +03:00
|
|
|
} else {
|
2002-11-19 00:46:44 +03:00
|
|
|
if ((ret.type.t & VT_BTYPE) == VT_LLONG)
|
2002-01-04 00:14:59 +03:00
|
|
|
ret.r2 = REG_LRET;
|
2001-12-23 03:34:26 +03:00
|
|
|
ret.r = REG_IRET;
|
2002-01-04 00:14:59 +03:00
|
|
|
}
|
2001-12-20 04:05:21 +03:00
|
|
|
ret.c.i = 0;
|
2001-12-03 00:24:43 +03:00
|
|
|
}
|
|
|
|
#ifndef INVERT_FUNC_PARAMS
|
2002-07-13 15:17:19 +04:00
|
|
|
if (tok != ')') {
|
|
|
|
for(;;) {
|
|
|
|
expr_eq();
|
|
|
|
gfunc_param_typed(&gf, s, sa);
|
|
|
|
if (sa)
|
|
|
|
sa = sa->next;
|
|
|
|
if (tok == ')')
|
|
|
|
break;
|
|
|
|
skip(',');
|
|
|
|
}
|
2001-11-01 17:48:10 +03:00
|
|
|
}
|
2001-11-11 21:00:58 +03:00
|
|
|
#endif
|
2001-12-18 00:56:48 +03:00
|
|
|
if (sa)
|
2001-12-20 02:01:51 +03:00
|
|
|
error("too few arguments to function");
|
2001-11-01 17:48:10 +03:00
|
|
|
skip(')');
|
2002-11-02 23:45:52 +03:00
|
|
|
if (!nocode_wanted)
|
|
|
|
gfunc_call(&gf);
|
|
|
|
else
|
|
|
|
vtop--;
|
2001-12-03 00:24:43 +03:00
|
|
|
/* return value */
|
2002-11-19 00:46:44 +03:00
|
|
|
vsetc(&ret.type, ret.r, &ret.c);
|
2002-01-04 00:14:59 +03:00
|
|
|
vtop->r2 = ret.r2;
|
2001-10-28 02:48:39 +03:00
|
|
|
} else {
|
2001-11-01 17:48:10 +03:00
|
|
|
break;
|
2001-10-28 02:48:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-02 23:45:52 +03:00
|
|
|
static void uneq(void)
|
2001-10-28 02:48:39 +03:00
|
|
|
{
|
2001-11-06 04:20:20 +03:00
|
|
|
int t;
|
2001-10-28 02:48:39 +03:00
|
|
|
|
|
|
|
unary();
|
2001-12-10 01:04:15 +03: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 18:20:12 +03:00
|
|
|
test_lvalue();
|
2001-11-06 04:20:20 +03:00
|
|
|
t = tok;
|
|
|
|
next();
|
|
|
|
if (t == '=') {
|
2001-10-29 01:24:16 +03:00
|
|
|
expr_eq();
|
2001-11-06 04:20:20 +03:00
|
|
|
} else {
|
2001-12-16 20:58:32 +03:00
|
|
|
vdup();
|
2001-11-06 04:20:20 +03:00
|
|
|
expr_eq();
|
|
|
|
gen_op(t & 0x7f);
|
|
|
|
}
|
|
|
|
vstore();
|
2001-10-28 02:48:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-24 01:26:46 +03:00
|
|
|
static void expr_prod(void)
|
2001-10-28 02:48:39 +03:00
|
|
|
{
|
2001-11-11 05:53:01 +03:00
|
|
|
int t;
|
2001-11-06 04:20:20 +03:00
|
|
|
|
2002-11-24 01:26:46 +03:00
|
|
|
uneq();
|
|
|
|
while (tok == '*' || tok == '/' || tok == '%') {
|
|
|
|
t = tok;
|
|
|
|
next();
|
2001-10-28 02:48:39 +03:00
|
|
|
uneq();
|
2002-11-24 01:26:46 +03:00
|
|
|
gen_op(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void expr_sum(void)
|
|
|
|
{
|
|
|
|
int t;
|
|
|
|
|
|
|
|
expr_prod();
|
|
|
|
while (tok == '+' || tok == '-') {
|
|
|
|
t = tok;
|
|
|
|
next();
|
|
|
|
expr_prod();
|
|
|
|
gen_op(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void expr_shift(void)
|
|
|
|
{
|
|
|
|
int t;
|
|
|
|
|
|
|
|
expr_sum();
|
|
|
|
while (tok == TOK_SHL || tok == TOK_SAR) {
|
|
|
|
t = tok;
|
|
|
|
next();
|
|
|
|
expr_sum();
|
|
|
|
gen_op(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void expr_cmp(void)
|
|
|
|
{
|
|
|
|
int t;
|
|
|
|
|
|
|
|
expr_shift();
|
|
|
|
while ((tok >= TOK_ULE && tok <= TOK_GT) ||
|
|
|
|
tok == TOK_ULT || tok == TOK_UGE) {
|
|
|
|
t = tok;
|
|
|
|
next();
|
|
|
|
expr_shift();
|
|
|
|
gen_op(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void expr_cmpeq(void)
|
|
|
|
{
|
|
|
|
int t;
|
|
|
|
|
|
|
|
expr_cmp();
|
|
|
|
while (tok == TOK_EQ || tok == TOK_NE) {
|
|
|
|
t = tok;
|
|
|
|
next();
|
|
|
|
expr_cmp();
|
|
|
|
gen_op(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void expr_and(void)
|
|
|
|
{
|
|
|
|
expr_cmpeq();
|
|
|
|
while (tok == '&') {
|
|
|
|
next();
|
|
|
|
expr_cmpeq();
|
|
|
|
gen_op('&');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void expr_xor(void)
|
|
|
|
{
|
|
|
|
expr_and();
|
|
|
|
while (tok == '^') {
|
|
|
|
next();
|
|
|
|
expr_and();
|
|
|
|
gen_op('^');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void expr_or(void)
|
|
|
|
{
|
|
|
|
expr_xor();
|
|
|
|
while (tok == '|') {
|
|
|
|
next();
|
|
|
|
expr_xor();
|
|
|
|
gen_op('|');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX: suppress this mess */
|
|
|
|
static void expr_land_const(void)
|
|
|
|
{
|
|
|
|
expr_or();
|
|
|
|
while (tok == TOK_LAND) {
|
|
|
|
next();
|
|
|
|
expr_or();
|
|
|
|
gen_op(TOK_LAND);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX: suppress this mess */
|
|
|
|
static void expr_lor_const(void)
|
|
|
|
{
|
|
|
|
expr_land_const();
|
|
|
|
while (tok == TOK_LOR) {
|
|
|
|
next();
|
|
|
|
expr_land_const();
|
|
|
|
gen_op(TOK_LOR);
|
2001-10-28 02:48:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-11 05:53:01 +03:00
|
|
|
/* only used if non constant */
|
2002-11-24 01:26:46 +03:00
|
|
|
static void expr_land(void)
|
2001-10-28 02:48:39 +03:00
|
|
|
{
|
|
|
|
int t;
|
|
|
|
|
2002-11-24 01:26:46 +03:00
|
|
|
expr_or();
|
2002-11-24 17:18:17 +03:00
|
|
|
if (tok == TOK_LAND) {
|
|
|
|
t = 0;
|
|
|
|
for(;;) {
|
|
|
|
t = gtst(1, t);
|
|
|
|
if (tok != TOK_LAND) {
|
2002-11-19 00:46:44 +03:00
|
|
|
vseti(VT_JMPI, t);
|
2002-11-24 17:18:17 +03:00
|
|
|
break;
|
2001-10-28 02:48:39 +03:00
|
|
|
}
|
2002-11-24 17:18:17 +03:00
|
|
|
next();
|
|
|
|
expr_or();
|
2001-10-28 02:48:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-24 01:26:46 +03:00
|
|
|
static void expr_lor(void)
|
2001-10-28 02:48:39 +03:00
|
|
|
{
|
2001-11-11 05:53:01 +03:00
|
|
|
int t;
|
2001-10-28 02:48:39 +03:00
|
|
|
|
2002-11-24 01:26:46 +03:00
|
|
|
expr_land();
|
2002-11-24 17:18:17 +03:00
|
|
|
if (tok == TOK_LOR) {
|
|
|
|
t = 0;
|
|
|
|
for(;;) {
|
|
|
|
t = gtst(0, t);
|
|
|
|
if (tok != TOK_LOR) {
|
2002-11-19 00:46:44 +03:00
|
|
|
vseti(VT_JMP, t);
|
2002-11-24 17:18:17 +03:00
|
|
|
break;
|
2001-10-28 02:48:39 +03:00
|
|
|
}
|
2002-11-24 17:18:17 +03:00
|
|
|
next();
|
|
|
|
expr_land();
|
2001-10-28 02:48:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-10-28 05:51:52 +03:00
|
|
|
|
2001-11-11 05:53:01 +03:00
|
|
|
/* XXX: better constant handling */
|
2002-11-02 23:45:52 +03:00
|
|
|
static void expr_eq(void)
|
2001-10-28 05:51:52 +03:00
|
|
|
{
|
2002-11-19 00:46:44 +03:00
|
|
|
int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
|
2002-09-08 16:42:36 +04:00
|
|
|
SValue sv;
|
2002-11-19 00:46:44 +03:00
|
|
|
CType type, type1, type2;
|
2001-11-11 05:53:01 +03:00
|
|
|
|
|
|
|
if (const_wanted) {
|
2002-09-08 16:42:36 +04:00
|
|
|
int c1, c;
|
2002-11-24 01:26:46 +03:00
|
|
|
expr_lor_const();
|
2001-11-11 05:53:01 +03:00
|
|
|
if (tok == '?') {
|
2001-12-18 00:56:48 +03:00
|
|
|
c = vtop->c.i;
|
2001-12-16 20:58:32 +03:00
|
|
|
vpop();
|
2001-11-11 05:53:01 +03:00
|
|
|
next();
|
2001-12-18 00:56:48 +03:00
|
|
|
gexpr();
|
2002-09-08 16:42:36 +04:00
|
|
|
c1 = vtop->c.i;
|
2001-12-16 20:58:32 +03:00
|
|
|
vpop();
|
2001-11-11 05:53:01 +03:00
|
|
|
skip(':');
|
|
|
|
expr_eq();
|
|
|
|
if (c)
|
2002-09-08 16:42:36 +04:00
|
|
|
vtop->c.i = c1;
|
2001-11-11 05:53:01 +03:00
|
|
|
}
|
|
|
|
} else {
|
2002-11-24 01:26:46 +03:00
|
|
|
expr_lor();
|
2001-11-11 05:53:01 +03:00
|
|
|
if (tok == '?') {
|
|
|
|
next();
|
2002-11-24 17:18:17 +03:00
|
|
|
if (vtop != vstack) {
|
|
|
|
/* needed to avoid having different registers saved in
|
|
|
|
each branch */
|
|
|
|
if (is_float(vtop->type.t))
|
|
|
|
rc = RC_FLOAT;
|
|
|
|
else
|
|
|
|
rc = RC_INT;
|
|
|
|
gv(rc);
|
|
|
|
save_regs(1);
|
|
|
|
}
|
2002-09-08 16:42:36 +04:00
|
|
|
tt = gtst(1, 0);
|
2001-12-18 00:56:48 +03:00
|
|
|
gexpr();
|
2002-11-19 00:46:44 +03:00
|
|
|
type1 = vtop->type;
|
2002-09-08 16:42:36 +04:00
|
|
|
sv = *vtop; /* save value to handle it later */
|
2002-01-26 21:05:29 +03:00
|
|
|
vtop--; /* no vpop so that FP stack is not flushed */
|
2001-11-11 05:53:01 +03:00
|
|
|
skip(':');
|
|
|
|
u = gjmp(0);
|
2002-09-08 16:42:36 +04:00
|
|
|
gsym(tt);
|
2001-11-11 05:53:01 +03:00
|
|
|
expr_eq();
|
2002-11-19 00:46:44 +03:00
|
|
|
type2 = vtop->type;
|
2002-09-08 16:42:36 +04:00
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
t1 = type1.t;
|
|
|
|
bt1 = t1 & VT_BTYPE;
|
|
|
|
t2 = type2.t;
|
2002-09-08 16:42:36 +04:00
|
|
|
bt2 = t2 & VT_BTYPE;
|
|
|
|
/* cast operands to correct type according to ISOC rules */
|
|
|
|
if (is_float(bt1) || is_float(bt2)) {
|
|
|
|
if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
|
2002-11-19 00:46:44 +03:00
|
|
|
type.t = VT_LDOUBLE;
|
2002-09-08 16:42:36 +04:00
|
|
|
} else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
|
2002-11-19 00:46:44 +03:00
|
|
|
type.t = VT_DOUBLE;
|
2002-09-08 16:42:36 +04:00
|
|
|
} else {
|
2002-11-19 00:46:44 +03:00
|
|
|
type.t = VT_FLOAT;
|
2002-09-08 16:42:36 +04:00
|
|
|
}
|
|
|
|
} else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
|
|
|
|
/* cast to biggest op */
|
2002-11-19 00:46:44 +03:00
|
|
|
type.t = VT_LLONG;
|
2002-09-08 16:42:36 +04:00
|
|
|
/* 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))
|
2002-11-19 00:46:44 +03:00
|
|
|
type.t |= VT_UNSIGNED;
|
2002-09-08 16:42:36 +04:00
|
|
|
} else if (bt1 == VT_PTR || bt2 == VT_PTR) {
|
|
|
|
/* XXX: test pointer compatibility */
|
2002-11-19 00:46:44 +03:00
|
|
|
type = type1;
|
2002-09-08 16:42:36 +04:00
|
|
|
} else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
|
|
|
|
/* XXX: test structure compatibility */
|
2002-11-19 00:46:44 +03:00
|
|
|
type = type1;
|
2002-09-08 16:42:36 +04:00
|
|
|
} else if (bt1 == VT_VOID || bt2 == VT_VOID) {
|
|
|
|
/* NOTE: as an extension, we accept void on only one side */
|
2002-11-19 00:46:44 +03:00
|
|
|
type.t = VT_VOID;
|
2002-09-08 16:42:36 +04:00
|
|
|
} else {
|
|
|
|
/* integer operations */
|
2002-11-19 00:46:44 +03:00
|
|
|
type.t = VT_INT;
|
2002-09-08 16:42:36 +04:00
|
|
|
/* 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))
|
2002-11-19 00:46:44 +03:00
|
|
|
type.t |= VT_UNSIGNED;
|
2002-09-08 16:42:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* now we convert second operand */
|
2002-11-19 00:46:44 +03:00
|
|
|
gen_cast(&type);
|
2002-09-08 16:42:36 +04:00
|
|
|
rc = RC_INT;
|
2002-11-19 00:46:44 +03:00
|
|
|
if (is_float(type.t)) {
|
2002-09-08 16:42:36 +04:00
|
|
|
rc = RC_FLOAT;
|
2002-11-19 00:46:44 +03:00
|
|
|
} else if ((type.t & VT_BTYPE) == VT_LLONG) {
|
2002-09-08 16:42:36 +04:00
|
|
|
/* for long longs, we use fixed registers to avoid having
|
|
|
|
to handle a complicated move */
|
|
|
|
rc = RC_IRET;
|
|
|
|
}
|
2002-11-24 17:18:17 +03:00
|
|
|
|
2001-12-23 03:34:26 +03:00
|
|
|
r2 = gv(rc);
|
2002-09-08 16:42:36 +04:00
|
|
|
/* this is horrible, but we must also convert first
|
|
|
|
operand */
|
|
|
|
tt = gjmp(0);
|
2001-11-11 05:53:01 +03:00
|
|
|
gsym(u);
|
2002-09-08 16:42:36 +04:00
|
|
|
/* put again first value and cast it */
|
|
|
|
*vtop = sv;
|
2002-11-19 00:46:44 +03:00
|
|
|
gen_cast(&type);
|
2002-09-08 16:42:36 +04:00
|
|
|
r1 = gv(rc);
|
|
|
|
move_reg(r2, r1);
|
|
|
|
vtop->r = r2;
|
|
|
|
gsym(tt);
|
2001-11-11 05:53:01 +03:00
|
|
|
}
|
2001-10-28 05:51:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-02 23:45:52 +03:00
|
|
|
static void gexpr(void)
|
2001-10-28 05:51:52 +03:00
|
|
|
{
|
|
|
|
while (1) {
|
|
|
|
expr_eq();
|
|
|
|
if (tok != ',')
|
|
|
|
break;
|
2001-12-16 20:58:32 +03:00
|
|
|
vpop();
|
2001-10-28 05:51:52 +03:00
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-02 23:45:52 +03:00
|
|
|
/* parse an expression and return its type without any side effect. */
|
2002-11-19 00:46:44 +03:00
|
|
|
static void expr_type(CType *type)
|
2002-11-02 23:45:52 +03:00
|
|
|
{
|
2002-11-19 00:46:44 +03:00
|
|
|
int a;
|
2002-11-02 23:45:52 +03:00
|
|
|
|
|
|
|
a = nocode_wanted;
|
|
|
|
nocode_wanted = 1;
|
|
|
|
gexpr();
|
2002-11-19 00:46:44 +03:00
|
|
|
*type = vtop->type;
|
2002-11-02 23:45:52 +03:00
|
|
|
vpop();
|
|
|
|
nocode_wanted = a;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse a unary expression and return its type without any side
|
|
|
|
effect. */
|
2002-11-19 00:46:44 +03:00
|
|
|
static void unary_type(CType *type)
|
2002-11-02 23:45:52 +03:00
|
|
|
{
|
2002-11-19 00:46:44 +03:00
|
|
|
int a;
|
2002-11-02 23:45:52 +03:00
|
|
|
|
|
|
|
a = nocode_wanted;
|
|
|
|
nocode_wanted = 1;
|
|
|
|
unary();
|
2002-11-19 00:46:44 +03:00
|
|
|
*type = vtop->type;
|
2002-11-02 23:45:52 +03:00
|
|
|
vpop();
|
|
|
|
nocode_wanted = a;
|
|
|
|
}
|
|
|
|
|
2002-08-30 03:05:59 +04:00
|
|
|
/* parse a constant expression and return value in vtop. */
|
|
|
|
static void expr_const1(void)
|
2001-11-11 05:53:01 +03:00
|
|
|
{
|
2001-12-18 00:56:48 +03:00
|
|
|
int a;
|
2001-11-11 05:53:01 +03:00
|
|
|
a = const_wanted;
|
|
|
|
const_wanted = 1;
|
|
|
|
expr_eq();
|
|
|
|
const_wanted = a;
|
2001-12-18 00:56:48 +03:00
|
|
|
}
|
|
|
|
|
2002-08-30 03:05:59 +04:00
|
|
|
/* parse an integer constant and return its value. */
|
|
|
|
static int expr_const(void)
|
2001-12-18 00:56:48 +03:00
|
|
|
{
|
|
|
|
int c;
|
|
|
|
expr_const1();
|
2002-08-30 03:05:59 +04:00
|
|
|
if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
|
|
|
|
expect("constant expression");
|
2001-12-18 00:56:48 +03:00
|
|
|
c = vtop->c.i;
|
2001-12-16 20:58:32 +03:00
|
|
|
vpop();
|
|
|
|
return c;
|
2001-11-11 05:53:01 +03:00
|
|
|
}
|
2001-10-28 02:48:39 +03:00
|
|
|
|
2001-12-05 05:02:25 +03:00
|
|
|
/* return the label token if current token is a label, otherwise
|
|
|
|
return zero */
|
2002-11-02 23:45:52 +03:00
|
|
|
static int is_label(void)
|
2001-12-05 05:02:25 +03:00
|
|
|
{
|
2001-12-14 01:28:53 +03:00
|
|
|
int t;
|
|
|
|
CValue c;
|
2001-12-05 05:02:25 +03:00
|
|
|
|
|
|
|
/* fast test first */
|
|
|
|
if (tok < TOK_UIDENT)
|
|
|
|
return 0;
|
2001-12-14 01:28:53 +03:00
|
|
|
/* no need to save tokc since we expect an identifier */
|
2001-12-05 05:02:25 +03: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-02 23:45:52 +03:00
|
|
|
static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg)
|
2001-10-28 02:48:39 +03:00
|
|
|
{
|
2001-10-30 23:27:54 +03:00
|
|
|
int a, b, c, d;
|
2001-11-01 17:48:10 +03:00
|
|
|
Sym *s;
|
2001-10-28 02:48:39 +03:00
|
|
|
|
2002-01-04 23:17:24 +03:00
|
|
|
/* generate line number info */
|
|
|
|
if (do_debug &&
|
2002-01-12 19:39:35 +03:00
|
|
|
(last_line_num != file->line_num || last_ind != ind)) {
|
|
|
|
put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
|
2002-01-04 23:17:24 +03:00
|
|
|
last_ind = ind;
|
2002-01-12 19:39:35 +03:00
|
|
|
last_line_num = file->line_num;
|
2002-01-04 23:17:24 +03:00
|
|
|
}
|
|
|
|
|
2001-10-28 02:48:39 +03:00
|
|
|
if (tok == TOK_IF) {
|
|
|
|
/* if test */
|
|
|
|
next();
|
|
|
|
skip('(');
|
2001-12-18 00:56:48 +03:00
|
|
|
gexpr();
|
2001-10-28 02:48:39 +03:00
|
|
|
skip(')');
|
|
|
|
a = gtst(1, 0);
|
2001-11-06 04:20:20 +03:00
|
|
|
block(bsym, csym, case_sym, def_sym, case_reg);
|
2001-10-28 02:48:39 +03:00
|
|
|
c = tok;
|
|
|
|
if (c == TOK_ELSE) {
|
|
|
|
next();
|
2001-11-06 04:20:20 +03:00
|
|
|
d = gjmp(0);
|
2001-10-28 02:48:39 +03:00
|
|
|
gsym(a);
|
2001-11-06 04:20:20 +03:00
|
|
|
block(bsym, csym, case_sym, def_sym, case_reg);
|
2001-10-28 02:48:39 +03:00
|
|
|
gsym(d); /* patch else jmp */
|
|
|
|
} else
|
|
|
|
gsym(a);
|
|
|
|
} else if (tok == TOK_WHILE) {
|
|
|
|
next();
|
|
|
|
d = ind;
|
|
|
|
skip('(');
|
2001-12-18 00:56:48 +03:00
|
|
|
gexpr();
|
2001-10-28 02:48:39 +03:00
|
|
|
skip(')');
|
2001-10-30 23:27:54 +03:00
|
|
|
a = gtst(1, 0);
|
|
|
|
b = 0;
|
2001-11-06 04:20:20 +03:00
|
|
|
block(&a, &b, case_sym, def_sym, case_reg);
|
2002-02-10 19:14:03 +03:00
|
|
|
gjmp_addr(d);
|
2001-10-30 23:27:54 +03:00
|
|
|
gsym(a);
|
|
|
|
gsym_addr(b, d);
|
2001-10-28 02:48:39 +03:00
|
|
|
} else if (tok == '{') {
|
|
|
|
next();
|
|
|
|
/* declarations */
|
2002-11-20 03:29:04 +03:00
|
|
|
s = local_stack;
|
2001-11-17 20:22:38 +03:00
|
|
|
while (tok != '}') {
|
|
|
|
decl(VT_LOCAL);
|
2001-12-05 03:45:08 +03:00
|
|
|
if (tok != '}')
|
|
|
|
block(bsym, csym, case_sym, def_sym, case_reg);
|
2001-11-17 20:22:38 +03:00
|
|
|
}
|
2001-11-01 17:48:10 +03:00
|
|
|
/* pop locally defined symbols */
|
|
|
|
sym_pop(&local_stack, s);
|
2001-10-28 02:48:39 +03:00
|
|
|
next();
|
|
|
|
} else if (tok == TOK_RETURN) {
|
|
|
|
next();
|
|
|
|
if (tok != ';') {
|
2001-12-18 00:56:48 +03:00
|
|
|
gexpr();
|
2002-11-19 00:46:44 +03:00
|
|
|
gen_assign_cast(&func_vt);
|
|
|
|
if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
|
|
|
|
CType type;
|
2001-12-03 00:24:43 +03:00
|
|
|
/* if returning structure, must copy it to implicit
|
|
|
|
first pointer arg location */
|
2002-11-19 00:46:44 +03:00
|
|
|
type = func_vt;
|
|
|
|
mk_pointer(&type);
|
|
|
|
vset(&type, VT_LOCAL | VT_LVAL, func_vc);
|
2001-12-03 00:24:43 +03:00
|
|
|
indir();
|
2001-12-16 20:58:32 +03:00
|
|
|
vswap();
|
2001-12-03 00:24:43 +03:00
|
|
|
/* copy structure value to pointer */
|
|
|
|
vstore();
|
2002-11-19 00:46:44 +03:00
|
|
|
} else if (is_float(func_vt.t)) {
|
2001-12-23 03:34:26 +03:00
|
|
|
gv(RC_FRET);
|
2001-12-03 00:24:43 +03:00
|
|
|
} else {
|
2001-12-23 03:34:26 +03:00
|
|
|
gv(RC_IRET);
|
2001-12-03 00:24:43 +03:00
|
|
|
}
|
2002-01-05 19:15:57 +03:00
|
|
|
vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
|
2001-10-28 02:48:39 +03:00
|
|
|
}
|
|
|
|
skip(';');
|
2001-11-06 04:20:20 +03:00
|
|
|
rsym = gjmp(rsym); /* jmp */
|
2001-10-28 02:48:39 +03:00
|
|
|
} else if (tok == TOK_BREAK) {
|
|
|
|
/* compute jump */
|
2001-10-30 23:27:54 +03:00
|
|
|
if (!bsym)
|
|
|
|
error("cannot break");
|
2001-11-06 04:20:20 +03:00
|
|
|
*bsym = gjmp(*bsym);
|
2001-10-30 23:27:54 +03:00
|
|
|
next();
|
|
|
|
skip(';');
|
|
|
|
} else if (tok == TOK_CONTINUE) {
|
|
|
|
/* compute jump */
|
|
|
|
if (!csym)
|
|
|
|
error("cannot continue");
|
2001-11-06 04:20:20 +03:00
|
|
|
*csym = gjmp(*csym);
|
2001-10-28 02:48:39 +03:00
|
|
|
next();
|
|
|
|
skip(';');
|
2001-11-11 05:53:01 +03:00
|
|
|
} else if (tok == TOK_FOR) {
|
2001-10-28 05:51:52 +03:00
|
|
|
int e;
|
|
|
|
next();
|
|
|
|
skip('(');
|
2001-12-16 20:58:32 +03:00
|
|
|
if (tok != ';') {
|
2001-12-18 00:56:48 +03:00
|
|
|
gexpr();
|
2001-12-16 20:58:32 +03:00
|
|
|
vpop();
|
|
|
|
}
|
2001-10-28 05:51:52 +03:00
|
|
|
skip(';');
|
|
|
|
d = ind;
|
|
|
|
c = ind;
|
|
|
|
a = 0;
|
2001-10-30 23:27:54 +03:00
|
|
|
b = 0;
|
2001-10-28 05:51:52 +03:00
|
|
|
if (tok != ';') {
|
2001-12-18 00:56:48 +03:00
|
|
|
gexpr();
|
2001-10-28 05:51:52 +03:00
|
|
|
a = gtst(1, 0);
|
|
|
|
}
|
|
|
|
skip(';');
|
|
|
|
if (tok != ')') {
|
2001-11-06 04:20:20 +03:00
|
|
|
e = gjmp(0);
|
2001-10-28 05:51:52 +03:00
|
|
|
c = ind;
|
2001-12-18 00:56:48 +03:00
|
|
|
gexpr();
|
2001-12-16 20:58:32 +03:00
|
|
|
vpop();
|
2002-02-10 19:14:03 +03:00
|
|
|
gjmp_addr(d);
|
2001-10-28 05:51:52 +03:00
|
|
|
gsym(e);
|
|
|
|
}
|
|
|
|
skip(')');
|
2001-11-06 04:20:20 +03:00
|
|
|
block(&a, &b, case_sym, def_sym, case_reg);
|
2002-02-10 19:14:03 +03:00
|
|
|
gjmp_addr(c);
|
2001-10-30 23:27:54 +03:00
|
|
|
gsym(a);
|
|
|
|
gsym_addr(b, c);
|
2001-10-28 05:51:52 +03:00
|
|
|
} else
|
2001-10-30 23:27:54 +03:00
|
|
|
if (tok == TOK_DO) {
|
|
|
|
next();
|
|
|
|
a = 0;
|
|
|
|
b = 0;
|
|
|
|
d = ind;
|
2001-11-06 04:20:20 +03:00
|
|
|
block(&a, &b, case_sym, def_sym, case_reg);
|
2001-10-30 23:27:54 +03:00
|
|
|
skip(TOK_WHILE);
|
|
|
|
skip('(');
|
|
|
|
gsym(b);
|
2001-12-18 00:56:48 +03:00
|
|
|
gexpr();
|
2001-10-30 23:27:54 +03:00
|
|
|
c = gtst(0, 0);
|
|
|
|
gsym_addr(c, d);
|
|
|
|
skip(')');
|
|
|
|
gsym(a);
|
2001-12-05 03:45:08 +03:00
|
|
|
skip(';');
|
2001-10-30 23:27:54 +03:00
|
|
|
} else
|
2001-10-31 01:43:19 +03:00
|
|
|
if (tok == TOK_SWITCH) {
|
|
|
|
next();
|
|
|
|
skip('(');
|
2001-12-18 00:56:48 +03:00
|
|
|
gexpr();
|
2002-01-04 00:14:59 +03:00
|
|
|
/* XXX: other types than integer */
|
2001-12-23 03:34:26 +03:00
|
|
|
case_reg = gv(RC_INT);
|
2001-12-16 20:58:32 +03:00
|
|
|
vpop();
|
2001-10-31 01:43:19 +03:00
|
|
|
skip(')');
|
|
|
|
a = 0;
|
2001-12-09 03:27:44 +03:00
|
|
|
b = gjmp(0); /* jump to first case */
|
2001-10-31 01:43:19 +03:00
|
|
|
c = 0;
|
2001-11-06 04:20:20 +03:00
|
|
|
block(&a, csym, &b, &c, case_reg);
|
2001-10-31 01:43:19 +03: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 00:14:59 +03:00
|
|
|
int v1, v2;
|
2001-10-31 01:43:19 +03:00
|
|
|
if (!case_sym)
|
2001-11-01 17:48:10 +03:00
|
|
|
expect("switch");
|
2002-01-04 00:14:59 +03: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 03:27:44 +03:00
|
|
|
/* since a case is like a label, we must skip it with a jmp */
|
|
|
|
b = gjmp(0);
|
2001-10-31 01:43:19 +03:00
|
|
|
gsym(*case_sym);
|
2002-11-19 00:46:44 +03:00
|
|
|
vseti(case_reg, 0);
|
2002-01-04 00:14:59 +03: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);
|
2002-11-19 00:46:44 +03:00
|
|
|
vseti(case_reg, 0);
|
2002-01-04 00:14:59 +03:00
|
|
|
vpushi(v2);
|
|
|
|
gen_op(TOK_LE);
|
|
|
|
*case_sym = gtst(1, *case_sym);
|
|
|
|
}
|
2001-12-09 03:27:44 +03:00
|
|
|
gsym(b);
|
2001-10-31 01:43:19 +03:00
|
|
|
skip(':');
|
2001-11-06 04:20:20 +03:00
|
|
|
block(bsym, csym, case_sym, def_sym, case_reg);
|
2001-10-31 01:43:19 +03:00
|
|
|
} else
|
|
|
|
if (tok == TOK_DEFAULT) {
|
|
|
|
next();
|
|
|
|
skip(':');
|
|
|
|
if (!def_sym)
|
2001-11-01 17:48:10 +03:00
|
|
|
expect("switch");
|
2001-10-31 01:43:19 +03:00
|
|
|
if (*def_sym)
|
|
|
|
error("too many 'default'");
|
|
|
|
*def_sym = ind;
|
2001-11-06 04:20:20 +03:00
|
|
|
block(bsym, csym, case_sym, def_sym, case_reg);
|
2001-10-31 01:43:19 +03:00
|
|
|
} else
|
|
|
|
if (tok == TOK_GOTO) {
|
|
|
|
next();
|
2002-11-03 03:43:55 +03:00
|
|
|
if (tok == '*' && gnu_ext) {
|
|
|
|
/* computed goto */
|
|
|
|
next();
|
|
|
|
gexpr();
|
2002-11-19 00:46:44 +03:00
|
|
|
if ((vtop->type.t & VT_BTYPE) != VT_PTR)
|
2002-11-03 03:43:55 +03:00
|
|
|
expect("pointer");
|
|
|
|
ggoto();
|
|
|
|
} else if (tok >= TOK_UIDENT) {
|
2002-11-20 03:29:04 +03:00
|
|
|
s = label_find(tok);
|
2002-11-03 03:43:55 +03:00
|
|
|
/* put forward definition if needed */
|
|
|
|
if (!s) {
|
2002-11-20 03:29:04 +03:00
|
|
|
s = label_push(tok, LABEL_FORWARD);
|
2002-11-03 03:43:55 +03:00
|
|
|
}
|
|
|
|
/* label already defined */
|
|
|
|
if (s->r & LABEL_FORWARD)
|
|
|
|
s->next = (void *)gjmp((long)s->next);
|
|
|
|
else
|
|
|
|
gjmp_addr((long)s->next);
|
|
|
|
next();
|
|
|
|
} else {
|
|
|
|
expect("label identifier");
|
|
|
|
}
|
2001-10-31 01:43:19 +03:00
|
|
|
skip(';');
|
2001-11-11 05:53:01 +03:00
|
|
|
} else {
|
2001-12-05 05:02:25 +03:00
|
|
|
b = is_label();
|
|
|
|
if (b) {
|
2001-10-31 01:43:19 +03:00
|
|
|
/* label case */
|
2002-11-20 03:29:04 +03:00
|
|
|
s = label_find(b);
|
2001-11-01 17:48:10 +03:00
|
|
|
if (s) {
|
2002-11-03 03:43:55 +03:00
|
|
|
if (!(s->r & LABEL_FORWARD))
|
2001-10-31 01:43:19 +03:00
|
|
|
error("multiple defined label");
|
2002-11-03 03:43:55 +03:00
|
|
|
gsym((long)s->next);
|
2001-11-01 17:48:10 +03:00
|
|
|
} else {
|
2002-11-20 03:29:04 +03:00
|
|
|
s = label_push(b, 0);
|
2001-10-31 01:43:19 +03:00
|
|
|
}
|
2002-11-03 03:43:55 +03:00
|
|
|
s->next = (void *)ind;
|
|
|
|
s->r = 0;
|
2001-12-05 05:02:25 +03: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 01:43:19 +03:00
|
|
|
} else {
|
2001-12-05 05:02:25 +03:00
|
|
|
/* expression case */
|
2001-10-31 01:43:19 +03:00
|
|
|
if (tok != ';') {
|
2001-12-18 00:56:48 +03:00
|
|
|
gexpr();
|
2001-12-16 20:58:32 +03:00
|
|
|
vpop();
|
2001-10-31 01:43:19 +03:00
|
|
|
}
|
|
|
|
skip(';');
|
|
|
|
}
|
2001-10-28 02:48:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-17 20:22:38 +03:00
|
|
|
/* t is the array or struct type. c is the array or struct
|
2001-11-27 01:57:27 +03: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) */
|
2002-11-19 00:46:44 +03:00
|
|
|
static void decl_designator(CType *type, Section *sec, unsigned long c,
|
2002-08-18 18:34:57 +04:00
|
|
|
int *cur_index, Sym **cur_field,
|
|
|
|
int size_only)
|
2001-11-17 20:22:38 +03:00
|
|
|
{
|
|
|
|
Sym *s, *f;
|
2001-12-05 05:02:25 +03:00
|
|
|
int notfirst, index, align, l;
|
2002-11-19 00:46:44 +03:00
|
|
|
CType type1;
|
2001-11-17 20:22:38 +03:00
|
|
|
|
|
|
|
notfirst = 0;
|
2001-12-05 05:02:25 +03:00
|
|
|
if (gnu_ext && (l = is_label()) != 0)
|
|
|
|
goto struct_field;
|
|
|
|
|
2001-11-17 20:22:38 +03:00
|
|
|
while (tok == '[' || tok == '.') {
|
|
|
|
if (tok == '[') {
|
2002-11-19 00:46:44 +03:00
|
|
|
if (!(type->t & VT_ARRAY))
|
2001-11-17 20:22:38 +03:00
|
|
|
expect("array type");
|
2002-11-19 00:46:44 +03:00
|
|
|
s = type->ref;
|
2001-11-17 20:22:38 +03:00
|
|
|
next();
|
|
|
|
index = expr_const();
|
|
|
|
if (index < 0 || (s->c >= 0 && index >= s->c))
|
|
|
|
expect("invalid index");
|
|
|
|
skip(']');
|
|
|
|
if (!notfirst)
|
|
|
|
*cur_index = index;
|
2002-11-19 00:46:44 +03:00
|
|
|
type = pointed_type(type);
|
|
|
|
c += index * type_size(type, &align);
|
2001-11-17 20:22:38 +03:00
|
|
|
} else {
|
2001-12-05 05:02:25 +03:00
|
|
|
next();
|
|
|
|
l = tok;
|
|
|
|
next();
|
|
|
|
struct_field:
|
2002-11-19 00:46:44 +03:00
|
|
|
if ((type->t & VT_BTYPE) != VT_STRUCT)
|
2001-11-17 20:22:38 +03:00
|
|
|
expect("struct/union type");
|
2002-11-19 00:46:44 +03:00
|
|
|
s = type->ref;
|
2001-12-05 05:02:25 +03:00
|
|
|
l |= SYM_FIELD;
|
2001-11-17 20:22:38 +03:00
|
|
|
f = s->next;
|
|
|
|
while (f) {
|
2001-12-05 05:02:25 +03:00
|
|
|
if (f->v == l)
|
2001-11-17 20:22:38 +03:00
|
|
|
break;
|
|
|
|
f = f->next;
|
|
|
|
}
|
|
|
|
if (!f)
|
|
|
|
expect("field");
|
|
|
|
if (!notfirst)
|
|
|
|
*cur_field = f;
|
2002-11-19 00:46:44 +03:00
|
|
|
/* XXX: suppress this mess by using explicit storage field */
|
|
|
|
type1 = f->type;
|
|
|
|
type1.t |= (type->t & ~VT_TYPE);
|
|
|
|
type = &type1;
|
2001-11-17 20:22:38 +03:00
|
|
|
c += f->c;
|
|
|
|
}
|
|
|
|
notfirst = 1;
|
|
|
|
}
|
|
|
|
if (notfirst) {
|
2001-12-05 05:02:25 +03:00
|
|
|
if (tok == '=') {
|
|
|
|
next();
|
|
|
|
} else {
|
|
|
|
if (!gnu_ext)
|
|
|
|
expect("=");
|
|
|
|
}
|
2001-11-17 20:22:38 +03:00
|
|
|
} else {
|
2002-11-19 00:46:44 +03:00
|
|
|
if (type->t & VT_ARRAY) {
|
2001-11-17 20:22:38 +03:00
|
|
|
index = *cur_index;
|
2002-11-19 00:46:44 +03:00
|
|
|
type = pointed_type(type);
|
|
|
|
c += index * type_size(type, &align);
|
2001-11-17 20:22:38 +03:00
|
|
|
} else {
|
|
|
|
f = *cur_field;
|
|
|
|
if (!f)
|
|
|
|
error("too many field init");
|
2002-11-19 00:46:44 +03:00
|
|
|
/* XXX: suppress this mess by using explicit storage field */
|
|
|
|
type1 = f->type;
|
|
|
|
type1.t |= (type->t & ~VT_TYPE);
|
|
|
|
type = &type1;
|
2001-11-17 20:22:38 +03:00
|
|
|
c += f->c;
|
|
|
|
}
|
|
|
|
}
|
2002-11-19 00:46:44 +03:00
|
|
|
decl_initializer(type, sec, c, 0, size_only);
|
2001-11-27 01:57:27 +03:00
|
|
|
}
|
|
|
|
|
2001-12-22 20:05:23 +03:00
|
|
|
#define EXPR_VAL 0
|
|
|
|
#define EXPR_CONST 1
|
|
|
|
#define EXPR_ANY 2
|
2001-11-27 01:57:27 +03:00
|
|
|
|
2001-12-22 20:05:23 +03:00
|
|
|
/* store a value or an expression directly in global data or in local array */
|
2002-11-19 00:46:44 +03:00
|
|
|
static void init_putv(CType *type, Section *sec, unsigned long c,
|
2002-08-18 18:34:57 +04:00
|
|
|
int v, int expr_type)
|
2001-11-27 01:57:27 +03:00
|
|
|
{
|
2001-12-18 00:56:48 +03:00
|
|
|
int saved_global_expr, bt;
|
2002-07-14 18:38:33 +04:00
|
|
|
void *ptr;
|
2001-12-02 17:47:48 +03:00
|
|
|
|
2001-12-22 20:05:23 +03: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;
|
2002-08-30 03:05:59 +04:00
|
|
|
/* NOTE: symbols are accepted */
|
|
|
|
if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
|
|
|
|
error("initializer element is not constant");
|
2001-12-22 20:05:23 +03:00
|
|
|
break;
|
|
|
|
case EXPR_ANY:
|
|
|
|
expr_eq();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-07-14 18:38:33 +04:00
|
|
|
if (sec) {
|
2001-12-18 00:56:48 +03:00
|
|
|
/* XXX: not portable */
|
2002-07-14 18:38:33 +04:00
|
|
|
/* XXX: generate error if incorrect relocation */
|
2002-11-19 00:46:44 +03:00
|
|
|
gen_assign_cast(type);
|
|
|
|
bt = type->t & VT_BTYPE;
|
2002-07-14 18:38:33 +04:00
|
|
|
ptr = sec->data + c;
|
|
|
|
if ((vtop->r & VT_SYM) &&
|
|
|
|
(bt == VT_BYTE ||
|
|
|
|
bt == VT_SHORT ||
|
|
|
|
bt == VT_DOUBLE ||
|
|
|
|
bt == VT_LDOUBLE ||
|
|
|
|
bt == VT_LLONG))
|
|
|
|
error("initializer element is not computable at load time");
|
2001-12-18 00:56:48 +03:00
|
|
|
switch(bt) {
|
|
|
|
case VT_BYTE:
|
2002-07-14 18:38:33 +04:00
|
|
|
*(char *)ptr = vtop->c.i;
|
2001-12-18 00:56:48 +03:00
|
|
|
break;
|
|
|
|
case VT_SHORT:
|
2002-07-14 18:38:33 +04:00
|
|
|
*(short *)ptr = vtop->c.i;
|
2001-12-18 00:56:48 +03:00
|
|
|
break;
|
|
|
|
case VT_DOUBLE:
|
2002-07-14 18:38:33 +04:00
|
|
|
*(double *)ptr = vtop->c.d;
|
2001-12-18 00:56:48 +03:00
|
|
|
break;
|
|
|
|
case VT_LDOUBLE:
|
2002-07-14 18:38:33 +04:00
|
|
|
*(long double *)ptr = vtop->c.ld;
|
2001-12-18 00:56:48 +03:00
|
|
|
break;
|
|
|
|
case VT_LLONG:
|
2002-07-14 18:38:33 +04:00
|
|
|
*(long long *)ptr = vtop->c.ll;
|
2001-12-18 00:56:48 +03:00
|
|
|
break;
|
|
|
|
default:
|
2002-07-14 18:38:33 +04:00
|
|
|
if (vtop->r & VT_SYM) {
|
2002-08-30 03:05:59 +04:00
|
|
|
greloc(sec, vtop->sym, c, R_DATA_32);
|
2002-07-14 18:38:33 +04:00
|
|
|
}
|
2002-08-30 03:05:59 +04:00
|
|
|
*(int *)ptr = vtop->c.i;
|
2001-12-18 00:56:48 +03:00
|
|
|
break;
|
|
|
|
}
|
2002-01-05 22:55:43 +03:00
|
|
|
vtop--;
|
2001-11-27 01:57:27 +03:00
|
|
|
} else {
|
2002-11-19 00:46:44 +03:00
|
|
|
vset(type, VT_LOCAL, c);
|
2001-12-22 20:05:23 +03:00
|
|
|
vswap();
|
2001-11-27 01:57:27 +03:00
|
|
|
vstore();
|
2001-12-16 20:58:32 +03:00
|
|
|
vpop();
|
2001-11-27 01:57:27 +03:00
|
|
|
}
|
2001-11-17 20:22:38 +03:00
|
|
|
}
|
|
|
|
|
2001-11-29 01:48:43 +03:00
|
|
|
/* put zeros for variable based init */
|
2002-11-19 00:46:44 +03:00
|
|
|
static void init_putz(CType *t, Section *sec, unsigned long c, int size)
|
2001-11-29 01:48:43 +03:00
|
|
|
{
|
2001-12-02 19:25:49 +03:00
|
|
|
GFuncContext gf;
|
2001-11-29 01:48:43 +03:00
|
|
|
|
2002-07-14 18:38:33 +04:00
|
|
|
if (sec) {
|
2002-01-05 22:55:43 +03:00
|
|
|
/* nothing to do because globals are already set to zero */
|
2001-11-29 01:48:43 +03:00
|
|
|
} else {
|
2002-01-26 21:05:29 +03:00
|
|
|
gfunc_start(&gf, FUNC_CDECL);
|
2001-12-20 04:05:21 +03:00
|
|
|
vpushi(size);
|
2001-12-02 19:25:49 +03:00
|
|
|
gfunc_param(&gf);
|
2001-12-20 04:05:21 +03:00
|
|
|
vpushi(0);
|
2001-12-02 19:25:49 +03:00
|
|
|
gfunc_param(&gf);
|
2002-11-19 00:46:44 +03:00
|
|
|
vseti(VT_LOCAL, c);
|
2001-12-02 19:25:49 +03:00
|
|
|
gfunc_param(&gf);
|
2002-11-19 00:46:44 +03:00
|
|
|
vpush_global_sym(&func_old_type, TOK_memset);
|
2001-12-08 23:24:33 +03:00
|
|
|
gfunc_call(&gf);
|
2001-11-29 01:48:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-14 18:38:33 +04:00
|
|
|
/* 't' contains the type and storage info. 'c' is the offset of the
|
|
|
|
object in section 'sec'. If 'sec' is NULL, it means stack based
|
|
|
|
allocation. 'first' is true if array '{' must be read (multi
|
|
|
|
dimension implicit array init handling). 'size_only' is true if
|
|
|
|
size only evaluation is wanted (only for arrays). */
|
2002-11-19 00:46:44 +03:00
|
|
|
static void decl_initializer(CType *type, Section *sec, unsigned long c,
|
2002-08-18 18:34:57 +04:00
|
|
|
int first, int size_only)
|
2001-11-17 20:22:38 +03:00
|
|
|
{
|
2001-11-29 01:48:43 +03:00
|
|
|
int index, array_length, n, no_oblock, nb, parlevel, i;
|
2002-11-19 00:46:44 +03:00
|
|
|
int size1, align1, expr_type;
|
2001-11-17 20:22:38 +03:00
|
|
|
Sym *s, *f;
|
2002-11-19 00:46:44 +03:00
|
|
|
CType *t1;
|
2001-11-17 20:22:38 +03:00
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
if (type->t & VT_ARRAY) {
|
|
|
|
s = type->ref;
|
2001-11-17 20:22:38 +03:00
|
|
|
n = s->c;
|
2001-11-27 01:57:27 +03:00
|
|
|
array_length = 0;
|
2002-11-19 00:46:44 +03:00
|
|
|
t1 = pointed_type(type);
|
2001-11-29 01:48:43 +03:00
|
|
|
size1 = type_size(t1, &align1);
|
2001-12-02 17:47:48 +03: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 &&
|
2002-11-19 00:46:44 +03:00
|
|
|
(t1->t & VT_BTYPE) == VT_INT) ||
|
2001-12-02 17:47:48 +03:00
|
|
|
(tok == TOK_STR &&
|
2002-11-19 00:46:44 +03:00
|
|
|
(t1->t & VT_BTYPE) == VT_BYTE)) {
|
2001-11-29 01:48:43 +03:00
|
|
|
while (tok == TOK_STR || tok == TOK_LSTR) {
|
2002-08-18 17:22:55 +04:00
|
|
|
int cstr_len, ch;
|
|
|
|
CString *cstr;
|
|
|
|
|
|
|
|
cstr = tokc.cstr;
|
2001-11-27 01:57:27 +03:00
|
|
|
/* compute maximum number of chars wanted */
|
2002-08-18 17:22:55 +04:00
|
|
|
if (tok == TOK_STR)
|
|
|
|
cstr_len = cstr->size;
|
|
|
|
else
|
|
|
|
cstr_len = cstr->size / sizeof(int);
|
|
|
|
cstr_len--;
|
|
|
|
nb = cstr_len;
|
2001-11-27 01:57:27 +03:00
|
|
|
if (n >= 0 && nb > (n - array_length))
|
|
|
|
nb = n - array_length;
|
|
|
|
if (!size_only) {
|
2002-08-18 17:22:55 +04:00
|
|
|
if (cstr_len > nb)
|
2001-11-27 01:57:27 +03:00
|
|
|
warning("initializer-string for array is too long");
|
2002-09-09 01:55:47 +04:00
|
|
|
/* in order to go faster for common case (char
|
|
|
|
string in global variable, we handle it
|
|
|
|
specifically */
|
|
|
|
if (sec && tok == TOK_STR && size1 == 1) {
|
|
|
|
memcpy(sec->data + c + array_length, cstr->data, nb);
|
|
|
|
} else {
|
|
|
|
for(i=0;i<nb;i++) {
|
|
|
|
if (tok == TOK_STR)
|
|
|
|
ch = ((unsigned char *)cstr->data)[i];
|
|
|
|
else
|
|
|
|
ch = ((int *)cstr->data)[i];
|
|
|
|
init_putv(t1, sec, c + (array_length + i) * size1,
|
|
|
|
ch, EXPR_VAL);
|
|
|
|
}
|
2001-11-27 01:57:27 +03:00
|
|
|
}
|
2001-11-17 20:22:38 +03:00
|
|
|
}
|
2001-11-27 01:57:27 +03: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) {
|
2002-07-14 18:38:33 +04:00
|
|
|
init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
|
2001-11-27 01:57:27 +03:00
|
|
|
}
|
|
|
|
array_length++;
|
2001-11-17 20:22:38 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
index = 0;
|
|
|
|
while (tok != '}') {
|
2002-11-19 00:46:44 +03:00
|
|
|
decl_designator(type, sec, c, &index, NULL, size_only);
|
2001-11-17 20:22:38 +03:00
|
|
|
if (n >= 0 && index >= n)
|
|
|
|
error("index too large");
|
2001-11-29 01:48:43 +03: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) {
|
2002-07-14 18:38:33 +04:00
|
|
|
init_putz(t1, sec, c + array_length * size1,
|
2001-11-29 01:48:43 +03:00
|
|
|
(index - array_length) * size1);
|
|
|
|
}
|
2001-11-17 20:22:38 +03:00
|
|
|
index++;
|
2001-11-27 01:57:27 +03:00
|
|
|
if (index > array_length)
|
|
|
|
array_length = index;
|
2001-11-17 20:22:38 +03: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 17:47:48 +03:00
|
|
|
if (!no_oblock)
|
|
|
|
skip('}');
|
2001-11-29 01:48:43 +03:00
|
|
|
/* put zeros at the end */
|
|
|
|
if (!size_only && n >= 0 && array_length < n) {
|
2002-07-14 18:38:33 +04:00
|
|
|
init_putz(t1, sec, c + array_length * size1,
|
2001-11-29 01:48:43 +03:00
|
|
|
(n - array_length) * size1);
|
|
|
|
}
|
2001-11-17 20:22:38 +03:00
|
|
|
/* patch type size if needed */
|
|
|
|
if (n < 0)
|
2001-11-27 01:57:27 +03:00
|
|
|
s->c = array_length;
|
2002-11-24 02:56:12 +03:00
|
|
|
} else if ((type->t & VT_BTYPE) == VT_STRUCT &&
|
|
|
|
(sec || !first || tok == '{')) {
|
|
|
|
/* NOTE: the previous test is a specific case for automatic
|
|
|
|
struct/union init */
|
2001-11-17 20:22:38 +03:00
|
|
|
/* XXX: union needs only one init */
|
2002-11-24 02:56:12 +03:00
|
|
|
/* XXX: handle bit fields */
|
|
|
|
no_oblock = 1;
|
|
|
|
if (first || tok == '{') {
|
|
|
|
skip('{');
|
|
|
|
no_oblock = 0;
|
|
|
|
}
|
2002-11-19 00:46:44 +03:00
|
|
|
s = type->ref;
|
2001-11-17 20:22:38 +03:00
|
|
|
f = s->next;
|
2001-11-29 01:48:43 +03:00
|
|
|
array_length = 0;
|
|
|
|
index = 0;
|
|
|
|
n = s->c;
|
2001-11-17 20:22:38 +03:00
|
|
|
while (tok != '}') {
|
2002-11-19 00:46:44 +03:00
|
|
|
decl_designator(type, sec, c, NULL, &f, size_only);
|
2002-11-24 02:56:12 +03:00
|
|
|
/* XXX: bitfields ? */
|
2001-11-29 01:48:43 +03:00
|
|
|
/* fill with zero between fields */
|
|
|
|
index = f->c;
|
|
|
|
if (!size_only && array_length < index) {
|
2002-11-19 00:46:44 +03:00
|
|
|
init_putz(type, sec, c + array_length,
|
2001-11-29 01:48:43 +03:00
|
|
|
index - array_length);
|
|
|
|
}
|
2002-11-19 00:46:44 +03:00
|
|
|
index = index + type_size(&f->type, &align1);
|
2001-11-29 01:48:43 +03:00
|
|
|
if (index > array_length)
|
|
|
|
array_length = index;
|
2002-11-24 02:56:12 +03:00
|
|
|
if (no_oblock && f->next == NULL)
|
|
|
|
break;
|
2001-11-17 20:22:38 +03:00
|
|
|
if (tok == '}')
|
|
|
|
break;
|
|
|
|
skip(',');
|
|
|
|
f = f->next;
|
|
|
|
}
|
2001-11-29 01:48:43 +03:00
|
|
|
/* put zeros at the end */
|
|
|
|
if (!size_only && array_length < n) {
|
2002-11-19 00:46:44 +03:00
|
|
|
init_putz(type, sec, c + array_length,
|
2001-11-29 01:48:43 +03:00
|
|
|
n - array_length);
|
|
|
|
}
|
2002-11-24 02:56:12 +03:00
|
|
|
if (!no_oblock)
|
|
|
|
skip('}');
|
2001-11-29 01:48:43 +03:00
|
|
|
} else if (tok == '{') {
|
|
|
|
next();
|
2002-11-19 00:46:44 +03:00
|
|
|
decl_initializer(type, sec, c, first, size_only);
|
2001-11-17 20:22:38 +03:00
|
|
|
skip('}');
|
2001-11-27 01:57:27 +03: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 01:48:43 +03:00
|
|
|
}
|
2001-11-17 20:22:38 +03:00
|
|
|
} else {
|
2001-12-22 20:05:23 +03:00
|
|
|
/* currently, we always use constant expression for globals
|
|
|
|
(may change for scripting case) */
|
|
|
|
expr_type = EXPR_CONST;
|
2002-07-14 18:38:33 +04:00
|
|
|
if (!sec)
|
2001-12-22 20:05:23 +03:00
|
|
|
expr_type = EXPR_ANY;
|
2002-11-19 00:46:44 +03:00
|
|
|
init_putv(type, sec, c, 0, expr_type);
|
2001-11-17 20:22:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-18 18:34:57 +04:00
|
|
|
/* parse an initializer for type 't' if 'has_init' is non zero, and
|
2001-12-22 20:05:23 +03:00
|
|
|
allocate space in local or global data space ('r' is either
|
2002-07-14 18:38:33 +04:00
|
|
|
VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
|
|
|
|
variable 'v' of scope 'scope' is declared before initializers are
|
|
|
|
parsed. If 'v' is zero, then a reference to the new object is put
|
2002-08-18 18:34:57 +04:00
|
|
|
in the value stack. If 'has_init' is 2, a special parsing is done
|
|
|
|
to handle string constants. */
|
2002-11-19 00:46:44 +03:00
|
|
|
static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
|
2002-08-18 18:34:57 +04:00
|
|
|
int has_init, int v, int scope)
|
2001-12-02 17:47:48 +03:00
|
|
|
{
|
2002-06-30 21:34:30 +04:00
|
|
|
int size, align, addr, data_offset;
|
|
|
|
int level;
|
|
|
|
ParseState saved_parse_state;
|
|
|
|
TokenString init_str;
|
2002-01-04 00:14:59 +03:00
|
|
|
Section *sec;
|
2001-12-02 17:47:48 +03:00
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
size = type_size(type, &align);
|
2001-12-02 17:47:48 +03:00
|
|
|
/* 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 */
|
2002-06-30 21:34:30 +04:00
|
|
|
tok_str_new(&init_str);
|
2001-12-02 17:47:48 +03:00
|
|
|
if (size < 0) {
|
|
|
|
if (!has_init)
|
|
|
|
error("unknown type size");
|
|
|
|
/* get all init string */
|
2002-08-18 18:34:57 +04:00
|
|
|
if (has_init == 2) {
|
|
|
|
/* only get strings */
|
|
|
|
while (tok == TOK_STR || tok == TOK_LSTR) {
|
|
|
|
tok_str_add_tok(&init_str);
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
level = 0;
|
|
|
|
while (level > 0 || (tok != ',' && tok != ';')) {
|
|
|
|
if (tok < 0)
|
|
|
|
error("unexpected end of file in initializer");
|
|
|
|
tok_str_add_tok(&init_str);
|
|
|
|
if (tok == '{')
|
|
|
|
level++;
|
|
|
|
else if (tok == '}') {
|
|
|
|
if (level == 0)
|
|
|
|
break;
|
|
|
|
level--;
|
|
|
|
}
|
|
|
|
next();
|
2001-12-02 17:47:48 +03:00
|
|
|
}
|
|
|
|
}
|
2002-06-30 21:34:30 +04:00
|
|
|
tok_str_add(&init_str, -1);
|
|
|
|
tok_str_add(&init_str, 0);
|
2001-12-02 17:47:48 +03:00
|
|
|
|
|
|
|
/* compute size */
|
2002-06-30 21:34:30 +04:00
|
|
|
save_parse_state(&saved_parse_state);
|
|
|
|
|
|
|
|
macro_ptr = init_str.str;
|
2001-12-02 17:47:48 +03:00
|
|
|
next();
|
2002-11-19 00:46:44 +03:00
|
|
|
decl_initializer(type, NULL, 0, 1, 1);
|
2001-12-02 17:47:48 +03:00
|
|
|
/* prepare second initializer parsing */
|
2002-06-30 21:34:30 +04:00
|
|
|
macro_ptr = init_str.str;
|
2001-12-02 17:47:48 +03:00
|
|
|
next();
|
|
|
|
|
|
|
|
/* if still unknown size, error */
|
2002-11-19 00:46:44 +03:00
|
|
|
size = type_size(type, &align);
|
2001-12-02 17:47:48 +03:00
|
|
|
if (size < 0)
|
|
|
|
error("unknown type size");
|
|
|
|
}
|
2002-01-04 00:14:59 +03:00
|
|
|
/* take into account specified alignment if bigger */
|
|
|
|
if (ad->aligned > align)
|
|
|
|
align = ad->aligned;
|
2001-12-22 20:05:23 +03:00
|
|
|
if ((r & VT_VALMASK) == VT_LOCAL) {
|
2002-07-14 18:38:33 +04:00
|
|
|
sec = NULL;
|
2002-11-19 00:46:44 +03:00
|
|
|
if (do_bounds_check && (type->t & VT_ARRAY))
|
2002-01-05 19:15:57 +03:00
|
|
|
loc--;
|
2002-02-10 19:14:03 +03:00
|
|
|
#ifdef TCC_TARGET_IL
|
|
|
|
/* XXX: ugly patch to allocate local variables for IL, just
|
|
|
|
for testing */
|
|
|
|
addr = loc;
|
|
|
|
loc++;
|
|
|
|
#else
|
2001-12-02 17:47:48 +03:00
|
|
|
loc = (loc - size) & -align;
|
|
|
|
addr = loc;
|
2002-02-10 19:14:03 +03:00
|
|
|
#endif
|
2002-01-05 19:15:57 +03:00
|
|
|
/* handles bounds */
|
|
|
|
/* XXX: currently, since we do only one pass, we cannot track
|
|
|
|
'&' operators, so we add only arrays */
|
2002-11-19 00:46:44 +03:00
|
|
|
if (do_bounds_check && (type->t & VT_ARRAY)) {
|
2002-07-27 18:05:37 +04:00
|
|
|
unsigned long *bounds_ptr;
|
2002-01-05 19:15:57 +03:00
|
|
|
/* add padding between regions */
|
|
|
|
loc--;
|
|
|
|
/* then add local bound info */
|
2002-07-27 18:05:37 +04:00
|
|
|
bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
|
|
|
|
bounds_ptr[0] = addr;
|
|
|
|
bounds_ptr[1] = size;
|
2002-01-05 19:15:57 +03:00
|
|
|
}
|
2001-12-02 17:47:48 +03:00
|
|
|
} else {
|
2002-01-04 00:14:59 +03:00
|
|
|
/* compute section */
|
|
|
|
sec = ad->section;
|
|
|
|
if (!sec) {
|
|
|
|
if (has_init)
|
|
|
|
sec = data_section;
|
|
|
|
else
|
|
|
|
sec = bss_section;
|
|
|
|
}
|
2002-07-27 18:05:37 +04:00
|
|
|
data_offset = sec->data_offset;
|
2002-01-04 00:14:59 +03:00
|
|
|
data_offset = (data_offset + align - 1) & -align;
|
|
|
|
addr = data_offset;
|
2002-07-14 18:38:33 +04:00
|
|
|
/* very important to increment global pointer at this time
|
|
|
|
because initializers themselves can create new initializers */
|
2002-01-04 00:14:59 +03:00
|
|
|
data_offset += size;
|
2002-07-27 18:05:37 +04:00
|
|
|
/* add padding if bound check */
|
|
|
|
if (do_bounds_check)
|
2002-01-04 23:17:24 +03:00
|
|
|
data_offset++;
|
2002-07-27 18:05:37 +04:00
|
|
|
sec->data_offset = data_offset;
|
2002-08-18 17:22:55 +04:00
|
|
|
/* allocate section space to put the data */
|
|
|
|
if (sec->sh_type != SHT_NOBITS &&
|
|
|
|
data_offset > sec->data_allocated)
|
|
|
|
section_realloc(sec, data_offset);
|
2001-12-02 17:47:48 +03:00
|
|
|
}
|
2002-07-27 18:05:37 +04:00
|
|
|
if (!sec) {
|
|
|
|
if (v) {
|
2002-07-14 18:38:33 +04:00
|
|
|
/* local variable */
|
2002-11-19 00:46:44 +03:00
|
|
|
sym_push(v, type, r, addr);
|
2002-07-14 18:38:33 +04:00
|
|
|
} else {
|
2002-07-27 18:05:37 +04:00
|
|
|
/* push local reference */
|
2002-11-19 00:46:44 +03:00
|
|
|
vset(type, r, addr);
|
2002-07-27 18:05:37 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Sym *sym;
|
|
|
|
|
|
|
|
if (v) {
|
2002-07-14 18:38:33 +04:00
|
|
|
if (scope == VT_CONST) {
|
|
|
|
/* global scope: see if already defined */
|
|
|
|
sym = sym_find(v);
|
|
|
|
if (!sym)
|
|
|
|
goto do_def;
|
2002-11-19 00:46:44 +03:00
|
|
|
if (!is_compatible_types(&sym->type, type))
|
2002-07-14 18:38:33 +04:00
|
|
|
error("incompatible types for redefinition of '%s'",
|
|
|
|
get_tok_str(v, NULL));
|
2002-11-19 00:46:44 +03:00
|
|
|
if (!(sym->type.t & VT_EXTERN))
|
2002-07-14 18:38:33 +04:00
|
|
|
error("redefinition of '%s'", get_tok_str(v, NULL));
|
2002-11-19 00:46:44 +03:00
|
|
|
sym->type.t &= ~VT_EXTERN;
|
2002-07-14 18:38:33 +04:00
|
|
|
} else {
|
|
|
|
do_def:
|
2002-11-19 00:46:44 +03:00
|
|
|
sym = sym_push(v, type, r | VT_SYM, 0);
|
2002-07-14 18:38:33 +04:00
|
|
|
}
|
2002-07-28 03:08:04 +04:00
|
|
|
put_extern_sym(sym, sec, addr, size);
|
2002-07-14 18:38:33 +04:00
|
|
|
} else {
|
2002-07-27 18:05:37 +04:00
|
|
|
CValue cval;
|
|
|
|
|
2002-07-14 18:38:33 +04:00
|
|
|
/* push global reference */
|
2002-11-19 00:46:44 +03:00
|
|
|
sym = get_sym_ref(type, sec, addr, size);
|
2002-08-30 03:05:59 +04:00
|
|
|
cval.ul = 0;
|
2002-11-19 00:46:44 +03:00
|
|
|
vsetc(type, VT_CONST | VT_SYM, &cval);
|
2002-08-30 03:05:59 +04:00
|
|
|
vtop->sym = sym;
|
2002-07-27 18:05:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* handles bounds now because the symbol must be defined
|
|
|
|
before for the relocation */
|
|
|
|
if (do_bounds_check) {
|
|
|
|
unsigned long *bounds_ptr;
|
|
|
|
|
|
|
|
greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_32);
|
|
|
|
/* then add global bound info */
|
|
|
|
bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
|
|
|
|
bounds_ptr[0] = 0; /* relocated */
|
|
|
|
bounds_ptr[1] = size;
|
2002-07-13 21:23:25 +04:00
|
|
|
}
|
|
|
|
}
|
2001-12-02 17:47:48 +03:00
|
|
|
if (has_init) {
|
2002-11-19 00:46:44 +03:00
|
|
|
decl_initializer(type, sec, addr, 1, 0);
|
2001-12-02 17:47:48 +03:00
|
|
|
/* restore parse state if needed */
|
2002-06-30 21:34:30 +04:00
|
|
|
if (init_str.str) {
|
2002-08-18 17:22:55 +04:00
|
|
|
tok_str_free(init_str.str);
|
2002-06-30 21:34:30 +04:00
|
|
|
restore_parse_state(&saved_parse_state);
|
2001-12-02 17:47:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-27 18:05:37 +04:00
|
|
|
void put_func_debug(Sym *sym)
|
2002-01-04 23:17:24 +03:00
|
|
|
{
|
|
|
|
char buf[512];
|
|
|
|
|
|
|
|
/* stabs info */
|
|
|
|
/* XXX: we put here a dummy type */
|
|
|
|
snprintf(buf, sizeof(buf), "%s:%c1",
|
2002-11-19 00:46:44 +03:00
|
|
|
funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
|
2002-07-27 18:05:37 +04:00
|
|
|
put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
|
|
|
|
cur_text_section, sym->c);
|
2002-01-04 23:17:24 +03:00
|
|
|
last_ind = 0;
|
|
|
|
last_line_num = 0;
|
|
|
|
}
|
|
|
|
|
2002-06-30 21:34:30 +04:00
|
|
|
/* not finished : try to put some local vars in registers */
|
|
|
|
//#define CONFIG_REG_VARS
|
|
|
|
|
|
|
|
#ifdef CONFIG_REG_VARS
|
|
|
|
void add_var_ref(int t)
|
|
|
|
{
|
|
|
|
printf("%s:%d: &%s\n",
|
|
|
|
file->filename, file->line_num,
|
|
|
|
get_tok_str(t, NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* first pass on a function with heuristic to extract variable usage
|
|
|
|
and pointer references to local variables for register allocation */
|
|
|
|
void analyse_function(void)
|
|
|
|
{
|
|
|
|
int level, t;
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
if (tok == -1)
|
|
|
|
break;
|
|
|
|
/* any symbol coming after '&' is considered as being a
|
|
|
|
variable whose reference is taken. It is highly unaccurate
|
|
|
|
but it is difficult to do better without a complete parse */
|
|
|
|
if (tok == '&') {
|
|
|
|
next();
|
|
|
|
/* if '& number', then no need to examine next tokens */
|
|
|
|
if (tok == TOK_CINT ||
|
|
|
|
tok == TOK_CUINT ||
|
|
|
|
tok == TOK_CLLONG ||
|
|
|
|
tok == TOK_CULLONG) {
|
|
|
|
continue;
|
|
|
|
} else if (tok >= TOK_UIDENT) {
|
|
|
|
/* if '& ident [' or '& ident ->', then ident address
|
|
|
|
is not needed */
|
|
|
|
t = tok;
|
|
|
|
next();
|
|
|
|
if (tok != '[' && tok != TOK_ARROW)
|
|
|
|
add_var_ref(t);
|
|
|
|
} else {
|
|
|
|
level = 0;
|
|
|
|
while (tok != '}' && tok != ';' &&
|
|
|
|
!((tok == ',' || tok == ')') && level == 0)) {
|
|
|
|
if (tok >= TOK_UIDENT) {
|
|
|
|
add_var_ref(tok);
|
|
|
|
} else if (tok == '(') {
|
|
|
|
level++;
|
|
|
|
} else if (tok == ')') {
|
|
|
|
level--;
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-11-02 23:45:52 +03:00
|
|
|
/* parse an old style function declaration list */
|
|
|
|
/* XXX: check multiple parameter */
|
|
|
|
static void func_decl_list(Sym *func_sym)
|
|
|
|
{
|
|
|
|
AttributeDef ad;
|
2002-11-19 00:46:44 +03:00
|
|
|
int v;
|
2002-11-02 23:45:52 +03:00
|
|
|
Sym *s;
|
2002-11-19 00:46:44 +03:00
|
|
|
CType btype, type;
|
|
|
|
|
2002-11-02 23:45:52 +03:00
|
|
|
/* parse each declaration */
|
2002-11-24 02:56:12 +03:00
|
|
|
while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF) {
|
2002-11-19 00:46:44 +03:00
|
|
|
if (!parse_btype(&btype, &ad))
|
2002-11-02 23:45:52 +03:00
|
|
|
expect("declaration list");
|
2002-11-19 00:46:44 +03:00
|
|
|
if (((btype.t & VT_BTYPE) == VT_ENUM ||
|
|
|
|
(btype.t & VT_BTYPE) == VT_STRUCT) &&
|
2002-11-02 23:45:52 +03:00
|
|
|
tok == ';') {
|
|
|
|
/* we accept no variable after */
|
|
|
|
} else {
|
|
|
|
for(;;) {
|
2002-11-19 00:46:44 +03:00
|
|
|
type = btype;
|
|
|
|
type_decl(&type, &ad, &v, TYPE_DIRECT);
|
2002-11-02 23:45:52 +03:00
|
|
|
/* find parameter in function parameter list */
|
|
|
|
s = func_sym->next;
|
|
|
|
while (s != NULL) {
|
|
|
|
if ((s->v & ~SYM_FIELD) == v)
|
|
|
|
goto found;
|
|
|
|
s = s->next;
|
|
|
|
}
|
|
|
|
error("declaration for parameter '%s' but no such parameter",
|
|
|
|
get_tok_str(v, NULL));
|
|
|
|
found:
|
|
|
|
/* check that no storage specifier except 'register' was given */
|
2002-11-19 00:46:44 +03:00
|
|
|
if (type.t & (VT_EXTERN | VT_STATIC | VT_TYPEDEF))
|
2002-11-02 23:45:52 +03:00
|
|
|
error("storage class specified for '%s'", get_tok_str(v, NULL));
|
|
|
|
/* we can add the type (NOTE: it could be local to the function) */
|
2002-11-19 00:46:44 +03:00
|
|
|
s->type = type;
|
2002-11-02 23:45:52 +03:00
|
|
|
/* accept other parameters */
|
|
|
|
if (tok == ',')
|
|
|
|
next();
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
skip(';');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-30 02:23:01 +03:00
|
|
|
/* 'l' is VT_LOCAL or VT_CONST to define default storage type */
|
2002-11-02 23:45:52 +03:00
|
|
|
static void decl(int l)
|
2001-10-28 02:48:39 +03:00
|
|
|
{
|
2002-11-19 00:46:44 +03:00
|
|
|
int v, has_init, r;
|
|
|
|
CType type, btype;
|
2001-11-06 04:20:20 +03:00
|
|
|
Sym *sym;
|
2002-01-04 00:14:59 +03:00
|
|
|
AttributeDef ad;
|
2002-06-30 21:34:30 +04:00
|
|
|
|
2001-12-02 17:59:18 +03:00
|
|
|
while (1) {
|
2002-11-19 00:46:44 +03:00
|
|
|
if (!parse_btype(&btype, &ad)) {
|
2001-12-05 03:45:08 +03:00
|
|
|
/* skip redundant ';' */
|
|
|
|
/* XXX: find more elegant solution */
|
|
|
|
if (tok == ';') {
|
|
|
|
next();
|
|
|
|
continue;
|
|
|
|
}
|
2001-12-02 17:59:18 +03: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;
|
2002-11-19 00:46:44 +03:00
|
|
|
btype.t = VT_INT;
|
2001-12-02 17:59:18 +03:00
|
|
|
}
|
2002-11-19 00:46:44 +03:00
|
|
|
if (((btype.t & VT_BTYPE) == VT_ENUM ||
|
|
|
|
(btype.t & VT_BTYPE) == VT_STRUCT) &&
|
2001-12-09 01:51:04 +03:00
|
|
|
tok == ';') {
|
2001-10-31 01:43:19 +03:00
|
|
|
/* we accept no variable after */
|
|
|
|
next();
|
|
|
|
continue;
|
|
|
|
}
|
2001-10-28 02:48:39 +03:00
|
|
|
while (1) { /* iterate thru each declaration */
|
2002-11-19 00:46:44 +03:00
|
|
|
type = btype;
|
|
|
|
type_decl(&type, &ad, &v, TYPE_DIRECT);
|
2001-12-18 00:56:48 +03:00
|
|
|
#if 0
|
|
|
|
{
|
|
|
|
char buf[500];
|
|
|
|
type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
|
|
|
|
printf("type = '%s'\n", buf);
|
|
|
|
}
|
|
|
|
#endif
|
2002-11-19 00:46:44 +03:00
|
|
|
if ((type.t & VT_BTYPE) == VT_FUNC) {
|
2002-11-02 23:45:52 +03:00
|
|
|
/* if old style function prototype, we accept a
|
|
|
|
declaration list */
|
2002-11-19 00:46:44 +03:00
|
|
|
sym = type.ref;
|
2002-11-02 23:45:52 +03:00
|
|
|
if (sym->c == FUNC_OLD)
|
|
|
|
func_decl_list(sym);
|
|
|
|
}
|
|
|
|
|
2001-10-28 02:48:39 +03:00
|
|
|
if (tok == '{') {
|
2002-06-30 21:34:30 +04:00
|
|
|
#ifdef CONFIG_REG_VARS
|
|
|
|
TokenString func_str;
|
|
|
|
ParseState saved_parse_state;
|
|
|
|
int block_level;
|
|
|
|
#endif
|
|
|
|
|
2001-12-05 03:45:08 +03:00
|
|
|
if (l == VT_LOCAL)
|
|
|
|
error("cannot use local functions");
|
2002-11-19 00:46:44 +03:00
|
|
|
if (!(type.t & VT_FUNC))
|
2001-11-29 01:48:43 +03:00
|
|
|
expect("function definition");
|
2002-06-30 21:34:30 +04:00
|
|
|
|
|
|
|
#ifdef CONFIG_REG_VARS
|
|
|
|
/* parse all function code and record it */
|
|
|
|
|
|
|
|
tok_str_new(&func_str);
|
|
|
|
|
|
|
|
block_level = 0;
|
|
|
|
for(;;) {
|
|
|
|
int t;
|
|
|
|
if (tok == -1)
|
|
|
|
error("unexpected end of file");
|
|
|
|
tok_str_add_tok(&func_str);
|
|
|
|
t = tok;
|
|
|
|
next();
|
|
|
|
if (t == '{') {
|
|
|
|
block_level++;
|
|
|
|
} else if (t == '}') {
|
|
|
|
block_level--;
|
|
|
|
if (block_level == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tok_str_add(&func_str, -1);
|
|
|
|
tok_str_add(&func_str, 0);
|
|
|
|
|
|
|
|
save_parse_state(&saved_parse_state);
|
|
|
|
|
|
|
|
macro_ptr = func_str.str;
|
|
|
|
next();
|
|
|
|
analyse_function();
|
|
|
|
#endif
|
|
|
|
|
2002-01-04 00:14:59 +03:00
|
|
|
/* compute text section */
|
|
|
|
cur_text_section = ad.section;
|
|
|
|
if (!cur_text_section)
|
|
|
|
cur_text_section = text_section;
|
2002-07-27 18:05:37 +04:00
|
|
|
ind = cur_text_section->data_offset;
|
2002-07-14 18:38:33 +04:00
|
|
|
funcname = get_tok_str(v, NULL);
|
|
|
|
sym = sym_find(v);
|
|
|
|
if (sym) {
|
|
|
|
/* if symbol is already defined, then put complete type */
|
2002-11-19 00:46:44 +03:00
|
|
|
sym->type = type;
|
2001-11-01 17:48:10 +03:00
|
|
|
} else {
|
2002-07-14 18:38:33 +04:00
|
|
|
/* put function symbol */
|
2002-11-20 03:29:04 +03:00
|
|
|
sym = global_identifier_push(v, type.t, 0);
|
2002-11-19 00:46:44 +03:00
|
|
|
sym->type.ref = type.ref;
|
2001-11-01 17:48:10 +03:00
|
|
|
}
|
2002-07-28 03:08:04 +04:00
|
|
|
/* NOTE: we patch the symbol size later */
|
|
|
|
put_extern_sym(sym, cur_text_section, ind, 0);
|
|
|
|
func_ind = ind;
|
2002-07-14 18:38:33 +04:00
|
|
|
sym->r = VT_SYM | VT_CONST;
|
2002-01-04 23:17:24 +03:00
|
|
|
/* put debug symbol */
|
|
|
|
if (do_debug)
|
2002-07-27 18:05:37 +04:00
|
|
|
put_func_debug(sym);
|
2001-11-08 02:13:29 +03:00
|
|
|
/* push a dummy symbol to enable local sym storage */
|
2002-11-20 03:29:04 +03:00
|
|
|
sym_push2(&local_stack, SYM_FIELD, 0, 0);
|
2002-11-19 00:46:44 +03:00
|
|
|
gfunc_prolog(&type);
|
2001-10-28 02:48:39 +03:00
|
|
|
loc = 0;
|
|
|
|
rsym = 0;
|
2002-06-30 21:34:30 +04:00
|
|
|
#ifdef CONFIG_REG_VARS
|
|
|
|
macro_ptr = func_str.str;
|
|
|
|
next();
|
|
|
|
#endif
|
2001-12-18 00:56:48 +03:00
|
|
|
block(NULL, NULL, NULL, NULL, 0);
|
2001-10-28 02:48:39 +03:00
|
|
|
gsym(rsym);
|
2002-01-04 01:43:10 +03:00
|
|
|
gfunc_epilog();
|
2002-07-27 18:05:37 +04:00
|
|
|
cur_text_section->data_offset = ind;
|
2002-11-03 03:43:55 +03:00
|
|
|
/* look if any labels are undefined. Define symbols if
|
|
|
|
'&&label' was used. */
|
|
|
|
{
|
2002-11-20 03:29:04 +03:00
|
|
|
Sym *s, *s1;
|
|
|
|
for(s = label_stack; s != NULL; s = s1) {
|
|
|
|
s1 = s->prev;
|
2002-11-03 03:43:55 +03:00
|
|
|
if (s->r & LABEL_FORWARD) {
|
|
|
|
error("label '%s' used but not defined",
|
|
|
|
get_tok_str(s->v, NULL));
|
|
|
|
}
|
|
|
|
if (s->c) {
|
|
|
|
/* define corresponding symbol. A size of
|
|
|
|
1 is put. */
|
|
|
|
put_extern_sym(s, cur_text_section, (long)s->next, 1);
|
|
|
|
}
|
2002-11-20 03:29:04 +03:00
|
|
|
/* remove label */
|
|
|
|
table_ident[s->v - TOK_IDENT]->sym_label = NULL;
|
|
|
|
tcc_free(s);
|
2002-11-03 03:43:55 +03:00
|
|
|
}
|
2002-11-20 03:29:04 +03:00
|
|
|
label_stack = NULL;
|
2002-11-03 03:43:55 +03:00
|
|
|
}
|
2001-12-13 00:16:17 +03:00
|
|
|
sym_pop(&local_stack, NULL); /* reset local stack */
|
2002-01-04 23:17:24 +03:00
|
|
|
/* end of function */
|
2002-07-28 03:08:04 +04:00
|
|
|
/* patch symbol size */
|
|
|
|
((Elf32_Sym *)symtab_section->data)[sym->c].st_size =
|
|
|
|
ind - func_ind;
|
2002-01-04 23:17:24 +03:00
|
|
|
if (do_debug) {
|
|
|
|
put_stabn(N_FUN, 0, 0, ind - func_ind);
|
|
|
|
}
|
2001-12-03 00:24:43 +03:00
|
|
|
funcname = ""; /* for safety */
|
2002-11-19 00:46:44 +03:00
|
|
|
func_vt.t = VT_VOID; /* for safety */
|
2002-01-05 22:55:43 +03:00
|
|
|
ind = 0; /* for safety */
|
2002-06-30 21:34:30 +04:00
|
|
|
|
|
|
|
#ifdef CONFIG_REG_VARS
|
2002-08-18 17:22:55 +04:00
|
|
|
tok_str_free(func_str.str);
|
2002-06-30 21:34:30 +04:00
|
|
|
restore_parse_state(&saved_parse_state);
|
|
|
|
#endif
|
2001-10-28 02:48:39 +03:00
|
|
|
break;
|
|
|
|
} else {
|
2002-11-19 00:46:44 +03:00
|
|
|
if (btype.t & VT_TYPEDEF) {
|
2001-12-08 18:04:01 +03:00
|
|
|
/* save typedefed type */
|
|
|
|
/* XXX: test storage specifiers ? */
|
2002-11-19 00:46:44 +03:00
|
|
|
sym = sym_push(v, &type, 0, 0);
|
|
|
|
sym->type.t |= VT_TYPEDEF;
|
|
|
|
} else if ((type.t & VT_BTYPE) == VT_FUNC) {
|
2001-10-30 02:23:01 +03:00
|
|
|
/* external function definition */
|
2002-11-19 00:46:44 +03:00
|
|
|
external_sym(v, &type, 0);
|
2001-10-28 02:48:39 +03:00
|
|
|
} else {
|
2001-10-30 02:23:01 +03:00
|
|
|
/* not lvalue if array */
|
2001-12-22 20:05:23 +03:00
|
|
|
r = 0;
|
2002-11-19 00:46:44 +03:00
|
|
|
if (!(type.t & VT_ARRAY))
|
|
|
|
r |= lvalue_type(type.t);
|
|
|
|
if (btype.t & VT_EXTERN) {
|
2001-10-30 02:23:01 +03:00
|
|
|
/* external variable */
|
2002-11-19 00:46:44 +03:00
|
|
|
external_sym(v, &type, r);
|
2001-10-30 02:23:01 +03:00
|
|
|
} else {
|
2002-11-19 00:46:44 +03:00
|
|
|
if (type.t & VT_STATIC)
|
2001-12-22 20:05:23 +03:00
|
|
|
r |= VT_CONST;
|
|
|
|
else
|
|
|
|
r |= l;
|
2001-12-02 17:47:48 +03:00
|
|
|
has_init = (tok == '=');
|
|
|
|
if (has_init)
|
2001-11-17 20:22:38 +03:00
|
|
|
next();
|
2002-11-19 00:46:44 +03:00
|
|
|
decl_initializer_alloc(&type, &ad, r,
|
2002-07-13 21:23:25 +04:00
|
|
|
has_init, v, l);
|
2001-10-30 02:23:01 +03:00
|
|
|
}
|
2001-10-28 02:48:39 +03:00
|
|
|
}
|
|
|
|
if (tok != ',') {
|
|
|
|
skip(';');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-14 03:00:17 +04:00
|
|
|
/* compile the C file opened in 'file'. Return non zero if errors. */
|
2002-11-02 17:12:32 +03:00
|
|
|
static int tcc_compile(TCCState *s1)
|
2001-12-08 18:04:01 +03:00
|
|
|
{
|
2002-11-19 00:46:44 +03:00
|
|
|
Sym *define_start;
|
2002-01-04 23:17:24 +03:00
|
|
|
char buf[512];
|
2002-11-02 17:12:32 +03:00
|
|
|
volatile int section_sym;
|
2002-07-23 03:37:39 +04:00
|
|
|
|
2002-09-08 23:34:32 +04:00
|
|
|
#ifdef INC_DEBUG
|
|
|
|
printf("%s: **** new file\n", file->filename);
|
|
|
|
#endif
|
2001-12-08 18:04:01 +03:00
|
|
|
funcname = "";
|
2002-11-02 17:12:32 +03:00
|
|
|
s1->include_stack_ptr = s1->include_stack;
|
2002-09-08 20:14:57 +04:00
|
|
|
/* XXX: move that before to avoid having to initialize
|
|
|
|
file->ifdef_stack_ptr ? */
|
2002-11-02 17:12:32 +03:00
|
|
|
s1->ifdef_stack_ptr = s1->ifdef_stack;
|
|
|
|
file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
|
2001-12-08 18:04:01 +03:00
|
|
|
|
2002-08-18 17:22:55 +04:00
|
|
|
/* XXX: not ANSI compliant: bound checking says error */
|
2001-12-16 20:58:32 +03:00
|
|
|
vtop = vstack - 1;
|
2001-12-08 18:04:01 +03:00
|
|
|
anon_sym = SYM_FIRST_ANOM;
|
2002-01-04 23:17:24 +03:00
|
|
|
|
|
|
|
/* file info: full path + filename */
|
2002-07-27 18:05:37 +04:00
|
|
|
section_sym = 0; /* avoid warning */
|
2002-01-04 23:17:24 +03:00
|
|
|
if (do_debug) {
|
2002-07-27 18:05:37 +04:00
|
|
|
section_sym = put_elf_sym(symtab_section, 0, 0,
|
|
|
|
ELF32_ST_INFO(STB_LOCAL, STT_SECTION), 0,
|
|
|
|
text_section->sh_num, NULL);
|
2002-01-04 23:17:24 +03:00
|
|
|
getcwd(buf, sizeof(buf));
|
|
|
|
pstrcat(buf, sizeof(buf), "/");
|
2002-07-27 18:05:37 +04:00
|
|
|
put_stabs_r(buf, N_SO, 0, 0,
|
|
|
|
text_section->data_offset, text_section, section_sym);
|
|
|
|
put_stabs_r(file->filename, N_SO, 0, 0,
|
|
|
|
text_section->data_offset, text_section, section_sym);
|
2002-01-04 23:17:24 +03:00
|
|
|
}
|
2002-07-15 03:00:39 +04:00
|
|
|
/* an elf symbol of type STT_FILE must be put so that STB_LOCAL
|
|
|
|
symbols can be safely used */
|
|
|
|
put_elf_sym(symtab_section, 0, 0,
|
|
|
|
ELF32_ST_INFO(STB_LOCAL, STT_FILE), 0,
|
|
|
|
SHN_ABS, file->filename);
|
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
/* define some often used types */
|
|
|
|
int_type.t = VT_INT;
|
|
|
|
|
|
|
|
char_pointer_type.t = VT_BYTE;
|
|
|
|
mk_pointer(&char_pointer_type);
|
|
|
|
|
|
|
|
func_old_type.t = VT_FUNC;
|
2002-11-20 03:29:04 +03:00
|
|
|
func_old_type.ref = sym_push(SYM_FIELD, &int_type, FUNC_CDECL, FUNC_OLD);
|
2002-11-19 00:46:44 +03:00
|
|
|
|
2002-08-31 16:43:39 +04:00
|
|
|
#if 0
|
|
|
|
/* define 'void *alloca(unsigned int)' builtin function */
|
|
|
|
{
|
|
|
|
Sym *s1;
|
|
|
|
|
|
|
|
p = anon_sym++;
|
|
|
|
sym = sym_push(p, mk_pointer(VT_VOID), FUNC_CDECL, FUNC_NEW);
|
2002-11-20 03:29:04 +03:00
|
|
|
s1 = sym_push(SYM_FIELD, VT_UNSIGNED | VT_INT, 0, 0);
|
2002-08-31 16:43:39 +04:00
|
|
|
s1->next = NULL;
|
|
|
|
sym->next = s1;
|
|
|
|
sym_push(TOK_alloca, VT_FUNC | (p << VT_STRUCT_SHIFT), VT_CONST, 0);
|
|
|
|
}
|
|
|
|
#endif
|
2002-01-04 23:17:24 +03:00
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
define_start = define_stack;
|
2001-12-08 18:04:01 +03:00
|
|
|
|
2002-11-02 17:12:32 +03:00
|
|
|
if (setjmp(s1->error_jmp_buf) == 0) {
|
|
|
|
s1->nb_errors = 0;
|
|
|
|
s1->error_set_jmp_enabled = 1;
|
|
|
|
|
2002-11-23 02:28:06 +03:00
|
|
|
ch = file->buf_ptr[0];
|
2002-11-23 01:16:30 +03:00
|
|
|
tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
|
2002-11-02 17:12:32 +03:00
|
|
|
next();
|
|
|
|
decl(VT_CONST);
|
|
|
|
if (tok != -1)
|
|
|
|
expect("declaration");
|
|
|
|
|
|
|
|
/* end of translation unit info */
|
|
|
|
if (do_debug) {
|
|
|
|
put_stabs_r(NULL, N_SO, 0, 0,
|
|
|
|
text_section->data_offset, text_section, section_sym);
|
|
|
|
}
|
2002-01-04 23:17:24 +03:00
|
|
|
}
|
2002-11-02 17:12:32 +03:00
|
|
|
s1->error_set_jmp_enabled = 0;
|
2002-01-04 23:17:24 +03:00
|
|
|
|
2001-12-08 18:04:01 +03:00
|
|
|
/* reset define stack, but leave -Dsymbols (may be incorrect if
|
|
|
|
they are undefined) */
|
2002-11-02 17:12:32 +03:00
|
|
|
free_defines(define_start);
|
2001-12-08 18:04:01 +03:00
|
|
|
|
|
|
|
sym_pop(&global_stack, NULL);
|
|
|
|
|
2002-11-02 17:12:32 +03:00
|
|
|
return s1->nb_errors != 0 ? -1 : 0;
|
2001-12-08 18:04:01 +03:00
|
|
|
}
|
|
|
|
|
2002-11-22 17:12:28 +03:00
|
|
|
#ifdef LIBTCC
|
2002-05-14 03:00:17 +04:00
|
|
|
int tcc_compile_string(TCCState *s, const char *str)
|
|
|
|
{
|
|
|
|
BufferedFile bf1, *bf = &bf1;
|
2002-11-22 17:12:28 +03:00
|
|
|
int ret, len;
|
|
|
|
char *buf;
|
2002-05-14 03:00:17 +04:00
|
|
|
|
|
|
|
/* init file structure */
|
|
|
|
bf->fd = -1;
|
2002-11-22 17:12:28 +03:00
|
|
|
/* XXX: avoid copying */
|
|
|
|
len = strlen(str);
|
|
|
|
buf = tcc_malloc(len + 1);
|
|
|
|
if (!buf)
|
|
|
|
return -1;
|
|
|
|
buf[len] = CH_EOB;
|
|
|
|
bf->buf_ptr = buf;
|
|
|
|
bf->buf_end = buf + len;
|
2002-05-14 03:00:17 +04:00
|
|
|
pstrcpy(bf->filename, sizeof(bf->filename), "<string>");
|
|
|
|
bf->line_num = 1;
|
|
|
|
file = bf;
|
|
|
|
|
|
|
|
ret = tcc_compile(s);
|
|
|
|
|
2002-11-22 17:12:28 +03:00
|
|
|
tcc_free(buf);
|
|
|
|
|
2002-05-14 03:00:17 +04:00
|
|
|
/* currently, no need to close */
|
|
|
|
return ret;
|
|
|
|
}
|
2002-11-22 17:12:28 +03:00
|
|
|
#endif
|
2002-05-14 03:00:17 +04:00
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
/* define a preprocessor symbol. A value can also be provided with the '=' operator */
|
2002-11-02 17:12:32 +03:00
|
|
|
void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
|
2002-01-04 00:14:59 +03:00
|
|
|
{
|
2002-01-13 14:07:19 +03:00
|
|
|
BufferedFile bf1, *bf = &bf1;
|
2002-01-04 00:14:59 +03:00
|
|
|
|
2002-01-13 14:07:19 +03:00
|
|
|
pstrcpy(bf->buffer, IO_BUF_SIZE, sym);
|
2002-05-14 03:00:17 +04:00
|
|
|
pstrcat(bf->buffer, IO_BUF_SIZE, " ");
|
|
|
|
/* default value */
|
|
|
|
if (!value)
|
|
|
|
value = "1";
|
|
|
|
pstrcat(bf->buffer, IO_BUF_SIZE, value);
|
2002-11-22 17:43:38 +03:00
|
|
|
|
2002-01-13 14:07:19 +03:00
|
|
|
/* init file structure */
|
|
|
|
bf->fd = -1;
|
|
|
|
bf->buf_ptr = bf->buffer;
|
|
|
|
bf->buf_end = bf->buffer + strlen(bf->buffer);
|
2002-11-22 17:43:38 +03:00
|
|
|
*bf->buf_end = CH_EOB;
|
2002-01-13 14:07:19 +03:00
|
|
|
bf->filename[0] = '\0';
|
|
|
|
bf->line_num = 1;
|
|
|
|
file = bf;
|
|
|
|
|
2002-11-02 17:12:32 +03:00
|
|
|
s1->include_stack_ptr = s1->include_stack;
|
2002-01-13 14:07:19 +03:00
|
|
|
|
|
|
|
/* parse with define parser */
|
2002-11-23 02:28:06 +03:00
|
|
|
ch = file->buf_ptr[0];
|
2002-01-13 14:07:19 +03:00
|
|
|
next_nomacro();
|
|
|
|
parse_define();
|
|
|
|
file = NULL;
|
2002-01-04 00:14:59 +03:00
|
|
|
}
|
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
/* undefine a preprocessor symbol */
|
2002-05-14 03:00:17 +04:00
|
|
|
void tcc_undefine_symbol(TCCState *s1, const char *sym)
|
2002-01-04 00:14:59 +03:00
|
|
|
{
|
|
|
|
TokenSym *ts;
|
|
|
|
Sym *s;
|
2002-09-08 20:14:57 +04:00
|
|
|
ts = tok_alloc(sym, strlen(sym));
|
2002-11-19 00:46:44 +03:00
|
|
|
s = define_find(ts->tok);
|
2002-01-04 00:14:59 +03:00
|
|
|
/* undefine symbol by putting an invalid name */
|
|
|
|
if (s)
|
2002-11-19 00:46:44 +03:00
|
|
|
define_undef(s);
|
2002-01-04 00:14:59 +03:00
|
|
|
}
|
|
|
|
|
2002-08-18 17:22:55 +04:00
|
|
|
#include "tccelf.c"
|
2002-07-14 18:38:33 +04:00
|
|
|
|
2002-08-18 17:22:55 +04: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)
|
2002-07-18 04:51:11 +04:00
|
|
|
{
|
2002-08-18 17:22:55 +04:00
|
|
|
Stab_Sym *sym, *sym_end;
|
|
|
|
char func_name[128], last_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;
|
2002-01-04 23:17:24 +03:00
|
|
|
|
2002-11-03 03:43:55 +03:00
|
|
|
fprintf(stderr, "0x%08lx:", wanted_pc);
|
|
|
|
|
2002-01-04 23:17:24 +03:00
|
|
|
func_name[0] = '\0';
|
|
|
|
func_addr = 0;
|
|
|
|
incl_index = 0;
|
2002-08-18 17:22:55 +04:00
|
|
|
last_func_name[0] = '\0';
|
2002-01-04 23:17:24 +03:00
|
|
|
last_pc = 0xffffffff;
|
|
|
|
last_line_num = 1;
|
|
|
|
sym = (Stab_Sym *)stab_section->data + 1;
|
2002-07-27 18:05:37 +04:00
|
|
|
sym_end = (Stab_Sym *)(stab_section->data + stab_section->data_offset);
|
2002-01-04 23:17:24 +03:00
|
|
|
while (sym < sym_end) {
|
|
|
|
switch(sym->n_type) {
|
|
|
|
/* function start or end */
|
|
|
|
case N_FUN:
|
|
|
|
if (sym->n_strx == 0) {
|
2002-11-03 03:43:55 +03:00
|
|
|
/* we test if between last line and end of function */
|
|
|
|
pc = sym->n_value + func_addr;
|
|
|
|
if (wanted_pc >= last_pc && wanted_pc < pc)
|
|
|
|
goto found;
|
2002-01-04 23:17:24 +03:00
|
|
|
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;
|
2002-08-18 17:22:55 +04:00
|
|
|
/* XXX: slow! */
|
|
|
|
strcpy(last_func_name, func_name);
|
2002-01-04 23:17:24 +03:00
|
|
|
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++;
|
|
|
|
}
|
2002-11-03 03:43:55 +03:00
|
|
|
|
|
|
|
/* second pass: we try symtab symbols (no line number info) */
|
|
|
|
incl_index = 0;
|
|
|
|
{
|
|
|
|
Elf32_Sym *sym, *sym_end;
|
|
|
|
int type;
|
|
|
|
|
|
|
|
sym_end = (Elf32_Sym *)(symtab_section->data + symtab_section->data_offset);
|
|
|
|
for(sym = (Elf32_Sym *)symtab_section->data + 1;
|
|
|
|
sym < sym_end;
|
|
|
|
sym++) {
|
|
|
|
type = ELF32_ST_TYPE(sym->st_info);
|
|
|
|
if (type == STT_FUNC) {
|
|
|
|
if (wanted_pc >= sym->st_value &&
|
|
|
|
wanted_pc < sym->st_value + sym->st_size) {
|
|
|
|
pstrcpy(last_func_name, sizeof(last_func_name),
|
|
|
|
strtab_section->data + sym->st_name);
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* did not find any info: */
|
|
|
|
fprintf(stderr, " ???\n");
|
2002-01-04 23:17:24 +03:00
|
|
|
return;
|
|
|
|
found:
|
2002-11-03 03:43:55 +03:00
|
|
|
if (last_func_name[0] != '\0') {
|
|
|
|
fprintf(stderr, " %s()", last_func_name);
|
|
|
|
}
|
2002-01-04 23:17:24 +03:00
|
|
|
if (incl_index > 0) {
|
2002-11-03 03:43:55 +03:00
|
|
|
fprintf(stderr, " (%s:%d",
|
2002-01-04 23:17:24 +03:00
|
|
|
incl_files[incl_index - 1], last_line_num);
|
2002-11-03 03:43:55 +03:00
|
|
|
for(i = incl_index - 2; i >= 0; i--)
|
|
|
|
fprintf(stderr, ", included from %s", incl_files[i]);
|
|
|
|
fprintf(stderr, ")");
|
2002-01-04 23:17:24 +03:00
|
|
|
}
|
2002-11-03 03:43:55 +03:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
#ifndef WIN32
|
|
|
|
|
2002-11-03 03:43:55 +03:00
|
|
|
#ifdef __i386__
|
|
|
|
|
|
|
|
#ifndef EIP
|
|
|
|
#define EIP 14
|
|
|
|
#define EBP 6
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* return the PC at frame level 'level'. Return non zero if not found */
|
|
|
|
static int rt_get_caller_pc(unsigned long *paddr,
|
|
|
|
struct ucontext *uc, int level)
|
|
|
|
{
|
|
|
|
unsigned long fp;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (level == 0) {
|
|
|
|
*paddr = uc->uc_mcontext.gregs[EIP];
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
fp = uc->uc_mcontext.gregs[EBP];
|
|
|
|
for(i=1;i<level;i++) {
|
|
|
|
/* XXX: check address validity with program info */
|
|
|
|
if (fp <= 0x1000 || fp >= 0xc0000000)
|
|
|
|
return -1;
|
|
|
|
fp = ((unsigned long *)fp)[0];
|
|
|
|
}
|
|
|
|
*paddr = ((unsigned long *)fp)[1];
|
|
|
|
return 0;
|
2002-01-04 23:17:24 +03:00
|
|
|
}
|
|
|
|
}
|
2002-11-03 03:43:55 +03:00
|
|
|
#else
|
2002-11-19 00:46:44 +03:00
|
|
|
#error add arch specific rt_get_caller_pc()
|
2002-11-03 03:43:55 +03:00
|
|
|
#endif
|
2002-01-04 23:17:24 +03:00
|
|
|
|
2002-01-05 20:02:38 +03:00
|
|
|
/* emit a run time error at position 'pc' */
|
2002-11-03 03:43:55 +03:00
|
|
|
void rt_error(struct ucontext *uc, const char *fmt, ...)
|
2002-01-05 20:02:38 +03:00
|
|
|
{
|
|
|
|
va_list ap;
|
2002-11-03 03:43:55 +03:00
|
|
|
unsigned long pc;
|
|
|
|
int i;
|
2002-01-05 20:02:38 +03:00
|
|
|
|
2002-11-03 03:43:55 +03:00
|
|
|
va_start(ap, fmt);
|
|
|
|
fprintf(stderr, "Runtime error: ");
|
2002-01-05 20:02:38 +03:00
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
fprintf(stderr, "\n");
|
2002-11-03 03:43:55 +03:00
|
|
|
for(i=0;i<num_callers;i++) {
|
|
|
|
if (rt_get_caller_pc(&pc, uc, i) < 0)
|
|
|
|
break;
|
|
|
|
if (i == 0)
|
|
|
|
fprintf(stderr, "at ");
|
|
|
|
else
|
|
|
|
fprintf(stderr, "by ");
|
|
|
|
rt_printline(pc);
|
|
|
|
}
|
2002-01-05 20:02:38 +03:00
|
|
|
exit(255);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
2002-01-04 23:17:24 +03:00
|
|
|
|
|
|
|
/* signal handler for fatal errors */
|
|
|
|
static void sig_error(int signum, siginfo_t *siginf, void *puc)
|
|
|
|
{
|
|
|
|
struct ucontext *uc = puc;
|
|
|
|
|
|
|
|
switch(signum) {
|
|
|
|
case SIGFPE:
|
|
|
|
switch(siginf->si_code) {
|
|
|
|
case FPE_INTDIV:
|
|
|
|
case FPE_FLTDIV:
|
2002-11-03 03:43:55 +03:00
|
|
|
rt_error(uc, "division by zero");
|
2002-01-04 23:17:24 +03:00
|
|
|
break;
|
|
|
|
default:
|
2002-11-03 03:43:55 +03:00
|
|
|
rt_error(uc, "floating point exception");
|
2002-01-04 23:17:24 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SIGBUS:
|
|
|
|
case SIGSEGV:
|
2002-11-03 03:43:55 +03:00
|
|
|
if (rt_bound_error_msg && *rt_bound_error_msg)
|
|
|
|
rt_error(uc, *rt_bound_error_msg);
|
|
|
|
else
|
|
|
|
rt_error(uc, "dereferencing invalid pointer");
|
2002-01-04 23:17:24 +03:00
|
|
|
break;
|
|
|
|
case SIGILL:
|
2002-11-03 03:43:55 +03:00
|
|
|
rt_error(uc, "illegal instruction");
|
2002-01-04 23:17:24 +03:00
|
|
|
break;
|
|
|
|
case SIGABRT:
|
2002-11-03 03:43:55 +03:00
|
|
|
rt_error(uc, "abort() called");
|
2002-01-04 23:17:24 +03:00
|
|
|
break;
|
|
|
|
default:
|
2002-11-03 03:43:55 +03:00
|
|
|
rt_error(uc, "caught signal %d", signum);
|
2002-01-04 23:17:24 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
exit(255);
|
|
|
|
}
|
2002-01-12 19:39:35 +03:00
|
|
|
#endif
|
2002-01-04 23:17:24 +03:00
|
|
|
|
2002-09-09 02:46:32 +04:00
|
|
|
/* do all relocations (needed before using tcc_get_symbol()) */
|
2002-11-02 17:12:32 +03:00
|
|
|
int tcc_relocate(TCCState *s1)
|
2002-07-14 18:38:33 +04:00
|
|
|
{
|
|
|
|
Section *s;
|
2002-07-22 04:20:38 +04:00
|
|
|
int i;
|
2002-07-18 04:51:11 +04:00
|
|
|
|
2002-11-02 17:12:32 +03:00
|
|
|
s1->nb_errors = 0;
|
|
|
|
|
2002-07-23 03:37:39 +04:00
|
|
|
tcc_add_runtime(s1);
|
|
|
|
|
2002-07-18 04:51:11 +04:00
|
|
|
relocate_common_syms();
|
2002-07-14 18:38:33 +04:00
|
|
|
|
2002-08-18 17:22:55 +04:00
|
|
|
/* compute relocation address : section are relocated in place. We
|
|
|
|
also alloc the bss space */
|
2002-11-02 17:12:32 +03:00
|
|
|
for(i = 1; i < s1->nb_sections; i++) {
|
|
|
|
s = s1->sections[i];
|
2002-08-18 17:22:55 +04:00
|
|
|
if (s->sh_flags & SHF_ALLOC) {
|
2002-11-02 17:12:32 +03:00
|
|
|
if (s->sh_type == SHT_NOBITS)
|
|
|
|
s->data = tcc_mallocz(s->data_offset);
|
|
|
|
s->sh_addr = (unsigned long)s->data;
|
2002-08-18 17:22:55 +04:00
|
|
|
}
|
2002-07-14 18:38:33 +04:00
|
|
|
}
|
|
|
|
|
2002-11-02 17:12:32 +03:00
|
|
|
relocate_syms(s1, 1);
|
|
|
|
|
|
|
|
if (s1->nb_errors != 0)
|
|
|
|
return -1;
|
2002-07-18 04:51:11 +04:00
|
|
|
|
2002-07-14 18:38:33 +04:00
|
|
|
/* relocate each section */
|
2002-11-02 17:12:32 +03:00
|
|
|
for(i = 1; i < s1->nb_sections; i++) {
|
|
|
|
s = s1->sections[i];
|
2002-07-27 18:05:37 +04:00
|
|
|
if (s->reloc)
|
2002-07-28 03:08:04 +04:00
|
|
|
relocate_section(s1, s);
|
2002-07-14 18:38:33 +04:00
|
|
|
}
|
2002-11-02 17:12:32 +03:00
|
|
|
return 0;
|
2002-09-09 02:46:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* launch the compiled program with the given arguments */
|
|
|
|
int tcc_run(TCCState *s1, int argc, char **argv)
|
|
|
|
{
|
|
|
|
int (*prog_main)(int, char **);
|
|
|
|
|
2002-11-02 17:12:32 +03:00
|
|
|
if (tcc_relocate(s1) < 0)
|
|
|
|
return -1;
|
2002-05-14 03:00:17 +04:00
|
|
|
|
2002-09-09 02:46:32 +04:00
|
|
|
prog_main = tcc_get_symbol(s1, "main");
|
2002-01-04 23:17:24 +03:00
|
|
|
|
|
|
|
if (do_debug) {
|
2002-01-12 19:39:35 +03:00
|
|
|
#ifdef WIN32
|
|
|
|
error("debug mode currently not available for Windows");
|
|
|
|
#else
|
|
|
|
struct sigaction sigact;
|
2002-01-04 23:17:24 +03: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-12 19:39:35 +03:00
|
|
|
#endif
|
2002-01-04 23:17:24 +03:00
|
|
|
}
|
|
|
|
|
2002-03-04 01:45:55 +03:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2002-01-04 23:17:24 +03:00
|
|
|
if (do_bounds_check) {
|
2002-08-18 17:22:55 +04:00
|
|
|
void (*bound_init)(void);
|
|
|
|
|
|
|
|
/* set error function */
|
2002-11-03 03:43:55 +03:00
|
|
|
rt_bound_error_msg = (void *)tcc_get_symbol(s1, "__bound_error_msg");
|
2002-08-18 17:22:55 +04:00
|
|
|
|
2002-07-25 02:10:59 +04:00
|
|
|
/* XXX: use .init section so that it also work in binary ? */
|
2002-09-09 02:46:32 +04:00
|
|
|
bound_init = (void *)tcc_get_symbol(s1, "__bound_init");
|
2002-07-25 02:10:59 +04:00
|
|
|
bound_init();
|
2002-01-04 23:17:24 +03:00
|
|
|
}
|
2002-03-04 01:45:55 +03:00
|
|
|
#endif
|
2002-07-14 18:38:33 +04:00
|
|
|
return (*prog_main)(argc, argv);
|
2002-01-04 23:17:24 +03:00
|
|
|
}
|
|
|
|
|
2002-05-14 03:00:17 +04:00
|
|
|
TCCState *tcc_new(void)
|
2002-01-04 00:14:59 +03:00
|
|
|
{
|
2002-11-02 23:45:52 +03:00
|
|
|
const char *p, *r;
|
2002-05-14 03:00:17 +04:00
|
|
|
TCCState *s;
|
2002-01-04 00:14:59 +03:00
|
|
|
|
2002-11-02 17:12:32 +03:00
|
|
|
s = tcc_mallocz(sizeof(TCCState));
|
2002-05-14 03:00:17 +04:00
|
|
|
if (!s)
|
|
|
|
return NULL;
|
2002-11-02 17:12:32 +03:00
|
|
|
tcc_state = s;
|
2002-07-22 04:20:38 +04:00
|
|
|
s->output_type = TCC_OUTPUT_MEMORY;
|
2002-11-02 17:12:32 +03:00
|
|
|
|
2002-05-14 03:00:17 +04:00
|
|
|
/* default include paths */
|
2002-08-18 17:22:55 +04:00
|
|
|
tcc_add_sysinclude_path(s, "/usr/local/include");
|
|
|
|
tcc_add_sysinclude_path(s, "/usr/include");
|
|
|
|
tcc_add_sysinclude_path(s, CONFIG_TCC_PREFIX "/lib/tcc/include");
|
2001-11-11 05:53:01 +03:00
|
|
|
|
|
|
|
/* add all tokens */
|
2002-11-02 17:12:32 +03:00
|
|
|
table_ident = NULL;
|
|
|
|
memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
|
|
|
|
|
2001-11-18 19:33:01 +03:00
|
|
|
tok_ident = TOK_IDENT;
|
2002-01-26 21:05:29 +03:00
|
|
|
p = tcc_keywords;
|
2001-11-11 05:53:01 +03:00
|
|
|
while (*p) {
|
|
|
|
r = p;
|
|
|
|
while (*r++);
|
|
|
|
tok_alloc(p, r - p - 1);
|
|
|
|
p = r;
|
2001-10-28 02:48:39 +03:00
|
|
|
}
|
2001-11-11 05:53:01 +03:00
|
|
|
|
2002-08-30 01:16:24 +04:00
|
|
|
/* we add dummy defines for some special macros to speed up tests
|
|
|
|
and to have working defined() */
|
2002-11-19 00:46:44 +03:00
|
|
|
define_push(TOK___LINE__, MACRO_OBJ, NULL, NULL);
|
|
|
|
define_push(TOK___FILE__, MACRO_OBJ, NULL, NULL);
|
|
|
|
define_push(TOK___DATE__, MACRO_OBJ, NULL, NULL);
|
|
|
|
define_push(TOK___TIME__, MACRO_OBJ, NULL, NULL);
|
2002-08-30 01:16:24 +04:00
|
|
|
|
2001-11-11 19:07:43 +03:00
|
|
|
/* standard defines */
|
2002-05-14 03:00:17 +04:00
|
|
|
tcc_define_symbol(s, "__STDC__", NULL);
|
2002-07-22 04:20:38 +04:00
|
|
|
#if defined(TCC_TARGET_I386)
|
2002-05-14 03:00:17 +04:00
|
|
|
tcc_define_symbol(s, "__i386__", NULL);
|
2002-08-30 01:16:24 +04:00
|
|
|
#endif
|
|
|
|
#if defined(linux)
|
|
|
|
tcc_define_symbol(s, "linux", NULL);
|
2001-11-11 19:07:43 +03:00
|
|
|
#endif
|
2001-12-08 18:04:01 +03:00
|
|
|
/* tiny C specific defines */
|
2002-05-14 03:00:17 +04:00
|
|
|
tcc_define_symbol(s, "__TINYC__", NULL);
|
2002-11-24 17:18:17 +03:00
|
|
|
|
|
|
|
/* tiny C & gcc defines */
|
|
|
|
tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned int");
|
|
|
|
tcc_define_symbol(s, "__PTRDIFF_TYPE__", "int");
|
|
|
|
tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
|
2001-11-11 19:07:43 +03:00
|
|
|
|
2002-07-22 04:20:38 +04:00
|
|
|
/* default library paths */
|
|
|
|
tcc_add_library_path(s, "/usr/local/lib");
|
|
|
|
tcc_add_library_path(s, "/usr/lib");
|
|
|
|
tcc_add_library_path(s, "/lib");
|
|
|
|
|
|
|
|
/* no section zero */
|
2002-11-02 17:12:32 +03:00
|
|
|
dynarray_add((void ***)&s->sections, &s->nb_sections, NULL);
|
2002-07-22 04:20:38 +04:00
|
|
|
|
2002-01-04 00:14:59 +03:00
|
|
|
/* create standard sections */
|
2002-11-02 17:12:32 +03:00
|
|
|
text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
|
|
|
|
data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
|
|
|
|
bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
|
2002-07-22 04:20:38 +04:00
|
|
|
|
2002-07-13 21:23:25 +04:00
|
|
|
/* symbols are always generated for linking stage */
|
2002-11-02 17:12:32 +03:00
|
|
|
symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
|
2002-07-28 03:08:04 +04:00
|
|
|
".strtab",
|
|
|
|
".hashtab", SHF_PRIVATE);
|
|
|
|
strtab_section = symtab_section->link;
|
2002-07-22 04:20:38 +04:00
|
|
|
|
|
|
|
/* private symbol table for dynamic symbols */
|
2002-11-02 17:12:32 +03:00
|
|
|
s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE,
|
|
|
|
".dynstrtab",
|
|
|
|
".dynhashtab", SHF_PRIVATE);
|
2002-05-14 03:00:17 +04:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2002-11-02 17:12:32 +03:00
|
|
|
void tcc_delete(TCCState *s1)
|
2002-05-14 03:00:17 +04:00
|
|
|
{
|
2002-11-02 17:12:32 +03:00
|
|
|
int i, n;
|
|
|
|
|
|
|
|
/* free -D defines */
|
|
|
|
free_defines(NULL);
|
|
|
|
|
|
|
|
/* free tokens */
|
|
|
|
n = tok_ident - TOK_IDENT;
|
|
|
|
for(i = 0; i < n; i++)
|
|
|
|
tcc_free(table_ident[i]);
|
|
|
|
tcc_free(table_ident);
|
|
|
|
|
|
|
|
/* free all sections */
|
|
|
|
|
|
|
|
free_section(symtab_section->hash);
|
|
|
|
|
|
|
|
free_section(s1->dynsymtab_section->hash);
|
|
|
|
free_section(s1->dynsymtab_section->link);
|
|
|
|
free_section(s1->dynsymtab_section);
|
|
|
|
|
|
|
|
for(i = 1; i < s1->nb_sections; i++)
|
|
|
|
free_section(s1->sections[i]);
|
|
|
|
tcc_free(s1->sections);
|
|
|
|
|
|
|
|
/* free loaded dlls array */
|
|
|
|
for(i = 0; i < s1->nb_loaded_dlls; i++)
|
|
|
|
tcc_free(s1->loaded_dlls[i]);
|
|
|
|
tcc_free(s1->loaded_dlls);
|
|
|
|
|
|
|
|
/* library paths */
|
|
|
|
for(i = 0; i < s1->nb_library_paths; i++)
|
|
|
|
tcc_free(s1->library_paths[i]);
|
|
|
|
tcc_free(s1->library_paths);
|
|
|
|
|
|
|
|
/* cached includes */
|
|
|
|
for(i = 0; i < s1->nb_cached_includes; i++)
|
|
|
|
tcc_free(s1->cached_includes[i]);
|
|
|
|
tcc_free(s1->cached_includes);
|
|
|
|
|
|
|
|
for(i = 0; i < s1->nb_include_paths; i++)
|
|
|
|
tcc_free(s1->include_paths[i]);
|
|
|
|
tcc_free(s1->include_paths);
|
|
|
|
|
|
|
|
for(i = 0; i < s1->nb_sysinclude_paths; i++)
|
|
|
|
tcc_free(s1->sysinclude_paths[i]);
|
|
|
|
tcc_free(s1->sysinclude_paths);
|
|
|
|
|
|
|
|
tcc_free(s1);
|
2002-05-14 03:00:17 +04:00
|
|
|
}
|
|
|
|
|
2002-11-02 17:12:32 +03:00
|
|
|
int tcc_add_include_path(TCCState *s1, const char *pathname)
|
2002-05-14 03:00:17 +04:00
|
|
|
{
|
|
|
|
char *pathname1;
|
2002-07-22 04:20:38 +04:00
|
|
|
|
2002-08-18 17:22:55 +04:00
|
|
|
pathname1 = tcc_strdup(pathname);
|
2002-11-02 17:12:32 +03:00
|
|
|
dynarray_add((void ***)&s1->include_paths, &s1->nb_include_paths, pathname1);
|
2002-05-14 03:00:17 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-11-02 17:12:32 +03:00
|
|
|
int tcc_add_sysinclude_path(TCCState *s1, const char *pathname)
|
2002-08-18 17:22:55 +04:00
|
|
|
{
|
|
|
|
char *pathname1;
|
|
|
|
|
|
|
|
pathname1 = tcc_strdup(pathname);
|
2002-11-02 17:12:32 +03:00
|
|
|
dynarray_add((void ***)&s1->sysinclude_paths, &s1->nb_sysinclude_paths, pathname1);
|
2002-08-18 17:22:55 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-11-02 17:12:32 +03:00
|
|
|
static int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
|
2002-07-22 04:20:38 +04:00
|
|
|
{
|
|
|
|
const char *ext;
|
|
|
|
Elf32_Ehdr ehdr;
|
2002-11-02 17:12:32 +03:00
|
|
|
int fd, ret;
|
2002-07-22 04:20:38 +04:00
|
|
|
BufferedFile *saved_file;
|
|
|
|
|
|
|
|
/* find source file type with extension */
|
|
|
|
ext = strrchr(filename, '.');
|
|
|
|
if (ext)
|
|
|
|
ext++;
|
|
|
|
|
|
|
|
/* open the file */
|
|
|
|
saved_file = file;
|
2002-11-02 17:12:32 +03:00
|
|
|
file = tcc_open(s1, filename);
|
2002-07-22 04:20:38 +04:00
|
|
|
if (!file) {
|
|
|
|
if (flags & AFF_PRINT_ERROR) {
|
2002-11-02 17:12:32 +03:00
|
|
|
error_noabort("file '%s' not found", filename);
|
2002-07-22 04:20:38 +04:00
|
|
|
}
|
2002-11-02 17:12:32 +03:00
|
|
|
ret = -1;
|
|
|
|
goto fail1;
|
2002-07-22 04:20:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!ext || !strcmp(ext, "c")) {
|
|
|
|
/* C file assumed */
|
2002-11-02 17:12:32 +03:00
|
|
|
ret = tcc_compile(s1);
|
2002-07-22 04:20:38 +04:00
|
|
|
} else {
|
|
|
|
fd = file->fd;
|
|
|
|
/* assume executable format: auto guess file type */
|
2002-11-02 17:12:32 +03:00
|
|
|
if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr)) {
|
|
|
|
error_noabort("could not read header");
|
|
|
|
goto fail;
|
|
|
|
}
|
2002-07-22 04:20:38 +04:00
|
|
|
lseek(fd, 0, SEEK_SET);
|
|
|
|
|
|
|
|
if (ehdr.e_ident[0] == ELFMAG0 &&
|
|
|
|
ehdr.e_ident[1] == ELFMAG1 &&
|
|
|
|
ehdr.e_ident[2] == ELFMAG2 &&
|
|
|
|
ehdr.e_ident[3] == ELFMAG3) {
|
|
|
|
file->line_num = 0; /* do not display line number if error */
|
|
|
|
if (ehdr.e_type == ET_REL) {
|
2002-11-02 17:12:32 +03:00
|
|
|
ret = tcc_load_object_file(s1, fd, 0);
|
2002-07-22 04:20:38 +04:00
|
|
|
} else if (ehdr.e_type == ET_DYN) {
|
2002-11-02 17:12:32 +03:00
|
|
|
ret = tcc_load_dll(s1, fd, filename,
|
|
|
|
(flags & AFF_REFERENCED_DLL) != 0);
|
2002-07-22 04:20:38 +04:00
|
|
|
} else {
|
2002-11-02 17:12:32 +03:00
|
|
|
error_noabort("unrecognized ELF file");
|
|
|
|
goto fail;
|
2002-07-22 04:20:38 +04:00
|
|
|
}
|
|
|
|
} else if (memcmp((char *)&ehdr, ARMAG, 8) == 0) {
|
|
|
|
file->line_num = 0; /* do not display line number if error */
|
2002-11-02 17:12:32 +03:00
|
|
|
ret = tcc_load_archive(s1, fd);
|
2002-07-22 04:20:38 +04:00
|
|
|
} else {
|
|
|
|
/* as GNU ld, consider it is an ld script if not recognized */
|
2002-11-02 17:12:32 +03:00
|
|
|
ret = tcc_load_ldscript(s1);
|
|
|
|
if (ret < 0) {
|
|
|
|
error_noabort("unrecognized file type");
|
|
|
|
goto fail;
|
|
|
|
}
|
2002-07-22 04:20:38 +04:00
|
|
|
}
|
|
|
|
}
|
2002-11-02 17:12:32 +03:00
|
|
|
the_end:
|
2002-07-22 04:20:38 +04:00
|
|
|
tcc_close(file);
|
2002-11-02 17:12:32 +03:00
|
|
|
fail1:
|
2002-07-22 04:20:38 +04:00
|
|
|
file = saved_file;
|
2002-11-02 17:12:32 +03:00
|
|
|
return ret;
|
|
|
|
fail:
|
|
|
|
ret = -1;
|
|
|
|
goto the_end;
|
2002-07-22 04:20:38 +04:00
|
|
|
}
|
2002-05-14 03:00:17 +04:00
|
|
|
|
2002-11-02 17:12:32 +03:00
|
|
|
int tcc_add_file(TCCState *s, const char *filename)
|
2002-07-16 01:33:00 +04:00
|
|
|
{
|
2002-11-02 17:12:32 +03:00
|
|
|
return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR);
|
2002-07-22 04:20:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int tcc_add_library_path(TCCState *s, const char *pathname)
|
|
|
|
{
|
|
|
|
char *pathname1;
|
2002-07-18 04:51:11 +04:00
|
|
|
|
2002-08-18 17:22:55 +04:00
|
|
|
pathname1 = tcc_strdup(pathname);
|
2002-11-02 17:12:32 +03:00
|
|
|
dynarray_add((void ***)&s->library_paths, &s->nb_library_paths, pathname1);
|
2002-07-22 04:20:38 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find and load a dll. Return non zero if not found */
|
|
|
|
/* XXX: add '-rpath' option support ? */
|
|
|
|
static int tcc_add_dll(TCCState *s, const char *filename, int flags)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
int i;
|
|
|
|
|
2002-11-02 17:12:32 +03:00
|
|
|
for(i = 0; i < s->nb_library_paths; i++) {
|
2002-07-22 04:20:38 +04:00
|
|
|
snprintf(buf, sizeof(buf), "%s/%s",
|
2002-11-02 17:12:32 +03:00
|
|
|
s->library_paths[i], filename);
|
2002-07-22 04:20:38 +04:00
|
|
|
if (tcc_add_file_internal(s, buf, flags) == 0)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the library name is the same as the argument of the '-l' option */
|
|
|
|
int tcc_add_library(TCCState *s, const char *libraryname)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
int i;
|
|
|
|
void *h;
|
|
|
|
|
|
|
|
/* first we look for the dynamic library if not static linking */
|
2002-11-02 17:12:32 +03:00
|
|
|
if (!s->static_link) {
|
2002-07-22 04:20:38 +04:00
|
|
|
snprintf(buf, sizeof(buf), "lib%s.so", libraryname);
|
2002-08-31 16:43:39 +04:00
|
|
|
/* if we output to memory, then we simply we dlopen(). */
|
|
|
|
if (s->output_type == TCC_OUTPUT_MEMORY) {
|
|
|
|
/* Since the libc is already loaded, we don't need to load it again */
|
|
|
|
if (!strcmp(libraryname, "c"))
|
|
|
|
return 0;
|
|
|
|
h = dlopen(buf, RTLD_GLOBAL | RTLD_LAZY);
|
|
|
|
if (h)
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
if (tcc_add_dll(s, buf, 0) == 0)
|
|
|
|
return 0;
|
|
|
|
}
|
2002-07-22 04:20:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* then we look for the static library */
|
2002-11-02 17:12:32 +03:00
|
|
|
for(i = 0; i < s->nb_library_paths; i++) {
|
2002-07-22 04:20:38 +04:00
|
|
|
snprintf(buf, sizeof(buf), "%s/lib%s.a",
|
2002-11-02 17:12:32 +03:00
|
|
|
s->library_paths[i], libraryname);
|
2002-07-22 04:20:38 +04:00
|
|
|
if (tcc_add_file_internal(s, buf, 0) == 0)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-07-25 02:10:59 +04:00
|
|
|
int tcc_add_symbol(TCCState *s, const char *name, unsigned long val)
|
|
|
|
{
|
|
|
|
add_elf_sym(symtab_section, val, 0,
|
|
|
|
ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
|
|
|
|
SHN_ABS, name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-22 04:20:38 +04:00
|
|
|
int tcc_set_output_type(TCCState *s, int output_type)
|
|
|
|
{
|
|
|
|
s->output_type = output_type;
|
|
|
|
|
2002-07-27 19:38:21 +04:00
|
|
|
/* if bound checking, then add corresponding sections */
|
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
|
|
|
if (do_bounds_check) {
|
|
|
|
/* define symbol */
|
|
|
|
tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
|
|
|
|
/* create bounds sections */
|
2002-11-02 17:12:32 +03:00
|
|
|
bounds_section = new_section(s, ".bounds",
|
2002-07-27 19:38:21 +04:00
|
|
|
SHT_PROGBITS, SHF_ALLOC);
|
2002-11-02 17:12:32 +03:00
|
|
|
lbounds_section = new_section(s, ".lbounds",
|
2002-07-27 19:38:21 +04:00
|
|
|
SHT_PROGBITS, SHF_ALLOC);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* add debug sections */
|
|
|
|
if (do_debug) {
|
|
|
|
/* stab symbols */
|
2002-11-02 17:12:32 +03:00
|
|
|
stab_section = new_section(s, ".stab", SHT_PROGBITS, 0);
|
2002-07-27 19:38:21 +04:00
|
|
|
stab_section->sh_entsize = sizeof(Stab_Sym);
|
2002-11-02 17:12:32 +03:00
|
|
|
stabstr_section = new_section(s, ".stabstr", SHT_STRTAB, 0);
|
2002-07-27 19:38:21 +04:00
|
|
|
put_elf_str(stabstr_section, "");
|
|
|
|
stab_section->link = stabstr_section;
|
|
|
|
/* put first entry */
|
|
|
|
put_stabs("", 0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
2002-07-22 04:20:38 +04:00
|
|
|
/* add libc crt1/crti objects */
|
|
|
|
if (output_type == TCC_OUTPUT_EXE ||
|
|
|
|
output_type == TCC_OUTPUT_DLL) {
|
|
|
|
if (output_type != TCC_OUTPUT_DLL)
|
|
|
|
tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crt1.o");
|
|
|
|
tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crti.o");
|
2002-07-16 01:33:00 +04:00
|
|
|
}
|
2002-07-22 04:20:38 +04:00
|
|
|
return 0;
|
2002-07-16 01:33:00 +04:00
|
|
|
}
|
|
|
|
|
2002-07-22 04:20:38 +04:00
|
|
|
#if !defined(LIBTCC)
|
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
static int64_t getclock_us(void)
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
struct _timeb tb;
|
|
|
|
_ftime(&tb);
|
|
|
|
return (tb.time * 1000LL + tb.millitm) * 1000LL;
|
|
|
|
#else
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
return tv.tv_sec * 1000000LL + tv.tv_usec;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2002-05-14 03:00:17 +04:00
|
|
|
void help(void)
|
|
|
|
{
|
2002-11-03 03:43:55 +03:00
|
|
|
printf("tcc version 0.9.13 - Tiny C Compiler - Copyright (C) 2001, 2002 Fabrice Bellard\n"
|
2002-07-25 02:10:59 +04:00
|
|
|
"usage: tcc [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
|
2002-11-03 03:43:55 +03:00
|
|
|
" [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-static]\n"
|
2002-07-16 02:17:02 +04:00
|
|
|
" [--] infile1 [infile2... --] [infile_args...]\n"
|
2002-05-14 03:00:17 +04:00
|
|
|
"\n"
|
2002-07-16 02:17:02 +04:00
|
|
|
"General options:\n"
|
|
|
|
" -c compile only - generate an object file\n"
|
2002-07-25 02:10:59 +04:00
|
|
|
" -o outfile set output filename\n"
|
2002-07-16 02:17:02 +04:00
|
|
|
" -- allows multiples input files if no -o option given. Also\n"
|
|
|
|
" separate input files from runtime arguments\n"
|
2002-07-25 02:10:59 +04:00
|
|
|
" -Bdir set tcc internal library path\n"
|
|
|
|
" -bench output compilation statistics\n"
|
2002-07-16 02:17:02 +04:00
|
|
|
"Preprocessor options:\n"
|
|
|
|
" -Idir add include path 'dir'\n"
|
|
|
|
" -Dsym[=val] define 'sym' with value 'val'\n"
|
|
|
|
" -Usym undefine 'sym'\n"
|
|
|
|
"Linker options:\n"
|
2002-07-22 04:20:38 +04:00
|
|
|
" -Ldir add library path 'dir'\n"
|
2002-08-18 17:22:55 +04:00
|
|
|
" -llib link with dynamic or static library 'lib'\n"
|
2002-07-28 03:08:04 +04:00
|
|
|
" -shared generate a shared library\n"
|
2002-07-25 02:10:59 +04:00
|
|
|
" -static static linking\n"
|
2002-08-31 02:46:35 +04:00
|
|
|
" -r relocatable output\n"
|
2002-11-03 03:43:55 +03:00
|
|
|
"Debugger options:\n"
|
|
|
|
" -g generate runtime debug info\n"
|
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
|
|
|
" -b compile with built-in memory and bounds checker (implies -g)\n"
|
|
|
|
#endif
|
|
|
|
" -bt N show N callers in stack traces\n"
|
2002-05-14 03:00:17 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *r, *outfile;
|
2002-08-31 02:46:35 +04:00
|
|
|
int optind, output_type, multiple_files, i, reloc_output;
|
2002-05-14 03:00:17 +04:00
|
|
|
TCCState *s;
|
2002-08-31 02:46:35 +04:00
|
|
|
char **files;
|
2002-11-02 17:12:32 +03:00
|
|
|
int nb_files, nb_libraries, nb_objfiles, dminus, ret;
|
2002-08-31 02:46:35 +04:00
|
|
|
char objfilename[1024];
|
2002-11-19 00:46:44 +03:00
|
|
|
int64_t start_time = 0;
|
2002-05-14 03:00:17 +04:00
|
|
|
|
|
|
|
s = tcc_new();
|
2002-07-22 04:20:38 +04:00
|
|
|
output_type = TCC_OUTPUT_MEMORY;
|
2002-05-14 03:00:17 +04:00
|
|
|
|
2001-11-11 05:53:01 +03:00
|
|
|
optind = 1;
|
2001-11-18 19:33:01 +03:00
|
|
|
outfile = NULL;
|
2002-07-16 02:17:02 +04:00
|
|
|
multiple_files = 0;
|
2002-09-08 16:42:36 +04:00
|
|
|
dminus = 0;
|
2002-08-31 02:46:35 +04:00
|
|
|
files = NULL;
|
|
|
|
nb_files = 0;
|
2002-07-22 04:20:38 +04:00
|
|
|
nb_libraries = 0;
|
2002-08-31 02:46:35 +04:00
|
|
|
reloc_output = 0;
|
2001-11-11 05:53:01 +03:00
|
|
|
while (1) {
|
|
|
|
if (optind >= argc) {
|
2002-08-31 02:46:35 +04:00
|
|
|
if (nb_files == 0)
|
|
|
|
goto show_help;
|
|
|
|
else
|
|
|
|
break;
|
2001-11-11 05:53:01 +03:00
|
|
|
}
|
2002-08-31 02:46:35 +04:00
|
|
|
r = argv[optind++];
|
|
|
|
if (r[0] != '-') {
|
|
|
|
/* add a new file */
|
|
|
|
dynarray_add((void ***)&files, &nb_files, r);
|
|
|
|
if (!multiple_files) {
|
|
|
|
optind--;
|
|
|
|
/* argv[0] will be this file */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (r[1] == '-') {
|
|
|
|
/* '--' enables multiple files input and also ends several file input */
|
2002-09-08 16:42:36 +04:00
|
|
|
if (dminus && multiple_files) {
|
2002-08-31 02:46:35 +04:00
|
|
|
optind--; /* argv[0] will be '--' */
|
|
|
|
break;
|
|
|
|
}
|
2002-09-08 16:42:36 +04:00
|
|
|
dminus = 1;
|
2002-07-16 02:17:02 +04:00
|
|
|
multiple_files = 1;
|
2002-07-18 04:51:11 +04:00
|
|
|
} else if (r[1] == 'h' || r[1] == '?') {
|
2002-08-31 02:46:35 +04:00
|
|
|
show_help:
|
|
|
|
help();
|
|
|
|
return 1;
|
2002-07-16 02:17:02 +04:00
|
|
|
} else if (r[1] == 'I') {
|
2002-05-14 03:00:17 +04:00
|
|
|
if (tcc_add_include_path(s, r + 2) < 0)
|
2001-11-11 05:53:01 +03:00
|
|
|
error("too many include paths");
|
2001-11-11 19:07:43 +03:00
|
|
|
} else if (r[1] == 'D') {
|
2002-05-14 03:00:17 +04:00
|
|
|
char *sym, *value;
|
|
|
|
sym = r + 2;
|
|
|
|
value = strchr(sym, '=');
|
|
|
|
if (value) {
|
|
|
|
*value = '\0';
|
|
|
|
value++;
|
|
|
|
}
|
|
|
|
tcc_define_symbol(s, sym, value);
|
2002-01-04 00:14:59 +03:00
|
|
|
} else if (r[1] == 'U') {
|
2002-05-14 03:00:17 +04:00
|
|
|
tcc_undefine_symbol(s, r + 2);
|
2002-07-22 04:20:38 +04:00
|
|
|
} else if (r[1] == 'L') {
|
|
|
|
tcc_add_library_path(s, r + 2);
|
2002-07-25 02:10:59 +04:00
|
|
|
} else if (r[1] == 'B') {
|
|
|
|
/* set tcc utilities path (mainly for tcc development) */
|
|
|
|
tcc_lib_path = r + 2;
|
2001-11-11 19:07:43 +03:00
|
|
|
} else if (r[1] == 'l') {
|
2002-08-31 02:46:35 +04:00
|
|
|
dynarray_add((void ***)&files, &nb_files, r);
|
|
|
|
nb_libraries++;
|
2002-01-26 21:05:29 +03:00
|
|
|
} else if (!strcmp(r + 1, "bench")) {
|
|
|
|
do_bench = 1;
|
2002-11-03 03:43:55 +03:00
|
|
|
} else if (!strcmp(r + 1, "bt")) {
|
|
|
|
num_callers = atoi(argv[optind++]);
|
|
|
|
} else
|
2002-03-04 01:45:55 +03:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2002-07-27 19:38:21 +04:00
|
|
|
if (r[1] == 'b') {
|
|
|
|
do_bounds_check = 1;
|
|
|
|
do_debug = 1;
|
2002-07-13 21:23:25 +04:00
|
|
|
} else
|
2002-07-27 19:38:21 +04:00
|
|
|
#endif
|
|
|
|
if (r[1] == 'g') {
|
|
|
|
do_debug = 1;
|
|
|
|
} else if (r[1] == 'c') {
|
2002-07-16 02:17:02 +04:00
|
|
|
multiple_files = 1;
|
2002-07-22 04:20:38 +04:00
|
|
|
output_type = TCC_OUTPUT_OBJ;
|
2002-07-16 01:33:00 +04:00
|
|
|
} else if (!strcmp(r + 1, "static")) {
|
2002-11-02 17:12:32 +03:00
|
|
|
s->static_link = 1;
|
2002-07-13 21:23:25 +04:00
|
|
|
} else if (!strcmp(r + 1, "shared")) {
|
2002-07-22 04:20:38 +04:00
|
|
|
output_type = TCC_OUTPUT_DLL;
|
2001-11-18 19:33:01 +03:00
|
|
|
} else if (r[1] == 'o') {
|
|
|
|
if (optind >= argc)
|
|
|
|
goto show_help;
|
2002-07-16 02:17:02 +04:00
|
|
|
multiple_files = 1;
|
2001-11-18 19:33:01 +03:00
|
|
|
outfile = argv[optind++];
|
2002-08-31 02:46:35 +04:00
|
|
|
} else if (r[1] == 'r') {
|
|
|
|
/* generate a .o merging several output files */
|
|
|
|
reloc_output = 1;
|
|
|
|
output_type = TCC_OUTPUT_OBJ;
|
2002-11-02 17:12:32 +03:00
|
|
|
} else if (r[1] == 'W' || r[1] == 'O' || r[1] == 'm' || r[1] == 'f') {
|
|
|
|
/* ignore those options to be a drop-in replacement for gcc */
|
2001-11-11 05:53:01 +03:00
|
|
|
} else {
|
2002-03-04 01:45:55 +03:00
|
|
|
error("invalid option -- '%s'", r);
|
2001-11-11 05:53:01 +03:00
|
|
|
}
|
|
|
|
}
|
2002-07-16 02:17:02 +04:00
|
|
|
|
2002-08-31 02:46:35 +04:00
|
|
|
nb_objfiles = nb_files - nb_libraries;
|
|
|
|
|
2002-07-22 04:20:38 +04:00
|
|
|
/* if outfile provided without other options, we output an
|
|
|
|
executable */
|
|
|
|
if (outfile && output_type == TCC_OUTPUT_MEMORY)
|
|
|
|
output_type = TCC_OUTPUT_EXE;
|
2002-07-18 04:51:11 +04:00
|
|
|
|
2002-08-31 02:46:35 +04:00
|
|
|
/* check -c consistency : only single file handled. XXX: checks file type */
|
|
|
|
if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
|
|
|
|
/* accepts only a single input file */
|
|
|
|
if (nb_objfiles != 1)
|
|
|
|
error("cannot specify multiple files with -c");
|
|
|
|
if (nb_libraries != 0)
|
|
|
|
error("cannot specify libraries with -c");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* compute default outfile name */
|
|
|
|
if (output_type != TCC_OUTPUT_MEMORY && !outfile) {
|
|
|
|
if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
|
|
|
|
char *ext;
|
|
|
|
/* add .o extension */
|
|
|
|
pstrcpy(objfilename, sizeof(objfilename) - 1, files[0]);
|
|
|
|
ext = strrchr(objfilename, '.');
|
|
|
|
if (!ext)
|
|
|
|
goto default_outfile;
|
|
|
|
strcpy(ext + 1, "o");
|
|
|
|
} else {
|
|
|
|
default_outfile:
|
|
|
|
pstrcpy(objfilename, sizeof(objfilename), "a.out");
|
2002-07-16 02:17:02 +04:00
|
|
|
}
|
2002-08-31 02:46:35 +04:00
|
|
|
outfile = objfilename;
|
2002-07-16 02:17:02 +04:00
|
|
|
}
|
2001-12-08 18:04:01 +03:00
|
|
|
|
2002-11-19 00:46:44 +03:00
|
|
|
if (do_bench) {
|
|
|
|
start_time = getclock_us();
|
|
|
|
}
|
|
|
|
|
2002-08-31 02:46:35 +04:00
|
|
|
tcc_set_output_type(s, output_type);
|
|
|
|
|
|
|
|
/* compile or add each files or library */
|
|
|
|
for(i = 0;i < nb_files; i++) {
|
|
|
|
const char *filename;
|
|
|
|
|
|
|
|
filename = files[i];
|
|
|
|
if (filename[0] == '-') {
|
|
|
|
if (tcc_add_library(s, filename + 2) < 0)
|
|
|
|
error("cannot find %s", filename);
|
|
|
|
} else {
|
2002-11-02 17:12:32 +03:00
|
|
|
if (tcc_add_file(s, filename) < 0) {
|
|
|
|
ret = 1;
|
|
|
|
goto the_end;
|
|
|
|
}
|
2002-08-31 02:46:35 +04:00
|
|
|
}
|
2002-07-18 04:51:11 +04:00
|
|
|
}
|
|
|
|
|
2002-11-02 17:12:32 +03:00
|
|
|
/* free all files */
|
|
|
|
tcc_free(files);
|
|
|
|
|
2002-01-26 21:05:29 +03:00
|
|
|
if (do_bench) {
|
2002-11-19 00:46:44 +03:00
|
|
|
double total_time;
|
|
|
|
total_time = (double)(getclock_us() - start_time) / 1000000.0;
|
|
|
|
if (total_time < 0.001)
|
|
|
|
total_time = 0.001;
|
|
|
|
if (total_bytes < 1)
|
|
|
|
total_bytes = 1;
|
|
|
|
printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
|
|
|
|
tok_ident - TOK_IDENT, total_lines, total_bytes,
|
|
|
|
total_time, (int)(total_lines / total_time),
|
|
|
|
total_bytes / total_time / 1000000.0);
|
2002-01-26 21:05:29 +03:00
|
|
|
}
|
|
|
|
|
2002-07-22 04:20:38 +04:00
|
|
|
if (s->output_type != TCC_OUTPUT_MEMORY) {
|
|
|
|
tcc_output_file(s, outfile);
|
2002-11-02 17:12:32 +03:00
|
|
|
ret = 0;
|
2001-11-18 19:33:01 +03:00
|
|
|
} else {
|
2002-11-02 17:12:32 +03:00
|
|
|
ret = tcc_run(s, argc - optind, argv + optind);
|
2001-11-18 19:33:01 +03:00
|
|
|
}
|
2002-11-02 17:12:32 +03:00
|
|
|
the_end:
|
2002-11-03 03:43:55 +03:00
|
|
|
/* XXX: cannot do it with bound checking because of the malloc hooks */
|
|
|
|
if (!do_bounds_check)
|
|
|
|
tcc_delete(s);
|
2002-11-02 17:12:32 +03:00
|
|
|
|
|
|
|
#ifdef MEM_DEBUG
|
|
|
|
if (do_bench) {
|
|
|
|
printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return ret;
|
2001-10-28 02:48:39 +03:00
|
|
|
}
|
2002-05-14 03:00:17 +04:00
|
|
|
|
|
|
|
#endif
|