From 6829053c2534aed16a8141dcf0651a4d33932a37 Mon Sep 17 00:00:00 2001 From: sandeep Date: Wed, 21 Nov 2001 17:10:33 +0000 Subject: [PATCH] added __typeof extention will return type of a variable / expression as enumerated in typeof.h git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@1627 4a8a32a2-be11-0410-ad9d-d568d2c75423 --- src/SDCC.lex | 1 + src/SDCC.y | 3 +- src/SDCCast.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++--- src/SDCCglobl.h | 3 +- src/SDCCmain.c | 3 +- src/SDCCsymt.h | 21 +++++++++++++ 6 files changed, 108 insertions(+), 7 deletions(-) diff --git a/src/SDCC.lex b/src/SDCC.lex index 44756051..b9aafd72 100644 --- a/src/SDCC.lex +++ b/src/SDCC.lex @@ -171,6 +171,7 @@ struct options save_options ; "while" { count(); return(WHILE); } "xdata" { count(); TKEYWORD(XDATA); } "..." { count(); return(VAR_ARGS);} +"__typeof" { count(); return TYPEOF;} {L}({L}|{D})* { count(); return(check_type()); } 0[xX]{H}+{IS}? { count(); yylval.val = constVal(yytext); return(CONSTANT); } 0{D}+{IS}? { count(); yylval.val = constVal(yytext); return(CONSTANT); } diff --git a/src/SDCC.y b/src/SDCC.y index b88ead94..7539a672 100644 --- a/src/SDCC.y +++ b/src/SDCC.y @@ -74,7 +74,7 @@ value *cenum = NULL ; /* current enumeration type chain*/ %token IDENTIFIER TYPE_NAME %token CONSTANT STRING_LITERAL -%token SIZEOF +%token SIZEOF TYPEOF %token PTR_OP INC_OP DEC_OP LEFT_OP RIGHT_OP LE_OP GE_OP EQ_OP NE_OP %token AND_OP OR_OP %token MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN ADD_ASSIGN @@ -278,6 +278,7 @@ unary_expr | unary_operator cast_expr { $$ = newNode($1,$2,NULL) ; } | SIZEOF unary_expr { $$ = newNode(SIZEOF,NULL,$2); } | SIZEOF '(' type_name ')' { $$ = newAst_VALUE(sizeofOp($3)); } + | TYPEOF unary_expr { $$ = newNode(TYPEOF,NULL,$2); } ; unary_operator diff --git a/src/SDCCast.c b/src/SDCCast.c index bf5b3abf..c631350c 100644 --- a/src/SDCCast.c +++ b/src/SDCCast.c @@ -1975,7 +1975,9 @@ decorateType (ast * tree) ast *dtl, *dtr; dtl = decorateType (tree->left); - dtr = decorateType (tree->right); + /* delay right side for '?' operator since conditional macro expansions might + rely on this */ + dtr = (tree->opval.op == '?' ? tree->right : decorateType (tree->right)); /* this is to take care of situations when the tree gets rewritten */ @@ -2885,6 +2887,79 @@ decorateType (ast * tree) tree->opval.val->type); return tree; + /*------------------------------------------------------------------*/ + /*----------------------------*/ + /* typeof */ + /*----------------------------*/ + case TYPEOF: + /* return typeof enum value */ + tree->type = EX_VALUE; + { + int typeofv = 0; + if (IS_SPEC(tree->right->ftype)) { + switch (SPEC_NOUN(tree->right->ftype)) { + case V_INT: + if (SPEC_LONG(tree->right->ftype)) typeofv = TYPEOF_LONG; + else typeofv = TYPEOF_INT; + break; + case V_FLOAT: + typeofv = TYPEOF_FLOAT; + break; + case V_CHAR: + typeofv = TYPEOF_CHAR; + break; + case V_VOID: + typeofv = TYPEOF_VOID; + break; + case V_STRUCT: + typeofv = TYPEOF_STRUCT; + break; + case V_BIT: + typeofv = TYPEOF_BIT; + break; + case V_SBIT: + typeofv = TYPEOF_SBIT; + break; + default: + break; + } + } else { + switch (DCL_TYPE(tree->right->ftype)) { + case POINTER: + typeofv = TYPEOF_POINTER; + break; + case FPOINTER: + typeofv = TYPEOF_FPOINTER; + break; + case CPOINTER: + typeofv = TYPEOF_CPOINTER; + break; + case GPOINTER: + typeofv = TYPEOF_GPOINTER; + break; + case PPOINTER: + typeofv = TYPEOF_PPOINTER; + break; + case IPOINTER: + typeofv = TYPEOF_IPOINTER; + break; + case ARRAY: + typeofv = TYPEOF_ARRAY; + break; + case FUNCTION: + typeofv = TYPEOF_FUNCTION; + break; + default: + break; + } + } + sprintf (buffer, "%d", typeofv); + tree->opval.val = constVal (buffer); + tree->right = tree->left = NULL; + TETYPE (tree) = getSpec (TTYPE (tree) = + tree->opval.val->type); + } + return tree; /*------------------------------------------------------------------*/ /*----------------------------*/ /* conditional operator '?' */ @@ -2894,13 +2969,14 @@ decorateType (ast * tree) assert(IS_COLON_OP(tree->right)); /* if already known then replace the tree : optimizer will do it but faster to do it here */ - if (IS_LITERAL (LTYPE(tree))) { + if (IS_LITERAL (LTYPE(tree))) { if ( ((int) floatFromVal (valFromType (LETYPE (tree)))) != 0) { - return tree->right->left ; + return decorateType(tree->right->left) ; } else { - return tree->right->right ; + return decorateType(tree->right->right) ; } } else { + tree->right = decorateType(tree->right); TTYPE (tree) = RTYPE(tree); TETYPE (tree) = getSpec (TTYPE (tree)); } diff --git a/src/SDCCglobl.h b/src/SDCCglobl.h index 0c1f440f..0efe1d67 100644 --- a/src/SDCCglobl.h +++ b/src/SDCCglobl.h @@ -230,6 +230,7 @@ struct options char *calleeSaves[128]; /* list of functions using callee save */ char *excludeRegs[32]; /* registers excluded from saving */ int all_callee_saves; /* callee saves for all functions */ + int stack_probe; /* insert call to function __stack_probe */ /* starting address of the segments */ int xstack_loc; /* initial location of external stack */ int stack_loc; /* initial value of internal stack pointer */ @@ -237,7 +238,7 @@ struct options int data_loc; /* interram start location */ int idata_loc; /* indirect address space */ int code_loc; /* code location start */ - int iram_size; /* internal ram size (used only for error checking) */ + int iram_size; /* internal ram size (used only for error checking) */ }; /* forward definition for variables accessed globally */ diff --git a/src/SDCCmain.c b/src/SDCCmain.c index 2c1363c0..a989fe2d 100644 --- a/src/SDCCmain.c +++ b/src/SDCCmain.c @@ -207,7 +207,8 @@ optionsTable[] = { { 0, "--profile", &options.profile, "On supported ports, generate extra profiling information" }, { 0, "--fommit-frame-pointer", &options.ommitFramePtr, "Leave out the frame pointer." }, { 0, "--all-callee-saves", &options.all_callee_saves, "callee will always save registers used" }, - { 0, "--use-accelerator", &options.useAccelerator,"generate code for DS390 Arithmetic Accelerator"} + { 0, "--use-accelerator", &options.useAccelerator,"generate code for DS390 Arithmetic Accelerator"}, + { 0, "--stack-probe", &options.stack_probe,"insert call to function __stack_probe at each function prologue"} }; /** Table of all unsupported options and help text to display when one diff --git a/src/SDCCsymt.h b/src/SDCCsymt.h index d0274ac0..8660ad3a 100644 --- a/src/SDCCsymt.h +++ b/src/SDCCsymt.h @@ -29,6 +29,27 @@ #define SDCC_NAME_MAX 3*SDCC_SYMNAME_MAX // big enough for ___etc #include "SDCChasht.h" +enum { + TYPEOF_INT=1, + TYPEOF_SHORT, + TYPEOF_CHAR, + TYPEOF_LONG, + TYPEOF_FLOAT, + TYPEOF_BIT, + TYPEOF_SBIT, + TYPEOF_SFR, + TYPEOF_VOID, + TYPEOF_STRUCT, + TYPEOF_ARRAY, + TYPEOF_FUNCTION, + TYPEOF_POINTER, + TYPEOF_FPOINTER, + TYPEOF_CPOINTER, + TYPEOF_GPOINTER, + TYPEOF_PPOINTER, + TYPEOF_IPOINTER, + TYPEOF_EEPPOINTER +}; #define HASHTAB_SIZE 256 -- 2.47.2