goomsl_yacc.c

来自「linux下的MPEG1」· C语言 代码 · 共 1,746 行 · 第 1/5 页

C
1,746
字号
/* A Bison parser, made by GNU Bison 1.875.  *//* Skeleton parser for Yacc-like parsing with Bison,   Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002 Free Software Foundation, Inc.   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, 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., 59 Temple Place - Suite 330,   Boston, MA 02111-1307, USA.  *//* As a special exception, when this file is copied by Bison into a   Bison output file, you may use that output file without restriction.   This special exception was added by the Free Software Foundation   in version 1.24 of Bison.  *//* Written by Richard Stallman by simplifying the original so called   ``semantic'' parser.  *//* All symbols defined below should begin with yy or YY, to avoid   infringing on user name space.  This should be done even for local   variables, as they might otherwise be expanded by user macros.   There are some unavoidable exceptions within include files to   define necessary library symbols; they are noted "INFRINGES ON   USER NAME SPACE" below.  *//* Identify Bison output.  */#define YYBISON 1/* Skeleton name.  */#define YYSKELETON_NAME "yacc.c"/* Pure parsers.  */#define YYPURE 0/* Using locations.  */#define YYLSP_NEEDED 0/* Tokens.  */#ifndef YYTOKENTYPE# define YYTOKENTYPE   /* Put the tokens into the symbol table, so that GDB and other debuggers      know about them.  */   enum yytokentype {     LTYPE_INTEGER = 258,     LTYPE_FLOAT = 259,     LTYPE_VAR = 260,     LTYPE_PTR = 261,     PTR_TK = 262,     INT_TK = 263,     FLOAT_TK = 264,     DECLARE = 265,     EXTERNAL = 266,     WHILE = 267,     DO = 268,     NOT = 269,     PLUS_EQ = 270,     SUB_EQ = 271,     DIV_EQ = 272,     MUL_EQ = 273,     SUP_EQ = 274,     LOW_EQ = 275,     NOT_EQ = 276,     STRUCT = 277,     FOR = 278,     IN = 279   };#endif#define LTYPE_INTEGER 258#define LTYPE_FLOAT 259#define LTYPE_VAR 260#define LTYPE_PTR 261#define PTR_TK 262#define INT_TK 263#define FLOAT_TK 264#define DECLARE 265#define EXTERNAL 266#define WHILE 267#define DO 268#define NOT 269#define PLUS_EQ 270#define SUB_EQ 271#define DIV_EQ 272#define MUL_EQ 273#define SUP_EQ 274#define LOW_EQ 275#define NOT_EQ 276#define STRUCT 277#define FOR 278#define IN 279/* Copy the first part of user declarations.  */#line 6 "goomsl_yacc.y"    #include <stdio.h>    #include <stdlib.h>    #include <string.h>    #include "goomsl.h"    #include "goomsl_private.h"#define STRUCT_ALIGNMENT 16/* #define VERBOSE  */    int yylex(void);    void yyerror(char *);    extern GoomSL *currentGoomSL;    static NodeType *nodeNew(const char *str, int type, int line_number);    static NodeType *nodeClone(NodeType *node);    static void nodeFreeInternals(NodeType *node);    static void nodeFree(NodeType *node);    static void commit_node(NodeType *node, int releaseIfTemp);    static void precommit_node(NodeType *node);    static NodeType *new_constInt(const char *str, int line_number);    static NodeType *new_constFloat(const char *str, int line_number);    static NodeType *new_constPtr(const char *str, int line_number);    static NodeType *new_var(const char *str, int line_number);    static NodeType *new_nop(const char *str);    static NodeType *new_op(const char *str, int type, int nbOp);    static int  allocateLabel();    static int  allocateTemp();    static void releaseTemp(int n);    static void releaseAllTemps();    static int is_tmp_expr(NodeType *node) {        if (node->str) {            return (!strncmp(node->str,"_i_tmp_",7))              || (!strncmp(node->str,"_f_tmp_",7))              || (!strncmp(node->str,"_p_tmp",7));        }        return 0;    }    /* pre: is_tmp_expr(node); */    static int get_tmp_id(NodeType *node)  { return atoi((node->str)+5); }    static int is_commutative_expr(int itype)    { /* {{{ */        return (itype == INSTR_ADD)            || (itype == INSTR_MUL)            || (itype == INSTR_ISEQUAL);    } /* }}} */    static void GSL_PUT_LABEL(char *name, int line_number)    { /* {{{ */#ifdef VERBOSE      printf("label %s\n", name);#endif      currentGoomSL->instr = gsl_instr_init(currentGoomSL, "label", INSTR_LABEL, 1, line_number);      gsl_instr_add_param(currentGoomSL->instr, name, TYPE_LABEL);    } /* }}} */    static void GSL_PUT_JUMP(char *name, int line_number)    { /* {{{ */#ifdef VERBOSE      printf("jump %s\n", name);#endif      currentGoomSL->instr = gsl_instr_init(currentGoomSL, "jump", INSTR_JUMP, 1, line_number);      gsl_instr_add_param(currentGoomSL->instr, name, TYPE_LABEL);    } /* }}} */    static void GSL_PUT_JXXX(char *name, char *iname, int instr_id, int line_number)    { /* {{{ */#ifdef VERBOSE      printf("%s %s\n", iname, name);#endif      currentGoomSL->instr = gsl_instr_init(currentGoomSL, iname, instr_id, 1, line_number);      gsl_instr_add_param(currentGoomSL->instr, name, TYPE_LABEL);    } /* }}} */    static void GSL_PUT_JZERO(char *name,int line_number)    { /* {{{ */      GSL_PUT_JXXX(name,"jzero.i",INSTR_JZERO,line_number);    } /* }}} */    static void GSL_PUT_JNZERO(char *name, int line_number)    { /* {{{ */      GSL_PUT_JXXX(name,"jnzero.i",INSTR_JNZERO,line_number);    } /* }}} */    /* Structures Management */#define ALIGN_ADDR(_addr,_align) {\   if (_align>1) {\       int _dec = (_addr%_align);\       if (_dec != 0) _addr += _align - _dec;\   }}    /* */    void gsl_prepare_struct(GSL_Struct *s, int s_align, int i_align, int f_align)    {      int i;      int consumed = 0;      int iblk=0, fblk=0;      s->iBlock[0].size = 0;      s->iBlock[0].data = 0;      s->fBlock[0].size = 0;      s->fBlock[0].data = 0;      /* Prepare sub-struct and calculate space needed for their storage */      for (i = 0; i < s->nbFields; ++i)      {        if (s->fields[i]->type < FIRST_RESERVED)        {          int j=0;          GSL_Struct *substruct = currentGoomSL->gsl_struct[s->fields[i]->type];          consumed += sizeof(int); /* stocke le prefix */          ALIGN_ADDR(consumed, s_align);          s->fields[i]->offsetInStruct = consumed;          gsl_prepare_struct(substruct, s_align, i_align, f_align);          for(j=0;substruct->iBlock[j].size>0;++j) {            s->iBlock[iblk].data = consumed + substruct->iBlock[j].data;            s->iBlock[iblk].size = substruct->iBlock[j].size;            iblk++;          }          for(j=0;substruct->fBlock[j].size>0;++j) {            s->fBlock[fblk].data = consumed + substruct->fBlock[j].data;            s->fBlock[fblk].size = substruct->fBlock[j].size;            fblk++;          }          consumed += substruct->size;        }      }      /* Then prepare integers */      ALIGN_ADDR(consumed, i_align);      for (i = 0; i < s->nbFields; ++i)      {        if (s->fields[i]->type == INSTR_INT)        {          if (s->iBlock[iblk].size == 0) {            s->iBlock[iblk].size = 1;            s->iBlock[iblk].data = consumed;          } else {            s->iBlock[iblk].size += 1;          }          s->fields[i]->offsetInStruct = consumed;          consumed += sizeof(int);        }      }      iblk++;      s->iBlock[iblk].size = 0;      s->iBlock[iblk].data = 0;      /* Then prepare floats */      ALIGN_ADDR(consumed, f_align);      for (i = 0; i < s->nbFields; ++i)      {        if (s->fields[i]->type == INSTR_FLOAT)        {          if (s->fBlock[fblk].size == 0) {            s->fBlock[fblk].size = 1;            s->fBlock[fblk].data = consumed;          } else {            s->fBlock[fblk].size += 1;          }          s->fields[i]->offsetInStruct = consumed;          consumed += sizeof(int);        }      }      fblk++;      s->fBlock[fblk].size = 0;      s->fBlock[fblk].data = 0;            /* Finally prepare pointers */      ALIGN_ADDR(consumed, i_align);      for (i = 0; i < s->nbFields; ++i)      {        if (s->fields[i]->type == INSTR_PTR)        {          s->fields[i]->offsetInStruct = consumed;          consumed += sizeof(int);        }      }      s->size = consumed;    }    /* Returns the ID of a struct from its name */    int gsl_get_struct_id(const char *name) /* {{{ */    {      HashValue *ret = goom_hash_get(currentGoomSL->structIDS, name);      if (ret != NULL) return ret->i;      return -1;    } /* }}} */    /* Adds the definition of a struct */    void gsl_add_struct(const char *name, GSL_Struct *gsl_struct) /* {{{ */    {      /* Prepare the struct: ie calculate internal storage format */      gsl_prepare_struct(gsl_struct, STRUCT_ALIGNMENT, STRUCT_ALIGNMENT, STRUCT_ALIGNMENT);            /* If the struct does not already exists */      if (gsl_get_struct_id(name) < 0)      {        /* adds it */        int id = currentGoomSL->nbStructID++;        goom_hash_put_int(currentGoomSL->structIDS, name, id);        if (currentGoomSL->gsl_struct_size <= id) {          currentGoomSL->gsl_struct_size *= 2;          currentGoomSL->gsl_struct = (GSL_Struct**)realloc(currentGoomSL->gsl_struct,                                                            sizeof(GSL_Struct*) * currentGoomSL->gsl_struct_size);        }        currentGoomSL->gsl_struct[id] = gsl_struct;      }    } /* }}} */        /* Creates a field for a struct */    GSL_StructField *gsl_new_struct_field(const char *name, int type)    {      GSL_StructField *field = (GSL_StructField*)malloc(sizeof(GSL_StructField));      strcpy(field->name, name);      field->type = type;      return field;    }        /* Create as field for a struct which will be a struct itself */    GSL_StructField *gsl_new_struct_field_struct(const char *name, const char *type)    {      GSL_StructField *field = gsl_new_struct_field(name, gsl_get_struct_id(type));      if (field->type < 0) {        fprintf(stderr, "ERROR: Line %d, Unknown structure: '%s'\n",                currentGoomSL->num_lines, type);        exit(1);      }      return field;    }    /* Creates a Struct */    GSL_Struct *gsl_new_struct(GSL_StructField *field)    {      GSL_Struct *s = (GSL_Struct*)malloc(sizeof(GSL_Struct));      s->nbFields = 1;      s->fields[0] = field;      return s;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?