[idle] Strukturite za analizatora i generatora na ICC
- From: Ivaylo Riskov <ivaylo_riskov@xxxxxxx>
- To: idle@xxxxxxxxxxxxx
- Date: Mon, 30 Dec 2002 18:25:07 +0200
Izmxdruwah nqkakwi strukturi za promenliwi, tipowe, funkcii i scope-owe! Tri
strukturi, koito obedinqwat wsichko towa! Osnownoto e che promenliwi i
funkcii sa edno i sxshto (e pochti)! Towa e wsxshtnost ideqta na Rangel
(interesno, edwa predi 5 minuti widqh che i toj e pisal sxshtoto - mozheh da
si spestq edin bezsxnen chas snoshti, ako bqh chel po-wnimatelno onowa pismo,
ama w 3 chasa sutrinta na chowek mu se spi i ne wnimawa), do koqto sxm
stignal bez da osxznawam. Taka ili inache neshtata stawat mnogo oprosteni!
Imame samo 3 strukturi - za promenliwi, za tipowe i za scope-owe! Ne
smeswajte funkcii sxs scope-owe - razlichni neshta sa!
Ideqta e che ako imame:
void x(int p, int q, int r);
to e sxshtoto kato da deklarirame promenliwa x ot tip:
void (*)(int, int, int);
i da i slozhim stoqnost pointer kxm istinskiqt kod!
Po tozi nachin x stawa edna sxwsem normalna promenliwa!
Dostatxchen e edin flag, kojto da kazwa, che ne mozhe da i se priswoqwat
stojnosti i wsichko e OK!
Wsxshtnost wsichko e opisano w koda! Snoshti si igrah da gledam header-ite na
gcc i ottam si otkradnah nqkoi idei! Za sprawka wishte gcc/tree.h ot source
direktoriqta na gcc.
Towa reshawa wsichki problemi dosega i sigurno sxzdawa nowi, no to e normalno!
Ako nqkoi ima komentari, da gi kazwa!
Sorry for the commenting style in the code - ANSI C does not support line
comments //!
I oshte neshto za wsichki flagowe sxm izpolzwal bitfields. Taka se raboti i
debugwa mnogo po-lesno!
Ivo
--
============================================
I used to think that the brain was the most wonderful
organ in my body. Then I realized who was telling me this.
-- Emo Phillips
============================================
struct SVar;
struct SType;
struct SScope;
/*********************************/
/* Variable definition structure */
/*********************************/
/* Unspecified just holds the null value - don't want to assign it*/
#define VAR_TYPE_UNSPECIFIED 0
#define VAR_TYPE_FUNCTION 1
#define VAR_TYPE_SCALAR 2
#define VAR_TYPE_STRUCT 3
#define VAR_TYPE_UNION 4
#define VAR_TYPE_ENUM 5
typedef struct {
/* Section 1 */
/* PCode oriented properties */
/* name of the identifier as assigned by the tokenizer */
uint name;
/* where the declaration of the var starts */
uint decl_start, decl_end;
/* where the definition of the var starts */
uint def_start, def_end;
/* where in the source are we? index in the tokenizer's stacks*/
uint line, file;
/* Section 2 */
/* global properties */
/* type of the variable see above */
uint8 type;
/* pointer to variable's memory - if it has any */
uint8 *mem_ptr;
/* meaningful for arrays, so that int a[5] is interpreted as a of */
/* type int *, having size of 20 and is_static(see below) */
/* if mem_ptr != NULL - the alloced size */
uint size;
/* how far the var is dereferenced so that int **a is
interpreted */
/* as a of type int having ptr_depth of 2 */
uint8 ptr_depth;
/* base type after removing all referencies */
SType *base_type;
/* The scope that holds it */
SScope *parent;
/* Section 3 */
/* IDLe specific properites */
/* Variable's handle in context of IDLe */
HMAPLIST maplist;
/* For functions - their body */
/* For other - their initializing code, if any */
HSTATEMENT code_start, code_end;
/* Section 4 */
/* Flags */
/* NOTE!!! All flags are to be put one after another so that the
compiler */
/* will unite the bitfields in one machine word, not in hundreds */
/* general flags */
/* does anybody need me? */
uint ;
uint is_prototyped:1;
uint is_addressable:1;
uint is_readonly:1;
uint is_tempvar:1;
/* var qualifier flags */
uint is_extern:1;
uint is_static:1;
uint is_volatile:1;
uint is_auto:1;
uint is_register:1;
/* unsigned is meaningful only for simple int types */
uint is_unsigned:1;
/* warning oriented flags */
/* to produce unused variable warnings */
uint is_used:1;
/* do not produce warning if is_used is cleared */
uint no_used_warn:1;
/* to produce 'var might be used uninitialized' warning */
uint is_init:1;
/* links to the next variable in the current scope context */
struct SVar *next;
} SVar;
/*********************************/
/* Type definition structure */
/*********************************/
#define TYPE_NONE 0
#define TYPE_FUNCTION 1
#define TYPE_ENUM 2
#define TYPE_STRUCT 3
#define TYPE_UNION 4
/* difference between TYPE_SIMPLE and TYPE_BASE: */
/* typedef int my_int; */
/* int is base, while my_int is simple */
#define TYPE_SIMPLE 5
#define TYPE_BASE 0x80
#define TYPE_BASE_VOID TYPE_BASE + 1
#define TYPE_BASE_INT TYPE_BASE + 2
#define TYPE_BASE_SHORT TYPE_BASE + 3
#define TYPE_BASE_LONG TYPE_BASE + 4
#define TYPE_BASE_CHAR TYPE_BASE + 5
#define TYPE_BASE_FLOAT TYPE_BASE + 6
#define TYPE_BASE_DOUBLE TYPE_BASE + 7
#deifne TYPE_BASE_LONG_DOUBLE TYPE_BASE + 8
typedef union {
/* when type is base this is not used */
/* when type is base this is not used: see Section 2 of SType
definiiton */
/* When type is function this points to array of SVar structs (not */
/* array of SVar * structs) to function parameters */
struct SVar *params;
/* when type is struct or union this points to array of SVar structs
that describe */
/* the members of the struct */
struct SVar *members;
} STypeParams;
typedef struct {
/* Section 1 */
/* PCode oriented properties */
/* name of the identifier as assigned by the tokenizer */
uint name;
/* where the declaration of the var starts */
uint decl_start, decl_end;
/* where the definition of the var starts */
uint def_start, def_end;
/* where in the source are we? index in the tokenizer's stacks
*/
/* NOTE!!! line, file refer to the definition since the
declaration */
/* is either correct or not recognized as a declaration and
therefore */
/* this struct is not created */
uint line, file;
/* Section 2 */
/* global properties */
/* type of the variable: see above */
uint8 type;
/* size of the type */
uint size;
/* bitfields is used so that a and b will have different types
when */
/* declared as int a; and int b:2; respectively */
/* NOTE!!! only for simple and base integer types */
uint bitfields;
/* will be useful here since a fool could type: typedef char
*str; */
/* when type is a function this and the following describe a
function's */
/* return value */
uint8 ptr_depth;
/* base type after stripping all references: see prev. comment */
/* has nothing to do with TYPE_BASE, just a name coincidence */
SType *base_type;
/* Section 3 */
/* parameters */
STypeParams params;
/* link to the next type - types don't care about scopes */
SType *next;
} SType;
extern SType *types;
/*********************************/
/* Scope tree structure */
/*********************************/
typedef struct {
/* all scope variables */
SVar *vars;
/* nesting_level: 0 for the global scope */
uint nesting_level;
/* is there are function hosting the scope - there always is, */
/* except for the global scope, where host is NULL */
SVar *host;
HSCOPE handle;
/* number of children scopes */
uint children_count;
/* pointer to array of children scopes of type struct SScope and not
type */
/* struct SScope * */
SScope *children;
} SScope;
extern SScope global_scope;
Other related posts:
- » [idle] Strukturite za analizatora i generatora na ICC