diff options
Diffstat (limited to 'projects/Stacker/lib/compiler/StackerCompiler.cpp')
-rw-r--r-- | projects/Stacker/lib/compiler/StackerCompiler.cpp | 1860 |
1 files changed, 930 insertions, 930 deletions
diff --git a/projects/Stacker/lib/compiler/StackerCompiler.cpp b/projects/Stacker/lib/compiler/StackerCompiler.cpp index 7b1ebe02f9..c9a3e62627 100644 --- a/projects/Stacker/lib/compiler/StackerCompiler.cpp +++ b/projects/Stacker/lib/compiler/StackerCompiler.cpp @@ -39,7 +39,7 @@ extern int Stackerparse(); StackerCompiler* StackerCompiler::TheInstance = 0; static Statistic<> NumDefinitions( - "numdefs","The # of definitions encoutered while compiling Stacker"); + "numdefs","The # of definitions encoutered while compiling Stacker"); StackerCompiler::StackerCompiler() : CurFilename("") @@ -106,160 +106,160 @@ StackerCompiler::compile( /// if (filename != "-") { - F = fopen(filename.c_str(), "r"); + F = fopen(filename.c_str(), "r"); - if (F == 0) - { - throw ParseException(filename, - "Could not open file '" + filename + "'"); - } + if (F == 0) + { + throw ParseException(filename, + "Could not open file '" + filename + "'"); + } } Module *Result; try { - // Create the module we'll return - TheModule = new Module( CurFilename ); + // Create the module we'll return + TheModule = new Module( CurFilename ); // Tell the module about our runtime library TheModule->addLibrary("stkr_runtime"); - // Create a type to represent the stack. This is the same as the LLVM - // Assembly type [ 256 x long ] - stack_type = ArrayType::get( Type::LongTy, stack_size ); - - // Create a global variable for the stack. Note the use of appending - // linkage linkage so that multiple modules will make the stack larger. - // Also note that the last argument causes the global to be inserted - // automatically into the module. - TheStack = new GlobalVariable( - /*type=*/ stack_type, - /*isConstant=*/ false, - /*Linkage=*/ GlobalValue::LinkOnceLinkage, - /*initializer=*/ Constant::getNullValue(stack_type), - /*name=*/ "_stack_", - /*parent=*/ TheModule - ); - - // Create a global variable for indexing into the stack. Note the use - // of LinkOnce linkage. Only one copy of _index_ will be retained - // after linking - TheIndex = new GlobalVariable( - /*type=*/Type::LongTy, - /*isConstant=*/false, - /*Linkage=*/GlobalValue::LinkOnceLinkage, - /*initializer=*/ Constant::getNullValue(Type::LongTy), - /*name=*/"_index_", - /*parent=*/TheModule - ); - - // Create a function prototype for definitions. No parameters, no - // result. This is used below any time a function is created. - std::vector<const Type*> params; // No parameters - DefinitionType = FunctionType::get( Type::VoidTy, params, false ); - - // Create a function for printf(3) - params.push_back( PointerType::get( Type::SByteTy ) ); - FunctionType* printf_type = - FunctionType::get( Type::IntTy, params, true ); - ThePrintf = new Function( - printf_type, GlobalValue::ExternalLinkage, "printf", TheModule); - - // Create a function for scanf(3) - TheScanf = new Function( - printf_type, GlobalValue::ExternalLinkage, "scanf", TheModule); - - // Create a function for exit(3) - params.clear(); - params.push_back( Type::IntTy ); - FunctionType* exit_type = - FunctionType::get( Type::VoidTy, params, false ); - TheExit = new Function( - exit_type, GlobalValue::ExternalLinkage, "exit", TheModule); - - Constant* str_format = ConstantArray::get("%s"); - StrFormat = new GlobalVariable( - /*type=*/ArrayType::get( Type::SByteTy, 3 ), - /*isConstant=*/true, - /*Linkage=*/GlobalValue::LinkOnceLinkage, - /*initializer=*/str_format, - /*name=*/"_str_format_", - /*parent=*/TheModule - ); - - Constant* in_str_format = ConstantArray::get(" %as"); - InStrFormat = new GlobalVariable( - /*type=*/ArrayType::get( Type::SByteTy, 5 ), - /*isConstant=*/true, - /*Linkage=*/GlobalValue::LinkOnceLinkage, - /*initializer=*/in_str_format, - /*name=*/"_in_str_format_", - /*parent=*/TheModule - ); - - Constant* num_format = ConstantArray::get("%d"); - NumFormat = new GlobalVariable( - /*type=*/ArrayType::get( Type::SByteTy, 3 ), - /*isConstant=*/true, - /*Linkage=*/GlobalValue::LinkOnceLinkage, - /*initializer=*/num_format, - /*name=*/"_num_format_", - /*parent=*/TheModule - ); - - Constant* in_num_format = ConstantArray::get(" %d"); - InNumFormat = new GlobalVariable( - /*type=*/ArrayType::get( Type::SByteTy, 4 ), - /*isConstant=*/true, - /*Linkage=*/GlobalValue::LinkOnceLinkage, - /*initializer=*/in_num_format, - /*name=*/"_in_num_format_", - /*parent=*/TheModule - ); - - Constant* chr_format = ConstantArray::get("%c"); - ChrFormat = new GlobalVariable( - /*type=*/ArrayType::get( Type::SByteTy, 3 ), - /*isConstant=*/true, - /*Linkage=*/GlobalValue::LinkOnceLinkage, - /*initializer=*/chr_format, - /*name=*/"_chr_format_", - /*parent=*/TheModule - ); - - Constant* in_chr_format = ConstantArray::get(" %c"); - InChrFormat = new GlobalVariable( - /*type=*/ArrayType::get( Type::SByteTy, 4 ), - /*isConstant=*/true, - /*Linkage=*/GlobalValue::LinkOnceLinkage, - /*initializer=*/in_chr_format, - /*name=*/"_in_chr_format_", - /*parent=*/TheModule - ); - - // Get some constants so we aren't always creating them - Zero = ConstantInt::get( Type::LongTy, 0 ); - One = ConstantInt::get( Type::LongTy, 1 ); - Two = ConstantInt::get( Type::LongTy, 2 ); - Three = ConstantInt::get( Type::LongTy, 3 ); - Four = ConstantInt::get( Type::LongTy, 4 ); - Five = ConstantInt::get( Type::LongTy, 5 ); - - // Reset the current line number - Stackerlineno = 1; - - // Reset the parser's input to F - Stackerin = F; // Set the input file. - - // Let the parse know about this instance - TheInstance = this; - - // Parse the file. The parser (see StackParser.y) will call back to - // the StackerCompiler via the "handle*" methods - Stackerparse(); - - // Avoid potential illegal use (TheInstance might be on the stack) - TheInstance = 0; + // Create a type to represent the stack. This is the same as the LLVM + // Assembly type [ 256 x long ] + stack_type = ArrayType::get( Type::LongTy, stack_size ); + + // Create a global variable for the stack. Note the use of appending + // linkage linkage so that multiple modules will make the stack larger. + // Also note that the last argument causes the global to be inserted + // automatically into the module. + TheStack = new GlobalVariable( + /*type=*/ stack_type, + /*isConstant=*/ false, + /*Linkage=*/ GlobalValue::LinkOnceLinkage, + /*initializer=*/ Constant::getNullValue(stack_type), + /*name=*/ "_stack_", + /*parent=*/ TheModule + ); + + // Create a global variable for indexing into the stack. Note the use + // of LinkOnce linkage. Only one copy of _index_ will be retained + // after linking + TheIndex = new GlobalVariable( + /*type=*/Type::LongTy, + /*isConstant=*/false, + /*Linkage=*/GlobalValue::LinkOnceLinkage, + /*initializer=*/ Constant::getNullValue(Type::LongTy), + /*name=*/"_index_", + /*parent=*/TheModule + ); + + // Create a function prototype for definitions. No parameters, no + // result. This is used below any time a function is created. + std::vector<const Type*> params; // No parameters + DefinitionType = FunctionType::get( Type::VoidTy, params, false ); + + // Create a function for printf(3) + params.push_back( PointerType::get( Type::SByteTy ) ); + FunctionType* printf_type = + FunctionType::get( Type::IntTy, params, true ); + ThePrintf = new Function( + printf_type, GlobalValue::ExternalLinkage, "printf", TheModule); + + // Create a function for scanf(3) + TheScanf = new Function( + printf_type, GlobalValue::ExternalLinkage, "scanf", TheModule); + + // Create a function for exit(3) + params.clear(); + params.push_back( Type::IntTy ); + FunctionType* exit_type = + FunctionType::get( Type::VoidTy, params, false ); + TheExit = new Function( + exit_type, GlobalValue::ExternalLinkage, "exit", TheModule); + + Constant* str_format = ConstantArray::get("%s"); + StrFormat = new GlobalVariable( + /*type=*/ArrayType::get( Type::SByteTy, 3 ), + /*isConstant=*/true, + /*Linkage=*/GlobalValue::LinkOnceLinkage, + /*initializer=*/str_format, + /*name=*/"_str_format_", + /*parent=*/TheModule + ); + + Constant* in_str_format = ConstantArray::get(" %as"); + InStrFormat = new GlobalVariable( + /*type=*/ArrayType::get( Type::SByteTy, 5 ), + /*isConstant=*/true, + /*Linkage=*/GlobalValue::LinkOnceLinkage, + /*initializer=*/in_str_format, + /*name=*/"_in_str_format_", + /*parent=*/TheModule + ); + + Constant* num_format = ConstantArray::get("%d"); + NumFormat = new GlobalVariable( + /*type=*/ArrayType::get( Type::SByteTy, 3 ), + /*isConstant=*/true, + /*Linkage=*/GlobalValue::LinkOnceLinkage, + /*initializer=*/num_format, + /*name=*/"_num_format_", + /*parent=*/TheModule + ); + + Constant* in_num_format = ConstantArray::get(" %d"); + InNumFormat = new GlobalVariable( + /*type=*/ArrayType::get( Type::SByteTy, 4 ), + /*isConstant=*/true, + /*Linkage=*/GlobalValue::LinkOnceLinkage, + /*initializer=*/in_num_format, + /*name=*/"_in_num_format_", + /*parent=*/TheModule + ); + + Constant* chr_format = ConstantArray::get("%c"); + ChrFormat = new GlobalVariable( + /*type=*/ArrayType::get( Type::SByteTy, 3 ), + /*isConstant=*/true, + /*Linkage=*/GlobalValue::LinkOnceLinkage, + /*initializer=*/chr_format, + /*name=*/"_chr_format_", + /*parent=*/TheModule + ); + + Constant* in_chr_format = ConstantArray::get(" %c"); + InChrFormat = new GlobalVariable( + /*type=*/ArrayType::get( Type::SByteTy, 4 ), + /*isConstant=*/true, + /*Linkage=*/GlobalValue::LinkOnceLinkage, + /*initializer=*/in_chr_format, + /*name=*/"_in_chr_format_", + /*parent=*/TheModule + ); + + // Get some constants so we aren't always creating them + Zero = ConstantInt::get( Type::LongTy, 0 ); + One = ConstantInt::get( Type::LongTy, 1 ); + Two = ConstantInt::get( Type::LongTy, 2 ); + Three = ConstantInt::get( Type::LongTy, 3 ); + Four = ConstantInt::get( Type::LongTy, 4 ); + Five = ConstantInt::get( Type::LongTy, 5 ); + + // Reset the current line number + Stackerlineno = 1; + + // Reset the parser's input to F + Stackerin = F; // Set the input file. + + // Let the parse know about this instance + TheInstance = this; + + // Parse the file. The parser (see StackParser.y) will call back to + // the StackerCompiler via the "handle*" methods + Stackerparse(); + + // Avoid potential illegal use (TheInstance might be on the stack) + TheInstance = 0; // Set up a pass manager PassManager Passes; @@ -342,8 +342,8 @@ StackerCompiler::compile( Passes.run(*TheModule); } catch (...) { - if (F != stdin) fclose(F); // Make sure to close file descriptor - throw; // if an exception is thrown + if (F != stdin) fclose(F); // Make sure to close file descriptor + throw; // if an exception is thrown } // Close the file @@ -370,7 +370,7 @@ StackerCompiler::incr_stack_index( BasicBlock* bb, Value* ival = 0 ) CastInst* caster = new CastInst( ival, Type::LongTy ); bb->getInstList().push_back( caster ); BinaryOperator* addop = BinaryOperator::create( Instruction::Add, - loadop, caster); + loadop, caster); bb->getInstList().push_back( addop ); // Store the incremented value @@ -391,7 +391,7 @@ StackerCompiler::decr_stack_index( BasicBlock* bb, Value* ival = 0 ) CastInst* caster = new CastInst( ival, Type::LongTy ); bb->getInstList().push_back( caster ); BinaryOperator* subop = BinaryOperator::create( Instruction::Sub, - loadop, caster); + loadop, caster); bb->getInstList().push_back( subop ); // Store the incremented value @@ -418,21 +418,21 @@ StackerCompiler::get_stack_pointer( BasicBlock* bb, Value* index = 0 ) if ( index == 0 ) { - indexVec.push_back(loadop); + indexVec.push_back(loadop); } else { - CastInst* caster = new CastInst( index, Type::LongTy ); - bb->getInstList().push_back( caster ); - BinaryOperator* subop = BinaryOperator::create( - Instruction::Sub, loadop, caster ); - bb->getInstList().push_back( subop ); - indexVec.push_back(subop); + CastInst* caster = new CastInst( index, Type::LongTy ); + bb->getInstList().push_back( caster ); + BinaryOperator* subop = BinaryOperator::create( + Instruction::Sub, loadop, caster ); + bb->getInstList().push_back( subop ); + indexVec.push_back(subop); } // Get the address of the indexed stack element GetElementPtrInst* gep = new GetElementPtrInst( TheStack, indexVec ); - bb->getInstList().push_back( gep ); // Put GEP in Block + bb->getInstList().push_back( gep ); // Put GEP in Block return gep; } @@ -445,7 +445,7 @@ StackerCompiler::push_value( BasicBlock* bb, Value* val ) // Get the stack pointer GetElementPtrInst* gep = cast<GetElementPtrInst>( - get_stack_pointer( bb ) ); + get_stack_pointer( bb ) ); // Cast the value to a long .. hopefully it works CastInst* cast_inst = new CastInst( val, Type::LongTy ); @@ -470,7 +470,7 @@ StackerCompiler::pop_integer( BasicBlock*bb ) { // Get the stack pointer GetElementPtrInst* gep = cast<GetElementPtrInst>( - get_stack_pointer( bb )); + get_stack_pointer( bb )); // Load the value LoadInst* load_inst = new LoadInst( gep ); @@ -498,12 +498,12 @@ StackerCompiler::push_string( BasicBlock* bb, const char* value ) // Create an internal linkage global variable to hold the constant. GlobalVariable* strconst = new GlobalVariable( - char_array, - /*isConstant=*/true, - GlobalValue::InternalLinkage, - /*initializer=*/initVal, - "", - TheModule + char_array, + /*isConstant=*/true, + GlobalValue::InternalLinkage, + /*initializer=*/initVal, + "", + TheModule ); // Push the casted value @@ -515,7 +515,7 @@ StackerCompiler::pop_string( BasicBlock* bb ) { // Get location of stack pointer GetElementPtrInst* gep = cast<GetElementPtrInst>( - get_stack_pointer( bb )); + get_stack_pointer( bb )); // Load the value from the stack LoadInst* loader = new LoadInst( gep ); @@ -537,7 +537,7 @@ StackerCompiler::replace_top( BasicBlock* bb, Value* new_top, Value* index = 0 ) { // Get the stack pointer GetElementPtrInst* gep = cast<GetElementPtrInst>( - get_stack_pointer( bb, index )); + get_stack_pointer( bb, index )); // Store the value there StoreInst* store_inst = new StoreInst( new_top, gep ); @@ -552,7 +552,7 @@ StackerCompiler::stack_top( BasicBlock* bb, Value* index = 0 ) { // Get the stack pointer GetElementPtrInst* gep = cast<GetElementPtrInst>( - get_stack_pointer( bb, index )); + get_stack_pointer( bb, index )); // Load the value LoadInst* load_inst = new LoadInst( gep ); @@ -567,7 +567,7 @@ StackerCompiler::stack_top_string( BasicBlock* bb, Value* index = 0 ) { // Get location of stack pointer GetElementPtrInst* gep = cast<GetElementPtrInst>( - get_stack_pointer( bb, index )); + get_stack_pointer( bb, index )); // Load the value from the stack LoadInst* loader = new LoadInst( gep ); @@ -586,8 +586,8 @@ add_block( Function*f, BasicBlock* bb ) { if ( ! f->empty() && f->back().getTerminator() == 0 ) { - BranchInst* branch = new BranchInst(bb); - f->back().getInstList().push_back( branch ); + BranchInst* branch = new BranchInst(bb); + f->back().getInstList().push_back( branch ); } f->getBasicBlockList().push_back( bb ); } @@ -622,11 +622,11 @@ StackerCompiler::handle_definition_list_end( Module* mod, Function* definition ) { if ( ! definition->empty() ) { - BasicBlock& last_block = definition->back(); - if ( last_block.getTerminator() == 0 ) - { - last_block.getInstList().push_back( new ReturnInst() ); - } + BasicBlock& last_block = definition->back(); + if ( last_block.getTerminator() == 0 ) + { + last_block.getInstList().push_back( new ReturnInst() ); + } } // Insert the definition into the module mod->getFunctionList().push_back( definition ); @@ -661,9 +661,9 @@ StackerCompiler::handle_forward( char * name ) { // Just create a placeholder function Function* the_function = new Function ( - DefinitionType, - GlobalValue::ExternalLinkage, - name ); + DefinitionType, + GlobalValue::ExternalLinkage, + name ); assert( the_function->isExternal() ); free( name ); @@ -681,9 +681,9 @@ StackerCompiler::handle_definition( char * name, Function* f ) // If the function already exists... if ( existing_function ) { - // Just get rid of the placeholder - existing_function->dropAllReferences(); - delete existing_function; + // Just get rid of the placeholder + existing_function->dropAllReferences(); + delete existing_function; } #endif @@ -720,7 +720,7 @@ StackerCompiler::handle_if( char* ifTrue, char* ifFalse ) // Compare the condition against 0 SetCondInst* cond_inst = new SetCondInst( Instruction::SetNE, cond, - ConstantSInt::get( Type::LongTy, 0) ); + ConstantSInt::get( Type::LongTy, 0) ); bb->getInstList().push_back( cond_inst ); // Create an exit block @@ -735,22 +735,22 @@ StackerCompiler::handle_if( char* ifTrue, char* ifFalse ) // Create a branch on the SetCond BranchInst* br_inst = new BranchInst( true_bb, - ( ifFalse ? false_bb : exit_bb ), cond_inst ); + ( ifFalse ? false_bb : exit_bb ), cond_inst ); bb->getInstList().push_back( br_inst ); // Fill the true block std::vector<Value*> args; if ( Function* true_func = TheModule->getNamedFunction(ifTrue) ) { - true_bb->getInstList().push_back( - new CallInst( true_func, args ) ); - true_bb->getInstList().push_back( - new BranchInst( exit_bb ) ); + true_bb->getInstList().push_back( + new CallInst( true_func, args ) ); + true_bb->getInstList().push_back( + new BranchInst( exit_bb ) ); } else { - ThrowException(std::string("Function '") + ifTrue + - "' must be declared first.'"); + ThrowException(std::string("Function '") + ifTrue + + "' must be declared first.'"); } free( ifTrue ); @@ -758,19 +758,19 @@ StackerCompiler::handle_if( char* ifTrue, char* ifFalse ) // Fill the false block if ( false_bb ) { - if ( Function* false_func = TheModule->getNamedFunction(ifFalse) ) - { - false_bb->getInstList().push_back( - new CallInst( false_func, args ) ); - false_bb->getInstList().push_back( - new BranchInst( exit_bb ) ); - } - else - { - ThrowException(std::string("Function '") + ifFalse + - "' must be declared first.'"); - } - free( ifFalse ); + if ( Function* false_func = TheModule->getNamedFunction(ifFalse) ) + { + false_bb->getInstList().push_back( + new CallInst( false_func, args ) ); + false_bb->getInstList().push_back( + new BranchInst( exit_bb ) ); + } + else + { + ThrowException(std::string("Function '") + ifFalse + + "' must be declared first.'"); + } + free( ifFalse ); } // Add the blocks to the function @@ -804,7 +804,7 @@ StackerCompiler::handle_while( char* todo ) // Compare the condition against 0 SetCondInst* cond_inst = new SetCondInst( - Instruction::SetNE, cond, ConstantSInt::get( Type::LongTy, 0) ); + Instruction::SetNE, cond, ConstantSInt::get( Type::LongTy, 0) ); test->getInstList().push_back( cond_inst ); // Add the branch instruction @@ -815,13 +815,13 @@ StackerCompiler::handle_while( char* todo ) std::vector<Value*> args; if ( Function* body_func = TheModule->getNamedFunction(todo) ) { - body->getInstList().push_back( new CallInst( body_func, args ) ); - body->getInstList().push_back( new BranchInst( test ) ); + body->getInstList().push_back( new CallInst( body_func, args ) ); + body->getInstList().push_back( new BranchInst( test ) ); } else { - ThrowException(std::string("Function '") + todo + - "' must be declared first.'"); + ThrowException(std::string("Function '") + todo + + "' must be declared first.'"); } free( todo ); @@ -841,13 +841,13 @@ StackerCompiler::handle_identifier( char * name ) BasicBlock* bb = new BasicBlock((echo?"call":"")); if ( func ) { - CallInst* call_def = new CallInst( func , no_arguments ); - bb->getInstList().push_back( call_def ); + CallInst* call_def = new CallInst( func , no_arguments ); + bb->getInstList().push_back( call_def ); } else { - ThrowException(std::string("Definition '") + name + - "' must be defined before it can be used."); + ThrowException(std::string("Definition '") + name + + "' must be defined before it can be used."); } free( name ); @@ -892,910 +892,910 @@ StackerCompiler::handle_word( int tkn ) { case DUMP : // Dump the stack (debugging aid) { - if (echo) bb->setName("DUMP"); - Function* f = TheModule->getOrInsertFunction( - "_stacker_dump_stack_", DefinitionType); - std::vector<Value*> args; - bb->getInstList().push_back( new CallInst( f, args ) ); - break; + if (echo) bb->setName("DUMP"); + Function* f = TheModule->getOrInsertFunction( + "_stacker_dump_stack_", DefinitionType); + std::vector<Value*> args; + bb->getInstList().push_back( new CallInst( f, args ) ); + break; } // Logical Operations case TRUETOK : // -- -1 { - if (echo) bb->setName("TRUE"); - push_integer(bb,-1); - break; + if (echo) bb->setName("TRUE"); + push_integer(bb,-1); + break; } case FALSETOK : // -- 0 { - if (echo) bb->setName("FALSE"); - push_integer(bb,0); - break; + if (echo) bb->setName("FALSE"); + push_integer(bb,0); + break; } case LESS : // w1 w2 -- w2<w1 { - if (echo) bb->setName("LESS"); - LoadInst* op1 = cast<LoadInst>(pop_integer(bb)); - LoadInst* op2 = cast<LoadInst>(pop_integer(bb)); - SetCondInst* cond_inst = - new SetCondInst( Instruction::SetLT, op1, op2 ); - bb->getInstList().push_back( cond_inst ); - push_value( bb, cond_inst ); - break; + if (echo) bb->setName("LESS"); + LoadInst* op1 = cast<LoadInst>(pop_integer(bb)); + LoadInst* op2 = cast<LoadInst>(pop_integer(bb)); + SetCondInst* cond_inst = + new SetCondInst( Instruction::SetLT, op1, op2 ); + bb->getInstList().push_back( cond_inst ); + push_value( bb, cond_inst ); + break; } case MORE : // w1 w2 -- w2>w1 { - if (echo) bb->setName("MORE"); - LoadInst* op1 = cast<LoadInst>(pop_integer(bb)); - LoadInst* op2 = cast<LoadInst>(pop_integer(bb)); - SetCondInst* cond_inst = - new SetCondInst( Instruction::SetGT, op1, op2 ); - bb->getInstList().push_back( cond_inst ); - push_value( bb, cond_inst ); - break; + if (echo) bb->setName("MORE"); + LoadInst* op1 = cast<LoadInst>(pop_integer(bb)); + LoadInst* op2 = cast<LoadInst>(pop_integer(bb)); + SetCondInst* cond_inst = + new SetCondInst( Instruction::SetGT, op1, op2 ); + bb->getInstList().push_back( cond_inst ); + push_value( bb, cond_inst ); + break; } case LESS_EQUAL : // w1 w2 -- w2<=w1 { - if (echo) bb->setName("LE"); - LoadInst* op1 = cast<LoadInst>(pop_integer(bb)); - LoadInst* op2 = cast<LoadInst>(pop_integer(bb)); - SetCondInst* cond_inst = - new SetCondInst( Instruction::SetLE, op1, op2 ); - bb->getInstList().push_back( cond_inst ); - push_value( bb, cond_inst ); - break; + if (echo) bb->setName("LE"); + LoadInst* op1 = cast<LoadInst>(pop_integer(bb)); + LoadInst* op2 = cast<LoadInst>(pop_integer(bb)); + SetCondInst* cond_inst = + new SetCondInst( Instruction::SetLE, op1, op2 ); + bb->getInstList().push_back( cond_inst ); + push_value( bb, cond_inst ); + break; } case MORE_EQUAL : // w1 w2 -- w2>=w1 { - if (echo) bb->setName("GE"); - LoadInst* op1 = cast<LoadInst>(pop_integer(bb)); - LoadInst* op2 = cast<LoadInst>(pop_integer(bb)); - SetCondInst* cond_inst = - new SetCondInst( Instruction::SetGE, op1, op2 ); - bb->getInstList().push_back( cond_inst ); - push_value( bb, cond_inst ); - break; + if (echo) bb->setName("GE"); + LoadInst* op1 = cast<LoadInst>(pop_integer(bb)); + LoadInst* op2 = cast<LoadInst>(pop_integer(bb)); + SetCondInst* cond_inst = + new SetCondInst( Instruction::SetGE, op1, op2 ); + bb->getInstList().push_back( cond_inst ); + push_value( bb, cond_inst ); + break; } case NOT_EQUAL : // w1 w2 -- w2!=w1 { - if (echo) bb->setName("NE"); - LoadInst* op1 = cast<LoadInst>(pop_integer(bb)); - LoadInst* op2 = cast<LoadInst>(pop_integer(bb)); - SetCondInst* cond_inst = - new SetCondInst( Instruction::SetNE, op1, op2 ); - bb->getInstList().push_back( cond_inst ); - push_value( bb, cond_inst ); - break; + if (echo) bb->setName("NE"); + LoadInst* op1 = cast<LoadInst>(pop_integer(bb)); + LoadInst* op2 = cast<LoadInst>(pop_integer(bb)); + SetCondInst* cond_inst = + new SetCondInst( Instruction::SetNE, op1, op2 ); + bb->getInstList().push_back( cond_inst ); + push_value( bb, cond_inst ); + break; } case EQUAL : // w1 w2 -- w1==w2 { - if (echo) bb->setName("EQ"); - LoadInst* op1 = cast<LoadInst>(pop_integer(bb)); - LoadInst* op2 = cast<LoadInst>(pop_integer(bb)); - SetCondInst* cond_inst = - new SetCondInst( Instruction::SetEQ, op1, op2 ); - bb->getInstList().push_back( cond_inst ); - push_value( bb, cond_inst ); - break; + if (echo) bb->setName("EQ"); + LoadInst* op1 = cast<LoadInst>(pop_integer(bb)); + LoadInst* op2 = cast<LoadInst>(pop_integer(bb)); + SetCondInst* cond_inst = + new SetCondInst( Instruction::SetEQ, op1, op2 ); + bb->getInstList().push_back( cond_inst ); + push_value( bb, cond_inst ); + break; } // Arithmetic Operations case PLUS : // w1 w2 -- w2+w1 { - if (echo) bb->setName("ADD"); - LoadInst* op1 = cast<LoadInst>(pop_integer(bb)); - LoadInst* op2 = cast<LoadInst>(pop_integer(bb)); - BinaryOperator* addop = - BinaryOperator::create( Instruction::Add, op1, op2); - bb->getInstList().push_back( addop ); - push_value( bb, addop ); - break; + if (echo) bb->setName("ADD"); + LoadInst* op1 = cast<LoadInst>(pop_integer(bb)); + LoadInst* op2 = cast<LoadInst>(pop_integer(bb)); + BinaryOperator* addop = + BinaryOperator::create( Instruction::Add, op1, op2); + bb->getInstList().push_back( addop ); + push_value( bb, addop ); + break; } case MINUS : // w1 w2 -- w2-w1 { - if (echo) bb->setName("SUB"); - LoadInst* op1 = cast<LoadInst>(pop_integer(bb)); - LoadInst* op2 = cast<LoadInst>(pop_integer(bb)); - BinaryOperator* subop = - BinaryOperator::create( Instruction::Sub, op1, op2); - bb->getInstList().push_back( subop ); - push_value( bb, subop ); - break; + if (echo) bb->setName("SUB"); + LoadInst* op1 = cast<LoadInst>(pop_integer(bb)); + LoadInst* op2 = cast<LoadInst>(pop_integer(bb)); + BinaryOperator* subop = + BinaryOperator::create( Instruction::Sub, op1, op2); + bb->getInstList().push_back( subop ); + push_value( bb, subop ); + break; } case INCR : // w1 -- w1+1 { - if (echo) bb->setName("INCR"); - LoadInst* op1 = cast<LoadInst>(pop_integer(bb)); - BinaryOperator* addop = - BinaryOperator::create( Instruction::Add, op1, One ); - bb->getInstList().push_back( addop ); - push_value( bb, addop ); - break; + if (echo) bb->setName("INCR"); + LoadInst* op1 = cast<LoadInst>(pop_integer(bb)); + BinaryOperator* addop = + BinaryOperator::create( Instruction::Add, op1, One ); + bb->getInstList().push_back( addop ); + push_value( bb, addop ); + break; } case DECR : // w1 -- w1-1 { - if (echo) bb->setName("DECR"); - LoadInst* op1 = cast<LoadInst>(pop_integer(bb)); - BinaryOperator* subop = BinaryOperator::create( Instruction::Sub, op1, - ConstantSInt::get( Type::LongTy, 1 ) ); - bb->getInstList().push_back( subop ); - push_value( bb, subop ); - break; + if (echo) bb->setName("DECR"); + LoadInst* op1 = cast<LoadInst>(pop_integer(bb)); + BinaryOperator* subop = BinaryOperator::create( Instruction::Sub, op1, + ConstantSInt::get( Type::LongTy, 1 ) ); + bb->getInstList().push_back( subop ); + push_value( bb, subop ); + break; } case MULT : // w1 w2 -- w2*w1 { - if (echo) bb->setName("MUL"); - LoadInst* op1 = cast<LoadInst>(pop_integer(bb)); - LoadInst* op2 = cast<LoadInst>(pop_integer(bb)); - BinaryOperator* multop = - BinaryOperator::create( Instruction::Mul, op1, op2); - bb->getInstList().push_back( multop ); - push_value( bb, multop ); - break; + if (echo) bb->setName("MUL"); + LoadInst* op1 = cast<LoadInst>(pop_integer(bb)); + LoadInst* op2 = cast<LoadInst>(pop_integer(bb)); + BinaryOperator* multop = + BinaryOperator::create( Instruction::Mul, op1, op2); + bb->getInstList().push_back( multop ); + push_value( bb, multop ); + break; } case DIV :// w1 w2 -- w2/w1 { - if (echo) bb->setName("DIV"); - LoadInst* op1 = cast<LoadInst>(pop_integer(bb)); - LoadInst* op2 = cast<LoadInst>(pop_integer(bb)); - BinaryOperator* divop = - BinaryOperator::create( Instruction::Div, op1, op2); - bb->getInstList().push_back( divop ); - push_value( bb, divop ); - break; + if (echo) bb->setName("DIV"); + LoadInst* op1 = cast<LoadInst>(pop_integer(bb)); + LoadInst* op2 = cast<LoadInst>(pop_integer(bb)); + BinaryOperator* divop = + BinaryOperator::create( Instruction::Div, op1, op2); + bb->getInstList().push_back( divop ); + push_value( bb, divop ); + break; } case MODULUS : // w1 w2 -- w2%w1 { - if (echo) bb->setName("MOD"); - LoadInst* op1 = cast<LoadInst>(pop_integer(bb)); - LoadInst* op2 = cast<LoadInst>(pop_integer(bb)); - BinaryOperator* divop = - BinaryOperator::create( Instruction::Rem, op1, op2); - bb->getInstList().push_back( divop ); - push_value( bb, divop ); - break; + if (echo) bb->setName("MOD"); + LoadInst* op1 = cast<LoadInst>(pop_integer(bb)); + LoadInst* op2 = cast<LoadInst>(pop_integer(bb)); + BinaryOperator* divop = + BinaryOperator::create( Instruction::Rem, op1, op2); + bb->getInstList().push_back( divop ); + push_value( bb, divop ); + break; } case STAR_SLASH : // w1 w2 w3 -- (w3*w2)/w1 { - if (echo) bb->setName("STAR_SLASH"); - // Get the operands - LoadInst* op1 = cast<LoadInst>(pop_integer(bb)); - LoadInst* op2 = cast<LoadInst>(pop_integer(bb)); - LoadInst* op3 = cast<LoadInst>(pop_integer(bb)); + if (echo) bb->setName("STAR_SLASH"); + // Get the operands + LoadInst* op1 = cast<LoadInst>(pop_integer(bb)); + LoadInst* op2 = cast<LoadInst>(pop_integer(bb)); + LoadInst* op3 = cast<LoadInst>(pop_integer(bb)); - // Multiply the first two - BinaryOperator* multop = - BinaryOperator::create( Instruction::Mul, op1, op2); - bb->getInstList().push_back( multop ); + // Multiply the first two + BinaryOperator* multop = + BinaryOperator::create( Instruction::Mul, op1, op2); + bb->getInstList().push_back( multop ); - // Divide by the third operand - BinaryOperator* divop = - BinaryOperator::create( Instruction::Div, multop, op3); - bb->getInstList().push_back( divop ); + // Divide by the third operand + BinaryOperator* divop = + BinaryOperator::create( Instruction::Div, multop, op3); + bb->getInstList().push_back( divop ); - // Push the result - push_value( bb, divop ); + // Push the result + push_value( bb, divop ); - break; + break; } case NEGATE : // w1 -- -w1 { - if (echo) bb->setName("NEG"); - LoadInst* op1 = cast<LoadInst>(pop_integer(bb)); - // APPARENTLY, the following doesn't work: - // BinaryOperator* negop = BinaryOperator::createNeg( op1 ); - // bb->getInstList().push_back( negop ); - // So we'll multiply by -1 (ugh) - BinaryOperator* multop = BinaryOperator::create( Instruction::Mul, op1, - ConstantSInt::get( Type::LongTy, -1 ) ); - bb->getInstList().push_back( multop ); - push_value( bb, multop ); - break; + if (echo) bb->setName("NEG"); + LoadInst* op1 = cast<LoadInst>(pop_integer(bb)); + // APPARENTLY, the following doesn't work: + // BinaryOperator* negop = BinaryOperator::createNeg( op1 ); + // bb->getInstList().push_back( negop ); + // So we'll multiply by -1 (ugh) + BinaryOperator* multop = BinaryOperator::create( Instruction::Mul, op1, + ConstantSInt::get( Type::LongTy, -1 ) ); + bb->getInstList().push_back( multop ); + push_value( bb, multop ); + break; } case ABS : // w1 -- |w1| { - if (echo) bb->setName("ABS"); - // Get the top of stack value - LoadInst* op1 = cast<LoadInst>(stack_top(bb)); + if (echo) bb->setName("ABS"); + // Get the top of stack value + LoadInst* op1 = cast<LoadInst>(stack_top(bb)); - // Determine if its negative - SetCondInst* cond_inst = - new SetCondInst( Instruction::SetLT, op1, Zero ); - bb->getInstList().push_back( cond_inst ); + // Determine if its negative + SetCondInst* cond_inst = + new SetCondInst( Instruction::SetLT, op1, Zero ); + bb->getInstList().push_back( cond_inst ); - // Create a block for storing the result - BasicBlock* exit_bb = new BasicBlock((echo?"exit":"")); + // Create a block for storing the result + BasicBlock* exit_bb = new BasicBlock((echo?"exit":"")); - // Create a block for mak |