void Ini::Section::extends(const Ini::Section& other) { std::list< Instruction >::const_iterator itinstr; for (itinstr = other.instructions().begin(); itinstr != other.instructions().end(); ++itinstr) addInstruction(*itinstr); std::map< std::string, std::string >::const_iterator itvars; for (itvars = other.vars().begin(); itvars != other.vars().end(); ++itvars) _vars[itvars->first] = itvars->second; }
nsresult txStylesheetCompiler::endElement() { if (NS_FAILED(mStatus)) { // ignore content after failure // XXX reevaluate once expat stops on failure return NS_OK; } nsresult rv = flushCharacters(); NS_ENSURE_SUCCESS(rv, rv); PRInt32 i; for (i = mInScopeVariables.Length() - 1; i >= 0; --i) { txInScopeVariable* var = mInScopeVariables[i]; if (!--(var->mLevel)) { nsAutoPtr<txInstruction> instr(new txRemoveVariable(var->mName)); NS_ENSURE_TRUE(instr, NS_ERROR_OUT_OF_MEMORY); rv = addInstruction(instr); NS_ENSURE_SUCCESS(rv, rv); mInScopeVariables.RemoveElementAt(i); delete var; } } const txElementHandler* handler = const_cast<const txElementHandler*> (static_cast<txElementHandler*>(popPtr())); rv = (handler->mEndFunction)(*this); NS_ENSURE_SUCCESS(rv, rv); if (!--mElementContext->mDepth) { // this will delete the old object mElementContext = static_cast<txElementContext*>(popObject()); } return NS_OK; }
// Arguments: // 1. lpeg Pattern object // Results: // 1. Array of instructions int parseLpegByteCode(lua_State* L) { Pattern* pattern = luaL_checkudata(L, 1, PATTERN_T); if (!pattern->code) { // Pattern is not compiled, call match with '' lua_getfield(L, 1, "match"); lua_pushvalue(L, 1); lua_pushliteral(L, ""); lua_call(L, 2, 0); } // lua_newtable(L); int codesize = pattern->codesize; Instruction* begin = pattern->code; Instruction* end = begin + codesize; Instruction* curr = begin; int n = 1; while (curr < end) { int offset = curr - begin; addInstruction(L, curr, n, offset); curr += lpeg_sizei(curr); n += 1; } return 1; }
InstructionSet& addInstructions(const Range& range) { for (const auto& instruction : range) { addInstruction(instruction); } return *this; }
PicAsm14bit::PicAsm14bit() : AsmInfo() { // Byte-orientated file register operations addInstruction( "ADDWF", 0, "000111dfffffff" ); addInstruction( "ANDWF", 0, "000101dfffffff" ); addInstruction( "CLRF", 0, "0000011fffffff" ); addInstruction( "CLRW", 0, "0000010xxxxxxx" ); addInstruction( "COMF", 0, "001001dfffffff" ); addInstruction( "DECF", 0, "000011dfffffff" ); addInstruction( "DECFSZ", 0, "001011dfffffff" ); addInstruction( "INCF", 0, "001010dfffffff" ); addInstruction( "INCFSZ", 0, "001111dfffffff" ); addInstruction( "IORWF", 0, "000100dfffffff" ); addInstruction( "MOVF", 0, "001000dfffffff" ); addInstruction( "MOVWF", 0, "0000001fffffff" ); addInstruction( "NOP", 0, "0000000xx00000" ); addInstruction( "RLF", 0, "001101dfffffff" ); addInstruction( "RRF", 0, "001100dfffffff" ); addInstruction( "SUBWF", 0, "000010dfffffff" ); addInstruction( "SWAPF", 0, "001110dfffffff" ); addInstruction( "XORWF", 0, "000110dfffffff" ); // Bit-orientated file register operations addInstruction( "BCF", 0, "0100bbbfffffff" ); addInstruction( "BSF", 0, "0101bbbfffffff" ); addInstruction( "BTFSC", 0, "0110bbbfffffff" ); addInstruction( "BTFSS", 0, "0111bbbfffffff" ); // Literal and control operations addInstruction( "ADDLW", 0, "11111xkkkkkkkk" ); addInstruction( "ANDLW", 0, "111001kkkkkkkk" ); addInstruction( "CALL", 0, "100kkkkkkkkkkk" ); addInstruction( "CLRWDT", 0, "00000001100100" ); addInstruction( "GOTO", 0, "101kkkkkkkkkkk" ); addInstruction( "IORLW", 0, "111000kkkkkkkk" ); addInstruction( "MOVLW", 0, "1100xxkkkkkkkk" ); addInstruction( "RETFIE", 0, "00000000001001" ); addInstruction( "RETLW", 0, "1101xxkkkkkkkk" ); addInstruction( "RETURN", 0, "00000000001000" ); addInstruction( "SLEEP", 0, "00000000000011" ); addInstruction( "SUBLW", 0, "11110xkkkkkkkk" ); addInstruction( "XORLW", 0, "111010kkkkkkkk" ); }
void processInstruction() { canCatch = 0; Options *options = (Options*) malloc(sizeof(Options)); char *fileName; char *text; char *varName; switch(userInput) { case 1: //write text = (char*) calloc(500, sizeof(char)); fileName = (char*) calloc(500, sizeof(char)); do { printf("[text] : "); fgets(text, 500, stdin); } while(*text == '\n'); do { printf("[destination] (ex: stdin, stdout, stderr, file, or varName) : "); fgets(fileName, 500, stdin); } while(*fileName == '\n'); options->arg1 = text; options->arg2 = fileName; addInstruction("write", options); break; case 2: //read varName = (char*) calloc(500, sizeof(char)); fileName = (char*) calloc(500, sizeof(char)); do { printf("[varName] (where will be stored the content) : "); fgets(varName, 500, stdin); } while(*text == '\n'); do { printf("[source] (ex: stdin, stdout, stderr, or a filename) : "); fgets(fileName, 500, stdin); } while(*fileName == '\n'); options->arg1 = text; options->arg2 = fileName; addInstruction("read", options); break; case 3: //exec break; case 4: //exit //Options *options = (Options*) malloc(sizeof(Option)); //options->integer = returnValue; break; } canCatch = 1; }
//Creates all processor instructions void createInstructions() { /// Data format: [0] - Type(R, I or J), [1] - Number of registers, [2] - number of constants, [3] - labels //R - instructions------------------------------------ //Arithmetic addInstruction("add", "R300000000100000"); addInstruction("addu", "R300000000100001"); addInstruction("clz", "R200011100100000"); addInstruction("clo", "R200011100100001"); addInstruction("sub", "R300000000100010"); addInstruction("subu", "R300000000100011"); addInstruction("seb", "R200011111100000"); addInstruction("seh", "R200011111100000"); //Logic addInstruction("and", "R300000000100100"); addInstruction("nor", "R300000000100111"); addInstruction("wsbh", "R200011111100000"); addInstruction("or", "R300000000100101"); addInstruction("xor", "R300000000100110"); addInstruction("ext", "R220011111000000"); addInstruction("ins", "R220011111000100"); //Multiplication and Division addInstruction("div", "R200000000011010"); addInstruction("divu", "R200000000011011"); addInstruction("madd", "R200011100000000"); addInstruction("maddu","R200011100000001"); addInstruction("msub", "R200011100000100"); addInstruction("msubu","R200011100000101"); addInstruction("mul", "R300011100000010"); addInstruction("mult", "R200000000011000"); addInstruction("multu","R200000000011001"); //Shift and Rotate addInstruction("sll", "R210000000000000"); addInstruction("sllv", "R300000000000100"); addInstruction("srl", "R210000000000010"); addInstruction("sra", "R210000000000011"); addInstruction("srav", "R300000000000111"); addInstruction("srlv", "R300000000000110"); addInstruction("rotr", "R210000000000010"); addInstruction("rotrv","R300000000000110"); //Conditionals and move addInstruction("slt", "R300000000101010"); addInstruction("sltu", "R300000000101011"); addInstruction("movn", "R300000000001011"); addInstruction("movz", "R300000000001010"); //Acc access addInstruction("mfhi", "R100000000010000"); addInstruction("mflo", "R100000000010010"); addInstruction("mthi", "R100000000010001"); addInstruction("mtlo", "R100000000010011"); //Branches and jump addInstruction("jr", "R100000000001000"); addInstruction("jalr", "R200000000001001"); //I - instructions------------------------------------ //Arithmetic addInstruction("addi", "I210001000"); addInstruction("move", "I200001000"); addInstruction("li", "I110001000"); addInstruction("addiu", "I210001001"); addInstruction("lui", "I110001111"); //Logic addInstruction("andi", "I210001100"); addInstruction("ori", "I210001101"); addInstruction("xori", "I210001110"); //Conditionals and move addInstruction("slti", "I210001010"); addInstruction("sltiu", "I210001011"); //Branches and jump addInstruction("beq", "I201000100"); addInstruction("bgtz", "I101000111"); addInstruction("bne", "I201000101"); addInstruction("bltz", "I101000001"); //Load and store addInstruction("lb", "I210100000"); addInstruction("lw", "I210100011"); addInstruction("lh", "I210100001"); addInstruction("sb", "I210101000"); addInstruction("sh", "I210101001"); addInstruction("sw", "I210101011"); //J - instructions------------------------------------ //Branches and jump addInstruction("j", "J001000010"); addInstruction("jal", "J001000011"); }
chunk *computeExpr(ANTLR3_BASE_TREE *node) { debug(DEBUG_GENERATION, "\033[22;93mCompute expression [%s] (%d:%d)\033[0m", (char *)node->toString(node)->chars, node->getLine(node), node->getCharPositionInLine(node)); chunk *chunk, *chunk_left = NULL, *chunk_right = NULL; int type = node->getType(node); char *template; char etiq_1[10], etiq_2[10]; static int etiq_nb = 0; chunk = initChunk(); // If it's an atom, get the address to access it if (type == INTEGER || type == STRING || type == ID || type == FUNC_CALL) { loadAtom(node, chunk); return chunk; } // Else get both operand chunk // unless it's assign, we only want the address of the left opperand if (type != ASSIGNE) chunk_left = computeExpr(node->getChild(node, 0)); // unless it's a NEG node, because NEG only has one child if (type != NEG) chunk_right = computeExpr(node->getChild(node, 1)); appendChunks(chunk, chunk_left); // It frees chunk_left->registre. Weird but ok because no more registre needed // unless for assign, but it will erased chunk_left->registre that is not initialised // SO KEEP THAT ORDER UNLESS YOU KNOW WHAT YOU'RE DOING ! appendChunks(chunk, chunk_right); // Do operation switch (type) { case SUP : case INF : case SUP_EQ : case INF_EQ : case EQ : case DIFF : addInstruction(chunk, "SUB R%d, R%d, R%d // %d:%d", chunk_left->registre, chunk_right->registre, chunk->registre, node->getLine(node), node->getCharPositionInLine(node)); sprintf(etiq_1, "ETIQ_%d", etiq_nb++); sprintf(etiq_2, "ETIQ_%d", etiq_nb++); jumpTo(chunk, type, etiq_1, 0); addInstruction(chunk, "LDW R%d, #0", chunk->registre); jumpTo(chunk, 0, etiq_2, 0); addEtiquette(chunk, etiq_1); addInstruction(chunk, "LDW R%d, #1", chunk->registre); addEtiquette(chunk, etiq_2); chunk_right->registre = -1; break; case AND : case OR : case MINUS : case PLUS : case DIV : case MULT : switch (type) { case OR : template = "OR R%d, R%d, R%d // %d:%d"; break; case MINUS :
void generateASM(ANTLR3_BASE_TREE *node) { debug(DEBUG_GENERATION, "\033[22;93mGenerate ASM\033[0m"); initRegisters(); program = initChunk(); addInstruction(program, "SP EQU R15"); addInstruction(program, "FP EQU R14"); addInstruction(program, "DISPLAY EQU R13"); // TODO - dynamise load adr addInstruction(program, "ORG 0xE000"); addInstruction(program, "START MAIN"); chunk *stack = stackEnvironement(); chunk *instructionASM = computeInstruction(node); chunk *unstack = unstackEnvironement(); addInstruction(program, "\n\n\n// PRGM"); addEtiquette(program, "MAIN"); addInstruction(program, "STACKBASE 0x1000"); addInstruction(program, "LDW FP, SP"); addInstruction(program, "LDW DISPLAY, #0x2000"); addInstruction(program, "// STACK FACK RETURN"); addInstruction(program, "ADQ -2, SP"); appendChunks(program, stack); appendChunks(program, instructionASM); appendChunks(program, unstack); addInstruction(program, "// UNSTACK FACK RETURN"); addInstruction(program, "ADQ 2, SP"); FILE *file = fopen("a.asm", "w"); fprintf(file, program->string); fclose(file); // printChunk(program); freeChunk(instructionASM); freeChunk(stack); freeChunk(unstack); freeChunk(program); }