private void detectSymbolsAndBaseAddresses() throws IOException { Hashtable<String, Integer> moduleSymbols = new Hashtable<String, Integer>(); int nd = parseNumber(); for (int i = 1; i <= nd; i++) { String symbol = parseWord(); if (SymbolTable.defines(symbol)) { String error = "Error: This variable is multiply defined; first value used."; SymbolTable.getInstance().getErrors().put(symbol, error); } else { moduleSymbols.put(symbol, parseNumber() + this.baseAddress); SymbolTable.getInstance().put(symbol, moduleSymbols.get(symbol)); } } skipUseList(); int moduleSize = parseNumber(); skipProgram(moduleSize); MemoryMap.createProgramModule(this.baseAddress, moduleSymbols); this.baseAddress += moduleSize; skipWhiteSpace(); }
/* * Function : parseInstruction * Description : Parses the IType instructions without displacements. */ public static ITypeImmediate parseInstruction(String instruction, int address) { ITypeImmediate instruct = new ITypeImmediate(); // set the icode instruct.icode = IMap.get(Instruction.getIcodeName(instruction)); // grab the string from the registers.java class that we will use to match registers Pattern registerPattern = Pattern.compile(Registers.registerString()); Matcher registerMatcher = registerPattern.matcher(instruction); // match the first register if there is one if (registerMatcher.find()) { instruct.rd = Registers.registerValue(registerMatcher.group(0)); // I clear off the bits we've already read instruction = instruction.substring(registerMatcher.end(0)); } // match the second register if we can if (registerMatcher.find()) { instruct.r1 = Registers.registerValue(registerMatcher.group(0)); } // This regex attempts to match a label name. The [^\\s\\d] means not a space or digit character // This ensures that the label is infact a valid label and does not start with a number. Pattern symbolPattern = Pattern.compile("\\b([^\\d\\s][^\\s]*)\\b$"); Matcher symbolMatcher = symbolPattern.matcher(instruction); Pattern literalPattern = Pattern.compile("\\b(0x)?\\d+\\b"); Matcher literalMatcher = literalPattern.matcher(instruction); if (symbolMatcher.find()) { SymbolTable symbolTable = SymbolTable.getInstance(); instruct.immediateValue = symbolTable.getSymbol(symbolMatcher.group(0).replace(" ", "")); if (instruct.icode == BEQZ || instruct.icode == BNEZ) { instruct.r1 = instruct.rd; instruct.rd = 0; instruct.immediateValue = instruct.immediateValue - (address + 4); } } else if (literalMatcher.find()) { instruct.immediateValue = Tools.processNumber(literalMatcher.group(0).replace(" ", "")); } else { instruct.r1 = instruct.rd; instruct.rd = 0; } return instruct; }