From 009505452b713ed2e3a8e99c5545a6e721c65495 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Wed, 6 Jun 2001 20:29:01 +0000 Subject: Initial revision git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/Makefile | 7 + lib/Analysis/ModuleAnalyzer.cpp | 150 ++ lib/AsmParser/Lexer.cpp | 2058 +++++++++++++++++++++++++++ lib/AsmParser/Lexer.l | 184 +++ lib/AsmParser/Makefile | 7 + lib/AsmParser/Parser.cpp | 84 ++ lib/AsmParser/ParserInternals.h | 159 +++ lib/AsmParser/llvmAsmParser.cpp | 2202 +++++++++++++++++++++++++++++ lib/AsmParser/llvmAsmParser.h | 87 ++ lib/AsmParser/llvmAsmParser.y | 954 +++++++++++++ lib/Bytecode/Makefile | 5 + lib/Bytecode/Reader/ConstantReader.cpp | 218 +++ lib/Bytecode/Reader/InstructionReader.cpp | 213 +++ lib/Bytecode/Reader/Makefile | 7 + lib/Bytecode/Reader/Reader.cpp | 478 +++++++ lib/Bytecode/Reader/ReaderInternals.h | 146 ++ lib/Bytecode/Writer/ConstantWriter.cpp | 154 ++ lib/Bytecode/Writer/InstructionWriter.cpp | 184 +++ lib/Bytecode/Writer/Makefile | 7 + lib/Bytecode/Writer/SlotCalculator.cpp | 195 +++ lib/Bytecode/Writer/SlotCalculator.h | 96 ++ lib/Bytecode/Writer/Writer.cpp | 182 +++ lib/Bytecode/Writer/WriterInternals.h | 74 + lib/Makefile | 5 + lib/Transforms/IPO/InlineSimple.cpp | 283 ++++ lib/Transforms/Scalar/ConstantProp.cpp | 239 ++++ lib/Transforms/Scalar/DCE.cpp | 193 +++ lib/Transforms/Scalar/SymbolStripping.cpp | 55 + lib/VMCore/AsmWriter.cpp | 328 +++++ lib/VMCore/BasicBlock.cpp | 113 ++ lib/VMCore/ConstantFold.cpp | 197 +++ lib/VMCore/ConstantFold.h | 145 ++ lib/VMCore/ConstantFolding.h | 145 ++ lib/VMCore/ConstantPool.cpp | 434 ++++++ lib/VMCore/Function.cpp | 75 + lib/VMCore/InstrTypes.cpp | 66 + lib/VMCore/Instruction.cpp | 61 + lib/VMCore/Makefile | 7 + lib/VMCore/Module.cpp | 42 + lib/VMCore/SlotCalculator.cpp | 195 +++ lib/VMCore/SymbolTable.cpp | 106 ++ lib/VMCore/Type.cpp | 308 ++++ lib/VMCore/Value.cpp | 143 ++ lib/VMCore/ValueHolderImpl.h | 85 ++ lib/VMCore/Verifier.cpp | 94 ++ lib/VMCore/iBranch.cpp | 69 + lib/VMCore/iCall.cpp | 48 + lib/VMCore/iOperators.cpp | 37 + lib/VMCore/iReturn.cpp | 25 + lib/VMCore/iSwitch.cpp | 81 ++ 50 files changed, 11430 insertions(+) create mode 100644 lib/Analysis/Makefile create mode 100644 lib/Analysis/ModuleAnalyzer.cpp create mode 100644 lib/AsmParser/Lexer.cpp create mode 100644 lib/AsmParser/Lexer.l create mode 100644 lib/AsmParser/Makefile create mode 100644 lib/AsmParser/Parser.cpp create mode 100644 lib/AsmParser/ParserInternals.h create mode 100644 lib/AsmParser/llvmAsmParser.cpp create mode 100644 lib/AsmParser/llvmAsmParser.h create mode 100644 lib/AsmParser/llvmAsmParser.y create mode 100644 lib/Bytecode/Makefile create mode 100644 lib/Bytecode/Reader/ConstantReader.cpp create mode 100644 lib/Bytecode/Reader/InstructionReader.cpp create mode 100644 lib/Bytecode/Reader/Makefile create mode 100644 lib/Bytecode/Reader/Reader.cpp create mode 100644 lib/Bytecode/Reader/ReaderInternals.h create mode 100644 lib/Bytecode/Writer/ConstantWriter.cpp create mode 100644 lib/Bytecode/Writer/InstructionWriter.cpp create mode 100644 lib/Bytecode/Writer/Makefile create mode 100644 lib/Bytecode/Writer/SlotCalculator.cpp create mode 100644 lib/Bytecode/Writer/SlotCalculator.h create mode 100644 lib/Bytecode/Writer/Writer.cpp create mode 100644 lib/Bytecode/Writer/WriterInternals.h create mode 100644 lib/Makefile create mode 100644 lib/Transforms/IPO/InlineSimple.cpp create mode 100644 lib/Transforms/Scalar/ConstantProp.cpp create mode 100644 lib/Transforms/Scalar/DCE.cpp create mode 100644 lib/Transforms/Scalar/SymbolStripping.cpp create mode 100644 lib/VMCore/AsmWriter.cpp create mode 100644 lib/VMCore/BasicBlock.cpp create mode 100644 lib/VMCore/ConstantFold.cpp create mode 100644 lib/VMCore/ConstantFold.h create mode 100644 lib/VMCore/ConstantFolding.h create mode 100644 lib/VMCore/ConstantPool.cpp create mode 100644 lib/VMCore/Function.cpp create mode 100644 lib/VMCore/InstrTypes.cpp create mode 100644 lib/VMCore/Instruction.cpp create mode 100644 lib/VMCore/Makefile create mode 100644 lib/VMCore/Module.cpp create mode 100644 lib/VMCore/SlotCalculator.cpp create mode 100644 lib/VMCore/SymbolTable.cpp create mode 100644 lib/VMCore/Type.cpp create mode 100644 lib/VMCore/Value.cpp create mode 100644 lib/VMCore/ValueHolderImpl.h create mode 100644 lib/VMCore/Verifier.cpp create mode 100644 lib/VMCore/iBranch.cpp create mode 100644 lib/VMCore/iCall.cpp create mode 100644 lib/VMCore/iOperators.cpp create mode 100644 lib/VMCore/iReturn.cpp create mode 100644 lib/VMCore/iSwitch.cpp (limited to 'lib') diff --git a/lib/Analysis/Makefile b/lib/Analysis/Makefile new file mode 100644 index 0000000000..4b672cd6ff --- /dev/null +++ b/lib/Analysis/Makefile @@ -0,0 +1,7 @@ + +LEVEL = ../.. + +LIBRARYNAME = analysis + +include $(LEVEL)/Makefile.common + diff --git a/lib/Analysis/ModuleAnalyzer.cpp b/lib/Analysis/ModuleAnalyzer.cpp new file mode 100644 index 0000000000..1c3464e48c --- /dev/null +++ b/lib/Analysis/ModuleAnalyzer.cpp @@ -0,0 +1,150 @@ +//===-- llvm/Analysis/ModuleAnalyzer.cpp - Module analysis driver ----------==// +// +// This class provides a nice interface to traverse a module in a predictable +// way. This is used by the AssemblyWriter, BytecodeWriter, and SlotCalculator +// to do analysis of a module. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Analysis/ModuleAnalyzer.h" +#include "llvm/ConstantPool.h" +#include "llvm/Method.h" +#include "llvm/Module.h" +#include "llvm/BasicBlock.h" +#include "llvm/DerivedTypes.h" +#include "llvm/ConstPoolVals.h" +#include + +// processModule - Driver function to call all of my subclasses virtual methods. +// +bool ModuleAnalyzer::processModule(const Module *M) { + // Loop over the constant pool, process all of the constants... + if (processConstPool(M->getConstantPool(), false)) + return true; + + return processMethods(M); +} + +inline bool ModuleAnalyzer::handleType(set &TypeSet, + const Type *T) { + if (!T->isDerivedType()) return false; // Boring boring types... + if (TypeSet.count(T) != 0) return false; // Already found this type... + TypeSet.insert(T); // Add it to the set + + // Recursively process interesting types... + switch (T->getPrimitiveID()) { + case Type::MethodTyID: { + const MethodType *MT = (const MethodType *)T; + if (handleType(TypeSet, MT->getReturnType())) return true; + const MethodType::ParamTypes &Params = MT->getParamTypes(); + + for (MethodType::ParamTypes::const_iterator I = Params.begin(); + I != Params.end(); ++I) + if (handleType(TypeSet, *I)) return true; + break; + } + + case Type::ArrayTyID: + if (handleType(TypeSet, ((const ArrayType *)T)->getElementType())) + return true; + break; + + case Type::StructTyID: { + const StructType *ST = (const StructType*)T; + const StructType::ElementTypes &Elements = ST->getElementTypes(); + for (StructType::ElementTypes::const_iterator I = Elements.begin(); + I != Elements.end(); ++I) + if (handleType(TypeSet, *I)) return true; + break; + } + + case Type::PointerTyID: + if (handleType(TypeSet, ((const PointerType *)T)->getValueType())) + return true; + break; + + default: + cerr << "ModuleAnalyzer::handleType, type unknown: '" + << T->getName() << "'\n"; + break; + } + + return processType(T); +} + + +bool ModuleAnalyzer::processConstPool(const ConstantPool &CP, bool isMethod) { + // TypeSet - Keep track of which types have already been processType'ed. We + // don't want to reprocess the same type more than once. + // + set TypeSet; + + for (ConstantPool::plane_const_iterator PI = CP.begin(); + PI != CP.end(); ++PI) { + const ConstantPool::PlaneType &Plane = **PI; + if (Plane.empty()) continue; // Skip empty type planes... + + if (processConstPoolPlane(CP, Plane, isMethod)) return true; + + for (ConstantPool::PlaneType::const_iterator CI = Plane.begin(); + CI != Plane.end(); CI++) { + if ((*CI)->getType() == Type::TypeTy) + if (handleType(TypeSet, ((const ConstPoolType*)(*CI))->getValue())) + return true; + if (handleType(TypeSet, (*CI)->getType())) return true; + + if (processConstant(*CI)) return true; + } + } + + if (!isMethod) { + assert(CP.getParent()->getValueType() == Value::ModuleVal); + const Module *M = (const Module*)CP.getParent(); + // Process the method types after the constant pool... + for (Module::MethodListType::const_iterator I = M->getMethodList().begin(); + I != M->getMethodList().end(); I++) { + if (handleType(TypeSet, (*I)->getType())) return true; + if (visitMethod(*I)) return true; + } + } + return false; +} + +bool ModuleAnalyzer::processMethods(const Module *M) { + for (Module::MethodListType::const_iterator I = M->getMethodList().begin(); + I != M->getMethodList().end(); I++) + if (processMethod(*I)) return true; + + return false; +} + +bool ModuleAnalyzer::processMethod(const Method *M) { + // Loop over the arguments, processing them... + const Method::ArgumentListType &ArgList = M->getArgumentList(); + for (Method::ArgumentListType::const_iterator AI = ArgList.begin(); + AI != ArgList.end(); AI++) + if (processMethodArgument(*AI)) return true; + + // Loop over the constant pool, adding the constants to the table... + processConstPool(M->getConstantPool(), true); + + // Loop over all the basic blocks, in order... + Method::BasicBlocksType::const_iterator BBI = M->getBasicBlocks().begin(); + for (; BBI != M->getBasicBlocks().end(); BBI++) + if (processBasicBlock(*BBI)) return true; + return false; +} + +bool ModuleAnalyzer::processBasicBlock(const BasicBlock *BB) { + // Process all of the instructions in the basic block + BasicBlock::InstListType::const_iterator Inst = BB->getInstList().begin(); + for (; Inst != BB->getInstList().end(); Inst++) { + if (preProcessInstruction(*Inst) || processInstruction(*Inst)) return true; + } + return false; +} + +bool ModuleAnalyzer::preProcessInstruction(const Instruction *I) { + + return false; +} diff --git a/lib/AsmParser/Lexer.cpp b/lib/AsmParser/Lexer.cpp new file mode 100644 index 0000000000..9edd3bf5b1 --- /dev/null +++ b/lib/AsmParser/Lexer.cpp @@ -0,0 +1,2058 @@ +#define yy_create_buffer llvmAsm_create_buffer +#define yy_delete_buffer llvmAsm_delete_buffer +#define yy_scan_buffer llvmAsm_scan_buffer +#define yy_scan_string llvmAsm_scan_string +#define yy_scan_bytes llvmAsm_scan_bytes +#define yy_flex_debug llvmAsm_flex_debug +#define yy_init_buffer llvmAsm_init_buffer +#define yy_flush_buffer llvmAsm_flush_buffer +#define yy_load_buffer_state llvmAsm_load_buffer_state +#define yy_switch_to_buffer llvmAsm_switch_to_buffer +#define yyin llvmAsmin +#define yyleng llvmAsmleng +#define yylex llvmAsmlex +#define yyout llvmAsmout +#define yyrestart llvmAsmrestart +#define yytext llvmAsmtext +#define yylineno llvmAsmlineno + +#line 20 "Lexer.cpp" +/* A lexical scanner generated by flex */ + +/* Scanner skeleton version: + * $Header$ + */ + +#define FLEX_SCANNER +#define YY_FLEX_MAJOR_VERSION 2 +#define YY_FLEX_MINOR_VERSION 5 + +#include + + +/* cfront 1.2 defines "c_plusplus" instead of "__cplusplus" */ +#ifdef c_plusplus +#ifndef __cplusplus +#define __cplusplus +#endif +#endif + + +#ifdef __cplusplus + +#include +#include + +/* Use prototypes in function declarations. */ +#define YY_USE_PROTOS + +/* The "const" storage-class-modifier is valid. */ +#define YY_USE_CONST + +#else /* ! __cplusplus */ + +#if __STDC__ + +#define YY_USE_PROTOS +#define YY_USE_CONST + +#endif /* __STDC__ */ +#endif /* ! __cplusplus */ + +#ifdef __TURBOC__ + #pragma warn -rch + #pragma warn -use +#include +#include +#define YY_USE_CONST +#define YY_USE_PROTOS +#endif + +#ifdef YY_USE_CONST +#define yyconst const +#else +#define yyconst +#endif + + +#ifdef YY_USE_PROTOS +#define YY_PROTO(proto) proto +#else +#define YY_PROTO(proto) () +#endif + +/* Returned upon end-of-file. */ +#define YY_NULL 0 + +/* Promotes a possibly negative, possibly signed char to an unsigned + * integer for use as an array index. If the signed char is negative, + * we want to instead treat it as an 8-bit unsigned char, hence the + * double cast. + */ +#define YY_SC_TO_UI(c) ((unsigned int) (unsigned char) c) + +/* Enter a start condition. This macro really ought to take a parameter, + * but we do it the disgusting crufty way forced on us by the ()-less + * definition of BEGIN. + */ +#define BEGIN yy_start = 1 + 2 * + +/* Translate the current start state into a value that can be later handed + * to BEGIN to return to the state. The YYSTATE alias is for lex + * compatibility. + */ +#define YY_START ((yy_start - 1) / 2) +#define YYSTATE YY_START + +/* Action number for EOF rule of a given start state. */ +#define YY_STATE_EOF(state) (YY_END_OF_BUFFER + state + 1) + +/* Special action meaning "start processing a new file". */ +#define YY_NEW_FILE yyrestart( yyin ) + +#define YY_END_OF_BUFFER_CHAR 0 + +/* Size of default input buffer. */ +#define YY_BUF_SIZE 16384 + +typedef struct yy_buffer_state *YY_BUFFER_STATE; + +extern int yyleng; +extern FILE *yyin, *yyout; + +#define EOB_ACT_CONTINUE_SCAN 0 +#define EOB_ACT_END_OF_FILE 1 +#define EOB_ACT_LAST_MATCH 2 + +/* The funky do-while in the following #define is used to turn the definition + * int a single C statement (which needs a semi-colon terminator). This + * avoids problems with code like: + * + * if ( condition_holds ) + * yyless( 5 ); + * else + * do_something_else(); + * + * Prior to using the do-while the compiler would get upset at the + * "else" because it interpreted the "if" statement as being all + * done when it reached the ';' after the yyless() call. + */ + +/* Return all but the first 'n' matched characters back to the input stream. */ + +#define yyless(n) \ + do \ + { \ + /* Undo effects of setting up yytext. */ \ + *yy_cp = yy_hold_char; \ + YY_RESTORE_YY_MORE_OFFSET \ + yy_c_buf_p = yy_cp = yy_bp + n - YY_MORE_ADJ; \ + YY_DO_BEFORE_ACTION; /* set up yytext again */ \ + } \ + while ( 0 ) + +#define unput(c) yyunput( c, yytext_ptr ) + +/* The following is because we cannot portably get our hands on size_t + * (without autoconf's help, which isn't available because we want + * flex-generated scanners to compile on their own). + */ +typedef unsigned int yy_size_t; + + +struct yy_buffer_state + { + FILE *yy_input_file; + + char *yy_ch_buf; /* input buffer */ + char *yy_buf_pos; /* current position in input buffer */ + + /* Size of input buffer in bytes, not including room for EOB + * characters. + */ + yy_size_t yy_buf_size; + + /* Number of characters read into yy_ch_buf, not including EOB + * characters. + */ + int yy_n_chars; + + /* Whether we "own" the buffer - i.e., we know we created it, + * and can realloc() it to grow it, and should free() it to + * delete it. + */ + int yy_is_our_buffer; + + /* Whether this is an "interactive" input source; if so, and + * if we're using stdio for input, then we want to use getc() + * instead of fread(), to make sure we stop fetching input after + * each newline. + */ + int yy_is_interactive; + + /* Whether we're considered to be at the beginning of a line. + * If so, '^' rules will be active on the next match, otherwise + * not. + */ + int yy_at_bol; + + /* Whether to try to fill the input buffer when we reach the + * end of it. + */ + int yy_fill_buffer; + + int yy_buffer_status; +#define YY_BUFFER_NEW 0 +#define YY_BUFFER_NORMAL 1 + /* When an EOF's been seen but there's still some text to process + * then we mark the buffer as YY_EOF_PENDING, to indicate that we + * shouldn't try reading from the input source any more. We might + * still have a bunch of tokens to match, though, because of + * possible backing-up. + * + * When we actually see the EOF, we change the status to "new" + * (via yyrestart()), so that the user can continue scanning by + * just pointing yyin at a new input file. + */ +#define YY_BUFFER_EOF_PENDING 2 + }; + +static YY_BUFFER_STATE yy_current_buffer = 0; + +/* We provide macros for accessing buffer states in case in the + * future we want to put the buffer states in a more general + * "scanner state". + */ +#define YY_CURRENT_BUFFER yy_current_buffer + + +/* yy_hold_char holds the character lost when yytext is formed. */ +static char yy_hold_char; + +static int yy_n_chars; /* number of characters read into yy_ch_buf */ + + +int yyleng; + +/* Points to current character in buffer. */ +static char *yy_c_buf_p = (char *) 0; +static int yy_init = 1; /* whether we need to initialize */ +static int yy_start = 0; /* start state number */ + +/* Flag which is used to allow yywrap()'s to do buffer switches + * instead of setting up a fresh yyin. A bit of a hack ... + */ +static int yy_did_buffer_switch_on_eof; + +void yyrestart YY_PROTO(( FILE *input_file )); + +void yy_switch_to_buffer YY_PROTO(( YY_BUFFER_STATE new_buffer )); +void yy_load_buffer_state YY_PROTO(( void )); +YY_BUFFER_STATE yy_create_buffer YY_PROTO(( FILE *file, int size )); +void yy_delete_buffer YY_PROTO(( YY_BUFFER_STATE b )); +void yy_init_buffer YY_PROTO(( YY_BUFFER_STATE b, FILE *file )); +void yy_flush_buffer YY_PROTO(( YY_BUFFER_STATE b )); +#define YY_FLUSH_BUFFER yy_flush_buffer( yy_current_buffer ) + +YY_BUFFER_STATE yy_scan_buffer YY_PROTO(( char *base, yy_size_t size )); +YY_BUFFER_STATE yy_scan_string YY_PROTO(( yyconst char *yy_str )); +YY_BUFFER_STATE yy_scan_bytes YY_PROTO(( yyconst char *bytes, int len )); + +static void *yy_flex_alloc YY_PROTO(( yy_size_t )); +static inline void *yy_flex_realloc YY_PROTO(( void *, yy_size_t )); +static void yy_flex_free YY_PROTO(( void * )); + +#define yy_new_buffer yy_create_buffer + +#define yy_set_interactive(is_interactive) \ + { \ + if ( ! yy_current_buffer ) \ + yy_current_buffer = yy_create_buffer( yyin, YY_BUF_SIZE ); \ + yy_current_buffer->yy_is_interactive = is_interactive; \ + } + +#define yy_set_bol(at_bol) \ + { \ + if ( ! yy_current_buffer ) \ + yy_current_buffer = yy_create_buffer( yyin, YY_BUF_SIZE ); \ + yy_current_buffer->yy_at_bol = at_bol; \ + } + +#define YY_AT_BOL() (yy_current_buffer->yy_at_bol) + + +#define YY_USES_REJECT + +#define yywrap() 1 +#define YY_SKIP_YYWRAP +typedef unsigned char YY_CHAR; +FILE *yyin = (FILE *) 0, *yyout = (FILE *) 0; +typedef int yy_state_type; +extern int yylineno; +int yylineno = 1; +extern char *yytext; +#define yytext_ptr yytext + +static yy_state_type yy_get_previous_state YY_PROTO(( void )); +static yy_state_type yy_try_NUL_trans YY_PROTO(( yy_state_type current_state )); +static int yy_get_next_buffer YY_PROTO(( void )); +static void yy_fatal_error YY_PROTO(( yyconst char msg[] )); + +/* Done after the current pattern has been matched and before the + * corresponding action - sets up yytext. + */ +#define YY_DO_BEFORE_ACTION \ + yytext_ptr = yy_bp; \ + yyleng = (int) (yy_cp - yy_bp); \ + yy_hold_char = *yy_cp; \ + *yy_cp = '\0'; \ + yy_c_buf_p = yy_cp; + +#define YY_NUM_RULES 58 +#define YY_END_OF_BUFFER 59 +static yyconst short int yy_acclist[113] = + { 0, + 59, 57, 58, 56, 57, 58, 56, 58, 57, 58, + 57, 58, 57, 58, 8, 57, 58, 52, 57, 58, + 1, 57, 58, 57, 58, 57, 58, 57, 58, 57, + 58, 57, 58, 57, 58, 57, 58, 57, 58, 57, + 58, 57, 58, 57, 58, 57, 58, 57, 58, 57, + 58, 57, 58, 57, 58, 57, 58, 50, 49, 54, + 53, 52, 1, 9, 40, 51, 49, 55, 28, 31, + 3, 16, 30, 24, 25, 26, 32, 39, 29, 11, + 27, 44, 45, 18, 4, 22, 17, 10, 2, 5, + 20, 23, 12, 34, 38, 36, 37, 35, 33, 14, + + 46, 13, 19, 43, 21, 42, 41, 15, 6, 47, + 48, 7 + } ; + +static yyconst short int yy_accept[199] = + { 0, + 1, 1, 1, 2, 4, 7, 9, 11, 13, 15, + 18, 21, 24, 26, 28, 30, 32, 34, 36, 38, + 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, + 58, 58, 59, 60, 60, 61, 62, 63, 64, 64, + 64, 65, 65, 65, 66, 66, 66, 66, 66, 66, + 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, + 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, + 66, 66, 66, 66, 66, 66, 66, 66, 67, 68, + 69, 70, 70, 70, 70, 70, 70, 71, 71, 72, + 72, 72, 72, 72, 72, 73, 73, 73, 73, 73, + + 74, 75, 76, 77, 77, 78, 79, 79, 79, 79, + 79, 80, 80, 80, 80, 80, 80, 80, 80, 80, + 80, 80, 81, 82, 82, 82, 82, 82, 83, 83, + 83, 83, 84, 85, 85, 85, 85, 85, 85, 85, + 85, 85, 85, 85, 86, 87, 87, 88, 88, 88, + 89, 89, 90, 90, 90, 91, 92, 92, 92, 93, + 93, 93, 94, 95, 96, 97, 98, 99, 100, 101, + 102, 102, 103, 104, 104, 105, 105, 106, 106, 106, + 107, 107, 108, 109, 110, 110, 110, 110, 111, 111, + 112, 112, 112, 112, 112, 112, 113, 113 + + } ; + +static yyconst int yy_ec[256] = + { 0, + 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 2, 1, 4, 1, 5, 6, 1, 1, 1, + 1, 1, 1, 1, 7, 5, 1, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 9, 10, 1, + 1, 1, 1, 1, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 1, 1, 1, 1, 5, 1, 11, 12, 13, 14, + + 15, 16, 17, 18, 19, 5, 5, 20, 21, 22, + 23, 24, 25, 26, 27, 28, 29, 30, 31, 5, + 32, 5, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1 + } ; + +static yyconst int yy_meta[33] = + { 0, + 1, 1, 2, 3, 4, 1, 5, 4, 6, 1, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4 + } ; + +static yyconst short int yy_base[203] = + { 0, + 0, 0, 389, 390, 390, 390, 0, 379, 26, 379, + 27, 0, 28, 40, 29, 35, 34, 42, 30, 38, + 56, 60, 55, 63, 65, 84, 68, 107, 52, 382, + 376, 390, 0, 376, 375, 374, 74, 0, 32, 64, + 372, 78, 67, 371, 77, 92, 76, 79, 89, 98, + 100, 95, 102, 105, 108, 112, 111, 119, 122, 123, + 116, 126, 128, 129, 132, 134, 137, 138, 139, 140, + 143, 144, 145, 149, 146, 156, 154, 390, 0, 371, + 369, 157, 166, 158, 161, 167, 368, 170, 367, 174, + 175, 179, 180, 182, 366, 183, 181, 188, 184, 365, + + 364, 363, 362, 190, 361, 360, 191, 211, 198, 199, + 359, 194, 200, 201, 202, 204, 205, 212, 203, 225, + 214, 358, 357, 228, 209, 231, 232, 356, 233, 234, + 235, 355, 354, 236, 238, 239, 241, 247, 249, 252, + 242, 254, 259, 353, 352, 256, 351, 264, 265, 350, + 267, 349, 270, 271, 348, 347, 273, 274, 346, 276, + 275, 345, 344, 343, 342, 341, 340, 335, 330, 325, + 283, 320, 319, 278, 317, 284, 316, 285, 288, 315, + 289, 313, 311, 310, 293, 291, 301, 308, 295, 244, + 303, 299, 302, 307, 309, 82, 390, 331, 334, 337, + + 342, 53 + } ; + +static yyconst short int yy_def[203] = + { 0, + 197, 1, 197, 197, 197, 197, 198, 199, 200, 197, + 199, 201, 199, 199, 199, 199, 199, 199, 199, 199, + 199, 199, 199, 199, 199, 199, 199, 199, 199, 198, + 199, 197, 202, 197, 197, 197, 199, 201, 199, 199, + 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, + 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, + 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, + 199, 199, 199, 199, 199, 199, 199, 197, 202, 197, + 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, + 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, + + 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, + 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, + 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, + 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, + 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, + 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, + 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, + 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, + 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, + 199, 199, 199, 199, 199, 199, 0, 197, 197, 197, + + 197, 197 + } ; + +static yyconst short int yy_nxt[423] = + { 0, + 4, 5, 6, 7, 8, 9, 10, 11, 4, 12, + 13, 14, 15, 16, 17, 18, 19, 8, 20, 21, + 22, 23, 8, 24, 8, 25, 26, 27, 28, 29, + 8, 8, 34, 35, 37, 32, 32, 32, 32, 45, + 32, 39, 32, 32, 53, 81, 32, 40, 32, 46, + 32, 41, 50, 47, 42, 49, 79, 48, 54, 55, + 32, 51, 43, 32, 32, 44, 56, 52, 32, 60, + 58, 32, 32, 32, 77, 32, 32, 61, 57, 64, + 62, 37, 32, 82, 32, 32, 32, 32, 59, 84, + 32, 63, 32, 71, 83, 65, 85, 32, 66, 72, + + 32, 67, 89, 32, 86, 87, 32, 88, 32, 92, + 32, 68, 69, 32, 70, 32, 32, 90, 73, 32, + 32, 97, 91, 96, 32, 74, 75, 32, 94, 93, + 32, 32, 98, 76, 32, 95, 32, 32, 99, 101, + 32, 100, 32, 102, 103, 32, 32, 32, 32, 105, + 111, 32, 32, 32, 32, 104, 106, 32, 112, 109, + 110, 108, 32, 107, 32, 32, 32, 114, 117, 32, + 116, 113, 119, 118, 32, 32, 115, 122, 32, 120, + 123, 125, 32, 32, 121, 127, 124, 32, 32, 32, + 32, 32, 32, 128, 132, 129, 32, 131, 32, 32, + + 126, 130, 32, 134, 133, 135, 32, 32, 32, 32, + 32, 32, 32, 32, 144, 145, 150, 32, 136, 32, + 32, 143, 32, 141, 142, 137, 148, 138, 154, 146, + 139, 147, 140, 32, 149, 152, 32, 151, 153, 32, + 32, 32, 32, 32, 32, 155, 32, 32, 158, 32, + 32, 157, 32, 162, 159, 32, 161, 32, 160, 156, + 32, 164, 32, 166, 32, 163, 168, 32, 170, 169, + 172, 171, 32, 32, 165, 32, 167, 175, 32, 32, + 173, 32, 32, 32, 32, 177, 32, 178, 180, 181, + 174, 32, 32, 32, 179, 176, 32, 32, 184, 32, + + 182, 32, 186, 32, 185, 183, 188, 32, 187, 32, + 32, 32, 189, 192, 190, 32, 32, 32, 32, 32, + 194, 32, 191, 32, 32, 32, 193, 32, 32, 195, + 196, 30, 30, 32, 30, 30, 30, 31, 32, 31, + 33, 33, 38, 32, 38, 38, 38, 38, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 80, 32, + 32, 36, 35, 80, 32, 78, 36, 32, 197, 3, + 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, + + 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, + 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, + 197, 197 + } ; + +static yyconst short int yy_chk[423] = + { 0, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 9, 9, 11, 11, 13, 15, 19, 15, + 39, 13, 17, 16, 19, 39, 20, 13, 14, 16, + 18, 14, 18, 16, 14, 17, 202, 16, 20, 20, + 29, 18, 14, 23, 21, 14, 21, 18, 22, 23, + 22, 24, 40, 25, 29, 43, 27, 23, 21, 25, + 24, 37, 37, 40, 47, 45, 42, 48, 22, 43, + 196, 24, 26, 27, 42, 26, 45, 49, 26, 27, + + 46, 26, 49, 52, 46, 47, 50, 48, 51, 52, + 53, 26, 26, 54, 26, 28, 55, 50, 28, 57, + 56, 57, 51, 56, 61, 28, 28, 58, 54, 53, + 59, 60, 57, 28, 62, 55, 63, 64, 58, 60, + 65, 59, 66, 61, 62, 67, 68, 69, 70, 64, + 69, 71, 72, 73, 75, 63, 64, 74, 70, 67, + 68, 66, 77, 65, 76, 82, 84, 72, 75, 85, + 74, 71, 77, 76, 83, 86, 73, 84, 88, 82, + 85, 88, 90, 91, 83, 91, 86, 92, 93, 97, + 94, 96, 99, 92, 97, 93, 98, 96, 104, 107, + + 90, 94, 112, 99, 98, 104, 109, 110, 113, 114, + 115, 119, 116, 117, 113, 114, 119, 125, 107, 108, + 118, 112, 121, 109, 110, 108, 117, 108, 125, 115, + 108, 116, 108, 120, 118, 121, 124, 120, 124, 126, + 127, 129, 130, 131, 134, 126, 135, 136, 130, 137, + 141, 129, 190, 136, 131, 138, 135, 139, 134, 127, + 140, 138, 142, 139, 146, 137, 140, 143, 142, 141, + 146, 143, 148, 149, 138, 151, 139, 151, 153, 154, + 148, 157, 158, 161, 160, 154, 174, 157, 160, 161, + 149, 171, 176, 178, 158, 153, 179, 181, 176, 186, + + 171, 185, 179, 189, 178, 174, 185, 192, 181, 187, + 193, 191, 186, 191, 187, 194, 188, 195, 184, 183, + 193, 182, 189, 180, 177, 175, 192, 173, 172, 194, + 195, 198, 198, 170, 198, 198, 198, 199, 169, 199, + 200, 200, 201, 168, 201, 201, 201, 201, 167, 166, + 165, 164, 163, 162, 159, 156, 155, 152, 150, 147, + 145, 144, 133, 132, 128, 123, 122, 111, 106, 105, + 103, 102, 101, 100, 95, 89, 87, 81, 80, 44, + 41, 36, 35, 34, 31, 30, 10, 8, 3, 197, + 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, + + 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, + 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, + 197, 197 + } ; + +static yy_state_type yy_state_buf[YY_BUF_SIZE + 2], *yy_state_ptr; +static char *yy_full_match; +static int yy_lp; +#define REJECT \ +{ \ +*yy_cp = yy_hold_char; /* undo effects of setting up yytext */ \ +yy_cp = yy_full_match; /* restore poss. backed-over text */ \ +++yy_lp; \ +goto find_rule; \ +} +#define yymore() yymore_used_but_not_detected +#define YY_MORE_ADJ 0 +#define YY_RESTORE_YY_MORE_OFFSET +char *yytext; +#line 1 "Lexer.l" +#define INITIAL 0 +/*===-- Lexer.l - Scanner for llvm assembly files ----------------*- C++ -*--=// +// +// This file implements the flex scanner for LLVM assembly languages files. +// +//===------------------------------------------------------------------------=*/ +#define YY_NEVER_INTERACTIVE 1 +#line 21 "Lexer.l" +#include "ParserInternals.h" +#include "llvm/BasicBlock.h" +#include "llvm/Method.h" +#include "llvm/Module.h" +#include +#include "llvmAsmParser.h" + +#define RET_TOK(type, Enum, sym) \ + llvmAsmlval.type = Instruction::Enum; return sym + + +// TODO: All of the static identifiers are figured out by the lexer, +// these should be hashed. + + +// atoull - Convert an ascii string of decimal digits into the unsigned long +// long representation... this does not have to do input error checking, +// because we know that the input will be matched by a suitable regex... +// +uint64_t atoull(const char *Buffer) { + uint64_t Result = 0; + for (; *Buffer; Buffer++) { + uint64_t OldRes = Result; + Result *= 10; + Result += *Buffer-'0'; + if (Result < OldRes) { // Uh, oh, overflow detected!!! + ThrowException("constant bigger than 64 bits detected!"); + } + } + return Result; +} + + +#define YY_NEVER_INTERACTIVE 1 +/* Comments start with a ; and go till end of line */ +/* Variable(Def) identifiers start with a % sign */ +/* Label identifiers end with a colon */ +/* Quoted names can contain any character except " and \ */ +/* [PN]Integer: match positive and negative literal integer values that + * are preceeded by a '%' character. These represent unnamed variable slots. + */ +/* E[PN]Integer: match positive and negative literal integer values */ +#line 618 "Lexer.cpp" + +/* Macros after this point can all be overridden by user definitions in + * section 1. + */ + +#ifndef YY_SKIP_YYWRAP +#ifdef __cplusplus +extern "C" int yywrap YY_PROTO(( void )); +#else +extern int yywrap YY_PROTO(( void )); +#endif +#endif + +#ifndef YY_NO_UNPUT +static inline void yyunput YY_PROTO(( int c, char *buf_ptr )); +#endif + +#ifndef yytext_ptr +static void yy_flex_strncpy YY_PROTO(( char *, yyconst char *, int )); +#endif + +#ifdef YY_NEED_STRLEN +static int yy_flex_strlen YY_PROTO(( yyconst char * )); +#endif + +#ifndef YY_NO_INPUT +#ifdef __cplusplus +static int yyinput YY_PROTO(( void )); +#else +static int input YY_PROTO(( void )); +#endif +#endif + +#if YY_STACK_USED +static int yy_start_stack_ptr = 0; +static int yy_start_stack_depth = 0; +static int *yy_start_stack = 0; +#ifndef YY_NO_PUSH_STATE +static void yy_push_state YY_PROTO(( int new_state )); +#endif +#ifndef YY_NO_POP_STATE +static void yy_pop_state YY_PROTO(( void )); +#endif +#ifndef YY_NO_TOP_STATE +static int yy_top_state YY_PROTO(( void )); +#endif + +#else +#define YY_NO_PUSH_STATE 1 +#define YY_NO_POP_STATE 1 +#define YY_NO_TOP_STATE 1 +#endif + +#ifdef YY_MALLOC_DECL +YY_MALLOC_DECL +#else +#if __STDC__ +#ifndef __cplusplus +#include +#endif +#else +/* Just try to get by without declaring the routines. This will fail + * miserably on non-ANSI systems for which sizeof(size_t) != sizeof(int) + * or sizeof(void*) != sizeof(int). + */ +#endif +#endif + +/* Amount of stuff to slurp up with each read. */ +#ifndef YY_READ_BUF_SIZE +#define YY_READ_BUF_SIZE 8192 +#endif + +/* Copy whatever the last rule matched to the standard output. */ + +#ifndef ECHO +/* This used to be an fputs(), but since the string might contain NUL's, + * we now use fwrite(). + */ +#define ECHO (void) fwrite( yytext, yyleng, 1, yyout ) +#endif + +/* Gets input and stuffs it into "buf". number of characters read, or YY_NULL, + * is returned in "result". + */ +#ifndef YY_INPUT +#define YY_INPUT(buf,result,max_size) \ + if ( yy_current_buffer->yy_is_interactive ) \ + { \ + int c = '*', n; \ + for ( n = 0; n < max_size && \ + (c = getc( yyin )) != EOF && c != '\n'; ++n ) \ + buf[n] = (char) c; \ + if ( c == '\n' ) \ + buf[n++] = (char) c; \ + if ( c == EOF && ferror( yyin ) ) \ + YY_FATAL_ERROR( "input in flex scanner failed" ); \ + result = n; \ + } \ + else if ( ((result = fread( buf, 1, max_size, yyin )) == 0) \ + && ferror( yyin ) ) \ + YY_FATAL_ERROR( "input in flex scanner failed" ); +#endif + +/* No semi-colon after return; correct usage is to write "yyterminate();" - + * we don't want an extra ';' after the "return" because that will cause + * some compilers to complain about unreachable statements. + */ +#ifndef yyterminate +#define yyterminate() return YY_NULL +#endif + +/* Number of entries by which start-condition stack grows. */ +#ifndef YY_START_STACK_INCR +#define YY_START_STACK_INCR 25 +#endif + +/* Report a fatal error. */ +#ifndef YY_FATAL_ERROR +#define YY_FATAL_ERROR(msg) yy_fatal_error( msg ) +#endif + +/* Default declaration of generated scanner - a define so the user can + * easily add parameters. + */ +#ifndef YY_DECL +#define YY_DECL int yylex YY_PROTO(( void )) +#endif + +/* Code executed at the beginning of each rule, after yytext and yyleng + * have been set up. + */ +#ifndef YY_USER_ACTION +#define YY_USER_ACTION +#endif + +/* Code executed at the end of each rule. */ +#ifndef YY_BREAK +#define YY_BREAK break; +#endif + +#define YY_RULE_SETUP \ + YY_USER_ACTION + +YY_DECL + { + register yy_state_type yy_current_state; + register char *yy_cp, *yy_bp; + register int yy_act; + +#line 83 "Lexer.l" + + +#line 772 "Lexer.cpp" + + if ( yy_init ) + { + yy_init = 0; + +#ifdef YY_USER_INIT + YY_USER_INIT; +#endif + + if ( ! yy_start ) + yy_start = 1; /* first start state */ + + if ( ! yyin ) + yyin = stdin; + + if ( ! yyout ) + yyout = stdout; + + if ( ! yy_current_buffer ) + yy_current_buffer = + yy_create_buffer( yyin, YY_BUF_SIZE ); + + yy_load_buffer_state(); + } + + while ( 1 ) /* loops until end-of-file is reached */ + { + yy_cp = yy_c_buf_p; + + /* Support of yytext. */ + *yy_cp = yy_hold_char; + + /* yy_bp points to the position in yy_ch_buf of the start of + * the current run. + */ + yy_bp = yy_cp; + + yy_current_state = yy_start; + yy_state_ptr = yy_state_buf; + *yy_state_ptr++ = yy_current_state; +yy_match: + do + { + register YY_CHAR yy_c = yy_ec[YY_SC_TO_UI(*yy_cp)]; + while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) + { + yy_current_state = (int) yy_def[yy_current_state]; + if ( yy_current_state >= 198 ) + yy_c = yy_meta[(unsigned int) yy_c]; + } + yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; + *yy_state_ptr++ = yy_current_state; + ++yy_cp; + } + while ( yy_current_state != 197 ); + +yy_find_action: + yy_current_state = *--yy_state_ptr; + yy_lp = yy_accept[yy_current_state]; + for ( ; ; ) /* until we find what rule we matched */ + { + if ( yy_lp && yy_lp < yy_accept[yy_current_state + 1] ) + { + yy_act = yy_acclist[yy_lp]; + { + yy_full_match = yy_cp; + break; + } + } + --yy_cp; + yy_current_state = *--yy_state_ptr; + yy_lp = yy_accept[yy_current_state]; + } + + YY_DO_BEFORE_ACTION; + + if ( yy_act != YY_END_OF_BUFFER ) + { + int yyl; + for ( yyl = 0; yyl < yyleng; ++yyl ) + if ( yytext[yyl] == '\n' ) + ++yylineno; + } + +do_action: /* This label is used only to access EOF actions. */ + + + switch ( yy_act ) + { /* beginning of action switch */ +case 1: +YY_RULE_SETUP +#line 85 "Lexer.l" +{ /* Ignore comments for now */ } + YY_BREAK +case 2: +YY_RULE_SETUP +#line 87 "Lexer.l" +{ return BEGINTOK; } + YY_BREAK +case 3: +YY_RULE_SETUP +#line 88 "Lexer.l" +{ return END; } + YY_BREAK +case 4: +YY_RULE_SETUP +#line 89 "Lexer.l" +{ return TRUE; } + YY_BREAK +case 5: +YY_RULE_SETUP +#line 90 "Lexer.l" +{ return FALSE; } + YY_BREAK +case 6: +YY_RULE_SETUP +#line 91 "Lexer.l" +{ return DECLARE; } + YY_BREAK +case 7: +YY_RULE_SETUP +#line 92 "Lexer.l" +{ return IMPLEMENTATION; } + YY_BREAK +case 8: +YY_RULE_SETUP +#line 94 "Lexer.l" +{ cerr << "deprecated argument '-' used!\n"; return '-'; } + YY_BREAK +case 9: +YY_RULE_SETUP +#line 95 "Lexer.l" +{ cerr << "deprecated type 'bb' used!\n"; llvmAsmlval.TypeVal = Type::LabelTy; return LABEL;} + YY_BREAK +case 10: +YY_RULE_SETUP +#line 97 "Lexer.l" +{ llvmAsmlval.TypeVal = Type::VoidTy ; return VOID; } + YY_BREAK +case 11: +YY_RULE_SETUP +#line 98 "Lexer.l" +{ llvmAsmlval.TypeVal = Type::BoolTy ; return BOOL; } + YY_BREAK +case 12: +YY_RULE_SETUP +#line 99 "Lexer.l" +{ llvmAsmlval.TypeVal = Type::SByteTy ; return SBYTE; } + YY_BREAK +case 13: +YY_RULE_SETUP +#line 100 "Lexer.l" +{ llvmAsmlval.TypeVal = Type::UByteTy ; return UBYTE; } + YY_BREAK +case 14: +YY_RULE_SETUP +#line 101 "Lexer.l" +{ llvmAsmlval.TypeVal = Type::ShortTy ; return SHORT; } + YY_BREAK +case 15: +YY_RULE_SETUP +#line 102 "Lexer.l" +{ llvmAsmlval.TypeVal = Type::UShortTy; return USHORT; } + YY_BREAK +case 16: +YY_RULE_SETUP +#line 103 "Lexer.l" +{ llvmAsmlval.TypeVal = Type::IntTy ; return INT; } + YY_BREAK +case 17: +YY_RULE_SETUP +#line 104 "Lexer.l" +{ llvmAsmlval.TypeVal = Type::UIntTy ; return UINT; } + YY_BREAK +case 18: +YY_RULE_SETUP +#line 105 "Lexer.l" +{ llvmAsmlval.TypeVal = Type::LongTy ; return LONG; } + YY_BREAK +case 19: +YY_RULE_SETUP +#line 106 "Lexer.l" +{ llvmAsmlval.TypeVal = Type::ULongTy ; return ULONG; } + YY_BREAK +case 20: +YY_RULE_SETUP +#line 107 "Lexer.l" +{ llvmAsmlval.TypeVal = Type::FloatTy ; return FLOAT; } + YY_BREAK +case 21: +YY_RULE_SETUP +#line 108 "Lexer.l" +{ llvmAsmlval.TypeVal = Type::DoubleTy; return DOUBLE; } + YY_BREAK +case 22: +YY_RULE_SETUP +#line 110 "Lexer.l" +{ llvmAsmlval.TypeVal = Type::TypeTy ; return TYPE; } + YY_BREAK +case 23: +YY_RULE_SETUP +#line 112 "Lexer.l" +{ llvmAsmlval.TypeVal = Type::LabelTy ; return LABEL; } + YY_BREAK +case 24: +YY_RULE_SETUP +#line 114 "Lexer.l" +{ RET_TOK(UnaryOpVal, Neg, NEG); } + YY_BREAK +case 25: +YY_RULE_SETUP +#line 115 "Lexer.l" +{ RET_TOK(UnaryOpVal, Not, NOT); } + YY_BREAK +case 26: +YY_RULE_SETUP +#line 117 "Lexer.l" +{ return PHI; } + YY_BREAK +case 27: +YY_RULE_SETUP +#line 118 "Lexer.l" +{ return CALL; } + YY_BREAK +case 28: +YY_RULE_SETUP +#line 119 "Lexer.l" +{ RET_TOK(BinaryOpVal, Add, ADD); } + YY_BREAK +case 29: +YY_RULE_SETUP +#line 120 "Lexer.l" +{ RET_TOK(BinaryOpVal, Sub, SUB); } + YY_BREAK +case 30: +YY_RULE_SETUP +#line 121 "Lexer.l" +{ RET_TOK(BinaryOpVal, Mul, MUL); } + YY_BREAK +case 31: +YY_RULE_SETUP +#line 122 "Lexer.l" +{ RET_TOK(BinaryOpVal, Div, DIV); } + YY_BREAK +case 32: +YY_RULE_SETUP +#line 123 "Lexer.l" +{ RET_TOK(BinaryOpVal, Rem, REM); } + YY_BREAK +case 33: +YY_RULE_SETUP +#line 124 "Lexer.l" +{ RET_TOK(BinaryOpVal, SetNE, SETNE); } + YY_BREAK +case 34: +YY_RULE_SETUP +#line 125 "Lexer.l" +{ RET_TOK(BinaryOpVal, SetEQ, SETEQ); } + YY_BREAK +case 35: +YY_RULE_SETUP +#line 126 "Lexer.l" +{ RET_TOK(BinaryOpVal, SetLT, SETLT); } + YY_BREAK +case 36: +YY_RULE_SETUP +#line 127 "Lexer.l" +{ RET_TOK(BinaryOpVal, SetGT, SETGT); } + YY_BREAK +case 37: +YY_RULE_SETUP +#line 128 "Lexer.l" +{ RET_TOK(BinaryOpVal, SetLE, SETLE); } + YY_BREAK +case 38: +YY_RULE_SETUP +#line 129 "Lexer.l" +{ RET_TOK(BinaryOpVal, SetGE, SETGE); } + YY_BREAK +case 39: +YY_RULE_SETUP +#line 131 "Lexer.l" +{ RET_TOK(TermOpVal, Ret, RET); } + YY_BREAK +case 40: +YY_RULE_SETUP +#line 132 "Lexer.l" +{ RET_TOK(TermOpVal, Br, BR); } + YY_BREAK +case 41: +YY_RULE_SETUP +#line 133 "Lexer.l" +{ RET_TOK(TermOpVal, Switch, SWITCH); } + YY_BREAK +case 42: +YY_RULE_SETUP +#line 136 "Lexer.l" +{ RET_TOK(MemOpVal, Malloc, MALLOC); } + YY_BREAK +case 43: +YY_RULE_SETUP +#line 137 "Lexer.l" +{ RET_TOK(MemOpVal, Alloca, ALLOCA); } + YY_BREAK +case 44: +YY_RULE_SETUP +#line 138 "Lexer.l" +{ RET_TOK(MemOpVal, Free, FREE); } + YY_BREAK +case 45: +YY_RULE_SETUP +#line 139 "Lexer.l" +{ RET_TOK(MemOpVal, Load, LOAD); } + YY_BREAK +case 46: +YY_RULE_SETUP +#line 140 "Lexer.l" +{ RET_TOK(MemOpVal, Store, STORE); } + YY_BREAK +case 47: +YY_RULE_SETUP +#line 141 "Lexer.l" +{ RET_TOK(MemOpVal, GetField, GETFIELD); } + YY_BREAK +case 48: +YY_RULE_SETUP +#line 142 "Lexer.l" +{ RET_TOK(MemOpVal, PutField, PUTFIELD); } + YY_BREAK +case 49: +YY_RULE_SETUP +#line 145 "Lexer.l" +{ llvmAsmlval.StrVal = strdup(yytext+1); return VAR_ID; } + YY_BREAK +case 50: +YY_RULE_SETUP +#line 146 "Lexer.l" +{ + yytext[strlen(yytext)-1] = 0; // nuke colon + llvmAsmlval.StrVal = strdup(yytext); + return LABELSTR; + } + YY_BREAK +case 51: +YY_RULE_SETUP +#line 152 "Lexer.l" +{ + yytext[strlen(yytext)-1] = 0; // nuke end quote + llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote + return STRINGCONSTANT; + } + YY_BREAK +case 52: +YY_RULE_SETUP +#line 159 "Lexer.l" +{ llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; } + YY_BREAK +case 53: +YY_RULE_SETUP +#line 160 "Lexer.l" +{ + uint64_t Val = atoull(yytext+1); + // +1: we have bigger negative range + if (Val > (uint64_t)INT64_MAX+1) + ThrowException("Constant too large for signed 64 bits!"); + llvmAsmlval.SInt64Val = -Val; + return ESINT64VAL; + } + YY_BREAK +case 54: +YY_RULE_SETUP +#line 170 "Lexer.l" +{ llvmAsmlval.UIntVal = atoull(yytext+1); return UINTVAL; } + YY_BREAK +case 55: +YY_RULE_SETUP +#line 171 "Lexer.l" +{ + uint64_t Val = atoull(yytext+2); + // +1: we have bigger negative range + if (Val > (uint64_t)INT32_MAX+1) + ThrowException("Constant too large for signed 32 bits!"); + llvmAsmlval.SIntVal = -Val; + return SINTVAL; + } + YY_BREAK +case 56: +YY_RULE_SETUP +#line 181 "Lexer.l" +{ /* Ignore whitespace */ } + YY_BREAK +case 57: +YY_RULE_SETUP +#line 182 "Lexer.l" +{ /*printf("'%s'", yytext);*/ return yytext[0]; } + YY_BREAK +case 58: +YY_RULE_SETUP +#line 184 "Lexer.l" +YY_FATAL_ERROR( "flex scanner jammed" ); + YY_BREAK +#line 1175 "Lexer.cpp" + case YY_STATE_EOF(INITIAL): + yyterminate(); + + case YY_END_OF_BUFFER: + { + /* Amount of text matched not including the EOB char. */ + int yy_amount_of_matched_text = (int) (yy_cp - yytext_ptr) - 1; + + /* Undo the effects of YY_DO_BEFORE_ACTION. */ + *yy_cp = yy_hold_char; + YY_RESTORE_YY_MORE_OFFSET + + if ( yy_current_buffer->yy_buffer_status == YY_BUFFER_NEW ) + { + /* We're scanning a new file or input source. It's + * possible that this happened because the user + * just pointed yyin at a new source and called + * yylex(). If so, then we have to assure + * consistency between yy_current_buffer and our + * globals. Here is the right place to do so, because + * this is the first action (other than possibly a + * back-up) that will match for the new input source. + */ + yy_n_chars = yy_current_buffer->yy_n_chars; + yy_current_buffer->yy_input_file = yyin; + yy_current_buffer->yy_buffer_status = YY_BUFFER_NORMAL; + } + + /* Note that here we test for yy_c_buf_p "<=" to the position + * of the first EOB in the buffer, since yy_c_buf_p will + * already have been incremented past the NUL character + * (since all states make transitions on EOB to the + * end-of-buffer state). Contrast this with the test + * in input(). + */ + if ( yy_c_buf_p <= &yy_current_buffer->yy_ch_buf[yy_n_chars] ) + { /* This was really a NUL. */ + yy_state_type yy_next_state; + + yy_c_buf_p = yytext_ptr + yy_amount_of_matched_text; + + yy_current_state = yy_get_previous_state(); + + /* Okay, we're now positioned to make the NUL + * transition. We couldn't have + * yy_get_previous_state() go ahead and do it + * for us because it doesn't know how to deal + * with the possibility of jamming (and we don't + * want to build jamming into it because then it + * will run more slowly). + */ + + yy_next_state = yy_try_NUL_trans( yy_current_state ); + + yy_bp = yytext_ptr + YY_MORE_ADJ; + + if ( yy_next_state ) + { + /* Consume the NUL. */ + yy_cp = ++yy_c_buf_p; + yy_current_state = yy_next_state; + goto yy_match; + } + + else + { + yy_cp = yy_c_buf_p; + goto yy_find_action; + } + } + + else switch ( yy_get_next_buffer() ) + { + case EOB_ACT_END_OF_FILE: + { + yy_did_buffer_switch_on_eof = 0; + + if ( yywrap() ) + { + /* Note: because we've taken care in + * yy_get_next_buffer() to have set up + * yytext, we can now set up + * yy_c_buf_p so that if some total + * hoser (like flex itself) wants to + * call the scanner after we return the + * YY_NULL, it'll still work - another + * YY_NULL will get returned. + */ + yy_c_buf_p = yytext_ptr + YY_MORE_ADJ; + + yy_act = YY_STATE_EOF(YY_START); + goto do_action; + } + + else + { + if ( ! yy_did_buffer_switch_on_eof ) + YY_NEW_FILE; + } + break; + } + + case EOB_ACT_CONTINUE_SCAN: + yy_c_buf_p = + yytext_ptr + yy_amount_of_matched_text; + + yy_current_state = yy_get_previous_state(); + + yy_cp = yy_c_buf_p; + yy_bp = yytext_ptr + YY_MORE_ADJ; + goto yy_match; + + case EOB_ACT_LAST_MATCH: + yy_c_buf_p = + &yy_current_buffer->yy_ch_buf[yy_n_chars]; + + yy_current_state = yy_get_previous_state(); + + yy_cp = yy_c_buf_p; + yy_bp = yytext_ptr + YY_MORE_ADJ; + goto yy_find_action; + } + break; + } + + default: + YY_FATAL_ERROR( + "fatal flex scanner internal error--no action found" ); + } /* end of action switch */ + } /* end of scanning one token */ + } /* end of yylex */ + + +/* yy_get_next_buffer - try to read in a new buffer + * + * Returns a code representing an action: + * EOB_ACT_LAST_MATCH - + * EOB_ACT_CONTINUE_SCAN - continue scanning from current position + * EOB_ACT_END_OF_FILE - end of file + */ + +static int yy_get_next_buffer() + { + register char *dest = yy_current_buffer->yy_ch_buf; + register char *source = yytext_ptr; + register int number_to_move, i; + int ret_val; + + if ( yy_c_buf_p > &yy_current_buffer->yy_ch_buf[yy_n_chars + 1] ) + YY_FATAL_ERROR( + "fatal flex scanner internal error--end of buffer missed" ); + + if ( yy_current_buffer->yy_fill_buffer == 0 ) + { /* Don't try to fill the buffer, so this is an EOF. */ + if ( yy_c_buf_p - yytext_ptr - YY_MORE_ADJ == 1 ) + { + /* We matched a single character, the EOB, so + * treat this as a final EOF. + */ + return EOB_ACT_END_OF_FILE; + } + + else + { + /* We matched some text prior to the EOB, first + * process it. + */ + return EOB_ACT_LAST_MATCH; + } + } + + /* Try to read more data. */ + + /* First move last chars to start of buffer. */ + number_to_move = (int) (yy_c_buf_p - yytext_ptr) - 1; + + for ( i = 0; i < number_to_move; ++i ) + *(dest++) = *(source++); + + if ( yy_current_buffer->yy_buffer_status == YY_BUFFER_EOF_PENDING ) + /* don't do the read, it's not guaranteed to return an EOF, + * just force an EOF + */ + yy_current_buffer->yy_n_chars = yy_n_chars = 0; + + else + { + int num_to_read = + yy_current_buffer->yy_buf_size - number_to_move - 1; + + while ( num_to_read <= 0 ) + { /* Not enough room in the buffer - grow it. */ +#ifdef YY_USES_REJECT + YY_FATAL_ERROR( +"input buffer overflow, can't enlarge buffer because scanner uses REJECT" ); +#else + + /* just a shorter name for the current buffer */ + YY_BUFFER_STATE b = yy_current_buffer; + + int yy_c_buf_p_offset = + (int) (yy_c_buf_p - b->yy_ch_buf); + + if ( b->yy_is_our_buffer ) + { + int new_size = b->yy_buf_size * 2; + + if ( new_size <= 0 ) + b->yy_buf_size += b->yy_buf_size / 8; + else + b->yy_buf_size *= 2; + + b->yy_ch_buf = (char *) + /* Include room in for 2 EOB chars. */ + yy_flex_realloc( (void *) b->yy_ch_buf, + b->yy_buf_size + 2 ); + } + else + /* Can't grow it, we don't own it. */ + b->yy_ch_buf = 0; + + if ( ! b->yy_ch_buf ) + YY_FATAL_ERROR( + "fatal error - scanner input buffer overflow" ); + + yy_c_buf_p = &b->yy_ch_buf[yy_c_buf_p_offset]; + + num_to_read = yy_current_buffer->yy_buf_size - + number_to_move - 1; +#endif + } + + if ( num_to_read > YY_READ_BUF_SIZE ) + num_to_read = YY_READ_BUF_SIZE; + + /* Read in more data. */ + YY_INPUT( (&yy_current_buffer->yy_ch_buf[number_to_move]), + yy_n_chars, num_to_read ); + + yy_current_buffer->yy_n_chars = yy_n_chars; + } + + if ( yy_n_chars == 0 ) + { + if ( number_to_move == YY_MORE_ADJ ) + { + ret_val = EOB_ACT_END_OF_FILE; + yyrestart( yyin ); + } + + else + { + ret_val = EOB_ACT_LAST_MATCH; + yy_current_buffer->yy_buffer_status = + YY_BUFFER_EOF_PENDING; + } + } + + else + ret_val = EOB_ACT_CONTINUE_SCAN; + + yy_n_chars += number_to_move; + yy_current_buffer->yy_ch_buf[yy_n_chars] = YY_END_OF_BUFFER_CHAR; + yy_current_buffer->yy_ch_buf[yy_n_chars + 1] = YY_END_OF_BUFFER_CHAR; + + yytext_ptr = &yy_current_buffer->yy_ch_buf[0]; + + return ret_val; + } + + +/* yy_get_previous_state - get the state just before the EOB char was reached */ + +static yy_state_type yy_get_previous_state() + { + register yy_state_type yy_current_state; + register char *yy_cp; + + yy_current_state = yy_start; + yy_state_ptr = yy_state_buf; + *yy_state_ptr++ = yy_current_state; + + for ( yy_cp = yytext_ptr + YY_MORE_ADJ; yy_cp < yy_c_buf_p; ++yy_cp ) + { + register YY_CHAR yy_c = (*yy_cp ? yy_ec[YY_SC_TO_UI(*yy_cp)] : 1); + while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) + { + yy_current_state = (int) yy_def[yy_current_state]; + if ( yy_current_state >= 198 ) + yy_c = yy_meta[(unsigned int) yy_c]; + } + yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; + *yy_state_ptr++ = yy_current_state; + } + + return yy_current_state; + } + + +/* yy_try_NUL_trans - try to make a transition on the NUL character + * + * synopsis + * next_state = yy_try_NUL_trans( current_state ); + */ + +#ifdef YY_USE_PROTOS +static yy_state_type yy_try_NUL_trans( yy_state_type yy_current_state ) +#else +static yy_state_type yy_try_NUL_trans( yy_current_state ) +yy_state_type yy_current_state; +#endif + { + register int yy_is_jam; + + register YY_CHAR yy_c = 1; + while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) + { + yy_current_state = (int) yy_def[yy_current_state]; + if ( yy_current_state >= 198 ) + yy_c = yy_meta[(unsigned int) yy_c]; + } + yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; + yy_is_jam = (yy_current_state == 197); + if ( ! yy_is_jam ) + *yy_state_ptr++ = yy_current_state; + + return yy_is_jam ? 0 : yy_current_state; + } + + +#ifndef YY_NO_UNPUT +#ifdef YY_USE_PROTOS +static inline void yyunput( int c, register char *yy_bp ) +#else +static inline void yyunput( c, yy_bp ) +int c; +register char *yy_bp; +#endif + { + register char *yy_cp = yy_c_buf_p; + + /* undo effects of setting up yytext */ + *yy_cp = yy_hold_char; + + if ( yy_cp < yy_current_buffer->yy_ch_buf + 2 ) + { /* need to shift things up to make room */ + /* +2 for EOB chars. */ + register int number_to_move = yy_n_chars + 2; + register char *dest = &yy_current_buffer->yy_ch_buf[ + yy_current_buffer->yy_buf_size + 2]; + register char *source = + &yy_current_buffer->yy_ch_buf[number_to_move]; + + while ( source > yy_current_buffer->yy_ch_buf ) + *--dest = *--source; + + yy_cp += (int) (dest - source); + yy_bp += (int) (dest - source); + yy_current_buffer->yy_n_chars = + yy_n_chars = yy_current_buffer->yy_buf_size; + + if ( yy_cp < yy_current_buffer->yy_ch_buf + 2 ) + YY_FATAL_ERROR( "flex scanner push-back overflow" ); + } + + *--yy_cp = (char) c; + + if ( c == '\n' ) + --yylineno; + + yytext_ptr = yy_bp; + yy_hold_char = *yy_cp; + yy_c_buf_p = yy_cp; + } +#endif /* ifndef YY_NO_UNPUT */ + + +#ifdef __cplusplus +static int yyinput() +#else +static int input() +#endif + { + int c; + + *yy_c_buf_p = yy_hold_char; + + if ( *yy_c_buf_p == YY_END_OF_BUFFER_CHAR ) + { + /* yy_c_buf_p now points to the character we want to return. + * If this occurs *before* the EOB characters, then it's a + * valid NUL; if not, then we've hit the end of the buffer. + */ + if ( yy_c_buf_p < &yy_current_buffer->yy_ch_buf[yy_n_chars] ) + /* This was really a NUL. */ + *yy_c_buf_p = '\0'; + + else + { /* need more input */ + int offset = yy_c_buf_p - yytext_ptr; + ++yy_c_buf_p; + + switch ( yy_get_next_buffer() ) + { + case EOB_ACT_LAST_MATCH: + /* This happens because yy_g_n_b() + * sees that we've accumulated a + * token and flags that we need to + * try matching the token before + * proceeding. But for input(), + * there's no matching to consider. + * So convert the EOB_ACT_LAST_MATCH + * to EOB_ACT_END_OF_FILE. + */ + + /* Reset buffer status. */ + yyrestart( yyin ); + + /* fall through */ + + case EOB_ACT_END_OF_FILE: + { + if ( yywrap() ) + return EOF; + + if ( ! yy_did_buffer_switch_on_eof ) + YY_NEW_FILE; +#ifdef __cplusplus + return yyinput(); +#else + return input(); +#endif + } + + case EOB_ACT_CONTINUE_SCAN: + yy_c_buf_p = yytext_ptr + offset; + break; + } + } + } + + c = *(unsigned char *) yy_c_buf_p; /* cast for 8-bit char's */ + *yy_c_buf_p = '\0'; /* preserve yytext */ + yy_hold_char = *++yy_c_buf_p; + + if ( c == '\n' ) + ++yylineno; + + return c; + } + + +#ifdef YY_USE_PROTOS +void yyrestart( FILE *input_file ) +#else +void yyrestart( input_file ) +FILE *input_file; +#endif + { + if ( ! yy_current_buffer ) + yy_current_buffer = yy_create_buffer( yyin, YY_BUF_SIZE ); + + yy_init_buffer( yy_current_buffer, input_file ); + yy_load_buffer_state(); + } + + +#ifdef YY_USE_PROTOS +void yy_switch_to_buffer( YY_BUFFER_STATE new_buffer ) +#else +void yy_switch_to_buffer( new_buffer ) +YY_BUFFER_STATE new_buffer; +#endif + { + if ( yy_current_buffer == new_buffer ) + return; + + if ( yy_current_buffer ) + { + /* Flush out information for old buffer. */ + *yy_c_buf_p = yy_hold_char; + yy_current_buffer->yy_buf_pos = yy_c_buf_p; + yy_current_buffer->yy_n_chars = yy_n_chars; + } + + yy_current_buffer = new_buffer; + yy_load_buffer_state(); + + /* We don't actually know whether we did this switch during + * EOF (yywrap()) processing, but the only time this flag + * is looked at is after yywrap() is called, so it's safe + * to go ahead and always set it. + */ + yy_did_buffer_switch_on_eof = 1; + } + + +#ifdef YY_USE_PROTOS +void yy_load_buffer_state( void ) +#else +void yy_load_buffer_state() +#endif + { + yy_n_chars = yy_current_buffer->yy_n_chars; + yytext_ptr = yy_c_buf_p = yy_current_buffer->yy_buf_pos; + yyin = yy_current_buffer->yy_input_file; + yy_hold_char = *yy_c_buf_p; + } + + +#ifdef YY_USE_PROTOS +YY_BUFFER_STATE yy_create_buffer( FILE *file, int size ) +#else +YY_BUFFER_STATE yy_create_buffer( file, size ) +FILE *file; +int size; +#endif + { + YY_BUFFER_STATE b; + + b = (YY_BUFFER_STATE) yy_flex_alloc( sizeof( struct yy_buffer_state ) ); + if ( ! b ) + YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" ); + + b->yy_buf_size = size; + + /* yy_ch_buf has to be 2 characters longer than the size given because + * we need to put in 2 end-of-buffer characters. + */ + b->yy_ch_buf = (char *) yy_flex_alloc( b->yy_buf_size + 2 ); + if ( ! b->yy_ch_buf ) + YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" ); + + b->yy_is_our_buffer = 1; + + yy_init_buffer( b, file ); + + return b; + } + + +#ifdef YY_USE_PROTOS +void yy_delete_buffer( YY_BUFFER_STATE b ) +#else +void yy_delete_buffer( b ) +YY_BUFFER_STATE b; +#endif + { + if ( ! b ) + return; + + if ( b == yy_current_buffer ) + yy_current_buffer = (YY_BUFFER_STATE) 0; + + if ( b->yy_is_our_buffer ) + yy_flex_free( (void *) b->yy_ch_buf ); + + yy_flex_free( (void *) b ); + } + + +#ifndef YY_ALWAYS_INTERACTIVE +#ifndef YY_NEVER_INTERACTIVE +extern int isatty YY_PROTO(( int )); +#endif +#endif + +#ifdef YY_USE_PROTOS +void yy_init_buffer( YY_BUFFER_STATE b, FILE *file ) +#else +void yy_init_buffer( b, file ) +YY_BUFFER_STATE b; +FILE *file; +#endif + + + { + yy_flush_buffer( b ); + + b->yy_input_file = file; + b->yy_fill_buffer = 1; + +#if YY_ALWAYS_INTERACTIVE + b->yy_is_interactive = 1; +#else +#if YY_NEVER_INTERACTIVE + b->yy_is_interactive = 0; +#else + b->yy_is_interactive = file ? (isatty( fileno(file) ) > 0) : 0; +#endif +#endif + } + + +#ifdef YY_USE_PROTOS +void yy_flush_buffer( YY_BUFFER_STATE b ) +#else +void yy_flush_buffer( b ) +YY_BUFFER_STATE b; +#endif + + { + if ( ! b ) + return; + + b->yy_n_chars = 0; + + /* We always need two end-of-buffer characters. The first causes + * a transition to the end-of-buffer state. The second causes + * a jam in that state. + */ + b->yy_ch_buf[0] = YY_END_OF_BUFFER_CHAR; + b->yy_ch_buf[1] = YY_END_OF_BUFFER_CHAR; + + b->yy_buf_pos = &b->yy_ch_buf[0]; + + b->yy_at_bol = 1; + b->yy_buffer_status = YY_BUFFER_NEW; + + if ( b == yy_current_buffer ) + yy_load_buffer_state(); + } + + +#ifndef YY_NO_SCAN_BUFFER +#ifdef YY_USE_PROTOS +YY_BUFFER_STATE yy_scan_buffer( char *base, yy_size_t size ) +#else +YY_BUFFER_STATE yy_scan_buffer( base, size ) +char *base; +yy_size_t size; +#endif + { + YY_BUFFER_STATE b; + + if ( size < 2 || + base[size-2] != YY_END_OF_BUFFER_CHAR || + base[size-1] != YY_END_OF_BUFFER_CHAR ) + /* They forgot to leave room for the EOB's. */ + return 0; + + b = (YY_BUFFER_STATE) yy_flex_alloc( sizeof( struct yy_buffer_state ) ); + if ( ! b ) + YY_FATAL_ERROR( "out of dynamic memory in yy_scan_buffer()" ); + + b->yy_buf_size = size - 2; /* "- 2" to take care of EOB's */ + b->yy_buf_pos = b->yy_ch_buf = base; + b->yy_is_our_buffer = 0; + b->yy_input_file = 0; + b->yy_n_chars = b->yy_buf_size; + b->yy_is_interactive = 0; + b->yy_at_bol = 1; + b->yy_fill_buffer = 0; + b->yy_buffer_status = YY_BUFFER_NEW; + + yy_switch_to_buffer( b ); + + return b; + } +#endif + + +#ifndef YY_NO_SCAN_STRING +#ifdef YY_USE_PROTOS +YY_BUFFER_STATE yy_scan_string( yyconst char *yy_str ) +#else +YY_BUFFER_STATE yy_scan_string( yy_str ) +yyconst char *yy_str; +#endif + { + int len; + for ( len = 0; yy_str[len]; ++len ) + ; + + return yy_scan_bytes( yy_str, len ); + } +#endif + + +#ifndef YY_NO_SCAN_BYTES +#ifdef YY_USE_PROTOS +YY_BUFFER_STATE yy_scan_bytes( yyconst char *bytes, int len ) +#else +YY_BUFFER_STATE yy_scan_bytes( bytes, len ) +yyconst char *bytes; +int len; +#endif + { + YY_BUFFER_STATE b; + char *buf; + yy_size_t n; + int i; + + /* Get memory for full buffer, including space for trailing EOB's. */ + n = len + 2; + buf = (char *) yy_flex_alloc( n ); + if ( ! buf ) + YY_FATAL_ERROR( "out of dynamic memory in yy_scan_bytes()" ); + + for ( i = 0; i < len; ++i ) + buf[i] = bytes[i]; + + buf[len] = buf[len+1] = YY_END_OF_BUFFER_CHAR; + + b = yy_scan_buffer( buf, n ); + if ( ! b ) + YY_FATAL_ERROR( "bad buffer in yy_scan_bytes()" ); + + /* It's okay to grow etc. this buffer, and we should throw it + * away when we're done. + */ + b->yy_is_our_buffer = 1; + + return b; + } +#endif + + +#ifndef YY_NO_PUSH_STATE +#ifdef YY_USE_PROTOS +static void yy_push_state( int new_state ) +#else +static void yy_push_state( new_state ) +int new_state; +#endif + { + if ( yy_start_stack_ptr >= yy_start_stack_depth ) + { + yy_size_t new_size; + + yy_start_stack_depth += YY_START_STACK_INCR; + new_size = yy_start_stack_depth * sizeof( int ); + + if ( ! yy_start_stack ) + yy_start_stack = (int *) yy_flex_alloc( new_size ); + + else + yy_start_stack = (int *) yy_flex_realloc( + (void *) yy_start_stack, new_size ); + + if ( ! yy_start_stack ) + YY_FATAL_ERROR( + "out of memory expanding start-condition stack" ); + } + + yy_start_stack[yy_start_stack_ptr++] = YY_START; + + BEGIN(new_state); + } +#endif + + +#ifndef YY_NO_POP_STATE +static void yy_pop_state() + { + if ( --yy_start_stack_ptr < 0 ) + YY_FATAL_ERROR( "start-condition stack underflow" ); + + BEGIN(yy_start_stack[yy_start_stack_ptr]); + } +#endif + + +#ifndef YY_NO_TOP_STATE +static int yy_top_state() + { + return yy_start_stack[yy_start_stack_ptr - 1]; + } +#endif + +#ifndef YY_EXIT_FAILURE +#define YY_EXIT_FAILURE 2 +#endif + +#ifdef YY_USE_PROTOS +static void yy_fatal_error( yyconst char msg[] ) +#else +static void yy_fatal_error( msg ) +char msg[]; +#endif + { + (void) fprintf( stderr, "%s\n", msg ); + exit( YY_EXIT_FAILURE ); + } + + + +/* Redefine yyless() so it works in section 3 code. */ + +#undef yyless +#define yyless(n) \ + do \ + { \ + /* Undo effects of setting up yytext. */ \ + yytext[yyleng] = yy_hold_char; \ + yy_c_buf_p = yytext + n; \ + yy_hold_char = *yy_c_buf_p; \ + *yy_c_buf_p = '\0'; \ + yyleng = n; \ + } \ + while ( 0 ) + + +/* Internal utility routines. */ + +#ifndef yytext_ptr +#ifdef YY_USE_PROTOS +static void yy_flex_strncpy( char *s1, yyconst char *s2, int n ) +#else +static void yy_flex_strncpy( s1, s2, n ) +char *s1; +yyconst char *s2; +int n; +#endif + { + register int i; + for ( i = 0; i < n; ++i ) + s1[i] = s2[i]; + } +#endif + +#ifdef YY_NEED_STRLEN +#ifdef YY_USE_PROTOS +static int yy_flex_strlen( yyconst char *s ) +#else +static int yy_flex_strlen( s ) +yyconst char *s; +#endif + { + register int n; + for ( n = 0; s[n]; ++n ) + ; + + return n; + } +#endif + + +#ifdef YY_USE_PROTOS +static void *yy_flex_alloc( yy_size_t size ) +#else +static void *yy_flex_alloc( size ) +yy_size_t size; +#endif + { + return (void *) malloc( size ); + } + +#ifdef YY_USE_PROTOS +static inline void *yy_flex_realloc( void *ptr, yy_size_t size ) +#else +static inline void *yy_flex_realloc( ptr, size ) +void *ptr; +yy_size_t size; +#endif + { + /* The cast to (char *) in the following accommodates both + * implementations that use char* generic pointers, and those + * that use void* generic pointers. It works with the latter + * because both ANSI C and C++ allow castless assignment from + * any pointer type to void*, and deal with argument conversions + * as though doing an assignment. + */ + return (void *) realloc( (char *) ptr, size ); + } + +#ifdef YY_USE_PROTOS +static void yy_flex_free( void *ptr ) +#else +static void yy_flex_free( ptr ) +void *ptr; +#endif + { + free( ptr ); + } + +#if YY_MAIN +int main() + { + yylex(); + return 0; + } +#endif +#line 184 "Lexer.l" + diff --git a/lib/AsmParser/Lexer.l b/lib/AsmParser/Lexer.l new file mode 100644 index 0000000000..89d776bbb0 --- /dev/null +++ b/lib/AsmParser/Lexer.l @@ -0,0 +1,184 @@ +/*===-- Lexer.l - Scanner for llvm assembly files ----------------*- C++ -*--=// +// +// This file implements the flex scanner for LLVM assembly languages files. +// +//===------------------------------------------------------------------------=*/ + +%option prefix="llvmAsm" +%option yylineno +%option nostdinit +%option never-interactive +%option batch +%option noyywrap +%option nodefault +%option 8bit +%option outfile="Lexer.cpp" +%option ecs +%option noreject +%option noyymore + +%{ +#include "ParserInternals.h" +#include "llvm/BasicBlock.h" +#include "llvm/Method.h" +#include "llvm/Module.h" +#include +#include "llvmAsmParser.h" + +#define RET_TOK(type, Enum, sym) \ + llvmAsmlval.type = Instruction::Enum; return sym + + +// TODO: All of the static identifiers are figured out by the lexer, +// these should be hashed. + + +// atoull - Convert an ascii string of decimal digits into the unsigned long +// long representation... this does not have to do input error checking, +// because we know that the input will be matched by a suitable regex... +// +uint64_t atoull(const char *Buffer) { + uint64_t Result = 0; + for (; *Buffer; Buffer++) { + uint64_t OldRes = Result; + Result *= 10; + Result += *Buffer-'0'; + if (Result < OldRes) { // Uh, oh, overflow detected!!! + ThrowException("constant bigger than 64 bits detected!"); + } + } + return Result; +} + + +#define YY_NEVER_INTERACTIVE 1 +%} + + + +/* Comments start with a ; and go till end of line */ +Comment ;.* + +/* Variable(Def) identifiers start with a % sign */ +VarID %[a-zA-Z$._][a-zA-Z$._0-9]* + +/* Label identifiers end with a colon */ +Label [a-zA-Z$._0-9]+: + +/* Quoted names can contain any character except " and \ */ +StringConstant \"[^\"]+\" + + +/* [PN]Integer: match positive and negative literal integer values that + * are preceeded by a '%' character. These represent unnamed variable slots. + */ +EPInteger %[0-9]+ +ENInteger %-[0-9]+ + + +/* E[PN]Integer: match positive and negative literal integer values */ +PInteger [0-9]+ +NInteger -[0-9]+ + +%% + +{Comment} { /* Ignore comments for now */ } + +begin { return BEGINTOK; } +end { return END; } +true { return TRUE; } +false { return FALSE; } +declare { return DECLARE; } +implementation { return IMPLEMENTATION; } + +- { cerr << "deprecated argument '-' used!\n"; return '-'; } +bb { cerr << "deprecated type 'bb' used!\n"; llvmAsmlval.TypeVal = Type::LabelTy; return LABEL;} + +void { llvmAsmlval.TypeVal = Type::VoidTy ; return VOID; } +bool { llvmAsmlval.TypeVal = Type::BoolTy ; return BOOL; } +sbyte { llvmAsmlval.TypeVal = Type::SByteTy ; return SBYTE; } +ubyte { llvmAsmlval.TypeVal = Type::UByteTy ; return UBYTE; } +short { llvmAsmlval.TypeVal = Type::ShortTy ; return SHORT; } +ushort { llvmAsmlval.TypeVal = Type::UShortTy; return USHORT; } +int { llvmAsmlval.TypeVal = Type::IntTy ; return INT; } +uint { llvmAsmlval.TypeVal = Type::UIntTy ; return UINT; } +long { llvmAsmlval.TypeVal = Type::LongTy ; return LONG; } +ulong { llvmAsmlval.TypeVal = Type::ULongTy ; return ULONG; } +float { llvmAsmlval.TypeVal = Type::FloatTy ; return FLOAT; } +double { llvmAsmlval.TypeVal = Type::DoubleTy; return DOUBLE; } + +type { llvmAsmlval.TypeVal = Type::TypeTy ; return TYPE; } + +label { llvmAsmlval.TypeVal = Type::LabelTy ; return LABEL; } + +neg { RET_TOK(UnaryOpVal, Neg, NEG); } +not { RET_TOK(UnaryOpVal, Not, NOT); } + +phi { return PHI; } +call { return CALL; } +add { RET_TOK(BinaryOpVal, Add, ADD); } +sub { RET_TOK(BinaryOpVal, Sub, SUB); } +mul { RET_TOK(BinaryOpVal, Mul, MUL); } +div { RET_TOK(BinaryOpVal, Div, DIV); } +rem { RET_TOK(BinaryOpVal, Rem, REM); } +setne { RET_TOK(BinaryOpVal, SetNE, SETNE); } +seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); } +setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); } +setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); } +setle { RET_TOK(BinaryOpVal, SetLE, SETLE); } +setge { RET_TOK(BinaryOpVal, SetGE, SETGE); } + +ret { RET_TOK(TermOpVal, Ret, RET); } +br { RET_TOK(TermOpVal, Br, BR); } +switch { RET_TOK(TermOpVal, Switch, SWITCH); } + + +malloc { RET_TOK(MemOpVal, Malloc, MALLOC); } +alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); } +free { RET_TOK(MemOpVal, Free, FREE); } +load { RET_TOK(MemOpVal, Load, LOAD); } +store { RET_TOK(MemOpVal, Store, STORE); } +getfield { RET_TOK(MemOpVal, GetField, GETFIELD); } +putfield { RET_TOK(MemOpVal, PutField, PUTFIELD); } + + +{VarID} { llvmAsmlval.StrVal = strdup(yytext+1); return VAR_ID; } +{Label} { + yytext[strlen(yytext)-1] = 0; // nuke colon + llvmAsmlval.StrVal = strdup(yytext); + return LABELSTR; + } + +{StringConstant} { + yytext[strlen(yytext)-1] = 0; // nuke end quote + llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote + return STRINGCONSTANT; + } + + +{PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; } +{NInteger} { + uint64_t Val = atoull(yytext+1); + // +1: we have bigger negative range + if (Val > (uint64_t)INT64_MAX+1) + ThrowException("Constant too large for signed 64 bits!"); + llvmAsmlval.SInt64Val = -Val; + return ESINT64VAL; + } + + +{EPInteger} { llvmAsmlval.UIntVal = atoull(yytext+1); return UINTVAL; } +{ENInteger} { + uint64_t Val = atoull(yytext+2); + // +1: we have bigger negative range + if (Val > (uint64_t)INT32_MAX+1) + ThrowException("Constant too large for signed 32 bits!"); + llvmAsmlval.SIntVal = -Val; + return SINTVAL; + } + + +[ \t\n] { /* Ignore whitespace */ } +. { /*printf("'%s'", yytext);*/ return yytext[0]; } + +%% diff --git a/lib/AsmParser/Makefile b/lib/AsmParser/Makefile new file mode 100644 index 0000000000..5bec1f4689 --- /dev/null +++ b/lib/AsmParser/Makefile @@ -0,0 +1,7 @@ + +LEVEL = ../../.. + +LIBRARYNAME = asmparser + +include $(LEVEL)/Makefile.common + diff --git a/lib/AsmParser/Parser.cpp b/lib/AsmParser/Parser.cpp new file mode 100644 index 0000000000..57c831e9e1 --- /dev/null +++ b/lib/AsmParser/Parser.cpp @@ -0,0 +1,84 @@ +//===- Parser.cpp - Main dispatch module for the Parser library -------------=== +// +// This library implements the functionality defined in llvm/assembly/parser.h +// +//===------------------------------------------------------------------------=== + +#include "llvm/Analysis/Verifier.h" +#include "llvm/Module.h" +#include "ParserInternals.h" +#include // for sprintf + +// The useful interface defined by this file... Parse an ascii file, and return +// the internal representation in a nice slice'n'dice'able representation. +// +Module *ParseAssemblyFile(const ToolCommandLine &Opts) throw (ParseException) { + FILE *F = stdin; + + if (Opts.getInputFilename() != "-") + F = fopen(Opts.getInputFilename().c_str(), "r"); + + if (F == 0) { + throw ParseException(Opts, string("Could not open file '") + + Opts.getInputFilename() + "'"); + } + + // TODO: If this throws an exception, F is not closed. + Module *Result = RunVMAsmParser(Opts, F); + + if (F != stdin) + fclose(F); + + if (Result) { // Check to see that it is valid... + vector Errors; + if (verify(Result, Errors)) { + delete Result; Result = 0; + string Message; + + for (unsigned i = 0; i < Errors.size(); i++) + Message += Errors[i] + "\n"; + + throw ParseException(Opts, Message); + } + } + return Result; +} + + +//===------------------------------------------------------------------------=== +// ParseException Class +//===------------------------------------------------------------------------=== + + +ParseException::ParseException(const ToolCommandLine &opts, + const string &message, int lineNo, int colNo) + : Opts(opts), Message(message) { + LineNo = lineNo; ColumnNo = colNo; +} + +ParseException::ParseException(const ParseException &E) + : Opts(E.Opts), Message(E.Message) { + LineNo = E.LineNo; + ColumnNo = E.ColumnNo; +} + +const string ParseException::getMessage() const { // Includes info from options + string Result; + char Buffer[10]; + + if (Opts.getInputFilename() == "-") + Result += ""; + else + Result += Opts.getInputFilename(); + + if (LineNo != -1) { + sprintf(Buffer, "%d", LineNo); + Result += string(":") + Buffer; + if (ColumnNo != -1) { + sprintf(Buffer, "%d", ColumnNo); + Result += string(",") + Buffer; + } + } + + return Result + ": " + Message; +} diff --git a/lib/AsmParser/ParserInternals.h b/lib/AsmParser/ParserInternals.h new file mode 100644 index 0000000000..2856c9b08c --- /dev/null +++ b/lib/AsmParser/ParserInternals.h @@ -0,0 +1,159 @@ +//===-- ParserInternals.h - Definitions internal to the parser ---*- C++ -*--=// +// +// This header file defines the various variables that are shared among the +// different components of the parser... +// +//===----------------------------------------------------------------------===// + +#ifndef PARSER_INTERNALS_H +#define PARSER_INTERNALS_H + +#include +#define __STDC_LIMIT_MACROS + +#include "llvm/InstrTypes.h" +#include "llvm/BasicBlock.h" +#include "llvm/ConstPoolVals.h" +#include "llvm/iOther.h" +#include "llvm/Method.h" +#include "llvm/Type.h" +#include "llvm/Assembly/Parser.h" +#include "llvm/Tools/CommandLine.h" +#include "llvm/Tools/StringExtras.h" + +class Module; + +// Global variables exported from the lexer... +extern FILE *llvmAsmin; +extern int llvmAsmlineno; + +// Globals exported by the parser... +extern const ToolCommandLine *CurOptions; +Module *RunVMAsmParser(const ToolCommandLine &Opts, FILE *F); + + +// ThrowException - Wrapper around the ParseException class that automatically +// fills in file line number and column number and options info. +// +// This also helps me because I keep typing 'throw new ParseException' instead +// of just 'throw ParseException'... sigh... +// +static inline void ThrowException(const string &message) { + // TODO: column number in exception + throw ParseException(*CurOptions, message, llvmAsmlineno); +} + +// ValID - Represents a reference of a definition of some sort. This may either +// be a numeric reference or a symbolic (%var) reference. This is just a +// discriminated union. +// +// Note that I can't implement this class in a straight forward manner with +// constructors and stuff because it goes in a union, and GCC doesn't like +// putting classes with ctor's in unions. :( +// +struct ValID { + int Type; // 0 = number, 1 = name, 2 = const pool, + // 3 = unsigned const pool, 4 = const string + union { + int Num; // If it's a numeric reference + char *Name; // If it's a named reference. Memory must be free'd. + int64_t ConstPool64; // Constant pool reference. This is the value + uint64_t UConstPool64;// Unsigned constant pool reference. + }; + + static ValID create(int Num) { + ValID D; D.Type = 0; D.Num = Num; return D; + } + + static ValID create(char *Name) { + ValID D; D.Type = 1; D.Name = Name; return D; + } + + static ValID create(int64_t Val) { + ValID D; D.Type = 2; D.ConstPool64 = Val; return D; + } + + static ValID create(uint64_t Val) { + ValID D; D.Type = 3; D.UConstPool64 = Val; return D; + } + + static ValID create_conststr(char *Name) { + ValID D; D.Type = 4; D.Name = Name; return D; + } + + inline void destroy() { + if (Type == 1 || Type == 4) free(Name); // Free this strdup'd memory... + } + + inline ValID copy() const { + if (Type != 1 && Type != 4) return *this; + ValID Result = *this; + Result.Name = strdup(Name); + return Result; + } + + inline string getName() const { + switch (Type) { + case 0: return string("#") + itostr(Num); + case 1: return Name; + case 4: return string("\"") + Name + string("\""); + default: return string("%") + itostr(ConstPool64); + } + } +}; + + + +template +class PlaceholderDef : public SuperType { + ValID D; + // TODO: Placeholder def should hold Line #/Column # of definition in case + // there is an error resolving the defintition! +public: + PlaceholderDef(const Type *Ty, const ValID &d) : SuperType(Ty), D(d) {} + ValID &getDef() { return D; } +}; + +struct InstPlaceHolderHelper : public Instruction { + InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {} + + virtual Instruction *clone() const { abort(); } + + inline virtual void dropAllReferences() {} + virtual string getOpcode() const { return "placeholder"; } + + // No "operands"... + virtual Value *getOperand(unsigned i) { return 0; } + virtual const Value *getOperand(unsigned i) const { return 0; } + virtual bool setOperand(unsigned i, Value *Val) { return false; } + virtual unsigned getNumOperands() const { return 0; } +}; + +struct BBPlaceHolderHelper : public BasicBlock { + BBPlaceHolderHelper(const Type *Ty) : BasicBlock() { + assert(Ty->isLabelType()); + } +}; + +struct MethPlaceHolderHelper : public Method { + MethPlaceHolderHelper(const Type *Ty) + : Method((const MethodType*)Ty) { + assert(Ty->isMethodType() && "Method placeholders must be method types!"); + } +}; + +typedef PlaceholderDef DefPlaceHolder; +typedef PlaceholderDef BBPlaceHolder; +typedef PlaceholderDef MethPlaceHolder; +//typedef PlaceholderDef ModulePlaceHolder; + +static inline ValID &getValIDFromPlaceHolder(Value *Def) { + switch (Def->getType()->getPrimitiveID()) { + case Type::LabelTyID: return ((BBPlaceHolder*)Def)->getDef(); + case Type::MethodTyID: return ((MethPlaceHolder*)Def)->getDef(); +//case Type::ModuleTyID: return ((ModulePlaceHolder*)Def)->getDef(); + default: return ((DefPlaceHolder*)Def)->getDef(); + } +} + +#endif diff --git a/lib/AsmParser/llvmAsmParser.cpp b/lib/AsmParser/llvmAsmParser.cpp new file mode 100644 index 0000000000..e79f1bf5f6 --- /dev/null +++ b/lib/AsmParser/llvmAsmParser.cpp @@ -0,0 +1,2202 @@ + +/* A Bison parser, made from llvmAsmParser.y + by GNU Bison version 1.28 */ + +#define YYBISON 1 /* Identify Bison output. */ + +#define yyparse llvmAsmparse +#define yylex llvmAsmlex +#define yyerror llvmAsmerror +#define yylval llvmAsmlval +#define yychar llvmAsmchar +#define yydebug llvmAsmdebug +#define yynerrs llvmAsmnerrs +#define ESINT64VAL 257 +#define EUINT64VAL 258 +#define SINTVAL 259 +#define UINTVAL 260 +#define VOID 261 +#define BOOL 262 +#define SBYTE 263 +#define UBYTE 264 +#define SHORT 265 +#define USHORT 266 +#define INT 267 +#define UINT 268 +#define LONG 269 +#define ULONG 270 +#define FLOAT 271 +#define DOUBLE 272 +#define STRING 273 +#define TYPE 274 +#define LABEL 275 +#define VAR_ID 276 +#define LABELSTR 277 +#define STRINGCONSTANT 278 +#define IMPLEMENTATION 279 +#define TRUE 280 +#define FALSE 281 +#define BEGINTOK 282 +#define END 283 +#define DECLARE 284 +#define PHI 285 +#define CALL 286 +#define RET 287 +#define BR 288 +#define SWITCH 289 +#define NEG 290 +#define NOT 291 +#define TOINT 292 +#define TOUINT 293 +#define ADD 294 +#define SUB 295 +#define MUL 296 +#define DIV 297 +#define REM 298 +#define SETLE 299 +#define SETGE 300 +#define SETLT 301 +#define SETGT 302 +#define SETEQ 303 +#define SETNE 304 +#define MALLOC 305 +#define ALLOCA 306 +#define FREE 307 +#define LOAD 308 +#define STORE 309 +#define GETFIELD 310 +#define PUTFIELD 311 + +#line 13 "llvmAsmParser.y" + +#include "ParserInternals.h" +#include "llvm/BasicBlock.h" +#include "llvm/Method.h" +#include "llvm/SymbolTable.h" +#include "llvm/Module.h" +#include "llvm/Type.h" +#include "llvm/DerivedTypes.h" +#include "llvm/Assembly/Parser.h" +#include "llvm/ConstantPool.h" +#include "llvm/iTerminators.h" +#include "llvm/iMemory.h" +#include +#include // Get definition of pair class +#include // This embarasment is due to our flex lexer... + +int yyerror(char *ErrorMsg); // Forward declarations to prevent "implicit +int yylex(); // declaration" of xxx warnings. +int yyparse(); + +static Module *ParserResult; +const ToolCommandLine *CurOptions = 0; + +// This contains info used when building the body of a method. It is destroyed +// when the method is completed. +// +typedef vector ValueList; // Numbered defs +static void ResolveDefinitions(vector &LateResolvers); + +static struct PerModuleInfo { + Module *CurrentModule; + vector Values; // Module level numbered definitions + vector LateResolveValues; + + void ModuleDone() { + // If we could not resolve some blocks at parsing time (forward branches) + // resolve the branches now... + ResolveDefinitions(LateResolveValues); + + Values.clear(); // Clear out method local definitions + CurrentModule = 0; + } +} CurModule; + +static struct PerMethodInfo { + Method *CurrentMethod; // Pointer to current method being created + + vector Values; // Keep track of numbered definitions + vector LateResolveValues; + + inline PerMethodInfo() { + CurrentMethod = 0; + } + + inline ~PerMethodInfo() {} + + inline void MethodStart(Method *M) { + CurrentMethod = M; + } + + void MethodDone() { + // If we could not resolve some blocks at parsing time (forward branches) + // resolve the branches now... + ResolveDefinitions(LateResolveValues); + + Values.clear(); // Clear out method local definitions + CurrentMethod = 0; + } +} CurMeth; // Info for the current method... + + +//===----------------------------------------------------------------------===// +// Code to handle definitions of all the types +//===----------------------------------------------------------------------===// + +static void InsertValue(Value *D, vector &ValueTab = CurMeth.Values) { + if (!D->hasName()) { // Is this a numbered definition? + unsigned type = D->getType()->getUniqueID(); + if (ValueTab.size() <= type) + ValueTab.resize(type+1, ValueList()); + //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D); + ValueTab[type].push_back(D); + } +} + +static Value *getVal(const Type *Type, ValID &D, + bool DoNotImprovise = false) { + switch (D.Type) { + case 0: { // Is it a numbered definition? + unsigned type = Type->getUniqueID(); + unsigned Num = (unsigned)D.Num; + + // Module constants occupy the lowest numbered slots... + if (type < CurModule.Values.size()) { + if (Num < CurModule.Values[type].size()) + return CurModule.Values[type][Num]; + + Num -= CurModule.Values[type].size(); + } + + // Make sure that our type is within bounds + if (CurMeth.Values.size() <= type) + break; + + // Check that the number is within bounds... + if (CurMeth.Values[type].size() <= Num) + break; + + return CurMeth.Values[type][Num]; + } + case 1: { // Is it a named definition? + string Name(D.Name); + SymbolTable *SymTab = 0; + if (CurMeth.CurrentMethod) + SymTab = CurMeth.CurrentMethod->getSymbolTable(); + Value *N = SymTab ? SymTab->lookup(Type, Name) : 0; + + if (N == 0) { + SymTab = CurModule.CurrentModule->getSymbolTable(); + if (SymTab) + N = SymTab->lookup(Type, Name); + if (N == 0) break; + } + + D.destroy(); // Free old strdup'd memory... + return N; + } + + case 2: // Is it a constant pool reference?? + case 3: // Is it an unsigned const pool reference? + case 4:{ // Is it a string const pool reference? + ConstPoolVal *CPV = 0; + + // Check to make sure that "Type" is an integral type, and that our + // value will fit into the specified type... + switch (D.Type) { + case 2: + if (Type == Type::BoolTy) { // Special handling for boolean data + CPV = new ConstPoolBool(D.ConstPool64 != 0); + } else { + if (!ConstPoolSInt::isValueValidForType(Type, D.ConstPool64)) + ThrowException("Symbolic constant pool reference is invalid!"); + CPV = new ConstPoolSInt(Type, D.ConstPool64); + } + break; + case 3: + if (!ConstPoolUInt::isValueValidForType(Type, D.UConstPool64)) { + if (!ConstPoolSInt::isValueValidForType(Type, D.ConstPool64)) { + ThrowException("Symbolic constant pool reference is invalid!"); + } else { // This is really a signed reference. Transmogrify. + CPV = new ConstPoolSInt(Type, D.ConstPool64); + } + } else { + CPV = new ConstPoolUInt(Type, D.UConstPool64); + } + break; + case 4: + cerr << "FIXME: TODO: String constants [sbyte] not implemented yet!\n"; + abort(); + //CPV = new ConstPoolString(D.Name); + D.destroy(); // Free the string memory + break; + } + assert(CPV && "How did we escape creating a constant??"); + + // Scan through the constant table and see if we already have loaded this + // constant. + // + ConstantPool &CP = CurMeth.CurrentMethod ? + CurMeth.CurrentMethod->getConstantPool() : + CurModule.CurrentModule->getConstantPool(); + ConstPoolVal *C = CP.find(CPV); // Already have this constant? + if (C) { + delete CPV; // Didn't need this after all, oh well. + return C; // Yup, we already have one, recycle it! + } + CP.insert(CPV); + + // Success, everything is kosher. Lets go! + return CPV; + } // End of case 2,3,4 + } // End of switch + + + // If we reached here, we referenced either a symbol that we don't know about + // or an id number that hasn't been read yet. We may be referencing something + // forward, so just create an entry to be resolved later and get to it... + // + if (DoNotImprovise) return 0; // Do we just want a null to be returned? + + // TODO: Attempt to coallecse nodes that are the same with previous ones. + Value *d = 0; + switch (Type->getPrimitiveID()) { + case Type::LabelTyID: d = new BBPlaceHolder(Type, D); break; + case Type::MethodTyID: + d = new MethPlaceHolder(Type, D); + InsertValue(d, CurModule.LateResolveValues); + return d; +//case Type::ClassTyID: d = new ClassPlaceHolder(Type, D); break; + default: d = new DefPlaceHolder(Type, D); break; + } + + assert(d != 0 && "How did we not make something?"); + InsertValue(d, CurMeth.LateResolveValues); + return d; +} + + +//===----------------------------------------------------------------------===// +// Code to handle forward references in instructions +//===----------------------------------------------------------------------===// +// +// This code handles the late binding needed with statements that reference +// values not defined yet... for example, a forward branch, or the PHI node for +// a loop body. +// +// This keeps a table (CurMeth.LateResolveValues) of all such forward references +// and back patchs after we are done. +// + +// ResolveDefinitions - If we could not resolve some defs at parsing +// time (forward branches, phi functions for loops, etc...) resolve the +// defs now... +// +static void ResolveDefinitions(vector &LateResolvers) { + // Loop over LateResolveDefs fixing up stuff that couldn't be resolved + for (unsigned ty = 0; ty < LateResolvers.size(); ty++) { + while (!LateResolvers[ty].empty()) { + Value *V = LateResolvers[ty].back(); + LateResolvers[ty].pop_back(); + ValID &DID = getValIDFromPlaceHolder(V); + + Value *TheRealValue = getVal(Type::getUniqueIDType(ty), DID, true); + + if (TheRealValue == 0 && DID.Type == 1) + ThrowException("Reference to an invalid definition: '" +DID.getName() + + "' of type '" + V->getType()->getName() + "'"); + else if (TheRealValue == 0) + ThrowException("Reference to an invalid definition: #" +itostr(DID.Num)+ + " of type '" + V->getType()->getName() + "'"); + + V->replaceAllUsesWith(TheRealValue); + assert(V->use_empty()); + delete V; + } + } + + LateResolvers.clear(); +} + +// addConstValToConstantPool - This code is used to insert a constant into the +// current constant pool. This is designed to make maximal (but not more than +// possible) reuse (merging) of constants in the constant pool. This means that +// multiple references to %4, for example will all get merged. +// +static ConstPoolVal *addConstValToConstantPool(ConstPoolVal *C) { + vector &ValTab = CurMeth.CurrentMethod ? + CurMeth.Values : CurModule.Values; + ConstantPool &CP = CurMeth.CurrentMethod ? + CurMeth.CurrentMethod->getConstantPool() : + CurModule.CurrentModule->getConstantPool(); + + if (ConstPoolVal *CPV = CP.find(C)) { + // Constant already in constant pool. Try to merge the two constants + if (CPV->hasName() && !C->hasName()) { + // Merge the two values, we inherit the existing CPV's name. + // InsertValue requires that the value have no name to insert correctly + // (because we want to fill the slot this constant would have filled) + // + string Name = CPV->getName(); + CPV->setName(""); + InsertValue(CPV, ValTab); + CPV->setName(Name); + delete C; + return CPV; + } else if (!CPV->hasName() && C->hasName()) { + // If we have a name on this value and there isn't one in the const + // pool val already, propogate it. + // + CPV->setName(C->getName()); + delete C; // Sorry, you're toast + return CPV; + } else if (CPV->hasName() && C->hasName()) { + // Both values have distinct names. We cannot merge them. + CP.insert(C); + InsertValue(C, ValTab); + return C; + } else if (!CPV->hasName() && !C->hasName()) { + // Neither value has a name, trivially merge them. + InsertValue(CPV, ValTab); + delete C; + return CPV; + } + + assert(0 && "Not reached!"); + return 0; + } else { // No duplication of value. + CP.insert(C); + InsertValue(C, ValTab); + return C; + } +} + +//===----------------------------------------------------------------------===// +// RunVMAsmParser - Define an interface to this parser +//===----------------------------------------------------------------------===// +// +Module *RunVMAsmParser(const ToolCommandLine &Opts, FILE *F) { + llvmAsmin = F; + CurOptions = &Opts; + llvmAsmlineno = 1; // Reset the current line number... + + CurModule.CurrentModule = new Module(); // Allocate a new module to read + yyparse(); // Parse the file. + Module *Result = ParserResult; + CurOptions = 0; + llvmAsmin = stdin; // F is about to go away, don't use it anymore... + ParserResult = 0; + + return Result; +} + + +#line 337 "llvmAsmParser.y" +typedef union { + Module *ModuleVal; + Method *MethodVal; + MethodArgument *MethArgVal; + BasicBlock *BasicBlockVal; + TerminatorInst *TermInstVal; + Instruction *InstVal; + ConstPoolVal *ConstVal; + const Type *TypeVal; + + list *MethodArgList; + list *ValueList; + list *TypeList; + list > *JumpTable; + vector *ConstVector; + + int64_t SInt64Val; + uint64_t UInt64Val; + int SIntVal; + unsigned UIntVal; + + char *StrVal; // This memory is allocated by strdup! + ValID ValIDVal; // May contain memory allocated by strdup + + Instruction::UnaryOps UnaryOpVal; + Instruction::BinaryOps BinaryOpVal; + Instruction::TermOps TermOpVal; + Instruction::MemoryOps MemOpVal; +} YYSTYPE; +#include + +#ifndef __cplusplus +#ifndef __STDC__ +#define const +#endif +#endif + + + +#define YYFINAL 220 +#define YYFLAG -32768 +#define YYNTBASE 68 + +#define YYTRANSLATE(x) ((unsigned)(x) <= 311 ? yytranslate[x] : 103) + +static const char yytranslate[] = { 0, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 65, + 66, 67, 2, 64, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 58, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 59, 2, 60, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 61, + 2, 2, 62, 2, 63, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 1, 3, 4, 5, 6, + 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, + 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, + 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, + 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, + 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, + 57 +}; + +#if YYDEBUG != 0 +static const short yyprhs[] = { 0, + 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, + 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, + 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, + 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, + 80, 82, 84, 86, 88, 90, 93, 94, 97, 100, + 103, 106, 109, 112, 119, 125, 134, 142, 149, 154, + 158, 160, 164, 165, 167, 170, 173, 175, 176, 179, + 183, 185, 187, 188, 194, 198, 201, 203, 205, 207, + 209, 211, 213, 215, 217, 219, 224, 228, 232, 238, + 242, 245, 248, 250, 254, 257, 260, 263, 267, 270, + 271, 275, 278, 282, 292, 302, 309, 315, 318, 321, + 325, 327, 328, 334, 338, 341, 348, 350, 353, 359, + 362, 368 +}; + +static const short yyrhs[] = { 5, + 0, 6, 0, 3, 0, 4, 0, 8, 0, 9, + 0, 10, 0, 11, 0, 12, 0, 13, 0, 14, + 0, 15, 0, 16, 0, 17, 0, 18, 0, 19, + 0, 20, 0, 21, 0, 70, 0, 7, 0, 36, + 0, 37, 0, 38, 0, 39, 0, 40, 0, 41, + 0, 42, 0, 43, 0, 44, 0, 45, 0, 46, + 0, 47, 0, 48, 0, 49, 0, 50, 0, 15, + 0, 13, 0, 11, 0, 9, 0, 16, 0, 14, + 0, 12, 0, 10, 0, 74, 0, 75, 0, 22, + 58, 0, 0, 74, 69, 0, 75, 4, 0, 8, + 26, 0, 8, 27, 0, 19, 24, 0, 20, 70, + 0, 59, 70, 60, 59, 79, 60, 0, 59, 70, + 60, 59, 60, 0, 59, 4, 61, 70, 60, 59, + 79, 60, 0, 59, 4, 61, 70, 60, 59, 60, + 0, 62, 92, 63, 62, 79, 63, 0, 62, 63, + 62, 63, 0, 79, 64, 78, 0, 78, 0, 80, + 77, 78, 0, 0, 82, 0, 82, 89, 0, 80, + 25, 0, 22, 0, 0, 70, 83, 0, 84, 64, + 85, 0, 84, 0, 85, 0, 0, 71, 24, 65, + 86, 66, 0, 87, 80, 28, 0, 93, 29, 0, + 3, 0, 4, 0, 26, 0, 27, 0, 24, 0, + 68, 0, 22, 0, 90, 0, 91, 0, 71, 65, + 92, 66, 0, 71, 65, 66, 0, 59, 70, 60, + 0, 59, 4, 61, 70, 60, 0, 62, 92, 63, + 0, 62, 63, 0, 70, 67, 0, 70, 0, 92, + 64, 70, 0, 93, 94, 0, 88, 94, 0, 95, + 96, 0, 23, 95, 96, 0, 95, 98, 0, 0, + 33, 70, 91, 0, 33, 7, 0, 34, 21, 91, + 0, 34, 8, 91, 64, 21, 91, 64, 21, 91, + 0, 35, 76, 91, 64, 21, 91, 59, 97, 60, + 0, 97, 76, 90, 64, 21, 91, 0, 76, 90, + 64, 21, 91, 0, 77, 101, 0, 70, 91, 0, + 99, 64, 91, 0, 99, 0, 0, 73, 70, 91, + 64, 91, 0, 72, 70, 91, 0, 31, 99, 0, + 32, 70, 91, 65, 100, 66, 0, 102, 0, 51, + 70, 0, 51, 70, 64, 14, 91, 0, 52, 70, + 0, 52, 70, 64, 14, 91, 0, 53, 70, 91, + 0 +}; + +#endif + +#if YYDEBUG != 0 +static const short yyrline[] = { 0, + 433, 434, 441, 442, 453, 453, 453, 453, 453, 453, + 453, 454, 454, 454, 454, 454, 454, 454, 457, 457, + 462, 462, 462, 462, 463, 463, 463, 463, 463, 464, + 464, 464, 464, 464, 464, 468, 468, 468, 468, 469, + 469, 469, 469, 470, 470, 472, 475, 479, 484, 489, + 492, 495, 501, 504, 517, 521, 539, 546, 554, 568, + 571, 577, 585, 596, 601, 606, 615, 615, 617, 625, + 629, 634, 637, 641, 668, 672, 681, 684, 687, 690, + 693, 698, 701, 704, 711, 719, 724, 728, 731, 734, + 739, 742, 747, 751, 756, 760, 769, 774, 783, 787, + 791, 794, 797, 800, 805, 816, 824, 834, 842, 846, + 852, 852, 854, 859, 864, 873, 910, 914, 919, 929, + 934, 944 +}; +#endif + + +#if YYDEBUG != 0 || defined (YYERROR_VERBOSE) + +static const char * const yytname[] = { "$","error","$undefined.","ESINT64VAL", +"EUINT64VAL","SINTVAL","UINTVAL","VOID","BOOL","SBYTE","UBYTE","SHORT","USHORT", +"INT","UINT","LONG","ULONG","FLOAT","DOUBLE","STRING","TYPE","LABEL","VAR_ID", +"LABELSTR","STRINGCONSTANT","IMPLEMENTATION","TRUE","FALSE","BEGINTOK","END", +"DECLARE","PHI","CALL","RET","BR","SWITCH","NEG","NOT","TOINT","TOUINT","ADD", +"SUB","MUL","DIV","REM","SETLE","SETGE","SETLT","SETGT","SETEQ","SETNE","MALLOC", +"ALLOCA","FREE","LOAD","STORE","GETFIELD","PUTFIELD","'='","'['","']'","'x'", +"'{'","'}'","','","'('","')'","'*'","INTVAL","EINT64VAL","Types","TypesV","UnaryOps", +"BinaryOps","SIntType","UIntType","IntType","OptAssign","ConstVal","ConstVector", +"ConstPool","Module","MethodList","OptVAR_ID","ArgVal","ArgListH","ArgList", +"MethodHeaderH","MethodHeader","Method","ConstValueRef","ValueRef","TypeList", +"BasicBlockList","BasicBlock","InstructionList","BBTerminatorInst","JumpTable", +"Inst","ValueRefList","ValueRefListE","InstVal","MemoryInst", NULL +}; +#endif + +static const short yyr1[] = { 0, + 68, 68, 69, 69, 70, 70, 70, 70, 70, 70, + 70, 70, 70, 70, 70, 70, 70, 70, 71, 71, + 72, 72, 72, 72, 73, 73, 73, 73, 73, 73, + 73, 73, 73, 73, 73, 74, 74, 74, 74, 75, + 75, 75, 75, 76, 76, 77, 77, 78, 78, 78, + 78, 78, 78, 78, 78, 78, 78, 78, 78, 79, + 79, 80, 80, 81, 82, 82, 83, 83, 84, 85, + 85, 86, 86, 87, 88, 89, 90, 90, 90, 90, + 90, 91, 91, 91, 70, 70, 70, 70, 70, 70, + 70, 70, 92, 92, 93, 93, 94, 94, 95, 95, + 96, 96, 96, 96, 96, 97, 97, 98, 99, 99, + 100, 100, 101, 101, 101, 101, 101, 102, 102, 102, + 102, 102 +}; + +static const short yyr2[] = { 0, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 2, 0, 2, 2, 2, + 2, 2, 2, 6, 5, 8, 7, 6, 4, 3, + 1, 3, 0, 1, 2, 2, 1, 0, 2, 3, + 1, 1, 0, 5, 3, 2, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 4, 3, 3, 5, 3, + 2, 2, 1, 3, 2, 2, 2, 3, 2, 0, + 3, 2, 3, 9, 9, 6, 5, 2, 2, 3, + 1, 0, 5, 3, 2, 6, 1, 2, 5, 2, + 5, 3 +}; + +static const short yydefact[] = { 63, + 47, 64, 0, 66, 0, 77, 78, 1, 2, 20, + 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, + 15, 16, 17, 18, 83, 81, 79, 80, 0, 0, + 82, 19, 0, 63, 100, 65, 84, 85, 100, 46, + 0, 39, 43, 38, 42, 37, 41, 36, 40, 0, + 0, 0, 0, 0, 0, 62, 78, 19, 0, 91, + 93, 0, 92, 0, 0, 47, 100, 96, 47, 76, + 95, 50, 51, 52, 53, 78, 19, 0, 0, 3, + 4, 48, 49, 0, 88, 90, 0, 73, 87, 0, + 75, 47, 0, 0, 0, 0, 97, 99, 0, 0, + 0, 0, 19, 94, 68, 71, 72, 0, 86, 98, + 102, 19, 0, 0, 44, 45, 0, 0, 0, 21, + 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, + 32, 33, 34, 35, 0, 0, 0, 0, 0, 108, + 117, 19, 0, 59, 0, 89, 67, 69, 0, 74, + 101, 0, 103, 0, 19, 115, 19, 118, 120, 19, + 19, 19, 0, 55, 61, 0, 0, 70, 0, 0, + 109, 0, 0, 0, 0, 122, 114, 0, 0, 54, + 0, 58, 0, 0, 110, 112, 0, 0, 0, 57, + 0, 60, 0, 0, 111, 0, 119, 121, 113, 56, + 0, 0, 116, 0, 0, 0, 104, 0, 105, 0, + 0, 0, 0, 0, 107, 0, 106, 0, 0, 0 +}; + +static const short yydefgoto[] = { 31, + 82, 61, 59, 138, 139, 54, 55, 117, 5, 165, + 166, 1, 218, 2, 148, 106, 107, 108, 34, 35, + 36, 37, 38, 62, 39, 68, 69, 97, 206, 98, + 156, 196, 140, 141 +}; + +static const short yypact[] = {-32768, + 59, 295, -23,-32768, 435,-32768,-32768,-32768,-32768,-32768, +-32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768, +-32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768, 320, 209, +-32768, -21, -20,-32768, 38,-32768,-32768,-32768, 83,-32768, + 66,-32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768, 77, + 295, 380, 234, 206, 122,-32768, 107, 29, 108,-32768, + 167, 6,-32768, 111, 145, 101,-32768,-32768, 45,-32768, +-32768,-32768,-32768,-32768, 167, 142, 44, 121, 81,-32768, +-32768,-32768,-32768, 295,-32768,-32768, 295, 295,-32768, 193, +-32768, 45, 405, 1, 264, 149,-32768,-32768, 295, 205, + 202, 204, 58, 167, 10, 203,-32768, 215,-32768,-32768, + 217, 7, 116, 116,-32768,-32768, 116, 295, 295,-32768, +-32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768, +-32768,-32768,-32768,-32768, 295, 295, 295, 295, 295,-32768, +-32768, 72, 28,-32768, 435,-32768,-32768,-32768, 295,-32768, +-32768, 219,-32768, 220, 7, 221, 7, -59, 141, 7, + 7, 7, 210,-32768,-32768, 110, 199,-32768, 249, 265, +-32768, 116, 222, 274, 275,-32768,-32768, 226, 43,-32768, + 435,-32768, 116, 116,-32768, 295, 116, 116, 116,-32768, + 115,-32768, 227, 233, 221, 228,-32768,-32768,-32768,-32768, + 297, 264,-32768, 116, 104, 5,-32768, 231,-32768, 104, + 299, 279, 116, 324,-32768, 116,-32768, 348, 349,-32768 +}; + +static const short yypgoto[] = {-32768, +-32768, -2, 350,-32768,-32768, -93, -92, -24, -62, -4, + -119, 316,-32768,-32768,-32768,-32768, 207,-32768,-32768,-32768, +-32768, -64, -89, 11,-32768, 312, 286, 263,-32768,-32768, + 172,-32768,-32768,-32768 +}; + + +#define YYLAST 497 + + +static const short yytable[] = { 32, + 56, 115, 116, 64, 174, -19, 96, 63, 113, 6, + 7, 8, 9, 42, 43, 44, 45, 46, 47, 48, + 49, 114, 151, 152, 153, 167, 58, 154, 25, 96, + 26, 147, 27, 28, 40, 41, 42, 43, 44, 45, + 46, 47, 48, 49, 65, 63, 50, 51, 75, 77, + 41, 42, 43, 44, 45, 46, 47, 48, 49, 191, + 67, 50, 51, 79, 209, 171, 3, 173, 86, 87, + 176, 177, 178, 63, -19, 90, 63, 93, 94, 95, + 3, 103, 185, 4, 104, 105, 52, 164, 85, 53, + 112, 72, 73, 193, 194, 63, 142, 197, 198, 199, + 74, 52, 190, 100, 53, 67, 6, 7, 115, 116, + 63, 70, 115, 116, 207, 155, 157, 146, 6, 7, + 8, 9, 3, 215, 63, 83, 217, 26, 91, 27, + 28, 163, 158, 159, 160, 161, 162, 25, 63, 26, + 208, 27, 28, 102, 87, 212, 105, 6, 7, 8, + 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, + 19, 20, 21, 22, 23, 24, 25, 84, 26, 180, + 27, 28, 65, 181, 200, 88, 192, 205, 181, 118, + 119, 210, 101, 155, 120, 121, 122, 123, 124, 125, + 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, + 136, 137, 99, 29, 175, -19, 30, 63, 80, 81, + 89, 6, 7, 8, 9, 10, 11, 12, 13, 14, + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, + 25, -19, 26, 63, 27, 28, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 87, 26, 109, 27, + 28, 182, 181, 143, 144, 145, 149, 29, 179, 183, + 30, 60, 42, 43, 44, 45, 46, 47, 48, 49, + 150, -20, 169, 170, 172, 184, 186, 187, 188, 189, + 201, 202, 29, 203, 211, 30, 78, 6, 7, 8, + 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, + 19, 20, 21, 22, 23, 24, 25, 204, 26, 213, + 27, 28, 6, 57, 8, 9, 10, 11, 12, 13, + 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, + 24, 25, 214, 26, 216, 27, 28, 219, 220, 66, + 71, 33, 92, 29, 110, 168, 30, 195, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, + 0, 30, 6, 76, 8, 9, 10, 11, 12, 13, + 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, + 24, 25, 0, 26, 0, 27, 28, 6, 7, 8, + 9, 111, 11, 12, 13, 14, 15, 16, 17, 18, + 19, 20, 21, 22, 23, 24, 25, 0, 26, 0, + 27, 28, 0, 0, 0, 0, 0, 0, 29, 0, + 0, 30, 41, 42, 43, 44, 45, 46, 47, 48, + 49, 0, 0, 50, 51, 0, 0, 0, 0, 0, + 0, 0, 0, 29, 0, 0, 30, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 52, 0, 0, 53 +}; + +static const short yycheck[] = { 2, + 5, 95, 95, 24, 64, 65, 69, 67, 8, 3, + 4, 5, 6, 9, 10, 11, 12, 13, 14, 15, + 16, 21, 112, 113, 114, 145, 29, 117, 22, 92, + 24, 22, 26, 27, 58, 8, 9, 10, 11, 12, + 13, 14, 15, 16, 65, 67, 19, 20, 51, 52, + 8, 9, 10, 11, 12, 13, 14, 15, 16, 179, + 23, 19, 20, 53, 60, 155, 22, 157, 63, 64, + 160, 161, 162, 67, 65, 65, 67, 33, 34, 35, + 22, 84, 172, 25, 87, 88, 59, 60, 60, 62, + 93, 26, 27, 183, 184, 67, 99, 187, 188, 189, + 24, 59, 60, 60, 62, 23, 3, 4, 202, 202, + 67, 29, 206, 206, 204, 118, 119, 60, 3, 4, + 5, 6, 22, 213, 67, 4, 216, 24, 28, 26, + 27, 60, 135, 136, 137, 138, 139, 22, 67, 24, + 205, 26, 27, 63, 64, 210, 149, 3, 4, 5, + 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 16, 17, 18, 19, 20, 21, 22, 61, 24, 60, + 26, 27, 65, 64, 60, 65, 181, 202, 64, 31, + 32, 206, 62, 186, 36, 37, 38, 39, 40, 41, + 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, + 52, 53, 61, 59, 64, 65, 62, 67, 3, 4, + 66, 3, 4, 5, 6, 7, 8, 9, 10, 11, + 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, + 22, 65, 24, 67, 26, 27, 3, 4, 5, 6, + 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, + 17, 18, 19, 20, 21, 22, 64, 24, 66, 26, + 27, 63, 64, 59, 63, 62, 64, 59, 59, 21, + 62, 63, 9, 10, 11, 12, 13, 14, 15, 16, + 66, 65, 64, 64, 64, 21, 65, 14, 14, 64, + 64, 59, 59, 66, 64, 62, 63, 3, 4, 5, + 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 16, 17, 18, 19, 20, 21, 22, 21, 24, 21, + 26, 27, 3, 4, 5, 6, 7, 8, 9, 10, + 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, + 21, 22, 64, 24, 21, 26, 27, 0, 0, 34, + 39, 2, 67, 59, 92, 149, 62, 186, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, 59, -1, + -1, 62, 3, 4, 5, 6, 7, 8, 9, 10, + 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, + 21, 22, -1, 24, -1, 26, 27, 3, 4, 5, + 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 16, 17, 18, 19, 20, 21, 22, -1, 24, -1, + 26, 27, -1, -1, -1, -1, -1, -1, 59, -1, + -1, 62, 8, 9, 10, 11, 12, 13, 14, 15, + 16, -1, -1, 19, 20, -1, -1, -1, -1, -1, + -1, -1, -1, 59, -1, -1, 62, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, 59, -1, -1, 62 +}; +/* -*-C-*- Note some compilers choke on comments on `#line' lines. */ +#line 3 "/usr/dcs/software/supported/encap/bison-1.28/share/bison.simple" +/* This file comes from bison-1.28. */ + +/* Skeleton output parser for bison, + Copyright (C) 1984, 1989, 1990 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. */ + +/* This is the parser code that is written into each bison parser + when the %semantic_parser declaration is not specified in the grammar. + It was written by Richard Stallman by simplifying the hairy parser + used when %semantic_parser is specified. */ + +#ifndef YYSTACK_USE_ALLOCA +#ifdef alloca +#define YYSTACK_USE_ALLOCA +#else /* alloca not defined */ +#ifdef __GNUC__ +#define YYSTACK_USE_ALLOCA +#define alloca __builtin_alloca +#else /* not GNU C. */ +#if (!defined (__STDC__) && defined (sparc)) || defined (__sparc__) || defined (__sparc) || defined (__sgi) || (defined (__sun) && defined (__i386)) +#define YYSTACK_USE_ALLOCA +#include +#else /* not sparc */ +/* We think this test detects Watcom and Microsoft C. */ +/* This used to test MSDOS, but that is a bad idea + since that symbol is in the user namespace. */ +#if (defined (_MSDOS) || defined (_MSDOS_)) && !defined (__TURBOC__) +#if 0 /* No need for malloc.h, which pollutes the namespace; + instead, just don't use alloca. */ +#include +#endif +#else /* not MSDOS, or __TURBOC__ */ +#if defined(_AIX) +/* I don't know what this was needed for, but it pollutes the namespace. + So I turned it off. rms, 2 May 1997. */ +/* #include */ + #pragma alloca +#define YYSTACK_USE_ALLOCA +#else /* not MSDOS, or __TURBOC__, or _AIX */ +#if 0 +#ifdef __hpux /* haible@ilog.fr says this works for HPUX 9.05 and up, + and on HPUX 10. Eventually we can turn this on. */ +#define YYSTACK_USE_ALLOCA +#define alloca __builtin_alloca +#endif /* __hpux */ +#endif +#endif /* not _AIX */ +#endif /* not MSDOS, or __TURBOC__ */ +#endif /* not sparc */ +#endif /* not GNU C */ +#endif /* alloca not defined */ +#endif /* YYSTACK_USE_ALLOCA not defined */ + +#ifdef YYSTACK_USE_ALLOCA +#define YYSTACK_ALLOC alloca +#else +#define YYSTACK_ALLOC malloc +#endif + +/* Note: there must be only one dollar sign in this file. + It is replaced by the list of actions, each action + as one case of the switch. */ + +#define yyerrok (yyerrstatus = 0) +#define yyclearin (yychar = YYEMPTY) +#define YYEMPTY -2 +#define YYEOF 0 +#define YYACCEPT goto yyacceptlab +#define YYABORT goto yyabortlab +#define YYERROR goto yyerrlab1 +/* Like YYERROR except do call yyerror. + This remains here temporarily to ease the + transition to the new meaning of YYERROR, for GCC. + Once GCC version 2 has supplanted version 1, this can go. */ +#define YYFAIL goto yyerrlab +#define YYRECOVERING() (!!yyerrstatus) +#define YYBACKUP(token, value) \ +do \ + if (yychar == YYEMPTY && yylen == 1) \ + { yychar = (token), yylval = (value); \ + yychar1 = YYTRANSLATE (yychar); \ + YYPOPSTACK; \ + goto yybackup; \ + } \ + else \ + { yyerror ("syntax error: cannot back up"); YYERROR; } \ +while (0) + +#define YYTERROR 1 +#define YYERRCODE 256 + +#ifndef YYPURE +#define YYLEX yylex() +#endif + +#ifdef YYPURE +#ifdef YYLSP_NEEDED +#ifdef YYLEX_PARAM +#define YYLEX yylex(&yylval, &yylloc, YYLEX_PARAM) +#else +#define YYLEX yylex(&yylval, &yylloc) +#endif +#else /* not YYLSP_NEEDED */ +#ifdef YYLEX_PARAM +#define YYLEX yylex(&yylval, YYLEX_PARAM) +#else +#define YYLEX yylex(&yylval) +#endif +#endif /* not YYLSP_NEEDED */ +#endif + +/* If nonreentrant, generate the variables here */ + +#ifndef YYPURE + +int yychar; /* the lookahead symbol */ +YYSTYPE yylval; /* the semantic value of the */ + /* lookahead symbol */ + +#ifdef YYLSP_NEEDED +YYLTYPE yylloc; /* location data for the lookahead */ + /* symbol */ +#endif + +int yynerrs; /* number of parse errors so far */ +#endif /* not YYPURE */ + +#if YYDEBUG != 0 +int yydebug; /* nonzero means print parse trace */ +/* Since this is uninitialized, it does not stop multiple parsers + from coexisting. */ +#endif + +/* YYINITDEPTH indicates the initial size of the parser's stacks */ + +#ifndef YYINITDEPTH +#define YYINITDEPTH 200 +#endif + +/* YYMAXDEPTH is the maximum size the stacks can grow to + (effective only if the built-in stack extension method is used). */ + +#if YYMAXDEPTH == 0 +#undef YYMAXDEPTH +#endif + +#ifndef YYMAXDEPTH +#define YYMAXDEPTH 10000 +#endif + +/* Define __yy_memcpy. Note that the size argument + should be passed with type unsigned int, because that is what the non-GCC + definitions require. With GCC, __builtin_memcpy takes an arg + of type size_t, but it can handle unsigned int. */ + +#if __GNUC__ > 1 /* GNU C and GNU C++ define this. */ +#define __yy_memcpy(TO,FROM,COUNT) __builtin_memcpy(TO,FROM,COUNT) +#else /* not GNU C or C++ */ +#ifndef __cplusplus + +/* This is the most reliable way to avoid incompatibilities + in available built-in functions on various systems. */ +static void +__yy_memcpy (to, from, count) + char *to; + char *from; + unsigned int count; +{ + register char *f = from; + register char *t = to; + register int i = count; + + while (i-- > 0) + *t++ = *f++; +} + +#else /* __cplusplus */ + +/* This is the most reliable way to avoid incompatibilities + in available built-in functions on various systems. */ +static void +__yy_memcpy (char *to, char *from, unsigned int count) +{ + register char *t = to; + register char *f = from; + register int i = count; + + while (i-- > 0) + *t++ = *f++; +} + +#endif +#endif + +#line 217 "/usr/dcs/software/supported/encap/bison-1.28/share/bison.simple" + +/* The user can define YYPARSE_PARAM as the name of an argument to be passed + into yyparse. The argument should have type void *. + It should actually point to an object. + Grammar actions can access the variable by casting it + to the proper pointer type. */ + +#ifdef YYPARSE_PARAM +#ifdef __cplusplus +#define YYPARSE_PARAM_ARG void *YYPARSE_PARAM +#define YYPARSE_PARAM_DECL +#else /* not __cplusplus */ +#define YYPARSE_PARAM_ARG YYPARSE_PARAM +#define YYPARSE_PARAM_DECL void *YYPARSE_PARAM; +#endif /* not __cplusplus */ +#else /* not YYPARSE_PARAM */ +#define YYPARSE_PARAM_ARG +#define YYPARSE_PARAM_DECL +#endif /* not YYPARSE_PARAM */ + +/* Prevent warning if -Wstrict-prototypes. */ +#ifdef __GNUC__ +#ifdef YYPARSE_PARAM +int yyparse (void *); +#else +int yyparse (void); +#endif +#endif + +int +yyparse(YYPARSE_PARAM_ARG) + YYPARSE_PARAM_DECL +{ + register int yystate; + register int yyn; + register short *yyssp; + register YYSTYPE *yyvsp; + int yyerrstatus; /* number of tokens to shift before error messages enabled */ + int yychar1 = 0; /* lookahead token as an internal (translated) token number */ + + short yyssa[YYINITDEPTH]; /* the state stack */ + YYSTYPE yyvsa[YYINITDEPTH]; /* the semantic value stack */ + + short *yyss = yyssa; /* refer to the stacks thru separate pointers */ + YYSTYPE *yyvs = yyvsa; /* to allow yyoverflow to reallocate them elsewhere */ + +#ifdef YYLSP_NEEDED + YYLTYPE yylsa[YYINITDEPTH]; /* the location stack */ + YYLTYPE *yyls = yylsa; + YYLTYPE *yylsp; + +#define YYPOPSTACK (yyvsp--, yyssp--, yylsp--) +#else +#define YYPOPSTACK (yyvsp--, yyssp--) +#endif + + int yystacksize = YYINITDEPTH; + int yyfree_stacks = 0; + +#ifdef YYPURE + int yychar; + YYSTYPE yylval; + int yynerrs; +#ifdef YYLSP_NEEDED + YYLTYPE yylloc; +#endif +#endif + + YYSTYPE yyval; /* the variable used to return */ + /* semantic values from the action */ + /* routines */ + + int yylen; + +#if YYDEBUG != 0 + if (yydebug) + fprintf(stderr, "Starting parse\n"); +#endif + + yystate = 0; + yyerrstatus = 0; + yynerrs = 0; + yychar = YYEMPTY; /* Cause a token to be read. */ + + /* Initialize stack pointers. + Waste one element of value and location stack + so that they stay on the same level as the state stack. + The wasted elements are never initialized. */ + + yyssp = yyss - 1; + yyvsp = yyvs; +#ifdef YYLSP_NEEDED + yylsp = yyls; +#endif + +/* Push a new state, which is found in yystate . */ +/* In all cases, when you get here, the value and location stacks + have just been pushed. so pushing a state here evens the stacks. */ +yynewstate: + + *++yyssp = yystate; + + if (yyssp >= yyss + yystacksize - 1) + { + /* Give user a chance to reallocate the stack */ + /* Use copies of these so that the &'s don't force the real ones into memory. */ + YYSTYPE *yyvs1 = yyvs; + short *yyss1 = yyss; +#ifdef YYLSP_NEEDED + YYLTYPE *yyls1 = yyls; +#endif + + /* Get the current used size of the three stacks, in elements. */ + int size = yyssp - yyss + 1; + +#ifdef yyoverflow + /* Each stack pointer address is followed by the size of + the data in use in that stack, in bytes. */ +#ifdef YYLSP_NEEDED + /* This used to be a conditional around just the two extra args, + but that might be undefined if yyoverflow is a macro. */ + yyoverflow("parser stack overflow", + &yyss1, size * sizeof (*yyssp), + &yyvs1, size * sizeof (*yyvsp), + &yyls1, size * sizeof (*yylsp), + &yystacksize); +#else + yyoverflow("parser stack overflow", + &yyss1, size * sizeof (*yyssp), + &yyvs1, size * sizeof (*yyvsp), + &yystacksize); +#endif + + yyss = yyss1; yyvs = yyvs1; +#ifdef YYLSP_NEEDED + yyls = yyls1; +#endif +#else /* no yyoverflow */ + /* Extend the stack our own way. */ + if (yystacksize >= YYMAXDEPTH) + { + yyerror("parser stack overflow"); + if (yyfree_stacks) + { + free (yyss); + free (yyvs); +#ifdef YYLSP_NEEDED + free (yyls); +#endif + } + return 2; + } + yystacksize *= 2; + if (yystacksize > YYMAXDEPTH) + yystacksize = YYMAXDEPTH; +#ifndef YYSTACK_USE_ALLOCA + yyfree_stacks = 1; +#endif + yyss = (short *) YYSTACK_ALLOC (yystacksize * sizeof (*yyssp)); + __yy_memcpy ((char *)yyss, (char *)yyss1, + size * (unsigned int) sizeof (*yyssp)); + yyvs = (YYSTYPE *) YYSTACK_ALLOC (yystacksize * sizeof (*yyvsp)); + __yy_memcpy ((char *)yyvs, (char *)yyvs1, + size * (unsigned int) sizeof (*yyvsp)); +#ifdef YYLSP_NEEDED + yyls = (YYLTYPE *) YYSTACK_ALLOC (yystacksize * sizeof (*yylsp)); + __yy_memcpy ((char *)yyls, (char *)yyls1, + size * (unsigned int) sizeof (*yylsp)); +#endif +#endif /* no yyoverflow */ + + yyssp = yyss + size - 1; + yyvsp = yyvs + size - 1; +#ifdef YYLSP_NEEDED + yylsp = yyls + size - 1; +#endif + +#if YYDEBUG != 0 + if (yydebug) + fprintf(stderr, "Stack size increased to %d\n", yystacksize); +#endif + + if (yyssp >= yyss + yystacksize - 1) + YYABORT; + } + +#if YYDEBUG != 0 + if (yydebug) + fprintf(stderr, "Entering state %d\n", yystate); +#endif + + goto yybackup; + yybackup: + +/* Do appropriate processing given the current state. */ +/* Read a lookahead token if we need one and don't already have one. */ +/* yyresume: */ + + /* First try to decide what to do without reference to lookahead token. */ + + yyn = yypact[yystate]; + if (yyn == YYFLAG) + goto yydefault; + + /* Not known => get a lookahead token if don't already have one. */ + + /* yychar is either YYEMPTY or YYEOF + or a valid token in external form. */ + + if (yychar == YYEMPTY) + { +#if YYDEBUG != 0 + if (yydebug) + fprintf(stderr, "Reading a token: "); +#endif + yychar = YYLEX; + } + + /* Convert token to internal form (in yychar1) for indexing tables with */ + + if (yychar <= 0) /* This means end of input. */ + { + yychar1 = 0; + yychar = YYEOF; /* Don't call YYLEX any more */ + +#if YYDEBUG != 0 + if (yydebug) + fprintf(stderr, "Now at end of input.\n"); +#endif + } + else + { + yychar1 = YYTRANSLATE(yychar); + +#if YYDEBUG != 0 + if (yydebug) + { + fprintf (stderr, "Next token is %d (%s", yychar, yytname[yychar1]); + /* Give the individual parser a way to print the precise meaning + of a token, for further debugging info. */ +#ifdef YYPRINT + YYPRINT (stderr, yychar, yylval); +#endif + fprintf (stderr, ")\n"); + } +#endif + } + + yyn += yychar1; + if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != yychar1) + goto yydefault; + + yyn = yytable[yyn]; + + /* yyn is what to do for this token type in this state. + Negative => reduce, -yyn is rule number. + Positive => shift, yyn is new state. + New state is final state => don't bother to shift, + just return success. + 0, or most negative number => error. */ + + if (yyn < 0) + { + if (yyn == YYFLAG) + goto yyerrlab; + yyn = -yyn; + goto yyreduce; + } + else if (yyn == 0) + goto yyerrlab; + + if (yyn == YYFINAL) + YYACCEPT; + + /* Shift the lookahead token. */ + +#if YYDEBUG != 0 + if (yydebug) + fprintf(stderr, "Shifting token %d (%s), ", yychar, yytname[yychar1]); +#endif + + /* Discard the token being shifted unless it is eof. */ + if (yychar != YYEOF) + yychar = YYEMPTY; + + *++yyvsp = yylval; +#ifdef YYLSP_NEEDED + *++yylsp = yylloc; +#endif + + /* count tokens shifted since error; after three, turn off error status. */ + if (yyerrstatus) yyerrstatus--; + + yystate = yyn; + goto yynewstate; + +/* Do the default action for the current state. */ +yydefault: + + yyn = yydefact[yystate]; + if (yyn == 0) + goto yyerrlab; + +/* Do a reduction. yyn is the number of a rule to reduce with. */ +yyreduce: + yylen = yyr2[yyn]; + if (yylen > 0) + yyval = yyvsp[1-yylen]; /* implement default value of the action */ + +#if YYDEBUG != 0 + if (yydebug) + { + int i; + + fprintf (stderr, "Reducing via rule %d (line %d), ", + yyn, yyrline[yyn]); + + /* Print the symbols being reduced, and their result. */ + for (i = yyprhs[yyn]; yyrhs[i] > 0; i++) + fprintf (stderr, "%s ", yytname[yyrhs[i]]); + fprintf (stderr, " -> %s\n", yytname[yyr1[yyn]]); + } +#endif + + + switch (yyn) { + +case 2: +#line 434 "llvmAsmParser.y" +{ + if (yyvsp[0].UIntVal > (uint32_t)INT32_MAX) // Outside of my range! + ThrowException("Value too large for type!"); + yyval.SIntVal = (int32_t)yyvsp[0].UIntVal; +; + break;} +case 4: +#line 442 "llvmAsmParser.y" +{ + if (yyvsp[0].UInt64Val > (uint64_t)INT64_MAX) // Outside of my range! + ThrowException("Value too large for type!"); + yyval.SInt64Val = (int64_t)yyvsp[0].UInt64Val; +; + break;} +case 46: +#line 472 "llvmAsmParser.y" +{ + yyval.StrVal = yyvsp[-1].StrVal; + ; + break;} +case 47: +#line 475 "llvmAsmParser.y" +{ + yyval.StrVal = 0; + ; + break;} +case 48: +#line 479 "llvmAsmParser.y" +{ // integral constants + if (!ConstPoolSInt::isValueValidForType(yyvsp[-1].TypeVal, yyvsp[0].SInt64Val)) + ThrowException("Constant value doesn't fit in type!"); + yyval.ConstVal = new ConstPoolSInt(yyvsp[-1].TypeVal, yyvsp[0].SInt64Val); + ; + break;} +case 49: +#line 484 "llvmAsmParser.y" +{ // integral constants + if (!ConstPoolUInt::isValueValidForType(yyvsp[-1].TypeVal, yyvsp[0].UInt64Val)) + ThrowException("Constant value doesn't fit in type!"); + yyval.ConstVal = new ConstPoolUInt(yyvsp[-1].TypeVal, yyvsp[0].UInt64Val); + ; + break;} +case 50: +#line 489 "llvmAsmParser.y" +{ // Boolean constants + yyval.ConstVal = new ConstPoolBool(true); + ; + break;} +case 51: +#line 492 "llvmAsmParser.y" +{ // Boolean constants + yyval.ConstVal = new ConstPoolBool(false); + ; + break;} +case 52: +#line 495 "llvmAsmParser.y" +{ // String constants + cerr << "FIXME: TODO: String constants [sbyte] not implemented yet!\n"; + abort(); + //$$ = new ConstPoolString($2); + free(yyvsp[0].StrVal); + ; + break;} +case 53: +#line 501 "llvmAsmParser.y" +{ // Type constants + yyval.ConstVal = new ConstPoolType(yyvsp[0].TypeVal); + ; + break;} +case 54: +#line 504 "llvmAsmParser.y" +{ // Nonempty array constant + // Verify all elements are correct type! + const ArrayType *AT = ArrayType::getArrayType(yyvsp[-4].TypeVal); + for (unsigned i = 0; i < yyvsp[-1].ConstVector->size(); i++) { + if (yyvsp[-4].TypeVal != (*yyvsp[-1].ConstVector)[i]->getType()) + ThrowException("Element #" + utostr(i) + " is not of type '" + + yyvsp[-4].TypeVal->getName() + "' as required!\nIt is of type '" + + (*yyvsp[-1].ConstVector)[i]->getType()->getName() + "'."); + } + + yyval.ConstVal = new ConstPoolArray(AT, *yyvsp[-1].ConstVector); + delete yyvsp[-1].ConstVector; + ; + break;} +case 55: +#line 517 "llvmAsmParser.y" +{ // Empty array constant + vector Empty; + yyval.ConstVal = new ConstPoolArray(ArrayType::getArrayType(yyvsp[-3].TypeVal), Empty); + ; + break;} +case 56: +#line 521 "llvmAsmParser.y" +{ + // Verify all elements are correct type! + const ArrayType *AT = ArrayType::getArrayType(yyvsp[-4].TypeVal, (int)yyvsp[-6].UInt64Val); + if (yyvsp[-6].UInt64Val != yyvsp[-1].ConstVector->size()) + ThrowException("Type mismatch: constant sized array initialized with " + + utostr(yyvsp[-1].ConstVector->size()) + " arguments, but has size of " + + itostr((int)yyvsp[-6].UInt64Val) + "!"); + + for (unsigned i = 0; i < yyvsp[-1].ConstVector->size(); i++) { + if (yyvsp[-4].TypeVal != (*yyvsp[-1].ConstVector)[i]->getType()) + ThrowException("Element #" + utostr(i) + " is not of type '" + + yyvsp[-4].TypeVal->getName() + "' as required!\nIt is of type '" + + (*yyvsp[-1].ConstVector)[i]->getType()->getName() + "'."); + } + + yyval.ConstVal = new ConstPoolArray(AT, *yyvsp[-1].ConstVector); + delete yyvsp[-1].ConstVector; + ; + break;} +case 57: +#line 539 "llvmAsmParser.y" +{ + if (yyvsp[-5].UInt64Val != 0) + ThrowException("Type mismatch: constant sized array initialized with 0" + " arguments, but has size of " + itostr((int)yyvsp[-5].UInt64Val) + "!"); + vector Empty; + yyval.ConstVal = new ConstPoolArray(ArrayType::getArrayType(yyvsp[-3].TypeVal, 0), Empty); + ; + break;} +case 58: +#line 546 "llvmAsmParser.y" +{ + StructType::ElementTypes Types(yyvsp[-4].TypeList->begin(), yyvsp[-4].TypeList->end()); + delete yyvsp[-4].TypeList; + + const StructType *St = StructType::getStructType(Types); + yyval.ConstVal = new ConstPoolStruct(St, *yyvsp[-1].ConstVector); + delete yyvsp[-1].ConstVector; + ; + break;} +case 59: +#line 554 "llvmAsmParser.y" +{ + const StructType *St = + StructType::getStructType(StructType::ElementTypes()); + vector Empty; + yyval.ConstVal = new ConstPoolStruct(St, Empty); + ; + break;} +case 60: +#line 568 "llvmAsmParser.y" +{ + (yyval.ConstVector = yyvsp[-2].ConstVector)->push_back(addConstValToConstantPool(yyvsp[0].ConstVal)); + ; + break;} +case 61: +#line 571 "llvmAsmParser.y" +{ + yyval.ConstVector = new vector(); + yyval.ConstVector->push_back(addConstValToConstantPool(yyvsp[0].ConstVal)); + ; + break;} +case 62: +#line 577 "llvmAsmParser.y" +{ + if (yyvsp[-1].StrVal) { + yyvsp[0].ConstVal->setName(yyvsp[-1].StrVal); + free(yyvsp[-1].StrVal); + } + + addConstValToConstantPool(yyvsp[0].ConstVal); + ; + break;} +case 63: +#line 585 "llvmAsmParser.y" +{ + ; + break;} +case 64: +#line 596 "llvmAsmParser.y" +{ + yyval.ModuleVal = ParserResult = yyvsp[0].ModuleVal; + CurModule.ModuleDone(); +; + break;} +case 65: +#line 601 "llvmAsmParser.y" +{ + yyvsp[-1].ModuleVal->getMethodList().push_back(yyvsp[0].MethodVal); + CurMeth.MethodDone(); + yyval.ModuleVal = yyvsp[-1].ModuleVal; + ; + break;} +case 66: +#line 606 "llvmAsmParser.y" +{ + yyval.ModuleVal = CurModule.CurrentModule; + ; + break;} +case 68: +#line 615 "llvmAsmParser.y" +{ yyval.StrVal = 0; ; + break;} +case 69: +#line 617 "llvmAsmParser.y" +{ + yyval.MethArgVal = new MethodArgument(yyvsp[-1].TypeVal); + if (yyvsp[0].StrVal) { // Was the argument named? + yyval.MethArgVal->setName(yyvsp[0].StrVal); + free(yyvsp[0].StrVal); // The string was strdup'd, so free it now. + } +; + break;} +case 70: +#line 625 "llvmAsmParser.y" +{ + yyval.MethodArgList = yyvsp[0].MethodArgList; + yyvsp[0].MethodArgList->push_front(yyvsp[-2].MethArgVal); + ; + break;} +case 71: +#line 629 "llvmAsmParser.y" +{ + yyval.MethodArgList = new list(); + yyval.MethodArgList->push_front(yyvsp[0].MethArgVal); + ; + break;} +case 72: +#line 634 "llvmAsmParser.y" +{ + yyval.MethodArgList = yyvsp[0].MethodArgList; + ; + break;} +case 73: +#line 637 "llvmAsmParser.y" +{ + yyval.MethodArgList = 0; + ; + break;} +case 74: +#line 641 "llvmAsmParser.y" +{ + MethodType::ParamTypes ParamTypeList; + if (yyvsp[-1].MethodArgList) + for (list::iterator I = yyvsp[-1].MethodArgList->begin(); I != yyvsp[-1].MethodArgList->end(); I++) + ParamTypeList.push_back((*I)->getType()); + + const MethodType *MT = MethodType::getMethodType(yyvsp[-4].TypeVal, ParamTypeList); + + Method *M = new Method(MT, yyvsp[-3].StrVal); + free(yyvsp[-3].StrVal); // Free strdup'd memory! + + InsertValue(M, CurModule.Values); + + CurMeth.MethodStart(M); + + // Add all of the arguments we parsed to the method... + if (yyvsp[-1].MethodArgList) { // Is null if empty... + Method::ArgumentListType &ArgList = M->getArgumentList(); + + for (list::iterator I = yyvsp[-1].MethodArgList->begin(); I != yyvsp[-1].MethodArgList->end(); I++) { + InsertValue(*I); + ArgList.push_back(*I); + } + delete yyvsp[-1].MethodArgList; // We're now done with the argument list + } +; + break;} +case 75: +#line 668 "llvmAsmParser.y" +{ + yyval.MethodVal = CurMeth.CurrentMethod; +; + break;} +case 76: +#line 672 "llvmAsmParser.y" +{ + yyval.MethodVal = yyvsp[-1].MethodVal; +; + break;} +case 77: +#line 681 "llvmAsmParser.y" +{ // A reference to a direct constant + yyval.ValIDVal = ValID::create(yyvsp[0].SInt64Val); + ; + break;} +case 78: +#line 684 "llvmAsmParser.y" +{ + yyval.ValIDVal = ValID::create(yyvsp[0].UInt64Val); + ; + break;} +case 79: +#line 687 "llvmAsmParser.y" +{ + yyval.ValIDVal = ValID::create((int64_t)1); + ; + break;} +case 80: +#line 690 "llvmAsmParser.y" +{ + yyval.ValIDVal = ValID::create((int64_t)0); + ; + break;} +case 81: +#line 693 "llvmAsmParser.y" +{ // Quoted strings work too... especially for methods + yyval.ValIDVal = ValID::create_conststr(yyvsp[0].StrVal); + ; + break;} +case 82: +#line 698 "llvmAsmParser.y" +{ // Is it an integer reference...? + yyval.ValIDVal = ValID::create(yyvsp[0].SIntVal); + ; + break;} +case 83: +#line 701 "llvmAsmParser.y" +{ // It must be a named reference then... + yyval.ValIDVal = ValID::create(yyvsp[0].StrVal); + ; + break;} +case 84: +#line 704 "llvmAsmParser.y" +{ + yyval.ValIDVal = yyvsp[0].ValIDVal; + ; + break;} +case 85: +#line 711 "llvmAsmParser.y" +{ + Value *D = getVal(Type::TypeTy, yyvsp[0].ValIDVal, true); + if (D == 0) ThrowException("Invalid user defined type: " + yyvsp[0].ValIDVal.getName()); + assert (D->getValueType() == Value::ConstantVal && + "Internal error! User defined type not in const pool!"); + ConstPoolType *CPT = (ConstPoolType*)D; + yyval.TypeVal = CPT->getValue(); + ; + break;} +case 86: +#line 719 "llvmAsmParser.y" +{ // Method derived type? + MethodType::ParamTypes Params(yyvsp[-1].TypeList->begin(), yyvsp[-1].TypeList->end()); + delete yyvsp[-1].TypeList; + yyval.TypeVal = MethodType::getMethodType(yyvsp[-3].TypeVal, Params); + ; + break;} +case 87: +#line 724 "llvmAsmParser.y" +{ // Method derived type? + MethodType::ParamTypes Params; // Empty list + yyval.TypeVal = MethodType::getMethodType(yyvsp[-2].TypeVal, Params); + ; + break;} +case 88: +#line 728 "llvmAsmParser.y" +{ + yyval.TypeVal = ArrayType::getArrayType(yyvsp[-1].TypeVal); + ; + break;} +case 89: +#line 731 "llvmAsmParser.y" +{ + yyval.TypeVal = ArrayType::getArrayType(yyvsp[-1].TypeVal, (int)yyvsp[-3].UInt64Val); + ; + break;} +case 90: +#line 734 "llvmAsmParser.y" +{ + StructType::ElementTypes Elements(yyvsp[-1].TypeList->begin(), yyvsp[-1].TypeList->end()); + delete yyvsp[-1].TypeList; + yyval.TypeVal = StructType::getStructType(Elements); + ; + break;} +case 91: +#line 739 "llvmAsmParser.y" +{ + yyval.TypeVal = StructType::getStructType(StructType::ElementTypes()); + ; + break;} +case 92: +#line 742 "llvmAsmParser.y" +{ + yyval.TypeVal = PointerType::getPointerType(yyvsp[-1].TypeVal); + ; + break;} +case 93: +#line 747 "llvmAsmParser.y" +{ + yyval.TypeList = new list(); + yyval.TypeList->push_back(yyvsp[0].TypeVal); + ; + break;} +case 94: +#line 751 "llvmAsmParser.y" +{ + (yyval.TypeList=yyvsp[-2].TypeList)->push_back(yyvsp[0].TypeVal); + ; + break;} +case 95: +#line 756 "llvmAsmParser.y" +{ + yyvsp[-1].MethodVal->getBasicBlocks().push_back(yyvsp[0].BasicBlockVal); + yyval.MethodVal = yyvsp[-1].MethodVal; + ; + break;} +case 96: +#line 760 "llvmAsmParser.y" +{ // Do not allow methods with 0 basic blocks + yyval.MethodVal = yyvsp[-1].MethodVal; // in them... + yyvsp[-1].MethodVal->getBasicBlocks().push_back(yyvsp[0].BasicBlockVal); + ; + break;} +case 97: +#line 769 "llvmAsmParser.y" +{ + yyvsp[-1].BasicBlockVal->getInstList().push_back(yyvsp[0].TermInstVal); + InsertValue(yyvsp[-1].BasicBlockVal); + yyval.BasicBlockVal = yyvsp[-1].BasicBlockVal; + ; + break;} +case 98: +#line 774 "llvmAsmParser.y" +{ + yyvsp[-1].BasicBlockVal->getInstList().push_back(yyvsp[0].TermInstVal); + yyvsp[-1].BasicBlockVal->setName(yyvsp[-2].StrVal); + free(yyvsp[-2].StrVal); // Free the strdup'd memory... + + InsertValue(yyvsp[-1].BasicBlockVal); + yyval.BasicBlockVal = yyvsp[-1].BasicBlockVal; + ; + break;} +case 99: +#line 783 "llvmAsmParser.y" +{ + yyvsp[-1].BasicBlockVal->getInstList().push_back(yyvsp[0].InstVal); + yyval.BasicBlockVal = yyvsp[-1].BasicBlockVal; + ; + break;} +case 100: +#line 787 "llvmAsmParser.y" +{ + yyval.BasicBlockVal = new BasicBlock(); + ; + break;} +case 101: +#line 791 "llvmAsmParser.y" +{ // Return with a result... + yyval.TermInstVal = new ReturnInst(getVal(yyvsp[-1].TypeVal, yyvsp[0].ValIDVal)); + ; + break;} +case 102: +#line 794 "llvmAsmParser.y" +{ // Return with no result... + yyval.TermInstVal = new ReturnInst(); + ; + break;} +case 103: +#line 797 "llvmAsmParser.y" +{ // Unconditional Branch... + yyval.TermInstVal = new BranchInst((BasicBlock*)getVal(Type::LabelTy, yyvsp[0].ValIDVal)); + ; + break;} +case 104: +#line 800 "llvmAsmParser.y" +{ + yyval.TermInstVal = new BranchInst((BasicBlock*)getVal(Type::LabelTy, yyvsp[-3].ValIDVal), + (BasicBlock*)getVal(Type::LabelTy, yyvsp[0].ValIDVal), + getVal(Type::BoolTy, yyvsp[-6].ValIDVal)); + ; + break;} +case 105: +#line 805 "llvmAsmParser.y" +{ + SwitchInst *S = new SwitchInst(getVal(yyvsp[-7].TypeVal, yyvsp[-6].ValIDVal), + (BasicBlock*)getVal(Type::LabelTy, yyvsp[-3].ValIDVal)); + yyval.TermInstVal = S; + + list >::iterator I = yyvsp[-1].JumpTable->begin(), + end = yyvsp[-1].JumpTable->end(); + for (; I != end; I++) + S->dest_push_back(I->first, I->second); + ; + break;} +case 106: +#line 816 "llvmAsmParser.y" +{ + yyval.JumpTable = yyvsp[-5].JumpTable; + ConstPoolVal *V = (ConstPoolVal*)getVal(yyvsp[-4].TypeVal, yyvsp[-3].ValIDVal, true); + if (V == 0) + ThrowException("May only switch on a constant pool value!"); + + yyval.JumpTable->push_back(make_pair(V, (BasicBlock*)getVal(yyvsp[-1].TypeVal, yyvsp[0].ValIDVal))); + ; + break;} +case 107: +#line 824 "llvmAsmParser.y" +{ + yyval.JumpTable = new list >(); + ConstPoolVal *V = (ConstPoolVal*)getVal(yyvsp[-4].TypeVal, yyvsp[-3].ValIDVal, true); + + if (V == 0) + ThrowException("May only switch on a constant pool value!"); + + yyval.JumpTable->push_back(make_pair(V, (BasicBlock*)getVal(yyvsp[-1].TypeVal, yyvsp[0].ValIDVal))); + ; + break;} +case 108: +#line 834 "llvmAsmParser.y" +{ + if (yyvsp[-1].StrVal) // Is this definition named?? + yyvsp[0].InstVal->setName(yyvsp[-1].StrVal); // if so, assign the name... + + InsertValue(yyvsp[0].InstVal); + yyval.InstVal = yyvsp[0].InstVal; +; + break;} +case 109: +#line 842 "llvmAsmParser.y" +{ // Used for PHI nodes and call statements... + yyval.ValueList = new list(); + yyval.ValueList->push_back(getVal(yyvsp[-1].TypeVal, yyvsp[0].ValIDVal)); + ; + break;} +case 110: +#line 846 "llvmAsmParser.y" +{ + yyval.ValueList = yyvsp[-2].ValueList; + yyvsp[-2].ValueList->push_back(getVal(yyvsp[-2].ValueList->front()->getType(), yyvsp[0].ValIDVal)); + ; + break;} +case 112: +#line 852 "llvmAsmParser.y" +{ yyval.ValueList = 0; ; + break;} +case 113: +#line 854 "llvmAsmParser.y" +{ + yyval.InstVal = Instruction::getBinaryOperator(yyvsp[-4].BinaryOpVal, getVal(yyvsp[-3].TypeVal, yyvsp[-2].ValIDVal), getVal(yyvsp[-3].TypeVal, yyvsp[0].ValIDVal)); + if (yyval.InstVal == 0) + ThrowException("binary operator returned null!"); + ; + break;} +case 114: +#line 859 "llvmAsmParser.y" +{ + yyval.InstVal = Instruction::getUnaryOperator(yyvsp[-2].UnaryOpVal, getVal(yyvsp[-1].TypeVal, yyvsp[0].ValIDVal)); + if (yyval.InstVal == 0) + ThrowException("unary operator returned null!"); + ; + break;} +case 115: +#line 864 "llvmAsmParser.y" +{ + yyval.InstVal = new PHINode(yyvsp[0].ValueList->front()->getType()); + while (yyvsp[0].ValueList->begin() != yyvsp[0].ValueList->end()) { + // TODO: Ensure all types are the same... + ((PHINode*)yyval.InstVal)->addIncoming(yyvsp[0].ValueList->front()); + yyvsp[0].ValueList->pop_front(); + } + delete yyvsp[0].ValueList; // Free the list... + ; + break;} +case 116: +#line 873 "llvmAsmParser.y" +{ + if (!yyvsp[-4].TypeVal->isMethodType()) + ThrowException("Can only call methods: invalid type '" + + yyvsp[-4].TypeVal->getName() + "'!"); + + const MethodType *Ty = (const MethodType*)yyvsp[-4].TypeVal; + + Value *V = getVal(Ty, yyvsp[-3].ValIDVal); + if (V->getValueType() != Value::MethodVal || V->getType() != Ty) + ThrowException("Cannot call: " + yyvsp[-3].ValIDVal.getName() + "!"); + + // Create or access a new type that corresponds to the function call... + vector Params; + + if (yyvsp[-1].ValueList) { + // Pull out just the arguments... + Params.insert(Params.begin(), yyvsp[-1].ValueList->begin(), yyvsp[-1].ValueList->end()); + delete yyvsp[-1].ValueList; + + // Loop through MethodType's arguments and ensure they are specified + // correctly! + // + MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin(); + unsigned i; + for (i = 0; i < Params.size() && I != Ty->getParamTypes().end(); ++i,++I){ + if (Params[i]->getType() != *I) + ThrowException("Parameter " + utostr(i) + " is not of type '" + + (*I)->getName() + "'!"); + } + + if (i != Params.size() || I != Ty->getParamTypes().end()) + ThrowException("Invalid number of parameters detected!"); + } + + // Create the call node... + yyval.InstVal = new CallInst((Method*)V, Params); + ; + break;} +case 117: +#line 910 "llvmAsmParser.y" +{ + yyval.InstVal = yyvsp[0].InstVal; + ; + break;} +case 118: +#line 914 "llvmAsmParser.y" +{ + ConstPoolVal *TyVal = new ConstPoolType(PointerType::getPointerType(yyvsp[0].TypeVal)); + TyVal = addConstValToConstantPool(TyVal); + yyval.InstVal = new MallocInst((ConstPoolType*)TyVal); + ; + break;} +case 119: +#line 919 "llvmAsmParser.y" +{ + if (!yyvsp[-3].TypeVal->isArrayType() || ((const ArrayType*)yyvsp[-3].TypeVal)->isSized()) + ThrowException("Trying to allocate " + yyvsp[-3].TypeVal->getName() + + " as unsized array!"); + + Value *ArrSize = getVal(yyvsp[-1].TypeVal, yyvsp[0].ValIDVal); + ConstPoolVal *TyVal = new ConstPoolType(PointerType::getPointerType(yyvsp[-3].TypeVal)); + TyVal = addConstValToConstantPool(TyVal); + yyval.InstVal = new MallocInst((ConstPoolType*)TyVal, ArrSize); + ; + break;} +case 120: +#line 929 "llvmAsmParser.y" +{ + ConstPoolVal *TyVal = new ConstPoolType(PointerType::getPointerType(yyvsp[0].TypeVal)); + TyVal = addConstValToConstantPool(TyVal); + yyval.InstVal = new AllocaInst((ConstPoolType*)TyVal); + ; + break;} +case 121: +#line 934 "llvmAsmParser.y" +{ + if (!yyvsp[-3].TypeVal->isArrayType() || ((const ArrayType*)yyvsp[-3].TypeVal)->isSized()) + ThrowException("Trying to allocate " + yyvsp[-3].TypeVal->getName() + + " as unsized array!"); + + Value *ArrSize = getVal(yyvsp[-1].TypeVal, yyvsp[0].ValIDVal); + ConstPoolVal *TyVal = new ConstPoolType(PointerType::getPointerType(yyvsp[-3].TypeVal)); + TyVal = addConstValToConstantPool(TyVal); + yyval.InstVal = new AllocaInst((ConstPoolType*)TyVal, ArrSize); + ; + break;} +case 122: +#line 944 "llvmAsmParser.y" +{ + if (!yyvsp[-1].TypeVal->isPointerType()) + ThrowException("Trying to free nonpointer type " + yyvsp[-1].TypeVal->getName() + "!"); + yyval.InstVal = new FreeInst(getVal(yyvsp[-1].TypeVal, yyvsp[0].ValIDVal)); + ; + break;} +} + /* the action file gets copied in in place of this dollarsign */ +#line 543 "/usr/dcs/software/supported/encap/bison-1.28/share/bison.simple" + + yyvsp -= yylen; + yyssp -= yylen; +#ifdef YYLSP_NEEDED + yylsp -= yylen; +#endif + +#if YYDEBUG != 0 + if (yydebug) + { + short *ssp1 = yyss - 1; + fprintf (stderr, "state stack now"); + while (ssp1 != yyssp) + fprintf (stderr, " %d", *++ssp1); + fprintf (stderr, "\n"); + } +#endif + + *++yyvsp = yyval; + +#ifdef YYLSP_NEEDED + yylsp++; + if (yylen == 0) + { + yylsp->first_line = yylloc.first_line; + yylsp->first_column = yylloc.first_column; + yylsp->last_line = (yylsp-1)->last_line; + yylsp->last_column = (yylsp-1)->last_column; + yylsp->text = 0; + } + else + { + yylsp->last_line = (yylsp+yylen-1)->last_line; + yylsp->last_column = (yylsp+yylen-1)->last_column; + } +#endif + + /* Now "shift" the result of the reduction. + Determine what state that goes to, + based on the state we popped back to + and the rule number reduced by. */ + + yyn = yyr1[yyn]; + + yystate = yypgoto[yyn - YYNTBASE] + *yyssp; + if (yystate >= 0 && yystate <= YYLAST && yycheck[yystate] == *yyssp) + yystate = yytable[yystate]; + else + yystate = yydefgoto[yyn - YYNTBASE]; + + goto yynewstate; + +yyerrlab: /* here on detecting error */ + + if (! yyerrstatus) + /* If not already recovering from an error, report this error. */ + { + ++yynerrs; + +#ifdef YYERROR_VERBOSE + yyn = yypact[yystate]; + + if (yyn > YYFLAG && yyn < YYLAST) + { + int size = 0; + char *msg; + int x, count; + + count = 0; + /* Start X at -yyn if nec to avoid negative indexes in yycheck. */ + for (x = (yyn < 0 ? -yyn : 0); + x < (sizeof(yytname) / sizeof(char *)); x++) + if (yycheck[x + yyn] == x) + size += strlen(yytname[x]) + 15, count++; + msg = (char *) malloc(size + 15); + if (msg != 0) + { + strcpy(msg, "parse error"); + + if (count < 5) + { + count = 0; + for (x = (yyn < 0 ? -yyn : 0); + x < (sizeof(yytname) / sizeof(char *)); x++) + if (yycheck[x + yyn] == x) + { + strcat(msg, count == 0 ? ", expecting `" : " or `"); + strcat(msg, yytname[x]); + strcat(msg, "'"); + count++; + } + } + yyerror(msg); + free(msg); + } + else + yyerror ("parse error; also virtual memory exceeded"); + } + else +#endif /* YYERROR_VERBOSE */ + yyerror("parse error"); + } + + goto yyerrlab1; +yyerrlab1: /* here on error raised explicitly by an action */ + + if (yyerrstatus == 3) + { + /* if just tried and failed to reuse lookahead token after an error, discard it. */ + + /* return failure if at end of input */ + if (yychar == YYEOF) + YYABORT; + +#if YYDEBUG != 0 + if (yydebug) + fprintf(stderr, "Discarding token %d (%s).\n", yychar, yytname[yychar1]); +#endif + + yychar = YYEMPTY; + } + + /* Else will try to reuse lookahead token + after shifting the error token. */ + + yyerrstatus = 3; /* Each real token shifted decrements this */ + + goto yyerrhandle; + +yyerrdefault: /* current state does not do anything special for the error token. */ + +#if 0 + /* This is wrong; only states that explicitly want error tokens + should shift them. */ + yyn = yydefact[yystate]; /* If its default is to accept any token, ok. Otherwise pop it.*/ + if (yyn) goto yydefault; +#endif + +yyerrpop: /* pop the current state because it cannot handle the error token */ + + if (yyssp == yyss) YYABORT; + yyvsp--; + yystate = *--yyssp; +#ifdef YYLSP_NEEDED + yylsp--; +#endif + +#if YYDEBUG != 0 + if (yydebug) + { + short *ssp1 = yyss - 1; + fprintf (stderr, "Error: state stack now"); + while (ssp1 != yyssp) + fprintf (stderr, " %d", *++ssp1); + fprintf (stderr, "\n"); + } +#endif + +yyerrhandle: + + yyn = yypact[yystate]; + if (yyn == YYFLAG) + goto yyerrdefault; + + yyn += YYTERROR; + if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != YYTERROR) + goto yyerrdefault; + + yyn = yytable[yyn]; + if (yyn < 0) + { + if (yyn == YYFLAG) + goto yyerrpop; + yyn = -yyn; + goto yyreduce; + } + else if (yyn == 0) + goto yyerrpop; + + if (yyn == YYFINAL) + YYACCEPT; + +#if YYDEBUG != 0 + if (yydebug) + fprintf(stderr, "Shifting error token, "); +#endif + + *++yyvsp = yylval; +#ifdef YYLSP_NEEDED + *++yylsp = yylloc; +#endif + + yystate = yyn; + goto yynewstate; + + yyacceptlab: + /* YYACCEPT comes here. */ + if (yyfree_stacks) + { + free (yyss); + free (yyvs); +#ifdef YYLSP_NEEDED + free (yyls); +#endif + } + return 0; + + yyabortlab: + /* YYABORT comes here. */ + if (yyfree_stacks) + { + free (yyss); + free (yyvs); +#ifdef YYLSP_NEEDED + free (yyls); +#endif + } + return 1; +} +#line 950 "llvmAsmParser.y" + +int yyerror(char *ErrorMsg) { + ThrowException(string("Parse error: ") + ErrorMsg); + return 0; +} diff --git a/lib/AsmParser/llvmAsmParser.h b/lib/AsmParser/llvmAsmParser.h new file mode 100644 index 0000000000..64e460f994 --- /dev/null +++ b/lib/AsmParser/llvmAsmParser.h @@ -0,0 +1,87 @@ +typedef union { + Module *ModuleVal; + Method *MethodVal; + MethodArgument *MethArgVal; + BasicBlock *BasicBlockVal; + TerminatorInst *TermInstVal; + Instruction *InstVal; + ConstPoolVal *ConstVal; + const Type *TypeVal; + + list *MethodArgList; + list *ValueList; + list *TypeList; + list > *JumpTable; + vector *ConstVector; + + int64_t SInt64Val; + uint64_t UInt64Val; + int SIntVal; + unsigned UIntVal; + + char *StrVal; // This memory is allocated by strdup! + ValID ValIDVal; // May contain memory allocated by strdup + + Instruction::UnaryOps UnaryOpVal; + Instruction::BinaryOps BinaryOpVal; + Instruction::TermOps TermOpVal; + Instruction::MemoryOps MemOpVal; +} YYSTYPE; +#define ESINT64VAL 257 +#define EUINT64VAL 258 +#define SINTVAL 259 +#define UINTVAL 260 +#define VOID 261 +#define BOOL 262 +#define SBYTE 263 +#define UBYTE 264 +#define SHORT 265 +#define USHORT 266 +#define INT 267 +#define UINT 268 +#define LONG 269 +#define ULONG 270 +#define FLOAT 271 +#define DOUBLE 272 +#define STRING 273 +#define TYPE 274 +#define LABEL 275 +#define VAR_ID 276 +#define LABELSTR 277 +#define STRINGCONSTANT 278 +#define IMPLEMENTATION 279 +#define TRUE 280 +#define FALSE 281 +#define BEGINTOK 282 +#define END 283 +#define DECLARE 284 +#define PHI 285 +#define CALL 286 +#define RET 287 +#define BR 288 +#define SWITCH 289 +#define NEG 290 +#define NOT 291 +#define TOINT 292 +#define TOUINT 293 +#define ADD 294 +#define SUB 295 +#define MUL 296 +#define DIV 297 +#define REM 298 +#define SETLE 299 +#define SETGE 300 +#define SETLT 301 +#define SETGT 302 +#define SETEQ 303 +#define SETNE 304 +#define MALLOC 305 +#define ALLOCA 306 +#define FREE 307 +#define LOAD 308 +#define STORE 309 +#define GETFIELD 310 +#define PUTFIELD 311 + + +extern YYSTYPE llvmAsmlval; diff --git a/lib/AsmParser/llvmAsmParser.y b/lib/AsmParser/llvmAsmParser.y new file mode 100644 index 0000000000..01709ca5de --- /dev/null +++ b/lib/AsmParser/llvmAsmParser.y @@ -0,0 +1,954 @@ +//===-- llvmAsmParser.y - Parser for llvm assembly files ---------*- C++ -*--=// +// +// This file implements the bison parser for LLVM assembly languages files. +// +//===------------------------------------------------------------------------=// + +// +// TODO: Parse comments and add them to an internal node... so that they may +// be saved in the bytecode format as well as everything else. Very important +// for a general IR format. +// + +%{ +#include "ParserInternals.h" +#include "llvm/BasicBlock.h" +#include "llvm/Method.h" +#include "llvm/SymbolTable.h" +#include "llvm/Module.h" +#include "llvm/Type.h" +#include "llvm/DerivedTypes.h" +#include "llvm/Assembly/Parser.h" +#include "llvm/ConstantPool.h" +#include "llvm/iTerminators.h" +#include "llvm/iMemory.h" +#include +#include // Get definition of pair class +#include // This embarasment is due to our flex lexer... + +int yyerror(char *ErrorMsg); // Forward declarations to prevent "implicit +int yylex(); // declaration" of xxx warnings. +int yyparse(); + +static Module *ParserResult; +const ToolCommandLine *CurOptions = 0; + +// This contains info used when building the body of a method. It is destroyed +// when the method is completed. +// +typedef vector ValueList; // Numbered defs +static void ResolveDefinitions(vector &LateResolvers); + +static struct PerModuleInfo { + Module *CurrentModule; + vector Values; // Module level numbered definitions + vector LateResolveValues; + + void ModuleDone() { + // If we could not resolve some blocks at parsing time (forward branches) + // resolve the branches now... + ResolveDefinitions(LateResolveValues); + + Values.clear(); // Clear out method local definitions + CurrentModule = 0; + } +} CurModule; + +static struct PerMethodInfo { + Method *CurrentMethod; // Pointer to current method being created + + vector Values; // Keep track of numbered definitions + vector LateResolveValues; + + inline PerMethodInfo() { + CurrentMethod = 0; + } + + inline ~PerMethodInfo() {} + + inline void MethodStart(Method *M) { + CurrentMethod = M; + } + + void MethodDone() { + // If we could not resolve some blocks at parsing time (forward branches) + // resolve the branches now... + ResolveDefinitions(LateResolveValues); + + Values.clear(); // Clear out method local definitions + CurrentMethod = 0; + } +} CurMeth; // Info for the current method... + + +//===----------------------------------------------------------------------===// +// Code to handle definitions of all the types +//===----------------------------------------------------------------------===// + +static void InsertValue(Value *D, vector &ValueTab = CurMeth.Values) { + if (!D->hasName()) { // Is this a numbered definition? + unsigned type = D->getType()->getUniqueID(); + if (ValueTab.size() <= type) + ValueTab.resize(type+1, ValueList()); + //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D); + ValueTab[type].push_back(D); + } +} + +static Value *getVal(const Type *Type, ValID &D, + bool DoNotImprovise = false) { + switch (D.Type) { + case 0: { // Is it a numbered definition? + unsigned type = Type->getUniqueID(); + unsigned Num = (unsigned)D.Num; + + // Module constants occupy the lowest numbered slots... + if (type < CurModule.Values.size()) { + if (Num < CurModule.Values[type].size()) + return CurModule.Values[type][Num]; + + Num -= CurModule.Values[type].size(); + } + + // Make sure that our type is within bounds + if (CurMeth.Values.size() <= type) + break; + + // Check that the number is within bounds... + if (CurMeth.Values[type].size() <= Num) + break; + + return CurMeth.Values[type][Num]; + } + case 1: { // Is it a named definition? + string Name(D.Name); + SymbolTable *SymTab = 0; + if (CurMeth.CurrentMethod) + SymTab = CurMeth.CurrentMethod->getSymbolTable(); + Value *N = SymTab ? SymTab->lookup(Type, Name) : 0; + + if (N == 0) { + SymTab = CurModule.CurrentModule->getSymbolTable(); + if (SymTab) + N = SymTab->lookup(Type, Name); + if (N == 0) break; + } + + D.destroy(); // Free old strdup'd memory... + return N; + } + + case 2: // Is it a constant pool reference?? + case 3: // Is it an unsigned const pool reference? + case 4:{ // Is it a string const pool reference? + ConstPoolVal *CPV = 0; + + // Check to make sure that "Type" is an integral type, and that our + // value will fit into the specified type... + switch (D.Type) { + case 2: + if (Type == Type::BoolTy) { // Special handling for boolean data + CPV = new ConstPoolBool(D.ConstPool64 != 0); + } else { + if (!ConstPoolSInt::isValueValidForType(Type, D.ConstPool64)) + ThrowException("Symbolic constant pool reference is invalid!"); + CPV = new ConstPoolSInt(Type, D.ConstPool64); + } + break; + case 3: + if (!ConstPoolUInt::isValueValidForType(Type, D.UConstPool64)) { + if (!ConstPoolSInt::isValueValidForType(Type, D.ConstPool64)) { + ThrowException("Symbolic constant pool reference is invalid!"); + } else { // This is really a signed reference. Transmogrify. + CPV = new ConstPoolSInt(Type, D.ConstPool64); + } + } else { + CPV = new ConstPoolUInt(Type, D.UConstPool64); + } + break; + case 4: + cerr << "FIXME: TODO: String constants [sbyte] not implemented yet!\n"; + abort(); + //CPV = new ConstPoolString(D.Name); + D.destroy(); // Free the string memory + break; + } + assert(CPV && "How did we escape creating a constant??"); + + // Scan through the constant table and see if we already have loaded this + // constant. + // + ConstantPool &CP = CurMeth.CurrentMethod ? + CurMeth.CurrentMethod->getConstantPool() : + CurModule.CurrentModule->getConstantPool(); + ConstPoolVal *C = CP.find(CPV); // Already have this constant? + if (C) { + delete CPV; // Didn't need this after all, oh well. + return C; // Yup, we already have one, recycle it! + } + CP.insert(CPV); + + // Success, everything is kosher. Lets go! + return CPV; + } // End of case 2,3,4 + } // End of switch + + + // If we reached here, we referenced either a symbol that we don't know about + // or an id number that hasn't been read yet. We may be referencing something + // forward, so just create an entry to be resolved later and get to it... + // + if (DoNotImprovise) return 0; // Do we just want a null to be returned? + + // TODO: Attempt to coallecse nodes that are the same with previous ones. + Value *d = 0; + switch (Type->getPrimitiveID()) { + case Type::LabelTyID: d = new BBPlaceHolder(Type, D); break; + case Type::MethodTyID: + d = new MethPlaceHolder(Type, D); + InsertValue(d, CurModule.LateResolveValues); + return d; +//case Type::ClassTyID: d = new ClassPlaceHolder(Type, D); break; + default: d = new DefPlaceHolder(Type, D); break; + } + + assert(d != 0 && "How did we not make something?"); + InsertValue(d, CurMeth.LateResolveValues); + return d; +} + + +//===----------------------------------------------------------------------===// +// Code to handle forward references in instructions +//===----------------------------------------------------------------------===// +// +// This code handles the late binding needed with statements that reference +// values not defined yet... for example, a forward branch, or the PHI node for +// a loop body. +// +// This keeps a table (CurMeth.LateResolveValues) of all such forward references +// and back patchs after we are done. +// + +// ResolveDefinitions - If we could not resolve some defs at parsing +// time (forward branches, phi functions for loops, etc...) resolve the +// defs now... +// +static void ResolveDefinitions(vector &LateResolvers) { + // Loop over LateResolveDefs fixing up stuff that couldn't be resolved + for (unsigned ty = 0; ty < LateResolvers.size(); ty++) { + while (!LateResolvers[ty].empty()) { + Value *V = LateResolvers[ty].back(); + LateResolvers[ty].pop_back(); + ValID &DID = getValIDFromPlaceHolder(V); + + Value *TheRealValue = getVal(Type::getUniqueIDType(ty), DID, true); + + if (TheRealValue == 0 && DID.Type == 1) + ThrowException("Reference to an invalid definition: '" +DID.getName() + + "' of type '" + V->getType()->getName() + "'"); + else if (TheRealValue == 0) + ThrowException("Reference to an invalid definition: #" +itostr(DID.Num)+ + " of type '" + V->getType()->getName() + "'"); + + V->replaceAllUsesWith(TheRealValue); + assert(V->use_empty()); + delete V; + } + } + + LateResolvers.clear(); +} + +// addConstValToConstantPool - This code is used to insert a constant into the +// current constant pool. This is designed to make maximal (but not more than +// possible) reuse (merging) of constants in the constant pool. This means that +// multiple references to %4, for example will all get merged. +// +static ConstPoolVal *addConstValToConstantPool(ConstPoolVal *C) { + vector &ValTab = CurMeth.CurrentMethod ? + CurMeth.Values : CurModule.Values; + ConstantPool &CP = CurMeth.CurrentMethod ? + CurMeth.CurrentMethod->getConstantPool() : + CurModule.CurrentModule->getConstantPool(); + + if (ConstPoolVal *CPV = CP.find(C)) { + // Constant already in constant pool. Try to merge the two constants + if (CPV->hasName() && !C->hasName()) { + // Merge the two values, we inherit the existing CPV's name. + // InsertValue requires that the value have no name to insert correctly + // (because we want to fill the slot this constant would have filled) + // + string Name = CPV->getName(); + CPV->setName(""); + InsertValue(CPV, ValTab); + CPV->setName(Name); + delete C; + return CPV; + } else if (!CPV->hasName() && C->hasName()) { + // If we have a name on this value and there isn't one in the const + // pool val already, propogate it. + // + CPV->setName(C->getName()); + delete C; // Sorry, you're toast + return CPV; + } else if (CPV->hasName() && C->hasName()) { + // Both values have distinct names. We cannot merge them. + CP.insert(C); + InsertValue(C, ValTab); + return C; + } else if (!CPV->hasName() && !C->hasName()) { + // Neither value has a name, trivially merge them. + InsertValue(CPV, ValTab); + delete C; + return CPV; + } + + assert(0 && "Not reached!"); + return 0; + } else { // No duplication of value. + CP.insert(C); + InsertValue(C, ValTab); + return C; + } +} + +//===----------------------------------------------------------------------===// +// RunVMAsmParser - Define an interface to this parser +//===----------------------------------------------------------------------===// +// +Module *RunVMAsmParser(const ToolCommandLine &Opts, FILE *F) { + llvmAsmin = F; + CurOptions = &Opts; + llvmAsmlineno = 1; // Reset the current line number... + + CurModule.CurrentModule = new Module(); // Allocate a new module to read + yyparse(); // Parse the file. + Module *Result = ParserResult; + CurOptions = 0; + llvmAsmin = stdin; // F is about to go away, don't use it anymore... + ParserResult = 0; + + return Result; +} + +%} + +%union { + Module *ModuleVal; + Method *MethodVal; + MethodArgument *MethArgVal; + BasicBlock *BasicBlockVal; + TerminatorInst *TermInstVal; + Instruction *InstVal; + ConstPoolVal *ConstVal; + const Type *TypeVal; + + list *MethodArgList; + list *ValueList; + list *TypeList; + list > *JumpTable; + vector *ConstVector; + + int64_t SInt64Val; + uint64_t UInt64Val; + int SIntVal; + unsigned UIntVal; + + char *StrVal; // This memory is allocated by strdup! + ValID ValIDVal; // May contain memory allocated by strdup + + Instruction::UnaryOps UnaryOpVal; + Instruction::BinaryOps BinaryOpVal; + Instruction::TermOps TermOpVal; + Instruction::MemoryOps MemOpVal; +} + +%type Module MethodList +%type Method MethodHeader BasicBlockList +%type BasicBlock InstructionList +%type BBTerminatorInst +%type Inst InstVal MemoryInst +%type ConstVal +%type ConstVector +%type ArgList ArgListH +%type ArgVal +%type ValueRefList ValueRefListE +%type TypeList +%type JumpTable + +%type ValueRef ConstValueRef // Reference to a definition or BB + +// Tokens and types for handling constant integer values +// +// ESINT64VAL - A negative number within long long range +%token ESINT64VAL + +// EUINT64VAL - A positive number within uns. long long range +%token EUINT64VAL +%type EINT64VAL + +%token SINTVAL // Signed 32 bit ints... +%token UINTVAL // Unsigned 32 bit ints... +%type INTVAL + +// Built in types... +%type Types TypesV SIntType UIntType IntType +%token VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG +%token FLOAT DOUBLE STRING TYPE LABEL + +%token VAR_ID LABELSTR STRINGCONSTANT +%type OptVAR_ID OptAssign + + +%token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE +%token PHI CALL + +// Basic Block Terminating Operators +%token RET BR SWITCH + +// Unary Operators +%type UnaryOps // all the unary operators +%token NEG NOT + +// Unary Conversion Operators +%token TOINT TOUINT + +// Binary Operators +%type BinaryOps // all the binary operators +%token ADD SUB MUL DIV REM + +// Binary Comarators +%token SETLE SETGE SETLT SETGT SETEQ SETNE + +// Memory Instructions +%token MALLOC ALLOCA FREE LOAD STORE GETFIELD PUTFIELD + +%start Module +%% + +// Handle constant integer size restriction and conversion... +// + +INTVAL : SINTVAL +INTVAL : UINTVAL { + if ($1 > (uint32_t)INT32_MAX) // Outside of my range! + ThrowException("Value too large for type!"); + $$ = (int32_t)$1; +} + + +EINT64VAL : ESINT64VAL // These have same type and can't cause problems... +EINT64VAL : EUINT64VAL { + if ($1 > (uint64_t)INT64_MAX) // Outside of my range! + ThrowException("Value too large for type!"); + $$ = (int64_t)$1; +} + +// Types includes all predefined types... except void, because you can't do +// anything with it except for certain specific things... +// +// User defined types are added latter... +// +Types : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT +Types : LONG | ULONG | FLOAT | DOUBLE | STRING | TYPE | LABEL + +// TypesV includes all of 'Types', but it also includes the void type. +TypesV : Types | VOID + +// Operations that are notably excluded from this list include: +// RET, BR, & SWITCH because they end basic blocks and are treated specially. +// +UnaryOps : NEG | NOT | TOINT | TOUINT +BinaryOps : ADD | SUB | MUL | DIV | REM +BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE + +// Valueine some types that allow classification if we only want a particular +// thing... +SIntType : LONG | INT | SHORT | SBYTE +UIntType : ULONG | UINT | USHORT | UBYTE +IntType : SIntType | UIntType + +OptAssign : VAR_ID '=' { + $$ = $1; + } + | /*empty*/ { + $$ = 0; + } + +ConstVal : SIntType EINT64VAL { // integral constants + if (!ConstPoolSInt::isValueValidForType($1, $2)) + ThrowException("Constant value doesn't fit in type!"); + $$ = new ConstPoolSInt($1, $2); + } + | UIntType EUINT64VAL { // integral constants + if (!ConstPoolUInt::isValueValidForType($1, $2)) + ThrowException("Constant value doesn't fit in type!"); + $$ = new ConstPoolUInt($1, $2); + } + | BOOL TRUE { // Boolean constants + $$ = new ConstPoolBool(true); + } + | BOOL FALSE { // Boolean constants + $$ = new ConstPoolBool(false); + } + | STRING STRINGCONSTANT { // String constants + cerr << "FIXME: TODO: String constants [sbyte] not implemented yet!\n"; + abort(); + //$$ = new ConstPoolString($2); + free($2); + } + | TYPE Types { // Type constants + $$ = new ConstPoolType($2); + } + | '[' Types ']' '[' ConstVector ']' { // Nonempty array constant + // Verify all elements are correct type! + const ArrayType *AT = ArrayType::getArrayType($2); + for (unsigned i = 0; i < $5->size(); i++) { + if ($2 != (*$5)[i]->getType()) + ThrowException("Element #" + utostr(i) + " is not of type '" + + $2->getName() + "' as required!\nIt is of type '" + + (*$5)[i]->getType()->getName() + "'."); + } + + $$ = new ConstPoolArray(AT, *$5); + delete $5; + } + | '[' Types ']' '[' ']' { // Empty array constant + vector Empty; + $$ = new ConstPoolArray(ArrayType::getArrayType($2), Empty); + } + | '[' EUINT64VAL 'x' Types ']' '[' ConstVector ']' { + // Verify all elements are correct type! + const ArrayType *AT = ArrayType::getArrayType($4, (int)$2); + if ($2 != $7->size()) + ThrowException("Type mismatch: constant sized array initialized with " + + utostr($7->size()) + " arguments, but has size of " + + itostr((int)$2) + "!"); + + for (unsigned i = 0; i < $7->size(); i++) { + if ($4 != (*$7)[i]->getType()) + ThrowException("Element #" + utostr(i) + " is not of type '" + + $4->getName() + "' as required!\nIt is of type '" + + (*$7)[i]->getType()->getName() + "'."); + } + + $$ = new ConstPoolArray(AT, *$7); + delete $7; + } + | '[' EUINT64VAL 'x' Types ']' '[' ']' { + if ($2 != 0) + ThrowException("Type mismatch: constant sized array initialized with 0" + " arguments, but has size of " + itostr((int)$2) + "!"); + vector Empty; + $$ = new ConstPoolArray(ArrayType::getArrayType($4, 0), Empty); + } + | '{' TypeList '}' '{' ConstVector '}' { + StructType::ElementTypes Types($2->begin(), $2->end()); + delete $2; + + const StructType *St = StructType::getStructType(Types); + $$ = new ConstPoolStruct(St, *$5); + delete $5; + } + | '{' '}' '{' '}' { + const StructType *St = + StructType::getStructType(StructType::ElementTypes()); + vector Empty; + $$ = new ConstPoolStruct(St, Empty); + } +/* + | Types '*' ConstVal { + assert(0); + $$ = 0; + } +*/ + + +ConstVector : ConstVector ',' ConstVal { + ($$ = $1)->push_back(addConstValToConstantPool($3)); + } + | ConstVal { + $$ = new vector(); + $$->push_back(addConstValToConstantPool($1)); + } + + +ConstPool : ConstPool OptAssign ConstVal { + if ($2) { + $3->setName($2); + free($2); + } + + addConstValToConstantPool($3); + } + | /* empty: end of list */ { + } + + +//===----------------------------------------------------------------------===// +// Rules to match Modules +//===----------------------------------------------------------------------===// + +// Module rule: Capture the result of parsing the whole file into a result +// variable... +// +Module : MethodList { + $$ = ParserResult = $1; + CurModule.ModuleDone(); +} + +MethodList : MethodList Method { + $1->getMethodList().push_back($2); + CurMeth.MethodDone(); + $$ = $1; + } + | ConstPool IMPLEMENTATION { + $$ = CurModule.CurrentModule; + } + + +//===----------------------------------------------------------------------===// +// Rules to match Method Headers +//===----------------------------------------------------------------------===// + +OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; } + +ArgVal : Types OptVAR_ID { + $$ = new MethodArgument($1); + if ($2) { // Was the argument named? + $$->setName($2); + free($2); // The string was strdup'd, so free it now. + } +} + +ArgListH : ArgVal ',' ArgListH { + $$ = $3; + $3->push_front($1); + } + | ArgVal { + $$ = new list(); + $$->push_front($1); + } + +ArgList : ArgListH { + $$ = $1; + } + | /* empty */ { + $$ = 0; + } + +MethodHeaderH : TypesV STRINGCONSTANT '(' ArgList ')' { + MethodType::ParamTypes ParamTypeList; + if ($4) + for (list::iterator I = $4->begin(); I != $4->end(); I++) + ParamTypeList.push_back((*I)->getType()); + + const MethodType *MT = MethodType::getMethodType($1, ParamTypeList); + + Method *M = new Method(MT, $2); + free($2); // Free strdup'd memory! + + InsertValue(M, CurModule.Values); + + CurMeth.MethodStart(M); + + // Add all of the arguments we parsed to the method... + if ($4) { // Is null if empty... + Method::ArgumentListType &ArgList = M->getArgumentList(); + + for (list::iterator I = $4->begin(); I != $4->end(); I++) { + InsertValue(*I); + ArgList.push_back(*I); + } + delete $4; // We're now done with the argument list + } +} + +MethodHeader : MethodHeaderH ConstPool BEGINTOK { + $$ = CurMeth.CurrentMethod; +} + +Method : BasicBlockList END { + $$ = $1; +} + + +//===----------------------------------------------------------------------===// +// Rules to match Basic Blocks +//===----------------------------------------------------------------------===// + +ConstValueRef : ESINT64VAL { // A reference to a direct constant + $$ = ValID::create($1); + } + | EUINT64VAL { + $$ = ValID::create($1); + } + | TRUE { + $$ = ValID::create((int64_t)1); + } + | FALSE { + $$ = ValID::create((int64_t)0); + } + | STRINGCONSTANT { // Quoted strings work too... especially for methods + $$ = ValID::create_conststr($1); + } + +// ValueRef - A reference to a definition... +ValueRef : INTVAL { // Is it an integer reference...? + $$ = ValID::create($1); + } + | VAR_ID { // It must be a named reference then... + $$ = ValID::create($1); + } + | ConstValueRef { + $$ = $1; + } + +// The user may refer to a user defined type by its typeplane... check for this +// now... +// +Types : ValueRef { + Value *D = getVal(Type::TypeTy, $1, true); + if (D == 0) ThrowException("Invalid user defined type: " + $1.getName()); + assert (D->getValueType() == Value::ConstantVal && + "Internal error! User defined type not in const pool!"); + ConstPoolType *CPT = (ConstPoolType*)D; + $$ = CPT->getValue(); + } + | TypesV '(' TypeList ')' { // Method derived type? + MethodType::ParamTypes Params($3->begin(), $3->end()); + delete $3; + $$ = MethodType::getMethodType($1, Params); + } + | TypesV '(' ')' { // Method derived type? + MethodType::ParamTypes Params; // Empty list + $$ = MethodType::getMethodType($1, Params); + } + | '[' Types ']' { + $$ = ArrayType::getArrayType($2); + } + | '[' EUINT64VAL 'x' Types ']' { + $$ = ArrayType::getArrayType($4, (int)$2); + } + | '{' TypeList '}' { + StructType::ElementTypes Elements($2->begin(), $2->end()); + delete $2; + $$ = StructType::getStructType(Elements); + } + | '{' '}' { + $$ = StructType::getStructType(StructType::ElementTypes()); + } + | Types '*' { + $$ = PointerType::getPointerType($1); + } + + +TypeList : Types { + $$ = new list(); + $$->push_back($1); + } + | TypeList ',' Types { + ($$=$1)->push_back($3); + } + + +BasicBlockList : BasicBlockList BasicBlock { + $1->getBasicBlocks().push_back($2); + $$ = $1; + } + | MethodHeader BasicBlock { // Do not allow methods with 0 basic blocks + $$ = $1; // in them... + $1->getBasicBlocks().push_back($2); + } + + +// Basic blocks are terminated by branching instructions: +// br, br/cc, switch, ret +// +BasicBlock : InstructionList BBTerminatorInst { + $1->getInstList().push_back($2); + InsertValue($1); + $$ = $1; + } + | LABELSTR InstructionList BBTerminatorInst { + $2->getInstList().push_back($3); + $2->setName($1); + free($1); // Free the strdup'd memory... + + InsertValue($2); + $$ = $2; + } + +InstructionList : InstructionList Inst { + $1->getInstList().push_back($2); + $$ = $1; + } + | /* empty */ { + $$ = new BasicBlock(); + } + +BBTerminatorInst : RET Types ValueRef { // Return with a result... + $$ = new ReturnInst(getVal($2, $3)); + } + | RET VOID { // Return with no result... + $$ = new ReturnInst(); + } + | BR LABEL ValueRef { // Unconditional Branch... + $$ = new BranchInst((BasicBlock*)getVal(Type::LabelTy, $3)); + } // Conditional Branch... + | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef { + $$ = new BranchInst((BasicBlock*)getVal(Type::LabelTy, $6), + (BasicBlock*)getVal(Type::LabelTy, $9), + getVal(Type::BoolTy, $3)); + } + | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' { + SwitchInst *S = new SwitchInst(getVal($2, $3), + (BasicBlock*)getVal(Type::LabelTy, $6)); + $$ = S; + + list >::iterator I = $8->begin(), + end = $8->end(); + for (; I != end; I++) + S->dest_push_back(I->first, I->second); + } + +JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef { + $$ = $1; + ConstPoolVal *V = (ConstPoolVal*)getVal($2, $3, true); + if (V == 0) + ThrowException("May only switch on a constant pool value!"); + + $$->push_back(make_pair(V, (BasicBlock*)getVal($5, $6))); + } + | IntType ConstValueRef ',' LABEL ValueRef { + $$ = new list >(); + ConstPoolVal *V = (ConstPoolVal*)getVal($1, $2, true); + + if (V == 0) + ThrowException("May only switch on a constant pool value!"); + + $$->push_back(make_pair(V, (BasicBlock*)getVal($4, $5))); + } + +Inst : OptAssign InstVal { + if ($1) // Is this definition named?? + $2->setName($1); // if so, assign the name... + + InsertValue($2); + $$ = $2; +} + +ValueRefList : Types ValueRef { // Used for PHI nodes and call statements... + $$ = new list(); + $$->push_back(getVal($1, $2)); + } + | ValueRefList ',' ValueRef { + $$ = $1; + $1->push_back(getVal($1->front()->getType(), $3)); + } + +// ValueRefListE - Just like ValueRefList, except that it may also be empty! +ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; } + +InstVal : BinaryOps Types ValueRef ',' ValueRef { + $$ = Instruction::getBinaryOperator($1, getVal($2, $3), getVal($2, $5)); + if ($$ == 0) + ThrowException("binary operator returned null!"); + } + | UnaryOps Types ValueRef { + $$ = Instruction::getUnaryOperator($1, getVal($2, $3)); + if ($$ == 0) + ThrowException("unary operator returned null!"); + } + | PHI ValueRefList { + $$ = new PHINode($2->front()->getType()); + while ($2->begin() != $2->end()) { + // TODO: Ensure all types are the same... + ((PHINode*)$$)->addIncoming($2->front()); + $2->pop_front(); + } + delete $2; // Free the list... + } + | CALL Types ValueRef '(' ValueRefListE ')' { + if (!$2->isMethodType()) + ThrowException("Can only call methods: invalid type '" + + $2->getName() + "'!"); + + const MethodType *Ty = (const MethodType*)$2; + + Value *V = getVal(Ty, $3); + if (V->getValueType() != Value::MethodVal || V->getType() != Ty) + ThrowException("Cannot call: " + $3.getName() + "!"); + + // Create or access a new type that corresponds to the function call... + vector Params; + + if ($5) { + // Pull out just the arguments... + Params.insert(Params.begin(), $5->begin(), $5->end()); + delete $5; + + // Loop through MethodType's arguments and ensure they are specified + // correctly! + // + MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin(); + unsigned i; + for (i = 0; i < Params.size() && I != Ty->getParamTypes().end(); ++i,++I){ + if (Params[i]->getType() != *I) + ThrowException("Parameter " + utostr(i) + " is not of type '" + + (*I)->getName() + "'!"); + } + + if (i != Params.size() || I != Ty->getParamTypes().end()) + ThrowException("Invalid number of parameters detected!"); + } + + // Create the call node... + $$ = new CallInst((Method*)V, Params); + } + | MemoryInst { + $$ = $1; + } + +MemoryInst : MALLOC Types { + ConstPoolVal *TyVal = new ConstPoolType(PointerType::getPointerType($2)); + TyVal = addConstValToConstantPool(TyVal); + $$ = new MallocInst((ConstPoolType*)TyVal); + } + | MALLOC Types ',' UINT ValueRef { + if (!$2->isArrayType() || ((const ArrayType*)$2)->isSized()) + ThrowException("Trying to allocate " + $2->getName() + + " as unsized array!"); + + Value *ArrSize = getVal($4, $5); + ConstPoolVal *TyVal = new ConstPoolType(PointerType::getPointerType($2)); + TyVal = addConstValToConstantPool(TyVal); + $$ = new MallocInst((ConstPoolType*)TyVal, ArrSize); + } + | ALLOCA Types { + ConstPoolVal *TyVal = new ConstPoolType(PointerType::getPointerType($2)); + TyVal = addConstValToConstantPool(TyVal); + $$ = new AllocaInst((ConstPoolType*)TyVal); + } + | ALLOCA Types ',' UINT ValueRef { + if (!$2->isArrayType() || ((const ArrayType*)$2)->isSized()) + ThrowException("Trying to allocate " + $2->getName() + + " as unsized array!"); + + Value *ArrSize = getVal($4, $5); + ConstPoolVal *TyVal = new ConstPoolType(PointerType::getPointerType($2)); + TyVal = addConstValToConstantPool(TyVal); + $$ = new AllocaInst((ConstPoolType*)TyVal, ArrSize); + } + | FREE Types ValueRef { + if (!$2->isPointerType()) + ThrowException("Trying to free nonpointer type " + $2->getName() + "!"); + $$ = new FreeInst(getVal($2, $3)); + } + +%% +int yyerror(char *ErrorMsg) { + ThrowException(string("Parse error: ") + ErrorMsg); + return 0; +} diff --git a/lib/Bytecode/Makefile b/lib/Bytecode/Makefile new file mode 100644 index 0000000000..75d4f7cd10 --- /dev/null +++ b/lib/Bytecode/Makefile @@ -0,0 +1,5 @@ +LEVEL = ../.. +DIRS = Reader Writer + +include $(LEVEL)/Makefile.common + diff --git a/lib/Bytecode/Reader/ConstantReader.cpp b/lib/Bytecode/Reader/ConstantReader.cpp new file mode 100644 index 0000000000..b85bd887ef --- /dev/null +++ b/lib/Bytecode/Reader/ConstantReader.cpp @@ -0,0 +1,218 @@ +//===- ReadConst.cpp - Code to constants and constant pools -----------------=== +// +// This file implements functionality to deserialize constants and entire +// constant pools. +// +// Note that this library should be as fast as possible, reentrant, and +// threadsafe!! +// +//===------------------------------------------------------------------------=== + +#include "llvm/Module.h" +#include "llvm/BasicBlock.h" +#include "llvm/ConstPoolVals.h" +#include "llvm/DerivedTypes.h" +#include "ReaderInternals.h" + +bool BytecodeParser::parseTypeConstant(const uchar *&Buf, const uchar *EndBuf, + ConstPoolVal *&V) { + const Type *Val = 0; + + unsigned PrimType; + if (read_vbr(Buf, EndBuf, PrimType)) return true; + + if ((Val = Type::getPrimitiveType((Type::PrimitiveID)PrimType))) { + V = new ConstPoolType(Val); // It's just a primitive ID. + return false; + } + + switch (PrimType) { + case Type::MethodTyID: { + unsigned Typ; + if (read_vbr(Buf, EndBuf, Typ)) return true; + const Type *RetType = getType(Typ); + if (RetType == 0) return true; + + MethodType::ParamTypes Params; + + if (read_vbr(Buf, EndBuf, Typ)) return true; + while (Typ) { + const Type *Ty = getType(Typ); + if (Ty == 0) return true; + Params.push_back(Ty); + + if (read_vbr(Buf, EndBuf, Typ)) return true; + } + + Val = MethodType::getMethodType(RetType, Params); + break; + } + case Type::ArrayTyID: { + unsigned ElTyp; + if (read_vbr(Buf, EndBuf, ElTyp)) return true; + const Type *ElementType = getType(ElTyp); + if (ElementType == 0) return true; + + int NumElements; + if (read_vbr(Buf, EndBuf, NumElements)) return true; + Val = ArrayType::getArrayType(ElementType, NumElements); + break; + } + case Type::StructTyID: { + unsigned Typ; + StructType::ElementTypes Elements; + + if (read_vbr(Buf, EndBuf, Typ)) return true; + while (Typ) { // List is terminated by void/0 typeid + const Type *Ty = getType(Typ); + if (Ty == 0) return true; + Elements.push_back(Ty); + + if (read_vbr(Buf, EndBuf, Typ)) return true; + } + + Val = StructType::getStructType(Elements); + break; + } + case Type::PointerTyID: { + unsigned ElTyp; + if (read_vbr(Buf, EndBuf, ElTyp)) return true; + const Type *ElementType = getType(ElTyp); + if (ElementType == 0) return true; + Val = PointerType::getPointerType(ElementType); + break; + } + + default: + cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to deserialize" + << " primitive Type " << PrimType << "\n"; + return true; + } + + V = new ConstPoolType(Val); + return false; +} + +bool BytecodeParser::parseConstPoolValue(const uchar *&Buf, + const uchar *EndBuf, + const Type *Ty, ConstPoolVal *&V) { + switch (Ty->getPrimitiveID()) { + case Type::BoolTyID: { + unsigned Val; + if (read_vbr(Buf, EndBuf, Val)) return true; + if (Val != 0 && Val != 1) return true; + V = new ConstPoolBool(Val == 1); + break; + } + + case Type::UByteTyID: // Unsigned integer types... + case Type::UShortTyID: + case Type::UIntTyID: { + unsigned Val; + if (read_vbr(Buf, EndBuf, Val)) return true; + if (!ConstPoolUInt::isValueValidForType(Ty, Val)) return true; + V = new ConstPoolUInt(Ty, Val); + break; + } + + case Type::ULongTyID: { + uint64_t Val; + if (read_vbr(Buf, EndBuf, Val)) return true; + V = new ConstPoolUInt(Ty, Val); + break; + } + + case Type::SByteTyID: // Unsigned integer types... + case Type::ShortTyID: + case Type::IntTyID: { + int Val; + if (read_vbr(Buf, EndBuf, Val)) return true; + if (!ConstPoolSInt::isValueValidForType(Ty, Val)) return 0; + V = new ConstPoolSInt(Ty, Val); + break; + } + + case Type::LongTyID: { + int64_t Val; + if (read_vbr(Buf, EndBuf, Val)) return true; + V = new ConstPoolSInt(Ty, Val); + break; + } + + case Type::TypeTyID: + if (parseTypeConstant(Buf, EndBuf, V)) return true; + break; + + case Type::ArrayTyID: { + const ArrayType *AT = (const ArrayType*)Ty; + unsigned NumElements; + if (AT->isSized()) // Sized array, # elements stored in type! + NumElements = (unsigned)AT->getNumElements(); + else // Unsized array, # elements stored in stream! + if (read_vbr(Buf, EndBuf, NumElements)) return true; + + vector Elements; + while (NumElements--) { // Read all of the elements of the constant. + unsigned Slot; + if (read_vbr(Buf, EndBuf, Slot)) return true; + Value *V = getValue(AT->getElementType(), Slot, false); + if (!V || V->getValueType() != Value::ConstantVal) + return true; + Elements.push_back((ConstPoolVal*)V); + } + V = new ConstPoolArray(AT, Elements); + break; + } + + case Type::StructTyID: { + const StructType *ST = (const StructType*)Ty; + const StructType::ElementTypes &ET = ST->getElementTypes(); + + vector Elements; + for (unsigned i = 0; i < ET.size(); ++i) { + unsigned Slot; + if (read_vbr(Buf, EndBuf, Slot)) return true; + Value *V = getValue(ET[i], Slot, false); + if (!V || V->getValueType() != Value::ConstantVal) + return true; + Elements.push_back((ConstPoolVal*)V); + } + + V = new ConstPoolStruct(ST, Elements); + break; + } + + default: + cerr << __FILE__ << ":" << __LINE__ + << ": Don't know how to deserialize constant value of type '" + << Ty->getName() << "'\n"; + return true; + } + return false; +} + +bool BytecodeParser::ParseConstantPool(const uchar *&Buf, const uchar *EndBuf, + SymTabValue::ConstantPoolType &CP, + ValueTable &Tab) { + while (Buf < EndBuf) { + unsigned NumEntries, Typ; + + if (read_vbr(Buf, EndBuf, NumEntries) || + read_vbr(Buf, EndBuf, Typ)) return true; + const Type *Ty = getType(Typ); + if (Ty == 0) return true; + + for (unsigned i = 0; i < NumEntries; i++) { + ConstPoolVal *I; + if (parseConstPoolValue(Buf, EndBuf, Ty, I)) return true; +#if 0 + cerr << " Read const value: <" << I->getType()->getName() + << ">: " << I->getStrValue() << endl; +#endif + insertValue(I, Tab); + CP.insert(I); + } + } + + return Buf > EndBuf; +} diff --git a/lib/Bytecode/Reader/InstructionReader.cpp b/lib/Bytecode/Reader/InstructionReader.cpp new file mode 100644 index 0000000000..667e144673 --- /dev/null +++ b/lib/Bytecode/Reader/InstructionReader.cpp @@ -0,0 +1,213 @@ +//===- ReadInst.cpp - Code to read an instruction from bytecode -------------=== +// +// This file defines the mechanism to read an instruction from a bytecode +// stream. +// +// Note that this library should be as fast as possible, reentrant, and +// threadsafe!! +// +// TODO: Change from getValue(Raw.Arg1) etc, to getArg(Raw, 1) +// Make it check type, so that casts are checked. +// +//===------------------------------------------------------------------------=== + +#include "llvm/iOther.h" +#include "llvm/iTerminators.h" +#include "llvm/iMemory.h" +#include "llvm/DerivedTypes.h" +#include "ReaderInternals.h" + +bool BytecodeParser::ParseRawInst(const uchar *&Buf, const uchar *EndBuf, + RawInst &Result) { + unsigned Op, Typ; + if (read(Buf, EndBuf, Op)) return true; + + Result.NumOperands = Op >> 30; + Result.Opcode = (Op >> 24) & 63; + + switch (Result.NumOperands) { + case 1: + Result.Ty = getType((Op >> 12) & 4095); + Result.Arg1 = Op & 4095; + if (Result.Arg1 == 4095) // Handle special encoding for 0 operands... + Result.NumOperands = 0; + break; + case 2: + Result.Ty = getType((Op >> 16) & 255); + Result.Arg1 = (Op >> 8 ) & 255; + Result.Arg2 = (Op >> 0 ) & 255; + break; + case 3: + Result.Ty = getType((Op >> 18) & 63); + Result.Arg1 = (Op >> 12) & 63; + Result.Arg2 = (Op >> 6 ) & 63; + Result.Arg3 = (Op >> 0 ) & 63; + break; + case 0: + Buf -= 4; // Hrm, try this again... + if (read_vbr(Buf, EndBuf, Result.Opcode)) return true; + if (read_vbr(Buf, EndBuf, Typ)) return true; + Result.Ty = getType(Typ); + if (read_vbr(Buf, EndBuf, Result.NumOperands)) return true; + + switch (Result.NumOperands) { + case 0: + cerr << "Zero Arg instr found!\n"; + return true; // This encoding is invalid! + case 1: + if (read_vbr(Buf, EndBuf, Result.Arg1)) return true; + break; + case 2: + if (read_vbr(Buf, EndBuf, Result.Arg1) || + read_vbr(Buf, EndBuf, Result.Arg2)) return true; + break; + case 3: + if (read_vbr(Buf, EndBuf, Result.Arg1) || + read_vbr(Buf, EndBuf, Result.Arg2) || + read_vbr(Buf, EndBuf, Result.Arg3)) return true; + break; + default: + if (read_vbr(Buf, EndBuf, Result.Arg1) || + read_vbr(Buf, EndBuf, Result.Arg2)) return true; + + // Allocate a vector to hold arguments 3, 4, 5, 6 ... + Result.VarArgs = new vector(Result.NumOperands-2); + for (unsigned a = 0; a < Result.NumOperands-2; a++) + if (read_vbr(Buf, EndBuf, (*Result.VarArgs)[a])) return true; + break; + } + if (align32(Buf, EndBuf)) return true; + break; + } + + //cerr << "NO: " << Result.NumOperands << " opcode: " << Result.Opcode + // << " Ty: " << Result.Ty->getName() << " arg1: " << Result.Arg1 << endl; + return false; +} + + +bool BytecodeParser::ParseInstruction(const uchar *&Buf, const uchar *EndBuf, + Instruction *&Res) { + RawInst Raw; + if (ParseRawInst(Buf, EndBuf, Raw)) return true;; + + if (Raw.Opcode >= Instruction::FirstUnaryOp && + Raw.Opcode < Instruction::NumUnaryOps && Raw.NumOperands == 1) { + Res = Instruction::getUnaryOperator(Raw.Opcode, getValue(Raw.Ty, Raw.Arg1)); + return false; + } else if (Raw.Opcode >= Instruction::FirstBinaryOp && + Raw.Opcode < Instruction::NumBinaryOps && Raw.NumOperands == 2) { + Res = Instruction::getBinaryOperator(Raw.Opcode, getValue(Raw.Ty, Raw.Arg1), + getValue(Raw.Ty, Raw.Arg2)); + return false; + } else if (Raw.Opcode == Instruction::PHINode) { + PHINode *PN = new PHINode(Raw.Ty); + switch (Raw.NumOperands) { + case 0: cerr << "Invalid phi node encountered!\n"; + delete PN; + return true; + case 1: PN->addIncoming(getValue(Raw.Ty, Raw.Arg1)); break; + case 2: PN->addIncoming(getValue(Raw.Ty, Raw.Arg1)); + PN->addIncoming(getValue(Raw.Ty, Raw.Arg2)); break; + case 3: PN->addIncoming(getValue(Raw.Ty, Raw.Arg1)); + PN->addIncoming(getValue(Raw.Ty, Raw.Arg2)); + PN->addIncoming(getValue(Raw.Ty, Raw.Arg3)); break; + default: + PN->addIncoming(getValue(Raw.Ty, Raw.Arg1)); + PN->addIncoming(getValue(Raw.Ty, Raw.Arg2)); + { + vector &args = *Raw.VarArgs; + for (unsigned i = 0; i < args.size(); i++) + PN->addIncoming(getValue(Raw.Ty, args[i])); + } + delete Raw.VarArgs; + } + Res = PN; + return false; + } else if (Raw.Opcode == Instruction::Ret) { + if (Raw.NumOperands == 0) { + Res = new ReturnInst(); return false; + } else if (Raw.NumOperands == 1) { + Res = new ReturnInst(getValue(Raw.Ty, Raw.Arg1)); return false; + } + } else if (Raw.Opcode == Instruction::Br) { + if (Raw.NumOperands == 1) { + Res = new BranchInst((BasicBlock*)getValue(Type::LabelTy, Raw.Arg1)); + return false; + } else if (Raw.NumOperands == 3) { + Res = new BranchInst((BasicBlock*)getValue(Type::LabelTy, Raw.Arg1), + (BasicBlock*)getValue(Type::LabelTy, Raw.Arg2), + getValue(Type::BoolTy , Raw.Arg3)); + return false; + } + } else if (Raw.Opcode == Instruction::Switch) { + SwitchInst *I = + new SwitchInst(getValue(Raw.Ty, Raw.Arg1), + (BasicBlock*)getValue(Type::LabelTy, Raw.Arg2)); + Res = I; + if (Raw.NumOperands < 3) return false; // No destinations? Wierd. + + if (Raw.NumOperands == 3 || Raw.VarArgs->size() & 1) { + cerr << "Switch statement with odd number of arguments!\n"; + delete I; + return true; + } + + vector &args = *Raw.VarArgs; + for (unsigned i = 0; i < args.size(); i += 2) + I->dest_push_back((ConstPoolVal*)getValue(Raw.Ty, args[i]), + (BasicBlock*)getValue(Type::LabelTy, args[i+1])); + + delete Raw.VarArgs; + return false; + } else if (Raw.Opcode == Instruction::Call) { + Method *M = (Method*)getValue(Raw.Ty, Raw.Arg1); + if (M == 0) return true; + + const MethodType::ParamTypes &PL = M->getMethodType()->getParamTypes(); + MethodType::ParamTypes::const_iterator It = PL.begin(); + + vector Params; + switch (Raw.NumOperands) { + case 0: cerr << "Invalid call instruction encountered!\n"; + return true; + case 1: break; + case 2: Params.push_back(getValue(*It++, Raw.Arg2)); break; + case 3: Params.push_back(getValue(*It++, Raw.Arg2)); + if (It == PL.end()) return true; + Params.push_back(getValue(*It++, Raw.Arg3)); break; + default: + Params.push_back(getValue(*It++, Raw.Arg2)); + { + vector &args = *Raw.VarArgs; + for (unsigned i = 0; i < args.size(); i++) { + if (It == PL.end()) return true; + Params.push_back(getValue(*It++, args[i])); + } + } + delete Raw.VarArgs; + } + if (It != PL.end()) return true; + + Res = new CallInst(M, Params); + return false; + } else if (Raw.Opcode == Instruction::Malloc) { + if (Raw.NumOperands > 2) return true; + Value *Sz = (Raw.NumOperands == 2) ? getValue(Type::UIntTy, Raw.Arg2) : 0; + Res = new MallocInst((ConstPoolType*)getValue(Type::TypeTy, Raw.Arg1), Sz); + return false; + } else if (Raw.Opcode == Instruction::Alloca) { + if (Raw.NumOperands > 2) return true; + Value *Sz = (Raw.NumOperands == 2) ? getValue(Type::UIntTy, Raw.Arg2) : 0; + Res = new AllocaInst((ConstPoolType*)getValue(Type::TypeTy, Raw.Arg1), Sz); + return false; + } else if (Raw.Opcode == Instruction::Free) { + Value *Val = getValue(Raw.Ty, Raw.Arg1); + if (!Val->getType()->isPointerType()) return true; + Res = new FreeInst(Val); + return false; + } + + cerr << "Unrecognized instruction! " << Raw.Opcode << endl; + return true; +} diff --git a/lib/Bytecode/Reader/Makefile b/lib/Bytecode/Reader/Makefile new file mode 100644 index 0000000000..2c79d15104 --- /dev/null +++ b/lib/Bytecode/Reader/Makefile @@ -0,0 +1,7 @@ + +LEVEL = ../../.. + +LIBRARYNAME = bcreader + +include $(LEVEL)/Makefile.common + diff --git a/lib/Bytecode/Reader/Reader.cpp b/lib/Bytecode/Reader/Reader.cpp new file mode 100644 index 0000000000..c3f4c907fe --- /dev/null +++ b/lib/Bytecode/Reader/Reader.cpp @@ -0,0 +1,478 @@ +//===- Reader.cpp - Code to read bytecode files -----------------------------=== +// +// This library implements the functionality defined in llvm/Bytecode/Reader.h +// +// Note that this library should be as fast as possible, reentrant, and +// threadsafe!! +// +// TODO: Make error message outputs be configurable depending on an option? +// TODO: Allow passing in an option to ignore the symbol table +// +//===------------------------------------------------------------------------=== + +#include "llvm/Bytecode/Reader.h" +#include "llvm/Bytecode/Format.h" +#include "llvm/Module.h" +#include "llvm/BasicBlock.h" +#include "llvm/DerivedTypes.h" +#include "llvm/ConstPoolVals.h" +#include "llvm/iOther.h" +#include "ReaderInternals.h" +#include +#include +#include +#include +#include +#include + +bool BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) { + if (Ty->isPrimitiveType()) { + Slot = Ty->getPrimitiveID(); + } else { + TypeMapType::iterator I = TypeMap.find(Ty); + if (I == TypeMap.end()) return true; // Didn't find type! + Slot = I->second; + } + //cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << endl; + return false; +} + +const Type *BytecodeParser::getType(unsigned ID) { + const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID); + if (T) return T; + + //cerr << "Looking up Type ID: " << ID << endl; + + const Value *D = getValue(Type::TypeTy, ID, false); + if (D == 0) return 0; + + assert(D->getType() == Type::TypeTy && + D->getValueType() == Value::ConstantVal); + + + return ((const ConstPoolType*)D)->getValue();; +} + +bool BytecodeParser::insertValue(Value *Def, vector &ValueTab) { + unsigned type; + if (getTypeSlot(Def->getType(), type)) return true; + + if (ValueTab.size() <= type) + ValueTab.resize(type+1, ValueList()); + + //cerr << "insertValue Values[" << type << "][" << ValueTab[type].size() + // << "] = " << Def << endl; + + if (type == Type::TypeTyID && Def->getValueType() == Value::ConstantVal) { + const Type *Ty = ((const ConstPoolType*)Def)->getValue(); + unsigned ValueOffset = FirstDerivedTyID; + + if (&ValueTab == &Values) // Take into consideration module level types + ValueOffset += ModuleValues[type].size(); + + if (TypeMap.find(Ty) == TypeMap.end()) + TypeMap[Ty] = ValueTab[type].size()+ValueOffset; + } + + ValueTab[type].push_back(Def); + + return false; +} + +Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) { + unsigned Num = oNum; + unsigned type; // The type plane it lives in... + + if (getTypeSlot(Ty, type)) return 0; // TODO: true + + if (type == Type::TypeTyID) { // The 'type' plane has implicit values + const Type *T = Type::getPrimitiveType((Type::PrimitiveID)Num); + if (T) return (Value*)T; // Asked for a primitive type... + + // Otherwise, derived types need offset... + Num -= FirstDerivedTyID; + } + + if (ModuleValues.size() > type) { + if (ModuleValues[type].size() > Num) + return ModuleValues[type][Num]; + Num -= ModuleValues[type].size(); + } + + if (Values.size() > type && Values[type].size() > Num) + return Values[type][Num]; + + if (!Create) return 0; // Do not create a placeholder? + + Value *d = 0; + switch (Ty->getPrimitiveID()) { + case Type::LabelTyID: d = new BBPHolder(Ty, oNum); break; + case Type::MethodTyID: + cerr << "Creating method pholder! : " << type << ":" << oNum << " " + << Ty->getName() << endl; + d = new MethPHolder(Ty, oNum); + insertValue(d, LateResolveModuleValues); + return d; + default: d = new DefPHolder(Ty, oNum); break; + } + + assert(d != 0 && "How did we not make something?"); + if (insertValue(d, LateResolveValues)) return 0; + return d; +} + +bool BytecodeParser::postResolveValues(ValueTable &ValTab) { + bool Error = false; + for (unsigned ty = 0; ty < ValTab.size(); ty++) { + ValueList &DL = ValTab[ty]; + unsigned Size; + while ((Size = DL.size())) { + unsigned IDNumber = getValueIDNumberFromPlaceHolder(DL[Size-1]); + + Value *D = DL[Size-1]; + DL.pop_back(); + + Value *NewDef = getValue(D->getType(), IDNumber, false); + if (NewDef == 0) { + Error = true; // Unresolved thinger + cerr << "Unresolvable reference found: <" << D->getType()->getName() + << ">:" << IDNumber << "!\n"; + } else { + // Fixup all of the uses of this placeholder def... + D->replaceAllUsesWith(NewDef); + + // Now that all the uses are gone, delete the placeholder... + // If we couldn't find a def (error case), then leak a little + delete D; // memory, 'cause otherwise we can't remove all uses! + } + } + } + + return Error; +} + +bool BytecodeParser::ParseBasicBlock(const uchar *&Buf, const uchar *EndBuf, + BasicBlock *&BB) { + BB = new BasicBlock(); + + while (Buf < EndBuf) { + Instruction *Def; + if (ParseInstruction(Buf, EndBuf, Def)) { + delete BB; + return true; + } + + if (Def == 0) { delete BB; return true; } + if (insertValue(Def, Values)) { delete BB; return true; } + + BB->getInstList().push_back(Def); + } + + return false; +} + +bool BytecodeParser::ParseSymbolTable(const uchar *&Buf, const uchar *EndBuf) { + while (Buf < EndBuf) { + // Symtab block header: [num entries][type id number] + unsigned NumEntries, Typ; + if (read_vbr(Buf, EndBuf, NumEntries) || + read_vbr(Buf, EndBuf, Typ)) return true; + const Type *Ty = getType(Typ); + if (Ty == 0) return true; + + for (unsigned i = 0; i < NumEntries; i++) { + // Symtab entry: [def slot #][name] + unsigned slot; + if (read_vbr(Buf, EndBuf, slot)) return true; + string Name; + if (read(Buf, EndBuf, Name, false)) // Not aligned... + return true; + + Value *D = getValue(Ty, slot, false); // Find mapping... + if (D == 0) return true; + D->setName(Name); + } + } + + return Buf > EndBuf; +} + + +bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf, + Module *C) { + // Clear out the local values table... + Values.clear(); + if (MethodSignatureList.empty()) return true; // Unexpected method! + + const MethodType *MTy = MethodSignatureList.front().first; + unsigned MethSlot = MethodSignatureList.front().second; + MethodSignatureList.pop_front(); + Method *M = new Method(MTy); + + const MethodType::ParamTypes &Params = MTy->getParamTypes(); + for (MethodType::ParamTypes::const_iterator It = Params.begin(); + It != Params.end(); It++) { + MethodArgument *MA = new MethodArgument(*It); + if (insertValue(MA, Values)) { delete M; return true; } + M->getArgumentList().push_back(MA); + } + + while (Buf < EndBuf) { + unsigned Type, Size; + const uchar *OldBuf = Buf; + if (readBlock(Buf, EndBuf, Type, Size)) { delete M; return true; } + + switch (Type) { + case BytecodeFormat::ConstantPool: + if (ParseConstantPool(Buf, Buf+Size, M->getConstantPool(), Values)) { + cerr << "Error reading constant pool!\n"; + delete M; return true; + } + break; + + case BytecodeFormat::BasicBlock: { + BasicBlock *BB; + if (ParseBasicBlock(Buf, Buf+Size, BB) || + insertValue(BB, Values)) { + cerr << "Error parsing basic block!\n"; + delete M; return true; // Parse error... :( + } + + M->getBasicBlocks().push_back(BB); + break; + } + + case BytecodeFormat::SymbolTable: + if (ParseSymbolTable(Buf, Buf+Size)) { + cerr << "Error reading method symbol table!\n"; + delete M; return true; + } + break; + + default: + Buf += Size; + if (OldBuf > Buf) return true; // Wrap around! + break; + } + if (align32(Buf, EndBuf)) { + delete M; // Malformed bc file, read past end of block. + return true; + } + } + + if (postResolveValues(LateResolveValues) || + postResolveValues(LateResolveModuleValues)) { + delete M; return true; // Unresolvable references! + } + + Value *MethPHolder = getValue(MTy, MethSlot, false); + assert(MethPHolder && "Something is broken no placeholder found!"); + assert(MethPHolder->getValueType() == Value::MethodVal && "Not a method?"); + + unsigned type; // Type slot + assert(!getTypeSlot(MTy, type) && "How can meth type not exist?"); + getTypeSlot(MTy, type); + + C->getMethodList().push_back(M); + + // Replace placeholder with the real method pointer... + ModuleValues[type][MethSlot] = M; + + // If anyone is using the placeholder make them use the real method instead + MethPHolder->replaceAllUsesWith(M); + + // We don't need the placeholder anymore! + delete MethPHolder; + + return false; +} + +bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End, + Module *C) { + + if (!MethodSignatureList.empty()) return true; // Two ModuleGlobal blocks? + + // Read the method signatures for all of the methods that are coming, and + // create fillers in the Value tables. + unsigned MethSignature; + if (read_vbr(Buf, End, MethSignature)) return true; + while (MethSignature != Type::VoidTyID) { // List is terminated by Void + const Type *Ty = getType(MethSignature); + if (!Ty || !Ty->isMethodType()) { + cerr << "Method not meth type! "; + if (Ty) cerr << Ty->getName(); else cerr << MethSignature; cerr << endl; + return true; + } + + // When the ModuleGlobalInfo section is read, we load the type of each method + // and the 'ModuleValues' slot that it lands in. We then load a placeholder + // into its slot to reserve it. When the method is loaded, this placeholder + // is replaced. + + // Insert the placeholder... + Value *Def = new MethPHolder(Ty, 0); + insertValue(Def, ModuleValues); + + // Figure out which entry of its typeslot it went into... + unsigned TypeSlot; + if (getTypeSlot(Def->getType(), TypeSlot)) return true; + + unsigned SlotNo = ModuleValues[TypeSlot].size()-1; + + // Keep track of this information in a linked list that is emptied as + // methods are loaded... + // + MethodSignatureList.push_back(make_pair((const MethodType*)Ty, SlotNo)); + if (read_vbr(Buf, End, MethSignature)) return true; + } + + if (align32(Buf, End)) return true; + + // This is for future proofing... in the future extra fields may be added that + // we don't understand, so we transparently ignore them. + // + Buf = End; + return false; +} + +bool BytecodeParser::ParseModule(const uchar *Buf, const uchar *EndBuf, + Module *&C) { + + unsigned Type, Size; + if (readBlock(Buf, EndBuf, Type, Size)) return true; + if (Type != BytecodeFormat::Module || Buf+Size != EndBuf) + return true; // Hrm, not a class? + + MethodSignatureList.clear(); // Just in case... + + // Read into instance variables... + if (read_vbr(Buf, EndBuf, FirstDerivedTyID)) return true; + if (align32(Buf, EndBuf)) return true; + + C = new Module(); + + while (Buf < EndBuf) { + const uchar *OldBuf = Buf; + if (readBlock(Buf, EndBuf, Type, Size)) { delete C; return true; } + switch (Type) { + case BytecodeFormat::ModuleGlobalInfo: + if (ParseModuleGlobalInfo(Buf, Buf+Size, C)) { + cerr << "Error reading class global info section!\n"; + delete C; return true; + } + break; + + case BytecodeFormat::ConstantPool: + if (ParseConstantPool(Buf, Buf+Size, C->getConstantPool(), ModuleValues)) { + cerr << "Error reading class constant pool!\n"; + delete C; return true; + } + break; + + case BytecodeFormat::Method: { + if (ParseMethod(Buf, Buf+Size, C)) { + delete C; return true; // Error parsing method + } + break; + } + + case BytecodeFormat::SymbolTable: + if (ParseSymbolTable(Buf, Buf+Size)) { + cerr << "Error reading class symbol table!\n"; + delete C; return true; + } + break; + + default: + cerr << "Unknown class block: " << Type << endl; + Buf += Size; + if (OldBuf > Buf) return true; // Wrap around! + break; + } + if (align32(Buf, EndBuf)) { delete C; return true; } + } + + if (!MethodSignatureList.empty()) // Expected more methods! + return true; + return false; +} + +Module *BytecodeParser::ParseBytecode(const uchar *Buf, const uchar *EndBuf) { + LateResolveValues.clear(); + unsigned Sig; + // Read and check signature... + if (read(Buf, EndBuf, Sig) || + Sig != ('l' | ('l' << 8) | ('v' << 16) | 'm' << 24)) + return 0; // Invalid signature! + + Module *Result; + if (ParseModule(Buf, EndBuf, Result)) return 0; + return Result; +} + + +Module *ParseBytecodeBuffer(const uchar *Buffer, unsigned Length) { + BytecodeParser Parser; + return Parser.ParseBytecode(Buffer, Buffer+Length); +} + +// Parse and return a class file... +// +Module *ParseBytecodeFile(const string &Filename) { + struct stat StatBuf; + Module *Result = 0; + + if (Filename != string("-")) { // Read from a file... + int FD = open(Filename.data(), O_RDONLY); + if (FD == -1) return 0; + + if (fstat(FD, &StatBuf) == -1) { close(FD); return 0; } + + int Length = StatBuf.st_size; + if (Length == 0) { close(FD); return 0; } + uchar *Buffer = (uchar*)mmap(0, Length, PROT_READ, + MAP_PRIVATE, FD, 0); + if (Buffer == (uchar*)-1) { close(FD); return 0; } + + BytecodeParser Parser; + Result = Parser.ParseBytecode(Buffer, Buffer+Length); + + munmap((char*)Buffer, Length); + close(FD); + } else { // Read from stdin + size_t FileSize = 0; + int BlockSize; + uchar Buffer[4096], *FileData = 0; + while ((BlockSize = read(0, Buffer, 4))) { + if (BlockSize == -1) { free(FileData); return 0; } + + FileData = (uchar*)realloc(FileData, FileSize+BlockSize); + memcpy(FileData+FileSize, Buffer, BlockSize); + FileSize += BlockSize; + } + + if (FileSize == 0) { free(FileData); return 0; } + +#define ALIGN_PTRS 1 +#if ALIGN_PTRS + uchar *Buf = (uchar*)mmap(0, FileSize, PROT_READ|PROT_WRITE, + MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); + assert((Buf != (uchar*)-1) && "mmap returned error!"); + free(FileData); + memcpy(Buf, FileData, FileSize); +#else + uchar *Buf = FileData; +#endif + + BytecodeParser Parser; + Result = Parser.ParseBytecode(Buf, Buf+FileSize); + +#if ALIGN_PTRS + munmap((char*)Buf, FileSize); // Free mmap'd data area +#else + free(FileData); // Free realloc'd block of memory +#endif + } + + return Result; +} diff --git a/lib/Bytecode/Reader/ReaderInternals.h b/lib/Bytecode/Reader/ReaderInternals.h new file mode 100644 index 0000000000..3bb0472674 --- /dev/null +++ b/lib/Bytecode/Reader/ReaderInternals.h @@ -0,0 +1,146 @@ +//===-- ReaderInternals.h - Definitions internal to the reader ---*- C++ -*--=// +// +// This header file defines various stuff that is used by the bytecode reader. +// +//===----------------------------------------------------------------------===// + +#ifndef READER_INTERNALS_H +#define READER_INTERNALS_H + +#include "llvm/Bytecode/Primitives.h" +#include "llvm/SymTabValue.h" +#include "llvm/Method.h" +#include "llvm/Instruction.h" +#include +#include + +class BasicBlock; +class Method; +class Module; +class Type; + +typedef unsigned char uchar; + +struct RawInst { // The raw fields out of the bytecode stream... + unsigned NumOperands; + unsigned Opcode; + const Type *Ty; + unsigned Arg1, Arg2; + union { + unsigned Arg3; + vector *VarArgs; // Contains arg #3,4,5... if NumOperands > 3 + }; +}; + +class BytecodeParser { +public: + BytecodeParser() { + // Define this in case we don't see a ModuleGlobalInfo block. + FirstDerivedTyID = Type::FirstDerivedTyID; + } + + Module *ParseBytecode(const uchar *Buf, const uchar *EndBuf); +private: // All of this data is transient across calls to ParseBytecode + typedef vector ValueList; + typedef vector ValueTable; + typedef map TypeMapType; + ValueTable Values, LateResolveValues; + ValueTable ModuleValues, LateResolveModuleValues; + TypeMapType TypeMap; + + // Information read from the ModuleGlobalInfo section of the file... + unsigned FirstDerivedTyID; + + // When the ModuleGlobalInfo section is read, we load the type of each method + // and the 'ModuleValues' slot that it lands in. We then load a placeholder + // into its slot to reserve it. When the method is loaded, this placeholder + // is replaced. + // + list > MethodSignatureList; + +private: + bool ParseModule (const uchar * Buf, const uchar *End, Module *&); + bool ParseModuleGlobalInfo (const uchar *&Buf, const uchar *End, Module *); + bool ParseSymbolTable (const uchar *&Buf, const uchar *End); + bool ParseMethod (const uchar *&Buf, const uchar *End, Module *); + bool ParseBasicBlock (const uchar *&Buf, const uchar *End, BasicBlock *&); + bool ParseInstruction (const uchar *&Buf, const uchar *End, Instruction *&); + bool ParseRawInst (const uchar *&Buf, const uchar *End, RawInst &); + + bool ParseConstantPool(const uchar *&Buf, const uchar *EndBuf, + SymTabValue::ConstantPoolType &CP, ValueTable &Tab); + + + bool parseConstPoolValue(const uchar *&Buf, const uchar *End, + const Type *Ty, ConstPoolVal *&V); + bool parseTypeConstant (const uchar *&Buf, const uchar *, ConstPoolVal *&); + + Value *getValue(const Type *Ty, unsigned num, bool Create = true); + const Type *getType(unsigned ID); + + bool insertValue(Value *D, vector &D); + bool postResolveValues(ValueTable &ValTab); + + bool getTypeSlot(const Type *Ty, unsigned &Slot); +}; + +template +class PlaceholderDef : public SuperType { + unsigned ID; +public: + PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {} + unsigned getID() { return ID; } +}; + +struct InstPlaceHolderHelper : public Instruction { + InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {} + inline virtual void dropAllReferences() {} + virtual string getOpcode() const { return "placeholder"; } + + virtual Instruction *clone() const { abort(); return 0; } + + // No "operands"... + virtual Value *getOperand(unsigned i) { return 0; } + virtual const Value *getOperand(unsigned i) const { return 0; } + virtual bool setOperand(unsigned i, Value *Val) { return false; } + virtual unsigned getNumOperands() const { return 0; } +}; + +struct BBPlaceHolderHelper : public BasicBlock { + BBPlaceHolderHelper(const Type *Ty) : BasicBlock() { + assert(Ty->isLabelType()); + } +}; + +struct MethPlaceHolderHelper : public Method { + MethPlaceHolderHelper(const Type *Ty) + : Method((const MethodType*)Ty) { + assert(Ty->isMethodType() && "Method placeholders must be method types!"); + } +}; + +typedef PlaceholderDef DefPHolder; +typedef PlaceholderDef BBPHolder; +typedef PlaceholderDef MethPHolder; + +static inline unsigned getValueIDNumberFromPlaceHolder(Value *Def) { + switch (Def->getType()->getPrimitiveID()) { + case Type::LabelTyID: return ((BBPHolder*)Def)->getID(); + case Type::MethodTyID: return ((MethPHolder*)Def)->getID(); + default: return ((DefPHolder*)Def)->getID(); + } +} + +static inline bool readBlock(const uchar *&Buf, const uchar *EndBuf, + unsigned &Type, unsigned &Size) { +#if DEBUG_OUTPUT + bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size); + cerr << "StartLoc = " << ((unsigned)Buf & 4095) + << " Type = " << Type << " Size = " << Size << endl; + return Result; +#else + return read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size); +#endif +} + +#endif diff --git a/lib/Bytecode/Writer/ConstantWriter.cpp b/lib/Bytecode/Writer/ConstantWriter.cpp new file mode 100644 index 0000000000..e0504a5b38 --- /dev/null +++ b/lib/Bytecode/Writer/ConstantWriter.cpp @@ -0,0 +1,154 @@ +//===-- WriteConst.cpp - Functions for writing constants ---------*- C++ -*--=// +// +// This file implements the routines for encoding constants to a bytecode +// stream. +// +// Note that the performance of this library is not terribly important, because +// it shouldn't be used by JIT type applications... so it is not a huge focus +// at least. :) +// +//===----------------------------------------------------------------------===// + +#include "WriterInternals.h" +#include "llvm/ConstPoolVals.h" +#include "llvm/SymbolTable.h" +#include "llvm/DerivedTypes.h" + +void BytecodeWriter::outputType(const Type *T) { + output_vbr((unsigned)T->getPrimitiveID(), Out); + + // That's all there is to handling primitive types... + if (T->isPrimitiveType()) + return; // We might do this if we alias a prim type: %x = type int + + switch (T->getPrimitiveID()) { // Handle derived types now. + case Type::MethodTyID: { + const MethodType *MT = (const MethodType*)T; + int Slot = Table.getValSlot(MT->getReturnType()); + assert(Slot != -1 && "Type used but not available!!"); + output_vbr((unsigned)Slot, Out); + + // Output all of the arguments... + MethodType::ParamTypes::const_iterator I = MT->getParamTypes().begin(); + for (; I != MT->getParamTypes().end(); I++) { + Slot = Table.getValSlot(*I); + assert(Slot != -1 && "Type used but not available!!"); + output_vbr((unsigned)Slot, Out); + } + + // Terminate list with VoidTy + output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out); + break; + } + + case Type::ArrayTyID: { + const ArrayType *AT = (const ArrayType*)T; + int Slot = Table.getValSlot(AT->getElementType()); + assert(Slot != -1 && "Type used but not available!!"); + output_vbr((unsigned)Slot, Out); + //cerr << "Type slot = " << Slot << " Type = " << T->getName() << endl; + + output_vbr(AT->getNumElements(), Out); + break; + } + + case Type::StructTyID: { + const StructType *ST = (const StructType*)T; + + // Output all of the element types... + StructType::ElementTypes::const_iterator I = ST->getElementTypes().begin(); + for (; I != ST->getElementTypes().end(); I++) { + int Slot = Table.getValSlot(*I); + assert(Slot != -1 && "Type used but not available!!"); + output_vbr((unsigned)Slot, Out); + } + + // Terminate list with VoidTy + output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out); + break; + } + + case Type::PointerTyID: { + const PointerType *PT = (const PointerType*)T; + int Slot = Table.getValSlot(PT->getValueType()); + assert(Slot != -1 && "Type used but not available!!"); + output_vbr((unsigned)Slot, Out); + break; + } + + case Type::ModuleTyID: + case Type::PackedTyID: + default: + cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize" + << " Type '" << T->getName() << "'\n"; + break; + } +} + +bool BytecodeWriter::outputConstant(const ConstPoolVal *CPV) { + switch (CPV->getType()->getPrimitiveID()) { + case Type::BoolTyID: // Boolean Types + if (((const ConstPoolBool*)CPV)->getValue()) + output_vbr((unsigned)1, Out); + else + output_vbr((unsigned)0, Out); + break; + + case Type::UByteTyID: // Unsigned integer types... + case Type::UShortTyID: + case Type::UIntTyID: + case Type::ULongTyID: + output_vbr(((const ConstPoolUInt*)CPV)->getValue(), Out); + break; + + case Type::SByteTyID: // Signed integer types... + case Type::ShortTyID: + case Type::IntTyID: + case Type::LongTyID: + output_vbr(((const ConstPoolSInt*)CPV)->getValue(), Out); + break; + + case Type::TypeTyID: // Serialize type type + outputType(((const ConstPoolType*)CPV)->getValue()); + break; + + case Type::ArrayTyID: { + const ConstPoolArray *CPA = (const ConstPoolArray *)CPV; + unsigned size = CPA->getValues().size(); + if (!((const ArrayType *)CPA->getType())->isSized()) + output_vbr(size, Out); // Not for sized arrays!!! + + for (unsigned i = 0; i < size; i++) { + int Slot = Table.getValSlot(CPA->getValues()[i]); + assert(Slot != -1 && "Constant used but not available!!"); + output_vbr((unsigned)Slot, Out); + } + break; + } + + case Type::StructTyID: { + const ConstPoolStruct *CPS = (const ConstPoolStruct*)CPV; + const vector &Vals = CPS->getValues(); + + for (unsigned i = 0; i < Vals.size(); ++i) { + int Slot = Table.getValSlot(Vals[i]); + assert(Slot != -1 && "Constant used but not available!!"); + output_vbr((unsigned)Slot, Out); + } + break; + } + + case Type::FloatTyID: // Floating point types... + case Type::DoubleTyID: + // TODO: Floating point type serialization + + + case Type::VoidTyID: + case Type::LabelTyID: + default: + cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize" + << " type '" << CPV->getType()->getName() << "'\n"; + break; + } + return false; +} diff --git a/lib/Bytecode/Writer/InstructionWriter.cpp b/lib/Bytecode/Writer/InstructionWriter.cpp new file mode 100644 index 0000000000..c7c04efb73 --- /dev/null +++ b/lib/Bytecode/Writer/InstructionWriter.cpp @@ -0,0 +1,184 @@ +//===-- WriteInst.cpp - Functions for writing instructions -------*- C++ -*--=// +// +// This file implements the routines for encoding instruction opcodes to a +// bytecode stream. +// +// Note that the performance of this library is not terribly important, because +// it shouldn't be used by JIT type applications... so it is not a huge focus +// at least. :) +// +//===----------------------------------------------------------------------===// + +#include "WriterInternals.h" +#include "llvm/Module.h" +#include "llvm/Method.h" +#include "llvm/BasicBlock.h" +#include "llvm/Instruction.h" +#include "llvm/DerivedTypes.h" +#include "llvm/Tools/DataTypes.h" +#include + +typedef unsigned char uchar; + +// outputInstructionFormat0 - Output those wierd instructions that have a large +// number of operands or have large operands themselves... +// +// Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg] +// +static void outputInstructionFormat0(const Instruction *I, + const SlotCalculator &Table, + unsigned Type, vector &Out) { + // Opcode must have top two bits clear... + output_vbr(I->getInstType(), Out); // Instruction Opcode ID + output_vbr(Type, Out); // Result type + + unsigned NumArgs; // Count the number of arguments to the instruction + for (NumArgs = 0; I->getOperand(NumArgs); NumArgs++) /*empty*/; + output_vbr(NumArgs, Out); + + for (unsigned i = 0; const Value *N = I->getOperand(i); i++) { + assert(i < NumArgs && "Count of arguments failed!"); + + int Slot = Table.getValSlot(N); + output_vbr((unsigned)Slot, Out); + } + align32(Out); // We must maintain correct alignment! +} + + +// outputInstructionFormat1 - Output one operand instructions, knowing that no +// operand index is >= 2^12. +// +static void outputInstructionFormat1(const Instruction *I, + const SlotCalculator &Table, int *Slots, + unsigned Type, vector &Out) { + unsigned IType = I->getInstType(); // Instruction Opcode ID + + // bits Instruction format: + // -------------------------- + // 31-30: Opcode type, fixed to 1. + // 29-24: Opcode + // 23-12: Resulting type plane + // 11- 0: Operand #1 (if set to (2^12-1), then zero operands) + // + unsigned Opcode = (1 << 30) | (IType << 24) | (Type << 12) | Slots[0]; + // cerr << "1 " << IType << " " << Type << " " << Slots[0] << endl; + output(Opcode, Out); +} + + +// outputInstructionFormat2 - Output two operand instructions, knowing that no +// operand index is >= 2^8. +// +static void outputInstructionFormat2(const Instruction *I, + const SlotCalculator &Table, int *Slots, + unsigned Type, vector &Out) { + unsigned IType = I->getInstType(); // Instruction Opcode ID + + // bits Instruction format: + // -------------------------- + // 31-30: Opcode type, fixed to 2. + // 29-24: Opcode + // 23-16: Resulting type plane + // 15- 8: Operand #1 + // 7- 0: Operand #2 + // + unsigned Opcode = (2 << 30) | (IType << 24) | (Type << 16) | + (Slots[0] << 8) | (Slots[1] << 0); + // cerr << "2 " << IType << " " << Type << " " << Slots[0] << " " + // << Slots[1] << endl; + output(Opcode, Out); +} + + +// outputInstructionFormat3 - Output three operand instructions, knowing that no +// operand index is >= 2^6. +// +static void outputInstructionFormat3(const Instruction *I, + const SlotCalculator &Table, int *Slots, + unsigned Type, vector &Out) { + unsigned IType = I->getInstType(); // Instruction Opcode ID + + // bits Instruction format: + // -------------------------- + // 31-30: Opcode type, fixed to 3 + // 29-24: Opcode + // 23-18: Resulting type plane + // 17-12: Operand #1 + // 11- 6: Operand #2 + // 5- 0: Operand #3 + // + unsigned Opcode = (3 << 30) | (IType << 24) | (Type << 18) | + (Slots[0] << 12) | (Slots[1] << 6) | (Slots[2] << 0); + // cerr << "3 " << IType << " " << Type << " " << Slots[0] << " " + // << Slots[1] << " " << Slots[2] << endl; + output(Opcode, Out); +} + +bool BytecodeWriter::processInstruction(const Instruction *I) { + assert(I->getInstType() < 64 && "Opcode too big???"); + + unsigned NumOperands = 0; + int MaxOpSlot = 0; + int Slots[3]; Slots[0] = (1 << 12)-1; + + const Value *Def; + while ((Def = I->getOperand(NumOperands))) { + int slot = Table.getValSlot(Def); + assert(slot != -1 && "Broken bytecode!"); + if (slot > MaxOpSlot) MaxOpSlot = slot; + if (NumOperands < 3) Slots[NumOperands] = slot; + NumOperands++; + } + + // Figure out which type to encode with the instruction. Typically we want + // the type of the first parameter, as opposed to the type of the instruction + // (for example, with setcc, we always know it returns bool, but the type of + // the first param is actually interesting). But if we have no arguments + // we take the type of the instruction itself. + // + + const Type *Ty; + if (NumOperands) + Ty = I->getOperand(0)->getType(); + else + Ty = I->getType(); + + unsigned Type; + int Slot = Table.getValSlot(Ty); + assert(Slot != -1 && "Type not available!!?!"); + Type = (unsigned)Slot; + + + // Decide which instruction encoding to use. This is determined primarily by + // the number of operands, and secondarily by whether or not the max operand + // will fit into the instruction encoding. More operands == fewer bits per + // operand. + // + switch (NumOperands) { + case 0: + case 1: + if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops + outputInstructionFormat1(I, Table, Slots, Type, Out); + return false; + } + break; + + case 2: + if (MaxOpSlot < (1 << 8)) { + outputInstructionFormat2(I, Table, Slots, Type, Out); + return false; + } + break; + + case 3: + if (MaxOpSlot < (1 << 6)) { + outputInstructionFormat3(I, Table, Slots, Type, Out); + return false; + } + break; + } + + outputInstructionFormat0(I, Table, Type, Out); + return false; +} diff --git a/lib/Bytecode/Writer/Makefile b/lib/Bytecode/Writer/Makefile new file mode 100644 index 0000000000..c03db56154 --- /dev/null +++ b/lib/Bytecode/Writer/Makefile @@ -0,0 +1,7 @@ + +LEVEL = ../../.. + +LIBRARYNAME = bcwriter + +include $(LEVEL)/Makefile.common + diff --git a/lib/Bytecode/Writer/SlotCalculator.cpp b/lib/Bytecode/Writer/SlotCalculator.cpp new file mode 100644 index 0000000000..01fae37e53 --- /dev/null +++ b/lib/Bytecode/Writer/SlotCalculator.cpp @@ -0,0 +1,195 @@ +//===-- SlotCalculator.cpp - Calculate what slots values land in ------------=// +// +// This file implements a useful analysis step to figure out what numbered +// slots values in a program will land in (keeping track of per plane +// information as required. +// +// This is used primarily for when writing a file to disk, either in bytecode +// or source format. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Analysis/SlotCalculator.h" +#include "llvm/ConstantPool.h" +#include "llvm/Method.h" +#include "llvm/Module.h" +#include "llvm/BasicBlock.h" +#include "llvm/ConstPoolVals.h" +#include "llvm/iOther.h" +#include "llvm/DerivedTypes.h" + +SlotCalculator::SlotCalculator(const Module *M, bool IgnoreNamed) { + IgnoreNamedNodes = IgnoreNamed; + TheModule = M; + + // Preload table... Make sure that all of the primitive types are in the table + // and that their Primitive ID is equal to their slot # + // + for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) { + assert(Type::getPrimitiveType((Type::PrimitiveID)i)); + insertVal(Type::getPrimitiveType((Type::PrimitiveID)i)); + } + + if (M == 0) return; // Empty table... + + bool Result = processModule(M); + assert(Result == false && "Error in processModule!"); +} + +SlotCalculator::SlotCalculator(const Method *M, bool IgnoreNamed) { + IgnoreNamedNodes = IgnoreNamed; + TheModule = M ? M->getParent() : 0; + + // Preload table... Make sure that all of the primitive types are in the table + // and that their Primitive ID is equal to their slot # + // + for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) { + assert(Type::getPrimitiveType((Type::PrimitiveID)i)); + insertVal(Type::getPrimitiveType((Type::PrimitiveID)i)); + } + + if (TheModule == 0) return; // Empty table... + + bool Result = processModule(TheModule); + assert(Result == false && "Error in processModule!"); + + incorporateMethod(M); +} + +void SlotCalculator::incorporateMethod(const Method *M) { + assert(ModuleLevel.size() == 0 && "Module already incorporated!"); + + // Save the Table state before we process the method... + for (unsigned i = 0; i < Table.size(); ++i) { + ModuleLevel.push_back(Table[i].size()); + } + + // Process the method to incorporate its values into our table + processMethod(M); +} + +void SlotCalculator::purgeMethod() { + assert(ModuleLevel.size() != 0 && "Module not incorporated!"); + unsigned NumModuleTypes = ModuleLevel.size(); + + // First, remove values from existing type planes + for (unsigned i = 0; i < NumModuleTypes; ++i) { + unsigned ModuleSize = ModuleLevel[i]; // Size of plane before method came + while (Table[i].size() != ModuleSize) { + NodeMap.erase(NodeMap.find(Table[i].back())); // Erase from nodemap + Table[i].pop_back(); // Shrink plane + } + } + + // We don't need this state anymore, free it up. + ModuleLevel.clear(); + + // Next, remove any type planes defined by the method... + while (NumModuleTypes != Table.size()) { + TypePlane &Plane = Table.back(); + while (Plane.size()) { + NodeMap.erase(NodeMap.find(Plane.back())); // Erase from nodemap + Plane.pop_back(); // Shrink plane + } + + Table.pop_back(); // Nuke the plane, we don't like it. + } +} + +bool SlotCalculator::processConstant(const ConstPoolVal *CPV) { + //cerr << "Inserting constant: '" << CPV->getStrValue() << endl; + insertVal(CPV); + return false; +} + +// processType - This callback occurs when an derived type is discovered +// at the class level. This activity occurs when processing a constant pool. +// +bool SlotCalculator::processType(const Type *Ty) { + //cerr << "processType: " << Ty->getName() << endl; + // TODO: Don't leak memory!!! Free this in the dtor! + insertVal(new ConstPoolType(Ty)); + return false; +} + +bool SlotCalculator::visitMethod(const Method *M) { + //cerr << "visitMethod: '" << M->getType()->getName() << "'\n"; + insertVal(M); + return false; +} + +bool SlotCalculator::processMethodArgument(const MethodArgument *MA) { + insertVal(MA); + return false; +} + +bool SlotCalculator::processBasicBlock(const BasicBlock *BB) { + insertVal(BB); + ModuleAnalyzer::processBasicBlock(BB); // Lets visit the instructions too! + return false; +} + +bool SlotCalculator::processInstruction(const Instruction *I) { + insertVal(I); + return false; +} + +int SlotCalculator::getValSlot(const Value *D) const { + map::const_iterator I = NodeMap.find(D); + if (I == NodeMap.end()) return -1; + + return (int)I->second; +} + +void SlotCalculator::insertVal(const Value *D) { + if (D == 0) return; + + // If this node does not contribute to a plane, or if the node has a + // name and we don't want names, then ignore the silly node... + // + if (D->getType() == Type::VoidTy || (IgnoreNamedNodes && D->hasName())) + return; + + const Type *Typ = D->getType(); + unsigned Ty = Typ->getPrimitiveID(); + if (Typ->isDerivedType()) { + int DefSlot = getValSlot(Typ); + if (DefSlot == -1) { // Have we already entered this type? + // This can happen if a type is first seen in an instruction. For + // example, if you say 'malloc uint', this defines a type 'uint*' that + // may be undefined at this point. + // + cerr << "SHOULDNT HAPPEN Adding Type ba: " << Typ->getName() << endl; + assert(0 && "SHouldn't this be taken care of by processType!?!?!"); + // Nope... add this to the Type plane now! + insertVal(Typ); + + DefSlot = getValSlot(Typ); + assert(DefSlot >= 0 && "Type didn't get inserted correctly!"); + } + Ty = (unsigned)DefSlot; + } + + if (Table.size() <= Ty) // Make sure we have the type plane allocated... + Table.resize(Ty+1, TypePlane()); + + // Insert node into table and NodeMap... + NodeMap[D] = Table[Ty].size(); + + if (Typ == Type::TypeTy && // If it's a type constant, add the Type also + D->getValueType() != Value::TypeVal) { + assert(D->getValueType() == Value::ConstantVal && + "All Type instances should be constant types!"); + + const ConstPoolType *CPT = (const ConstPoolType*)D; + int Slot = getValSlot(CPT->getValue()); + if (Slot == -1) { + // Only add if it's not already here! + NodeMap[CPT->getValue()] = Table[Ty].size(); + } else if (!CPT->hasName()) { // If the type has no name... + NodeMap[D] = (unsigned)Slot; // Don't readd type, merge. + return; + } + } + Table[Ty].push_back(D); +} diff --git a/lib/Bytecode/Writer/SlotCalculator.h b/lib/Bytecode/Writer/SlotCalculator.h new file mode 100644 index 0000000000..99e40cbeb5 --- /dev/null +++ b/lib/Bytecode/Writer/SlotCalculator.h @@ -0,0 +1,96 @@ +//===-- llvm/Analysis/SlotCalculator.h - Calculate value slots ---*- C++ -*-==// +// +// This ModuleAnalyzer subclass calculates the slots that values will land in. +// This is useful for when writing bytecode or assembly out, because you have +// to know these things. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_ANALYSIS_SLOTCALCULATOR_H +#define LLVM_ANALYSIS_SLOTCALCULATOR_H + +#include "llvm/Analysis/ModuleAnalyzer.h" +#include "llvm/SymTabValue.h" +#include +#include + +class SlotCalculator : public ModuleAnalyzer { + const Module *TheModule; + bool IgnoreNamedNodes; // Shall we not count named nodes? + + typedef vector TypePlane; + vector Table; + map NodeMap; + + // ModuleLevel - Used to keep track of which values belong to the module, + // and which values belong to the currently incorporated method. + // + vector ModuleLevel; + +public: + SlotCalculator(const Module *M, bool IgnoreNamed); + SlotCalculator(const Method *M, bool IgnoreNamed);// Start out in incorp state + inline ~SlotCalculator() {} + + // getValSlot returns < 0 on error! + int getValSlot(const Value *D) const; + + inline unsigned getNumPlanes() const { return Table.size(); } + inline unsigned getModuleLevel(unsigned Plane) const { + return Plane < ModuleLevel.size() ? ModuleLevel[Plane] : 0; + } + + inline const TypePlane &getPlane(unsigned Plane) const { + return Table[Plane]; + } + + // If you'd like to deal with a method, use these two methods to get its data + // into the SlotCalculator! + // + void incorporateMethod(const Method *M); + void purgeMethod(); + +protected: + // insertVal - Insert a value into the value table... + // + void insertVal(const Value *D); + + // visitMethod - This member is called after the constant pool has been + // processed. The default implementation of this is a noop. + // + virtual bool visitMethod(const Method *M); + + // processConstant is called once per each constant in the constant pool. It + // traverses the constant pool such that it visits each constant in the + // order of its type. Thus, all 'int' typed constants shall be visited + // sequentially, etc... + // + virtual bool processConstant(const ConstPoolVal *CPV); + + // processType - This callback occurs when an derived type is discovered + // at the class level. This activity occurs when processing a constant pool. + // + virtual bool processType(const Type *Ty); + + // processMethods - The default implementation of this method loops through + // all of the methods in the module and processModule's them. We don't want + // this (we want to explicitly visit them with incorporateMethod), so we + // disable it. + // + virtual bool processMethods(const Module *M) { return false; } + + // processMethodArgument - This member is called for every argument that + // is passed into the method. + // + virtual bool processMethodArgument(const MethodArgument *MA); + + // processBasicBlock - This member is called for each basic block in a methd. + // + virtual bool processBasicBlock(const BasicBlock *BB); + + // processInstruction - This member is called for each Instruction in a methd. + // + virtual bool processInstruction(const Instruction *I); +}; + +#endif diff --git a/lib/Bytecode/Writer/Writer.cpp b/lib/Bytecode/Writer/Writer.cpp new file mode 100644 index 0000000000..d03c945471 --- /dev/null +++ b/lib/Bytecode/Writer/Writer.cpp @@ -0,0 +1,182 @@ +//===-- Writer.cpp - Library for writing VM bytecode files -------*- C++ -*--=// +// +// This library implements the functionality defined in llvm/Bytecode/Writer.h +// +// This library uses the Analysis library to figure out offsets for +// variables in the method tables... +// +// Note that this file uses an unusual technique of outputting all the bytecode +// to a vector of unsigned char's, then copies the vector to an ostream. The +// reason for this is that we must do "seeking" in the stream to do back- +// patching, and some very important ostreams that we want to support (like +// pipes) do not support seeking. :( :( :( +// +// The choice of the vector data structure is influenced by the extremely fast +// "append" speed, plus the free "seek"/replace in the middle of the stream. +// +// Note that the performance of this library is not terribly important, because +// it shouldn't be used by JIT type applications... so it is not a huge focus +// at least. :) +// +//===----------------------------------------------------------------------===// + +#include "WriterInternals.h" +#include "llvm/Module.h" +#include "llvm/Method.h" +#include "llvm/BasicBlock.h" +#include "llvm/ConstPoolVals.h" +#include "llvm/SymbolTable.h" +#include "llvm/DerivedTypes.h" +#include +#include + +BytecodeWriter::BytecodeWriter(vector &o, const Module *M) + : Out(o), Table(M, false) { + + outputSignature(); + + // Emit the top level CLASS block. + BytecodeBlock ModuleBlock(BytecodeFormat::Module, Out); + + // Output largest ID of first "primitive" type: + output_vbr((unsigned)Type::FirstDerivedTyID, Out); + align32(Out); + + // Do the whole module now! + processModule(M); + + // If needed, output the symbol table for the class... + if (M->hasSymbolTable()) + outputSymbolTable(*M->getSymbolTable()); +} + +// TODO: REMOVE +#include "llvm/Assembly/Writer.h" + +bool BytecodeWriter::processConstPool(const ConstantPool &CP, bool isMethod) { + BytecodeBlock *CPool = new BytecodeBlock(BytecodeFormat::ConstantPool, Out); + + unsigned NumPlanes = Table.getNumPlanes(); + + for (unsigned pno = 0; pno < NumPlanes; pno++) { + const vector &Plane = Table.getPlane(pno); + if (Plane.empty()) continue; // Skip empty type planes... + + unsigned ValNo = 0; // Don't reemit module constants + if (isMethod) ValNo = Table.getModuleLevel(pno); + + unsigned NumConstants = 0; + for (unsigned vn = ValNo; vn < Plane.size(); vn++) + if (Plane[vn]->getValueType() == Value::ConstantVal) + NumConstants++; + + if (NumConstants == 0) continue; // Skip empty type planes... + + // Output type header: [num entries][type id number] + // + output_vbr(NumConstants, Out); + + // Output the Type ID Number... + int Slot = Table.getValSlot(Plane.front()->getType()); + assert (Slot != -1 && "Type in constant pool but not in method!!"); + output_vbr((unsigned)Slot, Out); + + //cerr << "NC: " << NumConstants << " Slot = " << hex << Slot << endl; + + for (; ValNo < Plane.size(); ValNo++) { + const Value *V = Plane[ValNo]; + if (V->getValueType() == Value::ConstantVal) { + //cerr << "Serializing value: <" << V->getType() << ">: " + // << ((const ConstPoolVal*)V)->getStrValue() << ":" + // << Out.size() << "\n"; + outputConstant((const ConstPoolVal*)V); + } + } + } + + delete CPool; // End bytecode block section! + + if (!isMethod) { // The ModuleInfoBlock follows directly after the c-pool + assert(CP.getParent()->getValueType() == Value::ModuleVal); + outputModuleInfoBlock((const Module*)CP.getParent()); + } + + return false; +} + +void BytecodeWriter::outputModuleInfoBlock(const Module *M) { + BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfo, Out); + + // Output the types of the methods in this class + Module::MethodListType::const_iterator I = M->getMethodList().begin(); + while (I != M->getMethodList().end()) { + int Slot = Table.getValSlot((*I)->getType()); + assert(Slot != -1 && "Module const pool is broken!"); + assert(Slot >= Type::FirstDerivedTyID && "Derived type not in range!"); + output_vbr((unsigned)Slot, Out); + I++; + } + output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out); + align32(Out); +} + +bool BytecodeWriter::processMethod(const Method *M) { + BytecodeBlock MethodBlock(BytecodeFormat::Method, Out); + + Table.incorporateMethod(M); + + if (ModuleAnalyzer::processMethod(M)) return true; + + // If needed, output the symbol table for the method... + if (M->hasSymbolTable()) + outputSymbolTable(*M->getSymbolTable()); + + Table.purgeMethod(); + return false; +} + + +bool BytecodeWriter::processBasicBlock(const BasicBlock *BB) { + BytecodeBlock MethodBlock(BytecodeFormat::BasicBlock, Out); + return ModuleAnalyzer::processBasicBlock(BB); +} + +void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) { + BytecodeBlock MethodBlock(BytecodeFormat::SymbolTable, Out); + + for (SymbolTable::const_iterator TI = MST.begin(); TI != MST.end(); TI++) { + SymbolTable::type_const_iterator I = MST.type_begin(TI->first); + SymbolTable::type_const_iterator End = MST.type_end(TI->first); + int Slot; + + if (I == End) continue; // Don't mess with an absent type... + + // Symtab block header: [num entries][type id number] + output_vbr(MST.type_size(TI->first), Out); + + Slot = Table.getValSlot(TI->first); + assert(Slot != -1 && "Type in symtab, but not in table!"); + output_vbr((unsigned)Slot, Out); + + for (; I != End; I++) { + // Symtab entry: [def slot #][name] + Slot = Table.getValSlot(I->second); + assert (Slot != -1 && "Value in symtab but not in method!!"); + output_vbr((unsigned)Slot, Out); + output(I->first, Out, false); // Don't force alignment... + } + } +} + +void WriteBytecodeToFile(const Module *C, ostream &Out) { + assert(C && "You can't write a null class!!"); + + vector Buffer; + + // This object populates buffer for us... + BytecodeWriter BCW(Buffer, C); + + // Okay, write the vector out to the ostream now... + Out.write(&Buffer[0], Buffer.size()); + Out.flush(); +} diff --git a/lib/Bytecode/Writer/WriterInternals.h b/lib/Bytecode/Writer/WriterInternals.h new file mode 100644 index 0000000000..be9ccf9667 --- /dev/null +++ b/lib/Bytecode/Writer/WriterInternals.h @@ -0,0 +1,74 @@ +//===-- WriterInternals.h - Data structures shared by the Writer -*- C++ -*--=// +// +// This header defines the interface used between components of the bytecode +// writer. +// +// Note that the performance of this library is not terribly important, because +// it shouldn't be used by JIT type applications... so it is not a huge focus +// at least. :) +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIB_BYTECODE_WRITER_WRITERINTERNALS_H +#define LLVM_LIB_BYTECODE_WRITER_WRITERINTERNALS_H + +#include "llvm/Bytecode/Writer.h" +#include "llvm/Bytecode/Format.h" +#include "llvm/Bytecode/Primitives.h" +#include "llvm/Analysis/SlotCalculator.h" +#include "llvm/Tools/DataTypes.h" +#include "llvm/Instruction.h" + +class BytecodeWriter : public ModuleAnalyzer { + vector &Out; + SlotCalculator Table; +public: + BytecodeWriter(vector &o, const Module *M); + +protected: + virtual bool processConstPool(const ConstantPool &CP, bool isMethod); + virtual bool processMethod(const Method *M); + virtual bool processBasicBlock(const BasicBlock *BB); + virtual bool processInstruction(const Instruction *I); + +private : + inline void outputSignature() { + static const unsigned char *Sig = (const unsigned char*)"llvm"; + Out.insert(Out.end(), Sig, Sig+4); // output the bytecode signature... + } + + void outputModuleInfoBlock(const Module *C); + void outputSymbolTable(const SymbolTable &ST); + bool outputConstant(const ConstPoolVal *CPV); + void outputType(const Type *T); +}; + + + + +// BytecodeBlock - Little helper class that helps us do backpatching of bytecode +// block sizes really easily. It backpatches when it goes out of scope. +// +class BytecodeBlock { + unsigned Loc; + vector &Out; + + BytecodeBlock(const BytecodeBlock &); // do not implement + void operator=(const BytecodeBlock &); // do not implement +public: + inline BytecodeBlock(unsigned ID, vector &o) : Out(o) { + output(ID, Out); + output((unsigned)0, Out); // Reserve the space for the block size... + Loc = Out.size(); + } + + inline ~BytecodeBlock() { // Do backpatch when block goes out + // of scope... + // cerr << "OldLoc = " << Loc << " NewLoc = " << NewLoc << " diff = " << (NewLoc-Loc) << endl; + output((unsigned)(Out.size()-Loc), Out, (int)Loc-4); + align32(Out); // Blocks must ALWAYS be aligned + } +}; + + +#endif diff --git a/lib/Makefile b/lib/Makefile new file mode 100644 index 0000000000..1f74058a22 --- /dev/null +++ b/lib/Makefile @@ -0,0 +1,5 @@ +LEVEL = .. +DIRS = VMCore Analysis Assembly Bytecode Optimizations + +include $(LEVEL)/Makefile.common + diff --git a/lib/Transforms/IPO/InlineSimple.cpp b/lib/Transforms/IPO/InlineSimple.cpp new file mode 100644 index 0000000000..a1e3156b3b --- /dev/null +++ b/lib/Transforms/IPO/InlineSimple.cpp @@ -0,0 +1,283 @@ +//===- MethodInlining.cpp - Code to perform method inlining ---------------===// +// +// This file implements inlining of methods. +// +// Specifically, this: +// * Exports functionality to inline any method call +// * Inlines methods that consist of a single basic block +// * Is able to inline ANY method call +// . Has a smart heuristic for when to inline a method +// +// Notice that: +// * This pass has a habit of introducing duplicated constant pool entries, +// and also opens up a lot of opportunities for constant propogation. It is +// a good idea to to run a constant propogation pass, then a DCE pass +// sometime after running this pass. +// +// TODO: Currently this throws away all of the symbol names in the method being +// inlined to try to avoid name clashes. Use a name if it's not taken +// +//===----------------------------------------------------------------------===// + +#include "llvm/Module.h" +#include "llvm/Method.h" +#include "llvm/BasicBlock.h" +#include "llvm/iTerminators.h" +#include "llvm/iOther.h" +#include "llvm/Opt/AllOpts.h" +#include +#include + +#include "llvm/Assembly/Writer.h" + +// RemapInstruction - Convert the instruction operands from referencing the +// current values into those specified by ValueMap. +// +static inline void RemapInstruction(Instruction *I, + map &ValueMap) { + + for (unsigned op = 0; const Value *Op = I->getOperand(op); op++) { + Value *V = ValueMap[Op]; + if (!V && Op->getValueType() == Value::MethodVal) + continue; // Methods don't get relocated + + if (!V) { + cerr << "Val = " << endl << Op << "Addr = " << (void*)Op << endl; + cerr << "Inst = " << I; + } + assert(V && "Referenced value not in value map!"); + I->setOperand(op, V); + } +} + +// InlineMethod - This function forcibly inlines the called method into the +// basic block of the caller. This returns false if it is not possible to +// inline this call. The program is still in a well defined state if this +// occurs though. +// +// Note that this only does one level of inlining. For example, if the +// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now +// exists in the instruction stream. Similiarly this will inline a recursive +// method by one level. +// +bool InlineMethod(BasicBlock::InstListType::iterator CIIt) { + assert((*CIIt)->getInstType() == Instruction::Call && + "InlineMethod only works on CallInst nodes!"); + assert((*CIIt)->getParent() && "Instruction not embedded in basic block!"); + assert((*CIIt)->getParent()->getParent() && "Instruction not in method!"); + + CallInst *CI = (CallInst*)*CIIt; + const Method *CalledMeth = CI->getCalledMethod(); + Method *CurrentMeth = CI->getParent()->getParent(); + + //cerr << "Inlining " << CalledMeth->getName() << " into " + // << CurrentMeth->getName() << endl; + + BasicBlock *OrigBB = CI->getParent(); + + // Call splitBasicBlock - The original basic block now ends at the instruction + // immediately before the call. The original basic block now ends with an + // unconditional branch to NewBB, and NewBB starts with the call instruction. + // + BasicBlock *NewBB = OrigBB->splitBasicBlock(CIIt); + + // Remove (unlink) the CallInst from the start of the new basic block. + NewBB->getInstList().remove(CI); + + // If we have a return value generated by this call, convert it into a PHI + // node that gets values from each of the old RET instructions in the original + // method. + // + PHINode *PHI = 0; + if (CalledMeth->getReturnType() != Type::VoidTy) { + PHI = new PHINode(CalledMeth->getReturnType(), CI->getName()); + + // The PHI node should go at the front of the new basic block to merge all + // possible incoming values. + // + NewBB->getInstList().push_front(PHI); + + // Anything that used the result of the function call should now use the PHI + // node as their operand. + // + CI->replaceAllUsesWith(PHI); + } + + // Keep a mapping between the original method's values and the new duplicated + // code's values. This includes all of: Method arguments, instruction values, + // constant pool entries, and basic blocks. + // + map ValueMap; + + // Add the method arguments to the mapping: (start counting at 1 to skip the + // method reference itself) + // + Method::ArgumentListType::const_iterator PTI = + CalledMeth->getArgumentList().begin(); + for (unsigned a = 1; Value *Operand = CI->getOperand(a); ++a, ++PTI) { + ValueMap[*PTI] = Operand; + } + + + ValueMap[NewBB] = NewBB; // Returns get converted to reference NewBB + + // Loop over all of the basic blocks in the method, inlining them as + // appropriate. Keep track of the first basic block of the method... + // + for (Method::BasicBlocksType::const_iterator BI = + CalledMeth->getBasicBlocks().begin(); + BI != CalledMeth->getBasicBlocks().end(); BI++) { + const BasicBlock *BB = *BI; + assert(BB->getTerminator() && "BasicBlock doesn't have terminator!?!?"); + + // Create a new basic block to copy instructions into! + BasicBlock *IBB = new BasicBlock("", NewBB->getParent()); + + ValueMap[*BI] = IBB; // Add basic block mapping. + + // Make sure to capture the mapping that a return will use... + // TODO: This assumes that the RET is returning a value computed in the same + // basic block as the return was issued from! + // + const TerminatorInst *TI = BB->getTerminator(); + + // Loop over all instructions copying them over... + Instruction *NewInst; + for (BasicBlock::InstListType::const_iterator II = BB->getInstList().begin(); + II != (BB->getInstList().end()-1); II++) { + IBB->getInstList().push_back((NewInst = (*II)->clone())); + ValueMap[*II] = NewInst; // Add instruction map to value. + } + + // Copy over the terminator now... + switch (TI->getInstType()) { + case Instruction::Ret: { + const ReturnInst *RI = (const ReturnInst*)TI; + + if (PHI) { // The PHI node should include this value! + assert(RI->getReturnValue() && "Ret should have value!"); + assert(RI->getReturnValue()->getType() == PHI->getType() && + "Ret value not consistent in method!"); + PHI->addIncoming((Value*)RI->getReturnValue()); + } + + // Add a branch to the code that was after the original Call. + IBB->getInstList().push_back(new BranchInst(NewBB)); + break; + } + case Instruction::Br: + IBB->getInstList().push_back(TI->clone()); + break; + + default: + cerr << "MethodInlining: Don't know how to handle terminator: " << TI; + abort(); + } + } + + + // Copy over the constant pool... + // + const ConstantPool &CP = CalledMeth->getConstantPool(); + ConstantPool &NewCP = CurrentMeth->getConstantPool(); + for (ConstantPool::plane_const_iterator PI = CP.begin(); PI != CP.end(); ++PI){ + ConstantPool::PlaneType &Plane = **PI; + for (ConstantPool::PlaneType::const_iterator I = Plane.begin(); + I != Plane.end(); ++I) { + ConstPoolVal *NewVal = (*I)->clone(); // Copy existing constant + NewCP.insert(NewVal); // Insert the new copy into local const pool + ValueMap[*I] = NewVal; // Keep track of constant value mappings + } + } + + // Loop over all of the instructions in the method, fixing up operand + // references as we go. This uses ValueMap to do all the hard work. + // + for (Method::BasicBlocksType::const_iterator BI = + CalledMeth->getBasicBlocks().begin(); + BI != CalledMeth->getBasicBlocks().end(); BI++) { + const BasicBlock *BB = *BI; + BasicBlock *NBB = (BasicBlock*)ValueMap[BB]; + + // Loop over all instructions, fixing each one as we find it... + // + for (BasicBlock::InstListType::iterator II = NBB->getInstList().begin(); + II != NBB->getInstList().end(); II++) + RemapInstruction(*II, ValueMap); + } + + if (PHI) RemapInstruction(PHI, ValueMap); // Fix the PHI node also... + + // Change the branch that used to go to NewBB to branch to the first basic + // block of the inlined method. + // + TerminatorInst *Br = OrigBB->getTerminator(); + assert(Br && Br->getInstType() == Instruction::Br && + "splitBasicBlock broken!"); + Br->setOperand(0, ValueMap[CalledMeth->getBasicBlocks().front()]); + + // Since we are now done with the CallInst, we can finally delete it. + delete CI; + return true; +} + +bool InlineMethod(CallInst *CI) { + assert(CI->getParent() && "CallInst not embeded in BasicBlock!"); + BasicBlock *PBB = CI->getParent(); + + BasicBlock::InstListType::iterator CallIt = find(PBB->getInstList().begin(), + PBB->getInstList().end(), + CI); + assert(CallIt != PBB->getInstList().end() && + "CallInst has parent that doesn't contain CallInst?!?"); + return InlineMethod(CallIt); +} + +static inline bool ShouldInlineMethod(const CallInst *CI, const Method *M) { + assert(CI->getParent() && CI->getParent()->getParent() && + "Call not embedded into a method!"); + + // Don't inline a recursive call. + if (CI->getParent()->getParent() == M) return false; + + // Don't inline something too big. This is a really crappy heuristic + if (M->getBasicBlocks().size() > 3) return false; + + // Don't inline into something too big. This is a **really** crappy heuristic + if (CI->getParent()->getParent()->getBasicBlocks().size() > 10) return false; + + // Go ahead and try just about anything else. + return true; +} + + +static inline bool DoMethodInlining(BasicBlock *BB) { + for (BasicBlock::InstListType::iterator I = BB->getInstList().begin(); + I != BB->getInstList().end(); I++) { + if ((*I)->getInstType() == Instruction::Call) { + // Check to see if we should inline this method + CallInst *CI = (CallInst*)*I; + Method *M = CI->getCalledMethod(); + if (ShouldInlineMethod(CI, M)) + return InlineMethod(I); + } + } + return false; +} + +bool DoMethodInlining(Method *M) { + Method::BasicBlocksType &BBs = M->getBasicBlocks(); + bool Changed = false; + + // Loop through now and inline instructions a basic block at a time... + for (Method::BasicBlocksType::iterator I = BBs.begin(); I != BBs.end(); ) + if (DoMethodInlining(*I)) { + Changed = true; + // Iterator is now invalidated by new basic blocks inserted + I = BBs.begin(); + } else { + ++I; + } + + return Changed; +} diff --git a/lib/Transforms/Scalar/ConstantProp.cpp b/lib/Transforms/Scalar/ConstantProp.cpp new file mode 100644 index 0000000000..eef5a039f0 --- /dev/null +++ b/lib/Transforms/Scalar/ConstantProp.cpp @@ -0,0 +1,239 @@ +//===- ConstantProp.cpp - Code to perform Constant Propogation ------------===// +// +// This file implements constant propogation and merging: +// +// Specifically, this: +// * Folds multiple identical constants in the constant pool together +// Note that if one is named and the other is not, that the result gets the +// original name. +// * Converts instructions like "add int %1, %2" into a direct def of %3 in +// the constant pool +// * Converts conditional branches on a constant boolean value into direct +// branches. +// * Converts phi nodes with one incoming def to the incoming def directly +// . Converts switch statements with one entry into a test & conditional +// branch +// . Converts switches on constant values into an unconditional branch. +// +// Notice that: +// * This pass has a habit of making definitions be dead. It is a good idea +// to to run a DCE pass sometime after running this pass. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Module.h" +#include "llvm/Method.h" +#include "llvm/BasicBlock.h" +#include "llvm/iTerminators.h" +#include "llvm/iOther.h" +#include "llvm/ConstPoolVals.h" +#include "llvm/ConstantPool.h" +#include "llvm/Opt/AllOpts.h" +#include "llvm/Opt/ConstantHandling.h" + +// Merge identical constant values in the constant pool. +// +// TODO: We can do better than this simplistic N^2 algorithm... +// +static bool MergeConstantPoolReferences(ConstantPool &CP) { + bool Modified = false; + for (ConstantPool::plane_iterator PI = CP.begin(); PI != CP.end(); ++PI) { + for (ConstantPool::PlaneType::iterator I = (*PI)->begin(); + I != (*PI)->end(); I++) { + ConstPoolVal *C = *I; + + ConstantPool::PlaneType::iterator J = I; + for (++J; J != (*PI)->end(); J++) { + if (C->equals(*J)) { + Modified = true; + // Okay we know that *I == *J. So now we need to make all uses of *I + // point to *J. + // + C->replaceAllUsesWith(*J); + + (*PI)->remove(I); // Remove C from constant pool... + + if (C->hasName() && !(*J)->hasName()) // The merged constant inherits + (*J)->setName(C->getName()); // the old name... + + delete C; // Delete the constant itself. + break; // Break out of inner for loop + } + } + } + } + return Modified; +} + +inline static bool +ConstantFoldUnaryInst(Method *M, Method::inst_iterator &DI, + UnaryOperator *Op, ConstPoolVal *D) { + ConstPoolVal *ReplaceWith = 0; + + switch (Op->getInstType()) { + case Instruction::Not: ReplaceWith = !*D; break; + case Instruction::Neg: ReplaceWith = -*D; break; + } + + if (!ReplaceWith) return false; // Nothing new to change... + + + // Add the new value to the constant pool... + M->getConstantPool().insert(ReplaceWith); + + // Replaces all of the uses of a variable with uses of the constant. + Op->replaceAllUsesWith(ReplaceWith); + + // Remove the operator from the list of definitions... + Op->getParent()->getInstList().remove(DI.getInstructionIterator()); + + // The new constant inherits the old name of the operator... + if (Op->hasName()) ReplaceWith->setName(Op->getName()); + + // Delete the operator now... + delete Op; + return true; +} + +inline static bool +ConstantFoldBinaryInst(Method *M, Method::inst_iterator &DI, + BinaryOperator *Op, + ConstPoolVal *D1, ConstPoolVal *D2) { + ConstPoolVal *ReplaceWith = 0; + + switch (Op->getInstType()) { + case Instruction::Add: ReplaceWith = *D1 + *D2; break; + case Instruction::Sub: ReplaceWith = *D1 - *D2; break; + + case Instruction::SetEQ: ReplaceWith = *D1 == *D2; break; + case Instruction::SetNE: ReplaceWith = *D1 != *D2; break; + case Instruction::SetLE: ReplaceWith = *D1 <= *D2; break; + case Instruction::SetGE: ReplaceWith = *D1 >= *D2; break; + case Instruction::SetLT: ReplaceWith = *D1 < *D2; break; + case Instruction::SetGT: ReplaceWith = *D1 > *D2; break; + } + + if (!ReplaceWith) return false; // Nothing new to change... + + // Add the new value to the constant pool... + M->getConstantPool().insert(ReplaceWith); + + // Replaces all of the uses of a variable with uses of the constant. + Op->replaceAllUsesWith(ReplaceWith); + + // Remove the operator from the list of definitions... + Op->getParent()->getInstList().remove(DI.getInstructionIterator()); + + // The new constant inherits the old name of the operator... + if (Op->hasName()) ReplaceWith->setName(Op->getName()); + + // Delete the operator now... + delete Op; + return true; +} + +inline static bool ConstantFoldTerminator(TerminatorInst *T) { + // Branch - See if we are conditional jumping on constant + if (T->getInstType() == Instruction::Br) { + BranchInst *BI = (BranchInst*)T; + if (!BI->isUnconditional() && // Are we branching on constant? + BI->getOperand(2)->getValueType() == Value::ConstantVal) { + // YES. Change to unconditional branch... + ConstPoolBool *Cond = (ConstPoolBool*)BI->getOperand(2); + Value *Destination = BI->getOperand(Cond->getValue() ? 0 : 1); + + BI->setOperand(0, Destination); // Set the unconditional destination + BI->setOperand(1, 0); // Clear the conditional destination + BI->setOperand(2, 0); // Clear the condition... + return true; + } + } + return false; +} + +// ConstantFoldInstruction - If an instruction references constants, try to fold +// them together... +// +inline static bool +ConstantFoldInstruction(Method *M, Method::inst_iterator &II) { + Instruction *Inst = *II; + if (Inst->isBinaryOp()) { + Value *D1, *D2; + if (((D1 = Inst->getOperand(0))->getValueType() == Value::ConstantVal) & + ((D2 = Inst->getOperand(1))->getValueType() == Value::ConstantVal)) + return ConstantFoldBinaryInst(M, II, (BinaryOperator*)Inst, + (ConstPoolVal*)D1, (ConstPoolVal*)D2); + + } else if (Inst->isUnaryOp()) { + Value *D; + if ((D = Inst->getOperand(0))->getValueType() == Value::ConstantVal) + return ConstantFoldUnaryInst(M, II, (UnaryOperator*)Inst, + (ConstPoolVal*)D); + } else if (Inst->isTerminator()) { + return ConstantFoldTerminator((TerminatorInst*)Inst); + + } else if (Inst->getInstType() == Instruction::PHINode) { + PHINode *PN = (PHINode*)Inst; // If it's a PHI node and only has one operand + // Then replace it directly with that operand. + assert(PN->getOperand(0) && "PHI Node must have at least one operand!"); + if (PN->getOperand(1) == 0) { // If the PHI Node has exactly 1 operand + Value *V = PN->getOperand(0); + PN->replaceAllUsesWith(V); // Replace all uses of this PHI + // Unlink from basic block + PN->getParent()->getInstList().remove(II.getInstructionIterator()); + if (PN->hasName()) V->setName(PN->getName()); // Inherit PHINode name + delete PN; // Finally, delete the node... + return true; + } + } + return false; +} + +// DoConstPropPass - Propogate constants and do constant folding on instructions +// this returns true if something was changed, false if nothing was changed. +// +static bool DoConstPropPass(Method *M) { + bool SomethingChanged = false; + +#if 1 + Method::inst_iterator It = M->inst_begin(); + while (It != M->inst_end()) + if (ConstantFoldInstruction(M, It)) { + SomethingChanged = true; // If returned true, iter is already incremented + + // Incrementing the iterator in an unchecked manner could mess up the + // internals of 'It'. To make sure everything is happy, tell it we might + // have broken it. + It.resyncInstructionIterator(); + } else { + ++It; + } +#else + Method::BasicBlocksType::iterator BBIt = M->getBasicBlocks().begin(); + for (; BBIt != M->getBasicBlocks().end(); BBIt++) { + BasicBlock *BB = *BBIt; + + BasicBlock::InstListType::iterator DI = BB->getInstList().begin(); + for (; DI != BB->getInstList().end(); DI++) + SomethingChanged |= ConstantFoldInstruction(M, DI); + } +#endif + return SomethingChanged; +} + + +// returns true on failure, false on success... +// +bool DoConstantPropogation(Method *M) { + bool Modified = false; + + // Fold constants until we make no progress... + while (DoConstPropPass(M)) Modified = true; + + // Merge identical constants last: this is important because we may have just + // introduced constants that already exist! + // + Modified |= MergeConstantPoolReferences(M->getConstantPool()); + + return Modified; +} diff --git a/lib/Transforms/Scalar/DCE.cpp b/lib/Transforms/Scalar/DCE.cpp new file mode 100644 index 0000000000..797edf5054 --- /dev/null +++ b/lib/Transforms/Scalar/DCE.cpp @@ -0,0 +1,193 @@ +//===- DCE.cpp - Code to perform dead code elimination --------------------===// +// +// This file implements dead code elimination and basic block merging. +// +// Specifically, this: +// * removes definitions with no uses (including unused constants) +// * removes basic blocks with no predecessors +// * merges a basic block into its predecessor if there is only one and the +// predecessor only has one successor. +// +// TODO: This should REALLY be recursive instead of iterative. Right now, we +// scan linearly through values, removing unused ones as we go. The problem is +// that this may cause other earlier values to become unused. To make sure that +// we get them all, we iterate until things stop changing. Instead, when +// removing a value, recheck all of its operands to see if they are now unused. +// Piece of cake, and more efficient as well. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Module.h" +#include "llvm/Method.h" +#include "llvm/BasicBlock.h" +#include "llvm/iTerminators.h" +#include "llvm/Opt/AllOpts.h" + +struct ConstPoolDCE { + enum { EndOffs = 0 }; + static bool isDCEable(const Value *) { return true; } +}; + +struct BasicBlockDCE { + enum { EndOffs = 1 }; + static bool isDCEable(const Instruction *I) { + return !I->hasSideEffects(); + } +}; + +template +static bool RemoveUnusedDefs(ValueHolder &Vals, + DCEController DCEControl) { + bool Changed = false; + typedef ValueHolder Container; + + int Offset = DCEController::EndOffs; + for (Container::iterator DI = Vals.begin(); DI != Vals.end()-Offset; ) { + // Look for un"used" definitions... + if ((*DI)->use_empty() && DCEController::isDCEable(*DI)) { + // Bye bye + delete Vals.remove(DI); + Changed = true; + } else { + DI++; + } + } + return Changed; +} + + +bool DoRemoveUnusedConstants(SymTabValue *S) { + bool Changed = false; + ConstantPool &CP = S->getConstantPool(); + for (ConstantPool::plane_iterator PI = CP.begin(); PI != CP.end(); ++PI) + Changed |= RemoveUnusedDefs(**PI, ConstPoolDCE()); + return Changed; +} + + +static void ReplaceUsesWithConstant(Instruction *I) { + // Get the method level constant pool + ConstantPool &CP = I->getParent()->getParent()->getConstantPool(); + + ConstPoolVal *CPV = 0; + ConstantPool::PlaneType *P; + if (!CP.getPlane(I->getType(), P)) { // Does plane exist? + // Yes, is it empty? + if (!P->empty()) CPV = P->front(); + } + + if (CPV == 0) { // We don't have an existing constant to reuse. Just add one. + CPV = ConstPoolVal::getNullConstant(I->getType()); // Create a new constant + + // Add the new value to the constant pool... + CP.insert(CPV); + } + + // Make all users of this instruction reference the constant instead + I->replaceAllUsesWith(CPV); +} + +static bool DoDCEPass(Method *M) { + Method::BasicBlocksType::iterator BBIt; + Method::BasicBlocksType &BBs = M->getBasicBlocks(); + bool Changed = false; + + // Loop through now and remove instructions that have no uses... + for (BBIt = BBs.begin(); BBIt != BBs.end(); BBIt++) + Changed |= RemoveUnusedDefs((*BBIt)->getInstList(), BasicBlockDCE()); + + // Scan through and remove basic blocks that have no predecessors (except, + // of course, the first one. :) (so skip first block) + // + for (BBIt = BBs.begin(), ++BBIt; BBIt != BBs.end(); BBIt++) { + BasicBlock *BB = *BBIt; + assert(BB->getTerminator() && + "Degenerate basic block encountered!"); // Empty bb??? + + if (BB->pred_begin() == BB->pred_end() && + !BB->hasConstantPoolReferences()) { + + while (!BB->getInstList().empty()) { + Instruction *I = BB->getInstList().front(); + // If this instruction is used, replace uses with an arbitrary + // constant value. Because control flow can't get here, we don't care + // what we replace the value with. + if (!I->use_empty()) ReplaceUsesWithConstant(I); + + // Remove the instruction from the basic block + BasicBlock::InstListType::iterator f = BB->getInstList().begin(); + delete BB->getInstList().remove(f); + } + + delete BBs.remove(BBIt); + ++BBIt; // remove puts use on the previous block, we want the next one + Changed = true; + } + } + + // Loop through an merge basic blocks into their predecessor if there is only + // one, and if there is only one successor of the predecessor. + // + for (BBIt = BBs.begin(); BBIt != BBs.end(); BBIt++) { + BasicBlock *BB = *BBIt; + + // Is there exactly one predecessor to this block? + BasicBlock::pred_iterator PI(BB->pred_begin()); + if (PI != BB->pred_end() && ++PI == BB->pred_end() && + !BB->hasConstantPoolReferences()) { + BasicBlock *Pred = *BB->pred_begin(); + TerminatorInst *Term = Pred->getTerminator(); + if (Term == 0) continue; // Err... malformed basic block! + + // Is it an unconditional branch? + if (Term->getInstType() != Instruction::Br || + !((BranchInst*)Term)->isUnconditional()) + continue; // Nope, maybe next time... + + Changed = true; + + // Make all branches to the predecessor now point to the successor... + Pred->replaceAllUsesWith(BB); + + // Move all definitions in the predecessor to the successor... + BasicBlock::InstListType::iterator DI = Pred->getInstList().end(); + assert(Pred->getTerminator() && + "Degenerate basic block encountered!"); // Empty bb??? + delete Pred->getInstList().remove(--DI); // Remove terminator + + while (Pred->getInstList().begin() != (DI = Pred->getInstList().end())) { + Instruction *Def = Pred->getInstList().remove(--DI); // Remove from end + BB->getInstList().push_front(Def); // Add to front... + } + + // Remove basic block from the method... + BBs.remove(Pred); + + // Always inherit predecessors name if it exists... + if (Pred->hasName()) BB->setName(Pred->getName()); + + // So long you waste of a basic block you... + delete Pred; + } + } + + // Remove unused constants + Changed |= DoRemoveUnusedConstants(M); + return Changed; +} + + +// It is possible that we may require multiple passes over the code to fully +// eliminate dead code. Iterate until we are done. +// +bool DoDeadCodeElimination(Method *M) { + bool Changed = false; + while (DoDCEPass(M)) Changed = true; + return Changed; +} + +bool DoDeadCodeElimination(Module *C) { + bool Val = ApplyOptToAllMethods(C, DoDeadCodeElimination); + while (DoRemoveUnusedConstants(C)) Val = true; + return Val; +} diff --git a/lib/Transforms/Scalar/SymbolStripping.cpp b/lib/Transforms/Scalar/SymbolStripping.cpp new file mode 100644 index 0000000000..af5f18f305 --- /dev/null +++ b/lib/Transforms/Scalar/SymbolStripping.cpp @@ -0,0 +1,55 @@ +//===- SymbolStripping.cpp - Code to string symbols for methods and modules -=// +// +// This file implements stripping symbols out of symbol tables. +// +// Specifically, this allows you to strip all of the symbols out of: +// * A method +// * All methods in a module +// * All symbols in a module (all method symbols + all module scope symbols) +// +// Notice that: +// * This pass makes code much less readable, so it should only be used in +// situations where the 'strip' utility would be used (such as reducing +// code size, and making it harder to reverse engineer code). +// +//===----------------------------------------------------------------------===// + +#include "llvm/Module.h" +#include "llvm/Method.h" +#include "llvm/SymbolTable.h" +#include "llvm/Opt/AllOpts.h" + +static bool StripSymbolTable(SymbolTable *SymTab) { + if (SymTab == 0) return false; // No symbol table? No problem. + bool RemovedSymbol = false; + + for (SymbolTable::iterator I = SymTab->begin(); I != SymTab->end(); I++) { + map &Plane = I->second; + + map::iterator B; + while ((B = Plane.begin()) != Plane.end()) { // Found nonempty type plane! + B->second->setName(""); // Set name to "", removing from symbol table! + RemovedSymbol = true; + assert(Plane.begin() != B); + } + } + + return RemovedSymbol; +} + + +// DoSymbolStripping - Remove all symbolic information from a method +// +bool DoSymbolStripping(Method *M) { + return StripSymbolTable(M->getSymbolTable()); +} + +// DoFullSymbolStripping - Remove all symbolic information from all methods +// in a module, and all module level symbols. (method names, etc...) +// +bool DoFullSymbolStripping(Module *M) { + // Remove all symbols from methods in this module... and then strip all of the + // symbols in this module... + // + return DoSymbolStripping(M) | StripSymbolTable(M->getSymbolTable()); +} diff --git a/lib/VMCore/AsmWriter.cpp b/lib/VMCore/AsmWriter.cpp new file mode 100644 index 0000000000..e23403b0d0 --- /dev/null +++ b/lib/VMCore/AsmWriter.cpp @@ -0,0 +1,328 @@ +//===-- Writer.cpp - Library for Printing VM assembly files ------*- C++ -*--=// +// +// This library implements the functionality defined in llvm/Assembly/Writer.h +// +// This library uses the Analysis library to figure out offsets for +// variables in the method tables... +// +// TODO: print out the type name instead of the full type if a particular type +// is in the symbol table... +// +//===----------------------------------------------------------------------===// + +#include "llvm/Assembly/Writer.h" +#include "llvm/Analysis/SlotCalculator.h" +#include "llvm/Module.h" +#include "llvm/Method.h" +#include "llvm/BasicBlock.h" +#include "llvm/ConstPoolVals.h" +#include "llvm/iOther.h" +#include "llvm/iMemory.h" + +class AssemblyWriter : public ModuleAnalyzer { + ostream &Out; + SlotCalculator &Table; +public: + inline AssemblyWriter(ostream &o, SlotCalculator &Tab) : Out(o), Table(Tab) { + } + + inline void write(const Module *M) { processModule(M); } + inline void write(const Method *M) { processMethod(M); } + inline void write(const BasicBlock *BB) { processBasicBlock(BB); } + inline void write(const Instruction *I) { processInstruction(I); } + inline void write(const ConstPoolVal *CPV) { processConstant(CPV); } + +protected: + virtual bool visitMethod(const Method *M); + virtual bool processConstPool(const ConstantPool &CP, bool isMethod); + virtual bool processConstant(const ConstPoolVal *CPV); + virtual bool processMethod(const Method *M); + virtual bool processMethodArgument(const MethodArgument *MA); + virtual bool processBasicBlock(const BasicBlock *BB); + virtual bool processInstruction(const Instruction *I); + +private : + void writeOperand(const Value *Op, bool PrintType, bool PrintName = true); +}; + + + +// visitMethod - This member is called after the above two steps, visting each +// method, because they are effectively values that go into the constant pool. +// +bool AssemblyWriter::visitMethod(const Method *M) { + return false; +} + +bool AssemblyWriter::processConstPool(const ConstantPool &CP, bool isMethod) { + // Done printing arguments... + if (isMethod) Out << ")\n"; + + ModuleAnalyzer::processConstPool(CP, isMethod); + + if (isMethod) + Out << "begin"; + else + Out << "implementation\n"; + return false; +} + + +// processConstant - Print out a constant pool entry... +// +bool AssemblyWriter::processConstant(const ConstPoolVal *CPV) { + Out << "\t"; + + // Print out name if it exists... + if (CPV->hasName()) + Out << "%" << CPV->getName() << " = "; + + // Print out the opcode... + Out << CPV->getType(); + + // Write the value out now... + writeOperand(CPV, false, false); + + if (!CPV->hasName() && CPV->getType() != Type::VoidTy) { + int Slot = Table.getValSlot(CPV); // Print out the def slot taken... + Out << "\t\t; <" << CPV->getType() << ">:"; + if (Slot >= 0) Out << Slot; + else Out << ""; + } + + Out << endl; + return false; +} + +// processMethod - Process all aspects of a method. +// +bool AssemblyWriter::processMethod(const Method *M) { + // Print out the return type and name... + Out << "\n" << M->getReturnType() << " \"" << M->getName() << "\"("; + Table.incorporateMethod(M); + ModuleAnalyzer::processMethod(M); + Table.purgeMethod(); + Out << "end\n"; + return false; +} + +// processMethodArgument - This member is called for every argument that +// is passed into the method. Simply print it out +// +bool AssemblyWriter::processMethodArgument(const MethodArgument *Arg) { + // Insert commas as we go... the first arg doesn't get a comma + if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", "; + + // Output type... + Out << Arg->getType(); + + // Output name, if available... + if (Arg->hasName()) + Out << " %" << Arg->getName(); + else if (Table.getValSlot(Arg) < 0) + Out << ""; + + return false; +} + +// processBasicBlock - This member is called for each basic block in a methd. +// +bool AssemblyWriter::processBasicBlock(const BasicBlock *BB) { + if (BB->hasName()) { // Print out the label if it exists... + Out << "\n" << BB->getName() << ":\n"; + } else { + int Slot = Table.getValSlot(BB); + Out << "\t\t\t\t;