├── .gitignore ├── LICENSE ├── README.md ├── bin └── .gitignore ├── bitconfigs_db.ini ├── configs_db.ini ├── gui ├── bg.png └── options.ini ├── instructions_db.ini ├── lib ├── forms-1.3.0-src.zip └── forms-1.3.0.jar ├── opcodes_db.ini ├── run-GUI.bat ├── scripts.zip ├── scripts_db.ini └── src ├── mgi └── tools │ └── jagdecs2 │ ├── Beautifier.java │ ├── CS2.java │ ├── CS2Decoder.java │ ├── CS2Decompiler.java │ ├── CS2Stack.java │ ├── CS2Type.java │ ├── CodePrinter.java │ ├── Constants.java │ ├── DecompilerException.java │ ├── FlowBlocksGenerator.java │ ├── FlowBlocksSolver.java │ ├── ICS2Provider.java │ ├── LocalVariablesAnalyzerT1.java │ ├── LocalVariablesAnalyzerT2.java │ ├── ast │ ├── AbstractCodeNode.java │ ├── ArrayLoadNode.java │ ├── ArrayStoreNode.java │ ├── BitConfigurationLoadNode.java │ ├── BitConfigurationStoreNode.java │ ├── BreakNode.java │ ├── BuildStringNode.java │ ├── CallExpressionNode.java │ ├── CaseAnnotation.java │ ├── CastNode.java │ ├── CommentNode.java │ ├── ConditionalExpressionNode.java │ ├── ConditionalFlowBlockJump.java │ ├── ConfigurationLoadNode.java │ ├── ConfigurationStoreNode.java │ ├── ContinueNode.java │ ├── ExpressionNode.java │ ├── FlowBlock.java │ ├── FunctionExpressionNode.java │ ├── FunctionNode.java │ ├── IBreakableNode.java │ ├── IConstantNode.java │ ├── IContinueableNode.java │ ├── IControllableFlowNode.java │ ├── IFlowControlNode.java │ ├── ILabelableNode.java │ ├── IfElseNode.java │ ├── IntExpressionNode.java │ ├── LoadNamedDataNode.java │ ├── LocalVariable.java │ ├── LongExpressionNode.java │ ├── LoopNode.java │ ├── NewArrayNode.java │ ├── NotExpressionNode.java │ ├── PopableNode.java │ ├── ReturnNode.java │ ├── ScopeNode.java │ ├── StoreNamedDataNode.java │ ├── StringExpressionNode.java │ ├── StructConstructExpr.java │ ├── StructPartLoadNode.java │ ├── SwitchFlowBlockJump.java │ ├── SwitchNode.java │ ├── UnconditionalFlowBlockJump.java │ ├── VariableAssignationNode.java │ └── VariableLoadNode.java │ ├── gui │ ├── EditorPanel.java │ ├── EditorPrinter.java │ ├── Main.java │ ├── OptionsPanel.java │ └── Window.java │ ├── instructions │ ├── AbstractInstruction.java │ ├── BitConfigInstruction.java │ ├── BooleanInstruction.java │ ├── ConfigInstruction.java │ ├── IntInstruction.java │ ├── JumpInstruction.java │ ├── Label.java │ ├── LongInstruction.java │ ├── Opcodes.java │ ├── StringInstruction.java │ └── SwitchInstruction.java │ └── util │ ├── ArrayQueue.java │ ├── BitConfigInfo.java │ ├── ByteBuffer.java │ ├── ConfigInfo.java │ ├── ConfigsDatabase.java │ ├── DecompilerUtils.java │ ├── FunctionDatabase.java │ ├── FunctionInfo.java │ ├── GenericsUtils.java │ ├── IOUtils.java │ ├── InstructionInfo.java │ ├── InstructionsDatabase.java │ ├── OpcodeUtils.java │ ├── Options.java │ ├── TextUtils.java │ └── UnsafeSerializer.java └── tests ├── .gitignore ├── DecompileAll.java ├── GenDatabases.java └── TestProvider.java /.gitignore: -------------------------------------------------------------------------------- 1 | /scripts/ 2 | /sources/ 3 | /.classpath 4 | /.project 5 | /db_missing.txt 6 | /instr_read_original.txt 7 | /instr_read.txt 8 | /instr_readx.txt 9 | /.settings/ 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CS2-Decompiler 2 | 3 | 4 | ## Instructions 5 | 6 | Create scripts/ directory 7 | 8 | Extract scripts.zip there 9 | 10 | Run! 11 | 12 | 13 | -------------------------------------------------------------------------------- /bin/.gitignore: -------------------------------------------------------------------------------- 1 | /mgi/ 2 | /src.rar 3 | /tests/ 4 | -------------------------------------------------------------------------------- /configs_db.ini: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Someone52/CS2-Decompiler/dea1a70a973efc2fb475603deb28e831afc1387e/configs_db.ini -------------------------------------------------------------------------------- /gui/bg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Someone52/CS2-Decompiler/dea1a70a973efc2fb475603deb28e831afc1387e/gui/bg.png -------------------------------------------------------------------------------- /gui/options.ini: -------------------------------------------------------------------------------- 1 | # Auto generated options - DO NOT EDIT 2 | 3 | 4 | 5 | scripts_db_path scripts_db.ini 6 | 7 | scripts_path scripts/ 8 | 9 | instructions_db_path instructions_db.ini 10 | 11 | opcodes_db_path opcodes_db.ini 12 | 13 | -------------------------------------------------------------------------------- /lib/forms-1.3.0-src.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Someone52/CS2-Decompiler/dea1a70a973efc2fb475603deb28e831afc1387e/lib/forms-1.3.0-src.zip -------------------------------------------------------------------------------- /lib/forms-1.3.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Someone52/CS2-Decompiler/dea1a70a973efc2fb475603deb28e831afc1387e/lib/forms-1.3.0.jar -------------------------------------------------------------------------------- /run-GUI.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | start javaw -cp lib/mgiutils_v4.jar;bin; mgi.tools.jagdecs2.gui.Main -------------------------------------------------------------------------------- /scripts.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Someone52/CS2-Decompiler/dea1a70a973efc2fb475603deb28e831afc1387e/scripts.zip -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/CS2.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2; 18 | 19 | import java.util.ArrayList; 20 | import java.util.List; 21 | 22 | import mgi.tools.jagdecs2.instructions.AbstractInstruction; 23 | import mgi.tools.jagdecs2.instructions.Label; 24 | import mgi.tools.jagdecs2.instructions.SwitchInstruction; 25 | 26 | public class CS2 { 27 | 28 | private String name; 29 | private CS2Type[] argumentsTypes; 30 | private String[] argumentNames; 31 | private CS2Type returnType = CS2Type.UNKNOWN; 32 | private int intLocalsSize, stringLocalsSize, longLocalsSize; 33 | private int intArgumentsCount, stringArgumentsCount, longArgumentsCount; 34 | private AbstractInstruction[] instructions; 35 | 36 | public CS2(String name, CS2Type[] args, int intls, int stringls, int longls, int intac, int stringtac, int longac, int codeSize) { 37 | this.name = name; 38 | this.argumentsTypes = args; 39 | this.intLocalsSize = intls; 40 | this.stringLocalsSize = stringls; 41 | this.longLocalsSize = longls; 42 | this.intArgumentsCount = intac; 43 | this.stringArgumentsCount = stringtac; 44 | this.longArgumentsCount = longac; 45 | this.instructions = new AbstractInstruction[codeSize * 2]; 46 | } 47 | 48 | public String getName() { 49 | return name; 50 | } 51 | 52 | public void setName(String name) { 53 | this.name = name; 54 | } 55 | 56 | public void setReturnType(CS2Type returnType) { 57 | this.returnType = returnType; 58 | } 59 | 60 | public CS2Type getReturnType() { 61 | return returnType; 62 | } 63 | 64 | public void setArgumentTypes(CS2Type[] arguments) { 65 | this.argumentsTypes = arguments; 66 | } 67 | 68 | public CS2Type[] getArgumentTypes() { 69 | return argumentsTypes; 70 | } 71 | 72 | public void setArgumentNames(String[] names) { 73 | this.argumentNames = names; 74 | } 75 | 76 | public String[] getArgumentNames() { 77 | return argumentNames; 78 | } 79 | 80 | public int getIntLocalsSize() { 81 | return intLocalsSize; 82 | } 83 | 84 | public int getStringLocalsSize() { 85 | return stringLocalsSize; 86 | } 87 | 88 | public int getLongLocalsSize() { 89 | return longLocalsSize; 90 | } 91 | 92 | public int getIntArgumentsCount() { 93 | return intArgumentsCount; 94 | } 95 | 96 | public int getStringArgumentsCount() { 97 | return stringArgumentsCount; 98 | } 99 | 100 | public int getLongArgumentsCount() { 101 | return longArgumentsCount; 102 | } 103 | 104 | public AbstractInstruction[] getInstructions() { 105 | return instructions; 106 | } 107 | 108 | 109 | @Deprecated 110 | public int addressOf(AbstractInstruction instr) { 111 | for (int i = 0; i < instructions.length; i++) 112 | if (instructions[i] == instr) 113 | return i; 114 | return -1; 115 | } 116 | 117 | 118 | public void prepareInstructions() { 119 | int nonLabelsAmt = instructions.length / 2; 120 | for (int i = 0; i < nonLabelsAmt; i++) { 121 | if (instructions[i * 2 + 1] instanceof SwitchInstruction) { 122 | SwitchInstruction instr = (SwitchInstruction) instructions[i * 2 + 1]; 123 | if (((i + 1) * 2 + 1) >= instructions.length) { 124 | AbstractInstruction[] buf = new AbstractInstruction[(nonLabelsAmt + 1) * 2]; 125 | System.arraycopy(instructions, 0, buf, 0, instructions.length); 126 | this.instructions = buf; 127 | } 128 | 129 | if (instructions[(i + 1) * 2] == null) 130 | instructions[(i + 1) * 2] = new Label(); 131 | instr.attachDefault((Label)instructions[(i + 1) * 2]); 132 | } 133 | } 134 | 135 | 136 | List buffer = new ArrayList(); 137 | for (int i = 0; i < this.instructions.length; i++) 138 | if (this.instructions[i] != null) 139 | buffer.add(this.instructions[i]); 140 | 141 | this.instructions = new AbstractInstruction[buffer.size()]; 142 | int write = 0; 143 | for (AbstractInstruction instr : buffer) 144 | this.instructions[write++] = instr; 145 | for (int i = 0; i < instructions.length; i++) 146 | instructions[i].setAddress(i); 147 | for (int i = 0,labelsFound = 0; i < instructions.length; i++) 148 | if (instructions[i] instanceof Label) 149 | ((Label)instructions[i]).setLabelID(labelsFound++); 150 | for (int i = 0; i < instructions.length; i++) 151 | if (instructions[i] instanceof SwitchInstruction) 152 | ((SwitchInstruction)instructions[i]).sort(); 153 | } 154 | 155 | public int countOf(Class type) { 156 | int total = 0; 157 | for (int i = 0; i < instructions.length; i++) 158 | if (instructions[i].getClass() == type) 159 | total++; 160 | return total; 161 | } 162 | } 163 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/CS2Decoder.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2; 18 | 19 | import java.io.File; 20 | import java.io.FileInputStream; 21 | import java.io.FileNotFoundException; 22 | import java.io.IOException; 23 | import java.util.HashMap; 24 | import java.util.Map; 25 | 26 | import mgi.tools.jagdecs2.instructions.BitConfigInstruction; 27 | import mgi.tools.jagdecs2.instructions.BooleanInstruction; 28 | import mgi.tools.jagdecs2.instructions.ConfigInstruction; 29 | import mgi.tools.jagdecs2.instructions.IntInstruction; 30 | import mgi.tools.jagdecs2.instructions.JumpInstruction; 31 | import mgi.tools.jagdecs2.instructions.Label; 32 | import mgi.tools.jagdecs2.instructions.LongInstruction; 33 | import mgi.tools.jagdecs2.instructions.Opcodes; 34 | import mgi.tools.jagdecs2.instructions.StringInstruction; 35 | import mgi.tools.jagdecs2.instructions.SwitchInstruction; 36 | import mgi.tools.jagdecs2.util.BitConfigInfo; 37 | import mgi.tools.jagdecs2.util.ByteBuffer; 38 | import mgi.tools.jagdecs2.util.ConfigInfo; 39 | import mgi.tools.jagdecs2.util.ConfigsDatabase; 40 | import mgi.tools.jagdecs2.util.InstructionInfo; 41 | import mgi.tools.jagdecs2.util.InstructionsDatabase; 42 | 43 | public class CS2Decoder { 44 | 45 | @SuppressWarnings({"unchecked", "rawtypes"}) 46 | public static final CS2 readScript(InstructionsDatabase idb, ConfigsDatabase cdb, File file) throws IOException { 47 | if (!file.exists() || !file.isFile() || !file.canRead()) 48 | throw new FileNotFoundException("Script file " + file + " does not exist."); 49 | FileInputStream stream = new FileInputStream(file); 50 | byte[] data = new byte[(int) file.length()]; 51 | int readed = stream.read(data); 52 | stream.close(); 53 | if (readed != data.length) 54 | throw new IOException("Reading failed."); 55 | ByteBuffer buffer = new ByteBuffer(data); 56 | 57 | buffer.setPosition(data.length - 2); 58 | int switchBlocksSize = buffer.readUnsignedShort(); 59 | int codeBlockEnd = data.length - switchBlocksSize - 16 - 2; 60 | buffer.setPosition(codeBlockEnd); 61 | int codeSize = buffer.readInt(); 62 | int intLocalsCount = buffer.readUnsignedShort(); 63 | int stringLocalsCount = buffer.readUnsignedShort(); 64 | int longLocalsCount = buffer.readUnsignedShort(); 65 | 66 | int intArgsCount = buffer.readUnsignedShort(); 67 | int stringArgsCount = buffer.readUnsignedShort(); 68 | int longArgsCount = buffer.readUnsignedShort(); 69 | 70 | int switchesCount = buffer.readUByte(); 71 | Map[] switches = new HashMap[switchesCount]; 72 | for (int i = 0; i < switchesCount; i++) { 73 | int numCases = buffer.readUnsignedShort(); 74 | switches[i] = new HashMap(numCases); 75 | while (numCases-- > 0) { 76 | switches[i].put(buffer.readInt(), buffer.readInt()); 77 | } 78 | } 79 | buffer.setPosition(0); 80 | String scriptName = buffer.readNullString(); 81 | int[] intPool = new int[codeSize]; 82 | Object[] objectPool = new Object[codeSize]; 83 | long[] longPool = new long[codeSize]; 84 | InstructionInfo[] instructions = new InstructionInfo[codeSize]; 85 | 86 | int writeOffset = 0; 87 | while (buffer.getPosition() < codeBlockEnd) { 88 | int opcode = buffer.readUnsignedShort(); 89 | InstructionInfo info = idb.getByScrampled(opcode); 90 | if (info == null) 91 | throw new RuntimeException("Unknown opcode:" + opcode); 92 | opcode = info.getOpcode(); 93 | 94 | if (opcode == Opcodes.LOAD_CONFIG || opcode == Opcodes.STORE_CONFIG) { 95 | int domainType = buffer.readUByte(); 96 | int id = buffer.readUnsignedShort(); 97 | ConfigInfo cfg = cdb.getConfigInfo(domainType, id); 98 | if (cfg == null) 99 | throw new RuntimeException("Unknown config:" + domainType + "," + id); 100 | intPool[writeOffset] = buffer.readUByte(); 101 | objectPool[writeOffset] = cfg; 102 | } 103 | else if (opcode == Opcodes.PUSH_STR) { 104 | int baseType = buffer.readUByte(); 105 | if (baseType == 0) { 106 | info = idb.getByUnscrampled(opcode = Opcodes.PUSH_INT); 107 | intPool[writeOffset] = buffer.readInt(); 108 | } 109 | else if (baseType == 1) { 110 | info = idb.getByUnscrampled(opcode = Opcodes.PUSH_LONG); 111 | longPool[writeOffset] = buffer.readLong(); 112 | } 113 | else if (baseType == 2) 114 | objectPool[writeOffset] = buffer.readString(); 115 | else 116 | throw new RuntimeException("Unknown base type:" + baseType); 117 | } 118 | else if (opcode == Opcodes.LOAD_BITCONFIG || opcode == Opcodes.STORE_BITCONFIG) { 119 | int id = buffer.readUnsignedShort(); 120 | BitConfigInfo cfg = cdb.getBitConfigInfo(id); 121 | if (cfg == null) 122 | throw new RuntimeException("Unknown bitconfig:" + id); 123 | intPool[writeOffset] = buffer.readUByte(); 124 | objectPool[writeOffset] = cfg; 125 | } 126 | else { 127 | intPool[writeOffset] = info.hasIntConstant() ? buffer.readInt() : buffer.readUByte(); 128 | } 129 | 130 | instructions[writeOffset++] = info; 131 | } 132 | 133 | CS2Type[] args = new CS2Type[intArgsCount + stringArgsCount + longArgsCount]; 134 | int write = 0; 135 | for (int i = 0; i < intArgsCount; i++) 136 | args[write++] = CS2Type.INT; 137 | for (int i = 0; i < stringArgsCount; i++) 138 | args[write++] = CS2Type.STRING; 139 | for (int i = 0; i < longArgsCount; i++) 140 | args[write++] = CS2Type.LONG; 141 | 142 | CS2 script = new CS2(scriptName, args, intLocalsCount, stringLocalsCount, longLocalsCount, intArgsCount, stringArgsCount,longArgsCount,codeSize); 143 | for (int i = 0; i < codeSize; i++) { 144 | InstructionInfo info = instructions[i]; 145 | int opcode = info.getOpcode(); 146 | if (opcode == Opcodes.PUSH_STR) 147 | script.getInstructions()[(i * 2) + 1] = new StringInstruction(info, (String) objectPool[i]); 148 | else if (opcode == Opcodes.PUSH_LONG) 149 | script.getInstructions()[(i * 2) + 1] = new LongInstruction(info, longPool[i]); 150 | else if (opcode == Opcodes.SWITCH) { // switch 151 | Map block = switches[intPool[i]]; 152 | int[] cases = new int[block.size()]; 153 | Label[] targets = new Label[block.size()]; 154 | int w = 0; 155 | for (Object key : block.keySet()) { 156 | cases[w] = (Integer) key; 157 | Object addr = block.get(key); 158 | int full = i + ((Integer) addr).intValue() + 1; 159 | if (script.getInstructions()[full * 2] == null) 160 | script.getInstructions()[full * 2] = new Label(); 161 | targets[w++] = (Label) script.getInstructions()[full * 2]; 162 | } 163 | script.getInstructions()[(i * 2) + 1] = new SwitchInstruction(info, cases, targets); 164 | } 165 | else if (opcode >= Opcodes.GOTO && opcode <= Opcodes.LONG_GE) { 166 | int fullAddr = i + intPool[i] + 1; 167 | if (script.getInstructions()[fullAddr * 2] == null) 168 | script.getInstructions()[fullAddr * 2] = new Label(); 169 | script.getInstructions()[(i * 2) + 1] = new JumpInstruction(info, (Label) script.getInstructions()[fullAddr * 2]); 170 | } 171 | else if (opcode == Opcodes.LOAD_CONFIG || opcode == Opcodes.STORE_CONFIG) 172 | script.getInstructions()[(i * 2) + 1] = new ConfigInstruction(info, (ConfigInfo)objectPool[i], intPool[i] == 1); 173 | else if (opcode == Opcodes.LOAD_BITCONFIG || opcode == Opcodes.STORE_BITCONFIG) 174 | script.getInstructions()[(i * 2) + 1] = new BitConfigInstruction(info, (BitConfigInfo)objectPool[i], intPool[i] == 1); 175 | else if (opcode < 150) 176 | script.getInstructions()[(i * 2) + 1] = new IntInstruction(info, intPool[i]); 177 | else 178 | script.getInstructions()[(i * 2) + 1] = new BooleanInstruction(info, intPool[i] == 1); 179 | } 180 | 181 | script.prepareInstructions(); 182 | return script; 183 | } 184 | 185 | } 186 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/CS2Decompiler.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Someone52/CS2-Decompiler/dea1a70a973efc2fb475603deb28e831afc1387e/src/mgi/tools/jagdecs2/CS2Decompiler.java -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/CS2Stack.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2; 18 | 19 | import mgi.tools.jagdecs2.ast.ExpressionNode; 20 | 21 | public class CS2Stack { 22 | 23 | private ExpressionNode[][] stack; 24 | private int[][] pushOrders; 25 | private int[] size; 26 | private int timesPushed; 27 | public static final int STACK_TYPES_COUNT = 3; 28 | public static final int BUFFER_SIZE = 500; 29 | 30 | public CS2Stack() { 31 | stack = new ExpressionNode[STACK_TYPES_COUNT][BUFFER_SIZE]; 32 | pushOrders = new int[STACK_TYPES_COUNT][BUFFER_SIZE]; 33 | size = new int[STACK_TYPES_COUNT]; 34 | } 35 | 36 | public ExpressionNode pop() { 37 | if (getSize() <= 0) 38 | throw new RuntimeException("Stack underflow"); 39 | int order = -1; 40 | int stackType = -1; 41 | for (int a = 0; a < STACK_TYPES_COUNT; a++) { 42 | if (size[a] > 0 && pushOrders[a][size[a] - 1] > order) 43 | stackType = a; 44 | } 45 | return pop(stackType); 46 | } 47 | 48 | public ExpressionNode peek() { 49 | if (getSize() <= 0) 50 | throw new RuntimeException("Stack underflow"); 51 | int order = -1; 52 | int stackType = -1; 53 | for (int a = 0; a < STACK_TYPES_COUNT; a++) { 54 | if (size[a] > 0 && pushOrders[a][size[a] - 1] > order) 55 | stackType = a; 56 | } 57 | return peek(stackType); 58 | } 59 | 60 | public ExpressionNode pop(int stackType) { 61 | if (size[stackType] <= 0) 62 | throw new RuntimeException("Stack underflow"); 63 | return stack[stackType][--size[stackType]]; 64 | } 65 | 66 | public ExpressionNode peek(int stackType) { 67 | if (size[stackType] <= 0) 68 | throw new RuntimeException("Stack underflow"); 69 | return stack[stackType][size[stackType] - 1]; 70 | } 71 | 72 | public void push(ExpressionNode expr,int stackType) { 73 | if ((size[stackType] + 1) >= BUFFER_SIZE) 74 | throw new RuntimeException("Stack overflow"); 75 | pushOrders[stackType][size[stackType]] = timesPushed++; 76 | stack[stackType][size[stackType]++] = expr; 77 | } 78 | 79 | public CS2Stack copy() { 80 | CS2Stack stack = new CS2Stack(); 81 | for (int a = 0; a < STACK_TYPES_COUNT; a++) { 82 | stack.size[a] = this.size[a]; 83 | for (int i = 0; i < BUFFER_SIZE; i++) { 84 | if (this.stack[a][i] != null) 85 | stack.stack[a][i] = this.stack[a][i].copy(); 86 | stack.pushOrders[a][i] = this.pushOrders[a][i]; 87 | } 88 | } 89 | return stack; 90 | } 91 | 92 | 93 | public int getSize() { 94 | int total = 0; 95 | for (int i = 0; i < STACK_TYPES_COUNT; i++) 96 | total += size[i]; 97 | return total; 98 | } 99 | 100 | public int getSize(int stackType) { 101 | return size[stackType]; 102 | } 103 | 104 | public void clear() { 105 | for (int i = 0; i < STACK_TYPES_COUNT; i++) 106 | size[i] = 0; 107 | } 108 | 109 | 110 | 111 | } 112 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/CodePrinter.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2; 18 | 19 | import java.io.StringWriter; 20 | 21 | import mgi.tools.jagdecs2.ast.AbstractCodeNode; 22 | 23 | public class CodePrinter { 24 | 25 | protected StringWriter writer; 26 | private int tabs; 27 | 28 | 29 | public CodePrinter() { 30 | writer = new StringWriter(); 31 | tabs = 0; 32 | } 33 | 34 | /** 35 | * Method , unused by default that notifies that specific node is 36 | * about to be printed. 37 | */ 38 | public void beginPrinting(AbstractCodeNode node) { } 39 | 40 | /** 41 | * Method, unused by default that notifies that specific node was 42 | * printed. 43 | */ 44 | public void endPrinting(AbstractCodeNode node) { } 45 | 46 | 47 | public void print(CharSequence str) { 48 | for (int i = 0; i < str.length(); i++) 49 | print(str.charAt(i)); 50 | } 51 | 52 | public void print(char c) { 53 | writer.append(c); 54 | if (c == '\n') 55 | writer.append(getTabs()); 56 | } 57 | 58 | protected String getTabs() { 59 | StringBuilder tabs = new StringBuilder(); 60 | for (int i = 0; i < this.tabs; i++) 61 | tabs.append('\t'); 62 | return tabs.toString(); 63 | } 64 | 65 | public void tab() { 66 | tabs++; 67 | } 68 | 69 | public void untab() { 70 | if (tabs <= 0) 71 | throw new RuntimeException("Not tabbed!"); 72 | tabs--; 73 | } 74 | 75 | @Override 76 | public String toString() { 77 | writer.flush(); 78 | return writer.toString(); 79 | } 80 | 81 | public static String print(AbstractCodeNode node) { 82 | CodePrinter printer = new CodePrinter(); 83 | node.print(printer); 84 | return printer.toString(); 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/Constants.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2; 18 | 19 | public class Constants { 20 | 21 | public Constants() { 22 | // TODO Auto-generated constructor stub 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/DecompilerException.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2; 18 | 19 | public class DecompilerException extends RuntimeException { 20 | private static final long serialVersionUID = 7195887685185051968L; 21 | 22 | public DecompilerException(String string) { 23 | super(string); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ICS2Provider.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2; 18 | 19 | import mgi.tools.jagdecs2.util.ConfigsDatabase; 20 | import mgi.tools.jagdecs2.util.FunctionDatabase; 21 | import mgi.tools.jagdecs2.util.InstructionsDatabase; 22 | 23 | public interface ICS2Provider { 24 | 25 | CS2 getCS2(InstructionsDatabase idb, ConfigsDatabase cdb, FunctionDatabase sdb, FunctionDatabase odb, int id); 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/LocalVariablesAnalyzerT2.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2; 18 | 19 | import java.util.ArrayList; 20 | import java.util.HashMap; 21 | import java.util.List; 22 | import java.util.Map; 23 | 24 | import mgi.tools.jagdecs2.ast.AbstractCodeNode; 25 | import mgi.tools.jagdecs2.ast.ConditionalFlowBlockJump; 26 | import mgi.tools.jagdecs2.ast.FlowBlock; 27 | import mgi.tools.jagdecs2.ast.FunctionNode; 28 | import mgi.tools.jagdecs2.ast.LocalVariable; 29 | import mgi.tools.jagdecs2.ast.ReturnNode; 30 | import mgi.tools.jagdecs2.ast.SwitchFlowBlockJump; 31 | import mgi.tools.jagdecs2.ast.UnconditionalFlowBlockJump; 32 | import mgi.tools.jagdecs2.ast.VariableAssignationNode; 33 | 34 | public class LocalVariablesAnalyzerT2 { 35 | 36 | @SuppressWarnings("unused") 37 | private CS2Decompiler decompiler; 38 | private FunctionNode function; 39 | private FlowBlock[] blocks; 40 | 41 | 42 | private boolean[] processed; 43 | private FlowBlockState[] states; 44 | 45 | private Map> assigns; 46 | 47 | public LocalVariablesAnalyzerT2(CS2Decompiler decompiler, FunctionNode function, FlowBlock[] blocks) { 48 | this.decompiler = decompiler; 49 | this.function = function; 50 | this.blocks = blocks; 51 | } 52 | 53 | 54 | 55 | public void analyze() throws DecompilerException { 56 | init(); 57 | process(); 58 | end(); 59 | } 60 | 61 | 62 | private void init() { 63 | processed = new boolean[blocks.length]; 64 | states = new FlowBlockState[blocks.length]; 65 | assigns = new HashMap>(); 66 | 67 | FlowBlockState s = new FlowBlockState(); 68 | for (LocalVariable arg : function.getArgumentLocals()) { 69 | Object o = s.set(null, arg); 70 | if (!assigns.containsKey(arg)) 71 | assigns.put(arg, new ArrayList()); 72 | List l = assigns.get(arg); 73 | if (!l.contains(o)) 74 | l.add(o); 75 | } 76 | 77 | 78 | queue(s, blocks[0]); 79 | } 80 | 81 | private void process() { 82 | int count; 83 | do { 84 | count = 0; 85 | for (int i = 0; i < states.length; i++) { 86 | if (states[i] == null || processed[i]) 87 | continue; 88 | processed[i] = true; 89 | 90 | processNode(states[i], blocks[i]); 91 | count++; 92 | } 93 | } 94 | while (count > 0); 95 | 96 | } 97 | 98 | private void end() { 99 | for (LocalVariable var : assigns.keySet()) { 100 | List objs = assigns.get(var); 101 | if (objs.size() != 1 || !(objs.get(0) instanceof VariableAssignationNode)) 102 | continue; 103 | 104 | VariableAssignationNode fassign = (VariableAssignationNode)objs.get(0); 105 | 106 | fassign.setIsDeclaration(true); 107 | var.setNeedsScopeDeclaration(false); 108 | } 109 | } 110 | 111 | 112 | 113 | private boolean processNode(FlowBlockState state, AbstractCodeNode n) { 114 | boolean quit = false; 115 | for (int i = 0; i < n.size(); i++) { 116 | boolean q = processNode(state, n.read(i)); 117 | quit = quit || q; 118 | } 119 | 120 | if (n instanceof ConditionalFlowBlockJump) { 121 | ConditionalFlowBlockJump jmp = (ConditionalFlowBlockJump)n; 122 | queue(state, jmp.getTarget()); 123 | } 124 | else if (n instanceof UnconditionalFlowBlockJump) { 125 | UnconditionalFlowBlockJump jmp = (UnconditionalFlowBlockJump)n; 126 | queue(state, jmp.getTarget()); 127 | return true; 128 | } 129 | else if (n instanceof SwitchFlowBlockJump) { 130 | SwitchFlowBlockJump jmp = (SwitchFlowBlockJump)n; 131 | for (int i = 0; i < jmp.getTargets().length; i++) 132 | queue(state, jmp.getTargets()[i]); 133 | return true; 134 | } 135 | else if (n instanceof VariableAssignationNode) { 136 | VariableAssignationNode ldr = (VariableAssignationNode)n; 137 | Object o = state.set(ldr, ldr.getVariable()); 138 | if (!assigns.containsKey(ldr.getVariable())) 139 | assigns.put(ldr.getVariable(), new ArrayList()); 140 | List l = assigns.get(ldr.getVariable()); 141 | if (!l.contains(o)) 142 | l.add(o); 143 | } 144 | else if (n instanceof ReturnNode) { 145 | return true; 146 | } 147 | else if (n instanceof FlowBlock) { 148 | FlowBlock f = (FlowBlock)n; 149 | if (!quit && f.getNext() != null) 150 | queue(state, f.getNext()); 151 | return true; 152 | } 153 | return quit; 154 | } 155 | 156 | 157 | private void queue(FlowBlockState current, FlowBlock target) { 158 | for (int i = 0; i < blocks.length; i++) { 159 | if (blocks[i] != target) 160 | continue; 161 | if (states[i] == null) { 162 | states[i] = current.copy(); 163 | return; 164 | } 165 | 166 | if (states[i].merge(current)) 167 | processed[i] = false; 168 | return; 169 | } 170 | throw new DecompilerException("logic error"); 171 | } 172 | 173 | 174 | private class FlowBlockState { 175 | private Map hits; 176 | 177 | public FlowBlockState() { 178 | hits = new HashMap(); 179 | } 180 | 181 | public Object set(Object obj, LocalVariable var) { 182 | if (!hits.containsKey(var)) 183 | hits.put(var, obj); 184 | return hits.get(var); 185 | } 186 | 187 | public boolean merge(FlowBlockState other) { 188 | boolean updated = false; 189 | for (LocalVariable ovar : other.hits.keySet()) { 190 | if (!hits.containsKey(ovar)) 191 | continue; 192 | 193 | if (hits.get(ovar) != other.hits.get(ovar)) { 194 | hits.put(ovar, null); 195 | updated = true; 196 | } 197 | } 198 | return updated; 199 | } 200 | 201 | public FlowBlockState copy() { 202 | FlowBlockState s = new FlowBlockState(); 203 | for (LocalVariable n : hits.keySet()) 204 | s.hits.put(n, hits.get(n)); 205 | return s; 206 | } 207 | } 208 | } 209 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/AbstractCodeNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import java.util.ArrayList; 20 | import java.util.List; 21 | 22 | import mgi.tools.jagdecs2.CodePrinter; 23 | 24 | public abstract class AbstractCodeNode { 25 | 26 | private static final int INITIAL_BUFFER_SIZE = 4; 27 | private AbstractCodeNode[] childs; 28 | private int codeAddress; 29 | 30 | 31 | public abstract void print(CodePrinter printer); 32 | 33 | public AbstractCodeNode() { 34 | childs = new AbstractCodeNode[INITIAL_BUFFER_SIZE]; 35 | codeAddress = 0; 36 | } 37 | 38 | public AbstractCodeNode read() { 39 | if (childs[codeAddress] == null) 40 | return null; 41 | return childs[codeAddress++]; 42 | } 43 | 44 | public AbstractCodeNode read(int addr) { 45 | if (addr < 0 || addr >= childs.length || (addr > 0 && childs[addr - 1] == null)) 46 | throw new IllegalArgumentException("Invalid address."); 47 | return childs[addr]; 48 | } 49 | 50 | public void write(AbstractCodeNode node) { 51 | if (needsExpand()) 52 | expand(); 53 | if (childs[codeAddress] == null) 54 | childs[codeAddress++] = node; 55 | else { 56 | List taken = new ArrayList(); 57 | for (int i = codeAddress; i < childs.length; i++) { 58 | if (childs[i] != null) { 59 | taken.add(childs[i]); 60 | childs[i] = null; 61 | } 62 | } 63 | childs[codeAddress] = node; 64 | int write = ++codeAddress; 65 | for (AbstractCodeNode n : taken) 66 | childs[write++] = n; 67 | } 68 | } 69 | 70 | public void delete() { 71 | delete(codeAddress); 72 | } 73 | 74 | public void delete(int address) { 75 | if (address < 0 || address >= childs.length || (address > 0 && childs[address - 1] == null)) 76 | throw new IllegalArgumentException("Invalid address."); 77 | if (childs[address] == null) 78 | throw new RuntimeException("No element to delete."); 79 | if ((address + 1) < childs.length && childs[address + 1] == null) 80 | childs[address] = null; 81 | else { 82 | childs[address] = null; 83 | for (int i = address + 1; i < childs.length; i++) { 84 | childs[i - 1] = childs[i]; 85 | childs[i] = null; 86 | } 87 | } 88 | } 89 | 90 | public int addressOf(AbstractCodeNode child) { 91 | for (int i = 0; i < childs.length; i++) 92 | if (childs[i] == child) 93 | return i; 94 | return -1; 95 | } 96 | 97 | public List listChilds() { 98 | List list = new ArrayList(); 99 | for (int i = 0; i < childs.length; i++) 100 | if (childs[i] != null) 101 | list.add(childs[i]); 102 | return list; 103 | } 104 | 105 | public int size() { 106 | int total = 0; 107 | for (int i = 0; i < childs.length; i++) 108 | if (childs[i] != null) 109 | total++; 110 | return total; 111 | } 112 | 113 | 114 | private boolean needsExpand() { 115 | double max = childs.length * 0.50; 116 | return (double)size() > max; 117 | } 118 | 119 | 120 | private void expand() { 121 | if (childs.length >= Integer.MAX_VALUE) 122 | throw new RuntimeException("Can't expand anymore."); 123 | long newSize = childs.length * 2; 124 | if (newSize > Integer.MAX_VALUE) 125 | newSize = Integer.MAX_VALUE; 126 | AbstractCodeNode[] newBuffer = new AbstractCodeNode[(int)newSize]; 127 | System.arraycopy(childs, 0, newBuffer, 0, childs.length); 128 | childs = newBuffer; 129 | } 130 | 131 | 132 | @Override 133 | public String toString() { 134 | return CodePrinter.print(this); 135 | } 136 | 137 | public void setCodeAddress(int codeAddress) { 138 | if (codeAddress < 0 || codeAddress >= childs.length || (codeAddress > 0 && childs[codeAddress - 1] == null)) 139 | throw new IllegalArgumentException("Invalid address."); 140 | this.codeAddress = codeAddress; 141 | } 142 | 143 | public int getCodeAddress() { 144 | return codeAddress; 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/ArrayLoadNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CS2Type; 20 | import mgi.tools.jagdecs2.CodePrinter; 21 | 22 | public class ArrayLoadNode extends ExpressionNode { 23 | 24 | 25 | private ExpressionNode array; 26 | private ExpressionNode index; 27 | 28 | public ArrayLoadNode(ExpressionNode array,ExpressionNode index) { 29 | this.array = array; 30 | this.index = index; 31 | this.write(array); 32 | this.write(index); 33 | array.setParent(this); 34 | index.setParent(this); 35 | } 36 | 37 | @Override 38 | public int getPriority() { 39 | return ExpressionNode.PRIORITY_ARRAY_INDEX; 40 | } 41 | 42 | @Override 43 | public CS2Type getType() { 44 | return array.getType().getElementType(); 45 | } 46 | 47 | @Override 48 | public ExpressionNode copy() { 49 | return new ArrayLoadNode(this.array.copy(), this.index.copy()); 50 | } 51 | 52 | @Override 53 | public void print(CodePrinter printer) { 54 | printer.beginPrinting(this); 55 | boolean needsParen = array.getPriority() > this.getPriority(); 56 | if (needsParen) 57 | printer.print('('); 58 | array.print(printer); 59 | if (needsParen) 60 | printer.print(')'); 61 | printer.print('['); 62 | index.print(printer); 63 | printer.print(']'); 64 | printer.endPrinting(this); 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/ArrayStoreNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CS2Type; 20 | import mgi.tools.jagdecs2.CodePrinter; 21 | 22 | public class ArrayStoreNode extends ExpressionNode { 23 | 24 | 25 | private ExpressionNode array; 26 | private ExpressionNode index; 27 | private ExpressionNode value; 28 | 29 | public ArrayStoreNode(ExpressionNode array,ExpressionNode index,ExpressionNode value) { 30 | this.array = array; 31 | this.index = index; 32 | this.value = value; 33 | this.write(array); 34 | this.write(index); 35 | this.write(value); 36 | array.setParent(this); 37 | index.setParent(this); 38 | value.setParent(this); 39 | 40 | } 41 | 42 | @Override 43 | public int getPriority() { 44 | return ExpressionNode.PRIORITY_ASSIGNMENT; 45 | } 46 | 47 | @Override 48 | public CS2Type getType() { 49 | return value.getType(); 50 | } 51 | 52 | @Override 53 | public ExpressionNode copy() { 54 | return new ArrayStoreNode(this.array.copy(), this.index.copy(), this.value.copy()); 55 | } 56 | 57 | @Override 58 | public void print(CodePrinter printer) { 59 | printer.beginPrinting(this); 60 | boolean needsParen = array.getPriority() > ExpressionNode.PRIORITY_ARRAY_INDEX; 61 | if (needsParen) 62 | printer.print('('); 63 | array.print(printer); 64 | if (needsParen) 65 | printer.print(')'); 66 | printer.print('['); 67 | index.print(printer); 68 | printer.print(']'); 69 | printer.print(" = "); 70 | value.print(printer); 71 | printer.endPrinting(this); 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/BitConfigurationLoadNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CS2Type; 20 | import mgi.tools.jagdecs2.CodePrinter; 21 | import mgi.tools.jagdecs2.util.BitConfigInfo; 22 | import mgi.tools.jagdecs2.util.TextUtils; 23 | 24 | public class BitConfigurationLoadNode extends ExpressionNode { 25 | 26 | private BitConfigInfo info; 27 | private boolean secondaryDomainRegister; 28 | 29 | public BitConfigurationLoadNode(BitConfigInfo info, boolean secondaryDomainRegister) { 30 | this.info = info; 31 | this.secondaryDomainRegister = secondaryDomainRegister; 32 | } 33 | 34 | @Override 35 | public CS2Type getType() { 36 | CS2Type t = info.getBase().getType(); 37 | if (t != CS2Type.INT) 38 | throw new RuntimeException("assert fail!"); 39 | return t; 40 | } 41 | 42 | @Override 43 | public int getPriority() { 44 | return ExpressionNode.PRIORITY_MEMBER_ACCESS; 45 | } 46 | 47 | @Override 48 | public ExpressionNode copy() { 49 | return new BitConfigurationLoadNode(this.info, this.secondaryDomainRegister); 50 | } 51 | 52 | @Override 53 | public void print(CodePrinter printer) { 54 | printer.beginPrinting(this); 55 | if (secondaryDomainRegister) 56 | printer.print("$."); 57 | printer.print(info.getBase().getDomainName()); 58 | printer.print('['); 59 | printer.print(TextUtils.quote(info.getBase().getConfigName()).replace('"', '\'')); 60 | printer.print(']'); 61 | printer.print('.'); 62 | printer.print(info.getName()); 63 | printer.endPrinting(this); 64 | } 65 | 66 | 67 | public BitConfigInfo getInfo() { 68 | return info; 69 | } 70 | 71 | public boolean isSecondaryDomainRegister() { 72 | return secondaryDomainRegister; 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/BitConfigurationStoreNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CS2Type; 20 | import mgi.tools.jagdecs2.CodePrinter; 21 | import mgi.tools.jagdecs2.util.BitConfigInfo; 22 | import mgi.tools.jagdecs2.util.TextUtils; 23 | 24 | public class BitConfigurationStoreNode extends ExpressionNode { 25 | 26 | private BitConfigInfo info; 27 | private boolean secondaryDomainRegister; 28 | private ExpressionNode expression; 29 | 30 | public BitConfigurationStoreNode(BitConfigInfo info, boolean secondaryDomainRegister, ExpressionNode expr) { 31 | this.info = info; 32 | this.secondaryDomainRegister = secondaryDomainRegister; 33 | this.expression = expr; 34 | this.write(expr); 35 | expr.setParent(this); 36 | } 37 | 38 | @Override 39 | public int getPriority() { 40 | return ExpressionNode.PRIORITY_ASSIGNMENT; 41 | } 42 | 43 | @Override 44 | public CS2Type getType() { 45 | return this.expression.getType(); 46 | } 47 | 48 | public BitConfigInfo getInfo() { 49 | return info; 50 | } 51 | 52 | public boolean isSecondaryDomainRegister() { 53 | return secondaryDomainRegister; 54 | } 55 | 56 | public ExpressionNode getExpression() { 57 | return expression; 58 | } 59 | 60 | @Override 61 | public ExpressionNode copy() { 62 | return new BitConfigurationStoreNode(this.info, this.secondaryDomainRegister, this.expression.copy()); 63 | } 64 | 65 | @Override 66 | public void print(CodePrinter printer) { 67 | printer.beginPrinting(this); 68 | boolean needsParen = expression.getPriority() > getPriority(); 69 | if (secondaryDomainRegister) 70 | printer.print("$."); 71 | printer.print(info.getBase().getDomainName()); 72 | printer.print('['); 73 | printer.print(TextUtils.quote(info.getBase().getConfigName()).replace('"', '\'')); 74 | printer.print(']'); 75 | printer.print('.'); 76 | printer.print(info.getName()); 77 | printer.print(" = "); 78 | if (needsParen) 79 | printer.print('('); 80 | expression.print(printer); 81 | if (needsParen) 82 | printer.print(')'); 83 | printer.endPrinting(this); 84 | } 85 | 86 | 87 | 88 | } 89 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/BreakNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CodePrinter; 20 | 21 | public class BreakNode extends AbstractCodeNode implements IFlowControlNode { 22 | 23 | private IBreakableNode node; 24 | private ScopeNode selfScope; 25 | 26 | 27 | public BreakNode(ScopeNode selfScope, IBreakableNode node) { 28 | this.node = node; 29 | this.selfScope = selfScope; 30 | // don't need to write self scope because it's just for label checks. (Not expressed). 31 | if (this.getSelfScope().getParent() != node && node.getLabelName() == null) 32 | node.enableLabelName(); 33 | } 34 | 35 | 36 | @Override 37 | public IBreakableNode getNode() { 38 | return node; 39 | } 40 | 41 | public ScopeNode getSelfScope() { 42 | return selfScope; 43 | } 44 | 45 | @Override 46 | public void print(CodePrinter printer) { 47 | printer.beginPrinting(this); 48 | if (this.getSelfScope().getParent() == node) 49 | printer.print("break;"); 50 | else 51 | printer.print("break " + node.getLabelName() + ";"); 52 | printer.endPrinting(this); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/BuildStringNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CS2Type; 20 | import mgi.tools.jagdecs2.CodePrinter; 21 | 22 | public class BuildStringNode extends ExpressionNode { 23 | 24 | private ExpressionNode[] arguments; 25 | 26 | 27 | public BuildStringNode(ExpressionNode[] arguments) { 28 | this.arguments = arguments; 29 | for (int i = 0; i < arguments.length; i++) { 30 | this.write(arguments[i]); 31 | arguments[i].setParent(this); 32 | } 33 | } 34 | 35 | @Override 36 | public int getPriority() { 37 | return ExpressionNode.PRIORITY_CONTACTSTRING; 38 | } 39 | 40 | @Override 41 | public CS2Type getType() { 42 | return CS2Type.STRING; 43 | } 44 | 45 | 46 | @Override 47 | public ExpressionNode copy() { 48 | ExpressionNode[] argsCopy = new ExpressionNode[arguments.length]; 49 | for (int i = 0; i < arguments.length; i++) 50 | argsCopy[i] = arguments[i].copy(); 51 | return new BuildStringNode(argsCopy); 52 | } 53 | 54 | @Override 55 | public void print(CodePrinter printer) { 56 | printer.beginPrinting(this); 57 | for (int i = 0; i < arguments.length; i++) { 58 | boolean needsParen = arguments[i].getPriority() > this.getPriority(); 59 | if (needsParen) 60 | printer.print('('); 61 | arguments[i].print(printer); 62 | if (needsParen) 63 | printer.print(')'); 64 | if ((i + 1) < arguments.length) 65 | printer.print(" + "); 66 | } 67 | printer.endPrinting(this); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/CallExpressionNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CS2Type; 20 | import mgi.tools.jagdecs2.CodePrinter; 21 | import mgi.tools.jagdecs2.util.FunctionInfo; 22 | 23 | public class CallExpressionNode extends ExpressionNode { 24 | 25 | private FunctionInfo info; 26 | private ExpressionNode[] arguments; 27 | 28 | 29 | public CallExpressionNode(FunctionInfo info,ExpressionNode[] arguments) { 30 | this.info = info; 31 | this.arguments = arguments; 32 | for (int i = 0; i < arguments.length; i++) { 33 | this.write(arguments[i]); 34 | arguments[i].setParent(this); 35 | } 36 | } 37 | 38 | @Override 39 | public int getPriority() { 40 | return ExpressionNode.PRIORITY_CALL; 41 | } 42 | 43 | @Override 44 | public CS2Type getType() { 45 | return info.getReturnType(); 46 | } 47 | 48 | 49 | @Override 50 | public ExpressionNode copy() { 51 | ExpressionNode[] argsCopy = new ExpressionNode[arguments.length]; 52 | for (int i = 0; i < arguments.length; i++) 53 | argsCopy[i] = arguments[i].copy(); 54 | return new CallExpressionNode(this.info,argsCopy); 55 | } 56 | 57 | @Override 58 | public void print(CodePrinter printer) { 59 | printer.beginPrinting(this); 60 | printer.print(info.getName()); 61 | printer.print('('); 62 | for (int i = 0; i < arguments.length; i++) { 63 | arguments[i].print(printer); 64 | if ((i + 1) < arguments.length) 65 | printer.print(", "); 66 | } 67 | printer.print(')'); 68 | printer.endPrinting(this); 69 | } 70 | 71 | public FunctionInfo getInfo() { 72 | return info; 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/CaseAnnotation.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CodePrinter; 20 | 21 | 22 | public class CaseAnnotation extends AbstractCodeNode { 23 | 24 | private int caseNumber; 25 | private boolean isDefault; 26 | 27 | public CaseAnnotation(int caseNumber) { 28 | this.caseNumber = caseNumber; 29 | this.isDefault = false; 30 | } 31 | 32 | public CaseAnnotation() { 33 | this.caseNumber = 0; 34 | this.isDefault = true; 35 | } 36 | 37 | public int getCaseNumber() { 38 | return caseNumber; 39 | } 40 | 41 | public boolean isDefault() { 42 | return isDefault; 43 | } 44 | 45 | @Override 46 | public void print(CodePrinter printer) { 47 | printer.beginPrinting(this); 48 | if (isDefault) 49 | printer.print("default:"); 50 | else 51 | printer.print("case " + caseNumber + ":"); 52 | printer.endPrinting(this); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/CastNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CS2Type; 20 | import mgi.tools.jagdecs2.CodePrinter; 21 | 22 | public class CastNode extends ExpressionNode { 23 | 24 | 25 | private CS2Type type; 26 | private ExpressionNode expression; 27 | 28 | public CastNode(CS2Type type,ExpressionNode expr) { 29 | this.type = type; 30 | this.expression = expr; 31 | this.write(expr); 32 | expr.setParent(this); 33 | } 34 | 35 | @Override 36 | public int getPriority() { 37 | return expression.getPriority(); 38 | //return ExpressionNode.PRIORITY_CAST; 39 | } 40 | 41 | @Override 42 | public CS2Type getType() { 43 | return type; 44 | } 45 | 46 | @Override 47 | public ExpressionNode copy() { 48 | return new CastNode(this.type,this.expression.copy()); 49 | } 50 | 51 | public ExpressionNode getExpression() { 52 | return expression; 53 | } 54 | 55 | @Override 56 | public void print(CodePrinter printer) { 57 | printer.beginPrinting(this); 58 | //boolean needsParen = expression.getPriority() > this.getPriority(); 59 | //printer.print("(" + type + ")"); 60 | //if (needsParen) 61 | // printer.print('('); 62 | expression.print(printer); 63 | //if (needsParen) 64 | // printer.print(')'); 65 | printer.endPrinting(this); 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/CommentNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CodePrinter; 20 | 21 | public class CommentNode extends AbstractCodeNode { 22 | 23 | public static final int STANDART_STYLE = 0; 24 | public static final int LOGO_STYLE = 1; 25 | 26 | private String comment; 27 | private int style; 28 | 29 | public CommentNode(String comment, int style) { 30 | this.comment = comment; 31 | this.style = style; 32 | } 33 | 34 | public int numLines() { 35 | int total = 0; 36 | for (int i = 0; i < comment.length(); i++) 37 | if (comment.charAt(i) == '\n') 38 | total++; 39 | return total; 40 | } 41 | 42 | public String getComment() { 43 | return comment; 44 | } 45 | 46 | public int getStyle() { 47 | return style; 48 | } 49 | 50 | @Override 51 | public void print(CodePrinter printer) { 52 | printer.beginPrinting(this); 53 | if (numLines() > 0) { 54 | if (style == LOGO_STYLE) { 55 | printer.print("/* \n * "); 56 | printer.print(comment.replace("\n", "\n * ")); 57 | printer.print("\n */"); 58 | } 59 | else { 60 | printer.tab(); 61 | printer.print("/* \n"); 62 | printer.print(comment); 63 | printer.untab(); 64 | printer.print("\n */"); 65 | } 66 | } 67 | else { 68 | printer.print("// " + comment); 69 | } 70 | printer.endPrinting(this); 71 | } 72 | 73 | 74 | 75 | 76 | } 77 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/ConditionalExpressionNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CS2Type; 20 | import mgi.tools.jagdecs2.CodePrinter; 21 | 22 | public class ConditionalExpressionNode extends ExpressionNode { 23 | 24 | /** 25 | * Contains left expression node. 26 | */ 27 | private ExpressionNode left; 28 | /** 29 | * Contains right expression node. 30 | */ 31 | private ExpressionNode right; 32 | /** 33 | * Contains expression conditional. 34 | * 0 means != 35 | * 1 means == 36 | * 2 means > 37 | * 3 means < 38 | * 4 means >= 39 | * 5 means <= 40 | * 6 means || 41 | * 7 means && 42 | */ 43 | private int conditional; 44 | 45 | public ConditionalExpressionNode(ExpressionNode left,ExpressionNode right,int conditional) { 46 | this.left = left; 47 | this.right = right; 48 | this.conditional = conditional; 49 | 50 | this.write(left); 51 | this.write(right); 52 | this.left.setParent(this); 53 | this.right.setParent(this); 54 | } 55 | 56 | @Override 57 | public int getPriority() { 58 | if (conditional == 0 || conditional == 1) 59 | return ExpressionNode.PRIORITY_EQNE; 60 | else if (conditional < 6) 61 | return ExpressionNode.PRIORITY_LELTGEGTINSTANCEOF; 62 | else if (conditional == 6) 63 | return ExpressionNode.PRIORITY_LOGICALOR; 64 | else if (conditional == 7) 65 | return ExpressionNode.PRIORITY_LOGICALAND; 66 | else 67 | return super.getPriority(); 68 | } 69 | 70 | 71 | @Override 72 | public CS2Type getType() { 73 | return CS2Type.BOOLEAN; 74 | } 75 | 76 | private String conditionalToString() { 77 | switch (this.conditional) { 78 | case 0: 79 | return "!="; 80 | case 1: 81 | return "=="; 82 | case 2: 83 | return ">"; 84 | case 3: 85 | return "<"; 86 | case 4: 87 | return ">="; 88 | case 5: 89 | return "<="; 90 | case 6: 91 | return "||"; 92 | case 7: 93 | return "&&"; 94 | default: 95 | return "??"; 96 | } 97 | } 98 | 99 | @Override 100 | public void print(CodePrinter printer) { 101 | printer.beginPrinting(this); 102 | boolean needsLeftParen = left.getPriority() > this.getPriority(); 103 | boolean needsRightParen = right.getPriority() > this.getPriority(); 104 | if (needsLeftParen) 105 | printer.print("("); 106 | this.left.print(printer); 107 | if (needsLeftParen) 108 | printer.print(")"); 109 | printer.print(" " + this.conditionalToString() + " "); 110 | if (needsRightParen) 111 | printer.print("("); 112 | this.right.print(printer); 113 | if (needsRightParen) 114 | printer.print(")"); 115 | printer.endPrinting(this); 116 | } 117 | 118 | 119 | @Override 120 | public ExpressionNode copy() { 121 | return new ConditionalExpressionNode(left.copy(),right.copy(),conditional); 122 | } 123 | 124 | public ExpressionNode getLeft() { 125 | return left; 126 | } 127 | 128 | public ExpressionNode getRight() { 129 | return right; 130 | } 131 | 132 | public int getConditional() { 133 | return conditional; 134 | } 135 | 136 | 137 | 138 | 139 | } 140 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/ConditionalFlowBlockJump.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CodePrinter; 20 | 21 | public class ConditionalFlowBlockJump extends AbstractCodeNode { 22 | 23 | /** 24 | * Contains expression which type is boolean. 25 | */ 26 | private ExpressionNode expression; 27 | /** 28 | * Contains target flow block. 29 | */ 30 | private FlowBlock target; 31 | 32 | public ConditionalFlowBlockJump(ExpressionNode expr,FlowBlock target) { 33 | this.expression = expr; 34 | this.target = target; 35 | this.write(expr); 36 | expr.setParent(this); 37 | } 38 | 39 | 40 | public ExpressionNode getExpression() { 41 | return expression; 42 | } 43 | 44 | public FlowBlock getTarget() { 45 | return target; 46 | } 47 | 48 | @Override 49 | public void print(CodePrinter printer) { 50 | printer.beginPrinting(this); 51 | printer.print("IF ("); 52 | expression.print(printer); 53 | printer.print(") "); 54 | printer.tab(); 55 | printer.print("\nGOTO\t" + "flow_" + target.getBlockID()); 56 | printer.untab(); 57 | printer.endPrinting(this); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/ConfigurationLoadNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CS2Type; 20 | import mgi.tools.jagdecs2.CodePrinter; 21 | import mgi.tools.jagdecs2.util.ConfigInfo; 22 | import mgi.tools.jagdecs2.util.TextUtils; 23 | 24 | public class ConfigurationLoadNode extends ExpressionNode { 25 | 26 | private ConfigInfo info; 27 | private boolean secondaryDomainRegister; 28 | 29 | public ConfigurationLoadNode(ConfigInfo info, boolean secondaryDomainRegister) { 30 | this.info = info; 31 | this.secondaryDomainRegister = secondaryDomainRegister; 32 | } 33 | 34 | 35 | 36 | @Override 37 | public CS2Type getType() { 38 | return info.getType(); 39 | } 40 | 41 | @Override 42 | public int getPriority() { 43 | return ExpressionNode.PRIORITY_ARRAY_INDEX; 44 | } 45 | 46 | @Override 47 | public ExpressionNode copy() { 48 | return new ConfigurationLoadNode(this.info, this.secondaryDomainRegister); 49 | } 50 | 51 | @Override 52 | public void print(CodePrinter printer) { 53 | printer.beginPrinting(this); 54 | if (secondaryDomainRegister) 55 | printer.print("$."); 56 | printer.print(info.getDomainName()); 57 | printer.print('['); 58 | printer.print(TextUtils.quote(info.getConfigName()).replace('"', '\'')); 59 | printer.print(']'); 60 | printer.endPrinting(this); 61 | } 62 | 63 | 64 | public ConfigInfo getInfo() { 65 | return info; 66 | } 67 | 68 | public boolean isSecondaryDomainRegister() { 69 | return secondaryDomainRegister; 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/ConfigurationStoreNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CS2Type; 20 | import mgi.tools.jagdecs2.CodePrinter; 21 | import mgi.tools.jagdecs2.util.ConfigInfo; 22 | import mgi.tools.jagdecs2.util.TextUtils; 23 | 24 | public class ConfigurationStoreNode extends ExpressionNode { 25 | 26 | private ConfigInfo info; 27 | private boolean secondaryDomainRegister; 28 | private ExpressionNode expression; 29 | 30 | public ConfigurationStoreNode(ConfigInfo info, boolean secondaryDomainRegister, ExpressionNode expr) { 31 | this.info = info; 32 | this.secondaryDomainRegister = secondaryDomainRegister; 33 | this.expression = expr; 34 | this.write(expr); 35 | expr.setParent(this); 36 | } 37 | 38 | @Override 39 | public int getPriority() { 40 | return ExpressionNode.PRIORITY_ASSIGNMENT; 41 | } 42 | 43 | @Override 44 | public CS2Type getType() { 45 | return this.expression.getType(); 46 | } 47 | 48 | public ConfigInfo getInfo() { 49 | return info; 50 | } 51 | 52 | public boolean isSecondaryDomainRegister() { 53 | return secondaryDomainRegister; 54 | } 55 | 56 | public ExpressionNode getExpression() { 57 | return expression; 58 | } 59 | 60 | @Override 61 | public ExpressionNode copy() { 62 | return new ConfigurationStoreNode(this.info, this.secondaryDomainRegister,this.expression.copy()); 63 | } 64 | 65 | @Override 66 | public void print(CodePrinter printer) { 67 | printer.beginPrinting(this); 68 | boolean needsParen = expression.getPriority() > getPriority(); 69 | if (secondaryDomainRegister) 70 | printer.print("$."); 71 | printer.print(info.getDomainName()); 72 | printer.print('['); 73 | printer.print(TextUtils.quote(info.getConfigName()).replace('"', '\'')); 74 | printer.print(']'); 75 | printer.print(" = "); 76 | if (needsParen) 77 | printer.print('('); 78 | expression.print(printer); 79 | if (needsParen) 80 | printer.print(')'); 81 | printer.endPrinting(this); 82 | } 83 | 84 | 85 | 86 | } 87 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/ContinueNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CodePrinter; 20 | 21 | public class ContinueNode extends AbstractCodeNode implements IFlowControlNode { 22 | 23 | private IContinueableNode node; 24 | private ScopeNode selfScope; 25 | 26 | 27 | public ContinueNode(ScopeNode selfScope, IContinueableNode node) { 28 | this.node = node; 29 | this.selfScope = selfScope; 30 | // don't need to write self scope because it's just for label checks. (Not expressed). 31 | if (this.getSelfScope().getParent() != node && node.getLabelName() == null) 32 | node.enableLabelName(); 33 | } 34 | 35 | @Override 36 | public IContinueableNode getNode() { 37 | return node; 38 | } 39 | 40 | public ScopeNode getSelfScope() { 41 | return selfScope; 42 | } 43 | 44 | @Override 45 | public void print(CodePrinter printer) { 46 | printer.beginPrinting(this); 47 | if (this.getSelfScope().getParent() == node) 48 | printer.print("continue;"); 49 | else 50 | printer.print("continue " + node.getLabelName() + ";"); 51 | printer.endPrinting(this); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/ExpressionNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CS2Type; 20 | 21 | 22 | public abstract class ExpressionNode extends AbstractCodeNode { 23 | 24 | public static final int PRIORITY_STANDART = 0; 25 | public static final int PRIORITY_ARRAY_INDEX = 1; 26 | public static final int PRIORITY_CALL = 1; 27 | public static final int PRIORITY_MEMBER_ACCESS = 1; 28 | public static final int PRIORITY_UNARYPLUSMINUS = 2; 29 | public static final int PRIORITY_PLUSMINUSPREFIXPOSTFIX = 2; 30 | public static final int PRIORITY_UNARYLOGICALNOT = 2; 31 | public static final int PRIORITY_UNARYBITWISENOT = 2; 32 | public static final int PRIORITY_CAST = 2; 33 | public static final int PRIORITY_NEWOPERATOR = 2; 34 | public static final int PRIORITY_MULDIVREM = 3; 35 | public static final int PRIORITY_ADDSUB = 4; 36 | public static final int PRIORITY_CONTACTSTRING = 4; 37 | public static final int PRIORITY_BITSHIFTS = 5; 38 | public static final int PRIORITY_LELTGEGTINSTANCEOF = 6; 39 | public static final int PRIORITY_EQNE = 7; 40 | public static final int PRIORITY_BITAND = 8; 41 | public static final int PRIORITY_BITXOR = 9; 42 | public static final int PRIORITY_BITOR = 10; 43 | public static final int PRIORITY_LOGICALAND = 11; 44 | public static final int PRIORITY_LOGICALOR = 12; 45 | public static final int PRIORITY_TERNARY = 13; 46 | public static final int PRIORITY_ASSIGNMENT = 14; 47 | 48 | 49 | /** 50 | * Contains parent or null if this expression return is 51 | * not used. 52 | */ 53 | private AbstractCodeNode parent; 54 | 55 | /** 56 | * Get's priority of this expression. 57 | */ 58 | public int getPriority() { 59 | return 0; 60 | } 61 | 62 | /** 63 | * Return's type of this expression. 64 | */ 65 | public abstract CS2Type getType(); 66 | 67 | /** 68 | * Copies this expression node. 69 | */ 70 | public abstract ExpressionNode copy(); 71 | 72 | public void setParent(AbstractCodeNode parent) { 73 | this.parent = parent; 74 | } 75 | 76 | public AbstractCodeNode getParent() { 77 | return parent; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/FlowBlock.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import java.util.ArrayList; 20 | import java.util.List; 21 | 22 | import mgi.tools.jagdecs2.CS2Stack; 23 | import mgi.tools.jagdecs2.CodePrinter; 24 | 25 | public class FlowBlock extends AbstractCodeNode { 26 | 27 | /** 28 | * Contains ID of this flow block. 29 | * Main flow block ID is always 0. 30 | */ 31 | private int blockID; 32 | /** 33 | * Contains all blocks to which this blocks 34 | * jumps with instructions. 35 | */ 36 | private List sucessors; 37 | /** 38 | * Contains all blocks which jumps to this block 39 | * with instructions. 40 | */ 41 | private List predecesors; 42 | /** 43 | * Contains next flow block by code order. 44 | * Can be null if this block is the last block. 45 | */ 46 | private FlowBlock next; 47 | /** 48 | * Contains previous flow block by code order. 49 | * Can be null if this block is the first block. 50 | */ 51 | private FlowBlock prev; 52 | /** 53 | * Contains stack content before jumping/walking into this 54 | * block dumped to local variables. 55 | */ 56 | private CS2Stack variableStack; 57 | /** 58 | * Contains start address of this flow block in instructions. 59 | * Not guaranteed to be correct. 60 | */ 61 | private int startAddress; 62 | 63 | public FlowBlock() { 64 | this(0, 0, new CS2Stack()); 65 | } 66 | 67 | public FlowBlock(int blockID, int startAddress,CS2Stack stack) { 68 | this.blockID = blockID; 69 | this.startAddress = startAddress; 70 | this.variableStack = stack; 71 | this.sucessors = new ArrayList(); 72 | this.predecesors = new ArrayList(); 73 | } 74 | 75 | 76 | 77 | @Override 78 | public void print(CodePrinter printer) { 79 | printer.beginPrinting(this); 80 | printer.print("flow_" + this.blockID + ":"); 81 | printer.tab(); 82 | List childs = this.listChilds(); 83 | for (AbstractCodeNode node : childs) { 84 | printer.print('\n'); 85 | node.print(printer); 86 | } 87 | printer.untab(); 88 | printer.endPrinting(this); 89 | } 90 | 91 | public List getPredecesors() { 92 | return predecesors; 93 | } 94 | 95 | public List getSucessors() { 96 | return sucessors; 97 | } 98 | 99 | public void setNext(FlowBlock next) { 100 | this.next = next; 101 | } 102 | 103 | public FlowBlock getNext() { 104 | return next; 105 | } 106 | 107 | public void setPrev(FlowBlock prev) { 108 | this.prev = prev; 109 | } 110 | 111 | public FlowBlock getPrev() { 112 | return prev; 113 | } 114 | 115 | public CS2Stack getStack() { 116 | return variableStack; 117 | } 118 | 119 | public int getStartAddress() { 120 | return startAddress; 121 | } 122 | 123 | public int getBlockID() { 124 | return blockID; 125 | } 126 | 127 | public boolean isSynthethic() { 128 | return startAddress < 0; 129 | } 130 | 131 | 132 | 133 | } 134 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/FunctionExpressionNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CS2Type; 20 | import mgi.tools.jagdecs2.CodePrinter; 21 | 22 | public class FunctionExpressionNode extends ExpressionNode { 23 | 24 | /** 25 | * Id of the function. 26 | */ 27 | private ExpressionNode id; 28 | /** 29 | * Contains int which this expression holds. 30 | */ 31 | private FunctionNode function; 32 | 33 | public FunctionExpressionNode(ExpressionNode id, FunctionNode function) { 34 | this.id = id; 35 | this.function = function; 36 | 37 | this.write(id); 38 | id.setParent(this); 39 | } 40 | 41 | public ExpressionNode getId() { 42 | return id; 43 | } 44 | 45 | public FunctionNode getFunction() { 46 | return function; 47 | } 48 | 49 | @Override 50 | public CS2Type getType() { 51 | return CS2Type.FUNCTION; 52 | } 53 | 54 | @Override 55 | public ExpressionNode copy() { 56 | return new FunctionExpressionNode(this.id.copy(), this.function); 57 | } 58 | 59 | @Override 60 | public void print(CodePrinter printer) { 61 | printer.beginPrinting(this); 62 | if (function != null) { 63 | printer.print("new " + getType() + "<" + function.getName() + ">" + "("); 64 | LocalVariable[] locals = function.getArgumentLocals(); 65 | for (int i = 0; i < locals.length; i++) { 66 | printer.print(locals[i].toString()); 67 | if ((i + 1) < locals.length) 68 | printer.print(','); 69 | } 70 | printer.print(") "); 71 | function.getScope().print(printer); 72 | } 73 | else { 74 | printer.print("load " + getType() + "<"); 75 | id.print(printer); 76 | printer.print('>'); 77 | 78 | } 79 | printer.endPrinting(this); 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/FunctionNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CS2Type; 20 | import mgi.tools.jagdecs2.CodePrinter; 21 | 22 | public class FunctionNode extends AbstractCodeNode { 23 | 24 | private int id; 25 | private String name; 26 | private CS2Type[] argumentTypes; 27 | private String[] argumentNames; 28 | private LocalVariable[] argumentLocals; 29 | private CS2Type returnType; 30 | private ScopeNode scope; 31 | 32 | 33 | public FunctionNode(int id, String name,CS2Type[] args, String[] argnames, CS2Type returnType) { 34 | this.id = id; 35 | this.name = name; 36 | this.argumentTypes = args; 37 | this.argumentNames = argnames; 38 | this.returnType = returnType; 39 | this.argumentLocals = new LocalVariable[args.length]; 40 | this.scope = new ScopeNode(); 41 | this.write(scope); 42 | scope.setParent(this); 43 | } 44 | 45 | 46 | public int getId() { 47 | return id; 48 | } 49 | 50 | public String getName() { 51 | return name; 52 | } 53 | 54 | public CS2Type[] getArgumentTypes() { 55 | return argumentTypes; 56 | } 57 | 58 | public String[] getArgumentNames() { 59 | return argumentNames; 60 | } 61 | 62 | public LocalVariable[] getArgumentLocals() { 63 | return argumentLocals; 64 | } 65 | 66 | public void setReturnType(CS2Type returnType) { 67 | this.returnType = returnType; 68 | } 69 | 70 | public CS2Type getReturnType() { 71 | return returnType; 72 | } 73 | 74 | 75 | public ScopeNode getScope() { 76 | return scope; 77 | } 78 | 79 | 80 | @Override 81 | public void print(CodePrinter printer) { 82 | printer.beginPrinting(this); 83 | int precount = 0; 84 | for (int i = 0; i < size(); i++) { 85 | if (read(i) == scope) 86 | break; 87 | read(i).print(printer); 88 | printer.print('\n'); 89 | precount++; 90 | } 91 | 92 | if (precount > 0) 93 | printer.print("\n\n"); 94 | 95 | printer.print(this.returnType.toString()); 96 | printer.print(' '); 97 | printer.print(this.name); 98 | printer.print('('); 99 | for (int i = 0; i < argumentLocals.length; i++) { 100 | printer.print(argumentLocals[i].toString()); 101 | if ((i + 1) < argumentTypes.length) 102 | printer.print(','); 103 | } 104 | printer.print(')'); 105 | printer.print(' '); 106 | this.scope.print(printer); 107 | printer.endPrinting(this); 108 | } 109 | 110 | 111 | 112 | } 113 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/IBreakableNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | 20 | public interface IBreakableNode extends IControllableFlowNode { 21 | public FlowBlock getEnd(); 22 | public boolean canBreak(); 23 | } 24 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/IConstantNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | public interface IConstantNode { 20 | 21 | /** 22 | * Returns constant of this node. 23 | */ 24 | Object getConst(); 25 | } 26 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/IContinueableNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | 20 | public interface IContinueableNode extends IControllableFlowNode { 21 | public FlowBlock getStart(); 22 | public boolean canContinue(); 23 | } 24 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/IControllableFlowNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | public interface IControllableFlowNode extends ILabelableNode { 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/IFlowControlNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | public interface IFlowControlNode { 20 | 21 | public IControllableFlowNode getNode(); 22 | } 23 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/ILabelableNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | public interface ILabelableNode { 20 | public String getLabelName(); 21 | public void enableLabelName(); 22 | } 23 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/IfElseNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CodePrinter; 20 | 21 | public class IfElseNode extends AbstractCodeNode { 22 | 23 | /** 24 | * Contains expressions for each scope. 25 | */ 26 | private ExpressionNode[] expressions; 27 | /** 28 | * Contains scopes for each expression. 29 | */ 30 | private ScopeNode[] scopes; 31 | /** 32 | * Contains else scope if all expressions conditions 33 | * were not met. 34 | */ 35 | private ScopeNode elseScope; 36 | 37 | 38 | public IfElseNode(ExpressionNode[] expressions, ScopeNode[] scopes,ScopeNode elseScope) { 39 | this.expressions = expressions; 40 | this.scopes = scopes; 41 | this.elseScope = elseScope; 42 | for (int i = 0; i < expressions.length; i++) { 43 | this.write(expressions[i]); 44 | expressions[i].setParent(this); 45 | } 46 | for (int i = 0; i < scopes.length; i++) { 47 | this.write(scopes[i]); 48 | scopes[i].setParent(this); 49 | } 50 | this.write(elseScope); 51 | elseScope.setParent(this); 52 | } 53 | 54 | 55 | public boolean hasElseScope() { 56 | return !this.elseScope.isEmpty(); 57 | } 58 | 59 | public ExpressionNode[] getExpressions() { 60 | return expressions; 61 | } 62 | 63 | public ScopeNode[] getScopes() { 64 | return scopes; 65 | } 66 | 67 | public ScopeNode getElseScope() { 68 | return elseScope; 69 | } 70 | 71 | 72 | @Override 73 | public void print(CodePrinter printer) { 74 | printer.beginPrinting(this); 75 | for (int i = 0; i < expressions.length; i++) { 76 | boolean first = i == 0; 77 | printer.print(first ? "if (" : "\nelse if ("); 78 | expressions[i].print(printer); 79 | printer.print(") "); 80 | scopes[i].print(printer); 81 | } 82 | if (hasElseScope()) { 83 | printer.print("\nelse "); 84 | elseScope.print(printer); 85 | } 86 | printer.endPrinting(this); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/IntExpressionNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CS2Type; 20 | import mgi.tools.jagdecs2.CodePrinter; 21 | 22 | public class IntExpressionNode extends ExpressionNode implements IConstantNode { 23 | 24 | /** 25 | * Contains int which this expression holds. 26 | */ 27 | private int data; 28 | 29 | public IntExpressionNode(int data) { 30 | this.data = data; 31 | } 32 | 33 | 34 | public int getData() { 35 | return data; 36 | } 37 | 38 | @Override 39 | public CS2Type getType() { 40 | return CS2Type.INT; 41 | } 42 | 43 | @Override 44 | public Object getConst() { 45 | return this.data; 46 | } 47 | 48 | @Override 49 | public ExpressionNode copy() { 50 | return new IntExpressionNode(this.data); 51 | } 52 | 53 | @Override 54 | public void print(CodePrinter printer) { 55 | printer.beginPrinting(this); 56 | getType().printConstant(printer, data); 57 | printer.endPrinting(this); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/LoadNamedDataNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CS2Type; 20 | import mgi.tools.jagdecs2.CodePrinter; 21 | 22 | public class LoadNamedDataNode extends ExpressionNode { 23 | 24 | private String name; 25 | private CS2Type type; 26 | 27 | public LoadNamedDataNode(String name, CS2Type type) { 28 | this.name = name; 29 | this.type = type; 30 | } 31 | 32 | @Override 33 | public int getPriority() { 34 | return ExpressionNode.PRIORITY_STANDART; 35 | } 36 | 37 | @Override 38 | public CS2Type getType() { 39 | return type; 40 | } 41 | 42 | @Override 43 | public ExpressionNode copy() { 44 | return new LoadNamedDataNode(this.name,this.type); 45 | } 46 | 47 | public String getName() { 48 | return name; 49 | } 50 | 51 | @Override 52 | public void print(CodePrinter printer) { 53 | printer.beginPrinting(this); 54 | printer.print(name); 55 | printer.endPrinting(this); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/LocalVariable.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CS2Type; 20 | 21 | public class LocalVariable { 22 | 23 | private String name; 24 | private CS2Type type; 25 | private int identifier = -1; 26 | private boolean needsScopeDeclaration = true; 27 | private boolean isArgument; 28 | 29 | public LocalVariable(String name,CS2Type type) { 30 | this(name,type, false); 31 | } 32 | 33 | public LocalVariable(String name,CS2Type type, boolean isArgument) { 34 | this.name = name; 35 | this.type = type; 36 | this.isArgument = isArgument; 37 | } 38 | 39 | public CS2Type getType() { 40 | return type; 41 | } 42 | 43 | public String getName() { 44 | return name; 45 | } 46 | 47 | public void setName(String name) { 48 | this.name = name; 49 | } 50 | 51 | public boolean isArgument() { 52 | return isArgument; 53 | } 54 | 55 | public String toString() { 56 | return type + " " + name; 57 | } 58 | 59 | public void setIdentifier(int identifier) { 60 | this.identifier = identifier; 61 | } 62 | 63 | public int getIdentifier() { 64 | return identifier; 65 | } 66 | 67 | public boolean needsScopeDeclaration() { 68 | return needsScopeDeclaration; 69 | } 70 | 71 | public void setNeedsScopeDeclaration(boolean v) { 72 | needsScopeDeclaration = v; 73 | } 74 | 75 | public static int makeIdentifier(int index,int stackType) { 76 | return index | stackType << 16; 77 | } 78 | 79 | public static int makeStackDumpIdentifier(int index, int stackType) { 80 | return index | (stackType << 16) | 0x40000000; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/LongExpressionNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CS2Type; 20 | import mgi.tools.jagdecs2.CodePrinter; 21 | 22 | public class LongExpressionNode extends ExpressionNode implements IConstantNode { 23 | 24 | /** 25 | * Contains long which this expression holds. 26 | */ 27 | private long data; 28 | 29 | public LongExpressionNode(long data) { 30 | this.data = data; 31 | } 32 | 33 | public long getData() { 34 | return data; 35 | } 36 | 37 | @Override 38 | public CS2Type getType() { 39 | return CS2Type.LONG; 40 | } 41 | 42 | @Override 43 | public Object getConst() { 44 | return this.data; 45 | } 46 | 47 | @Override 48 | public ExpressionNode copy() { 49 | return new LongExpressionNode(data); 50 | } 51 | 52 | @Override 53 | public void print(CodePrinter printer) { 54 | printer.beginPrinting(this); 55 | getType().printConstant(printer, data); 56 | printer.endPrinting(this); 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/LoopNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CodePrinter; 20 | 21 | public class LoopNode extends AbstractCodeNode implements IBreakableNode,IContinueableNode { 22 | 23 | public static final int LOOPTYPE_WHILE = 0; 24 | public static final int LOOPTYPE_DOWHILE = 1; 25 | public static final int LOOPTYPE_FOR = 2; 26 | 27 | /** 28 | * Contains pre expression assigns. 29 | */ 30 | private VariableAssignationNode[] preAssigns; 31 | /** 32 | * Contains expression which type is boolean. 33 | */ 34 | private ExpressionNode expression; 35 | /** 36 | * Contains scope which should be executed if 37 | * expression results in true. 38 | */ 39 | private ScopeNode scope; 40 | /** 41 | * Contains after assigns. 42 | */ 43 | private VariableAssignationNode[] afterAssigns; 44 | /** 45 | * Contains start of while node. 46 | */ 47 | private FlowBlock start; 48 | /** 49 | * Contains end of while node. 50 | */ 51 | private FlowBlock end; 52 | /** 53 | * Contains label name of this node. 54 | */ 55 | private String labelName; 56 | /** 57 | * Contains type of this loop node. 58 | */ 59 | private int type; 60 | 61 | public LoopNode(int type, ScopeNode scope, ExpressionNode expr,FlowBlock start,FlowBlock end) { 62 | this.type = type; 63 | this.expression = expr; 64 | this.scope = scope; 65 | this.start = start; 66 | this.end = end; 67 | this.write(expr); 68 | this.write(scope); 69 | expr.setParent(this); 70 | this.scope.setParent(this); 71 | } 72 | 73 | public ScopeNode getScope() { 74 | return scope; 75 | } 76 | 77 | 78 | public ExpressionNode getExpression() { 79 | return expression; 80 | } 81 | 82 | public VariableAssignationNode[] getPreAssigns() { 83 | return preAssigns; 84 | } 85 | 86 | public VariableAssignationNode[] getAfterAssigns() { 87 | return afterAssigns; 88 | } 89 | 90 | public void forTransform(VariableAssignationNode[] preAssigns, VariableAssignationNode[] afterAssigns) { 91 | this.type = LOOPTYPE_FOR; 92 | this.preAssigns = preAssigns; 93 | this.afterAssigns = afterAssigns; 94 | 95 | setCodeAddress(0); 96 | for (int i = 0; i < preAssigns.length; i++) { 97 | preAssigns[i].setParent(this); 98 | write(preAssigns[i]); 99 | } 100 | 101 | setCodeAddress(size() - 1); 102 | for (int i = 0; i < afterAssigns.length; i++) { 103 | afterAssigns[i].setParent(this); 104 | write(afterAssigns[i]); 105 | } 106 | } 107 | 108 | 109 | @Override 110 | public void print(CodePrinter printer) { 111 | printer.beginPrinting(this); 112 | if (this.labelName != null) 113 | printer.print(labelName + " "); 114 | if (this.type == LOOPTYPE_WHILE) { 115 | printer.print("while ("); 116 | expression.print(printer); 117 | printer.print(") "); 118 | scope.print(printer); 119 | } 120 | else if (this.type == LOOPTYPE_DOWHILE) { 121 | printer.print("do "); 122 | scope.print(printer); 123 | printer.print(" while ("); 124 | expression.print(printer); 125 | printer.print(");"); 126 | } 127 | else if (this.type == LOOPTYPE_FOR) { 128 | if (preAssigns != null) { 129 | printer.print("for ("); 130 | for (int i = 0; i < preAssigns.length; i++) { 131 | if (i > 0) 132 | printer.print(", "); 133 | preAssigns[i].print(printer); 134 | } 135 | printer.print("; "); 136 | expression.print(printer); 137 | printer.print(";"); 138 | for (int i = 0; i < afterAssigns.length; i++) { 139 | printer.print(i > 0 ? ", " : " "); 140 | afterAssigns[i].print(printer); 141 | } 142 | printer.print(") "); 143 | scope.print(printer); 144 | } 145 | else { 146 | printer.print("for (;"); 147 | expression.print(printer); 148 | printer.print(";) "); 149 | scope.print(printer); 150 | } 151 | } 152 | else { 153 | throw new RuntimeException("Unknown loop type:" + this.type); 154 | } 155 | printer.endPrinting(this); 156 | } 157 | 158 | @Override 159 | public boolean canContinue() { 160 | return start != null; 161 | } 162 | 163 | @Override 164 | public boolean canBreak() { 165 | return end != null; 166 | } 167 | 168 | @Override 169 | public FlowBlock getStart() { 170 | return start; 171 | } 172 | 173 | @Override 174 | public FlowBlock getEnd() { 175 | return end; 176 | } 177 | 178 | @Override 179 | public void enableLabelName() { 180 | labelName = "loop_" + this.hashCode() + ":"; 181 | } 182 | 183 | @Override 184 | public String getLabelName() { 185 | return labelName; 186 | } 187 | 188 | public void setType(int type) { 189 | this.type = type; 190 | } 191 | 192 | public int getType() { 193 | return type; 194 | } 195 | 196 | 197 | } 198 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/NewArrayNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CS2Type; 20 | import mgi.tools.jagdecs2.CodePrinter; 21 | 22 | public class NewArrayNode extends ExpressionNode { 23 | 24 | private ExpressionNode expression; 25 | private CS2Type type; 26 | 27 | public NewArrayNode(ExpressionNode expr,CS2Type type) { 28 | this.expression = expr; 29 | this.type = type; 30 | this.write(expr); 31 | expr.setParent(this); 32 | } 33 | 34 | @Override 35 | public int getPriority() { 36 | return ExpressionNode.PRIORITY_ARRAY_INDEX; 37 | } 38 | 39 | @Override 40 | public CS2Type getType() { 41 | return this.type; 42 | } 43 | 44 | @Override 45 | public ExpressionNode copy() { 46 | return new NewArrayNode(this.expression.copy(),this.type); 47 | } 48 | 49 | public ExpressionNode getExpression() { 50 | return expression; 51 | } 52 | 53 | @Override 54 | public void print(CodePrinter printer) { 55 | printer.beginPrinting(this); 56 | printer.print("new " + type.name()); 57 | printer.print('['); 58 | expression.print(printer); 59 | printer.print(']'); 60 | printer.endPrinting(this); 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/NotExpressionNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CS2Type; 20 | import mgi.tools.jagdecs2.CodePrinter; 21 | 22 | public class NotExpressionNode extends ExpressionNode { 23 | 24 | /** 25 | * Contains left expression node. 26 | */ 27 | private ExpressionNode expression; 28 | 29 | public NotExpressionNode(ExpressionNode expression) { 30 | this.expression = expression; 31 | 32 | this.write(expression); 33 | this.expression.setParent(this); 34 | } 35 | 36 | @Override 37 | public int getPriority() { 38 | return ExpressionNode.PRIORITY_UNARYLOGICALNOT; 39 | } 40 | 41 | @Override 42 | public CS2Type getType() { 43 | return CS2Type.BOOLEAN; 44 | } 45 | 46 | @Override 47 | public void print(CodePrinter printer) { 48 | printer.beginPrinting(this); 49 | boolean needsParen = expression.getPriority() > this.getPriority(); 50 | printer.print('!'); 51 | if (needsParen) 52 | printer.print("("); 53 | this.expression.print(printer); 54 | if (needsParen) 55 | printer.print(")"); 56 | printer.endPrinting(this); 57 | } 58 | 59 | 60 | @Override 61 | public ExpressionNode copy() { 62 | return new NotExpressionNode(expression.copy()); 63 | } 64 | 65 | public ExpressionNode getExpression() { 66 | return expression; 67 | } 68 | 69 | 70 | 71 | 72 | } 73 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/PopableNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CodePrinter; 20 | 21 | public class PopableNode extends AbstractCodeNode { 22 | 23 | private ExpressionNode expression; 24 | 25 | public PopableNode(ExpressionNode expression) { 26 | this.expression = expression; 27 | this.write(expression); 28 | expression.setParent(this); 29 | } 30 | 31 | public ExpressionNode getExpression() { 32 | return expression; 33 | } 34 | 35 | @Override 36 | public void print(CodePrinter printer) { 37 | printer.beginPrinting(this); 38 | expression.print(printer); 39 | printer.print(';'); 40 | printer.endPrinting(this); 41 | } 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/ReturnNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CodePrinter; 20 | 21 | public class ReturnNode extends AbstractCodeNode { 22 | 23 | /** 24 | * Contains return expression. 25 | */ 26 | private ExpressionNode expression; 27 | 28 | public ReturnNode() { 29 | this(null); 30 | } 31 | 32 | public ReturnNode(ExpressionNode expr) { 33 | this.expression = expr; 34 | if (expr != null) { 35 | this.write(expr); 36 | expr.setParent(this); 37 | } 38 | } 39 | 40 | public ExpressionNode getExpression() { 41 | return expression; 42 | } 43 | 44 | @Override 45 | public void print(CodePrinter printer) { 46 | printer.beginPrinting(this); 47 | if (this.expression != null) { 48 | printer.print("return "); 49 | expression.print(printer); 50 | printer.print(';'); 51 | } 52 | else { 53 | printer.print("return;"); 54 | } 55 | printer.endPrinting(this); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/StoreNamedDataNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CS2Type; 20 | import mgi.tools.jagdecs2.CodePrinter; 21 | 22 | public class StoreNamedDataNode extends ExpressionNode { 23 | 24 | private String name; 25 | private ExpressionNode expression; 26 | 27 | public StoreNamedDataNode(String name,ExpressionNode expr) { 28 | this.name = name; 29 | this.expression = expr; 30 | this.write(expr); 31 | expr.setParent(this); 32 | } 33 | 34 | @Override 35 | public int getPriority() { 36 | return ExpressionNode.PRIORITY_ASSIGNMENT; 37 | } 38 | 39 | @Override 40 | public CS2Type getType() { 41 | return this.expression.getType(); 42 | } 43 | 44 | @Override 45 | public ExpressionNode copy() { 46 | return new StoreNamedDataNode(this.name, this.expression.copy()); 47 | } 48 | 49 | public String getName() { 50 | return name; 51 | } 52 | 53 | public ExpressionNode getExpression() { 54 | return expression; 55 | } 56 | 57 | @Override 58 | public void print(CodePrinter printer) { 59 | printer.beginPrinting(this); 60 | boolean needsParen = expression.getPriority() > getPriority(); 61 | printer.print(name + " = "); 62 | if (needsParen) 63 | printer.print('('); 64 | expression.print(printer); 65 | if (needsParen) 66 | printer.print(')'); 67 | printer.endPrinting(this); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/StringExpressionNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CS2Type; 20 | import mgi.tools.jagdecs2.CodePrinter; 21 | 22 | public class StringExpressionNode extends ExpressionNode implements IConstantNode { 23 | 24 | /** 25 | * Contains string which this expression holds. 26 | */ 27 | private String data; 28 | 29 | public StringExpressionNode(String data) { 30 | this.data = data; 31 | } 32 | 33 | 34 | public String getData() { 35 | return data; 36 | } 37 | 38 | @Override 39 | public CS2Type getType() { 40 | return CS2Type.STRING; 41 | } 42 | 43 | @Override 44 | public Object getConst() { 45 | return this.data; 46 | } 47 | 48 | @Override 49 | public ExpressionNode copy() { 50 | return new StringExpressionNode(this.data); 51 | } 52 | 53 | @Override 54 | public void print(CodePrinter printer) { 55 | printer.beginPrinting(this); 56 | getType().printConstant(printer, data); 57 | printer.endPrinting(this); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/StructConstructExpr.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CS2Type; 20 | import mgi.tools.jagdecs2.CodePrinter; 21 | 22 | public class StructConstructExpr extends ExpressionNode { 23 | 24 | 25 | private CS2Type type; 26 | private ExpressionNode[] arguments; 27 | 28 | 29 | public StructConstructExpr(CS2Type type,ExpressionNode[] arguments) { 30 | this.type = type; 31 | this.arguments = arguments; 32 | for (int i = 0; i < arguments.length; i++) { 33 | this.write(arguments[i]); 34 | arguments[i].setParent(this); 35 | } 36 | } 37 | 38 | @Override 39 | public int getPriority() { 40 | return ExpressionNode.PRIORITY_CALL; 41 | } 42 | 43 | @Override 44 | public CS2Type getType() { 45 | return type; 46 | } 47 | 48 | 49 | @Override 50 | public ExpressionNode copy() { 51 | ExpressionNode[] argsCopy = new ExpressionNode[arguments.length]; 52 | for (int i = 0; i < arguments.length; i++) 53 | argsCopy[i] = arguments[i].copy(); 54 | return new StructConstructExpr(this.type,argsCopy); 55 | } 56 | 57 | @Override 58 | public void print(CodePrinter printer) { 59 | printer.beginPrinting(this); 60 | printer.print(type.name() + ""); 61 | printer.print('('); 62 | for (int i = 0; i < arguments.length; i++) { 63 | arguments[i].print(printer); 64 | if ((i + 1) < arguments.length) 65 | printer.print(", "); 66 | } 67 | printer.print(')'); 68 | printer.endPrinting(this); 69 | 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/StructPartLoadNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CS2Type; 20 | import mgi.tools.jagdecs2.CodePrinter; 21 | 22 | public class StructPartLoadNode extends ExpressionNode { 23 | 24 | private String name; 25 | private CS2Type type; 26 | private ExpressionNode expression; 27 | 28 | public StructPartLoadNode(String name,CS2Type type,ExpressionNode expr) { 29 | this.name = name; 30 | this.type = type; 31 | this.expression = expr; 32 | this.write(expr); 33 | expr.setParent(this); 34 | } 35 | 36 | @Override 37 | public CS2Type getType() { 38 | return this.type; 39 | } 40 | 41 | public String getName() { 42 | return name; 43 | } 44 | 45 | public ExpressionNode getExpression() { 46 | return expression; 47 | } 48 | 49 | @Override 50 | public ExpressionNode copy() { 51 | return new StructPartLoadNode(this.name,this.type,this.expression.copy()); 52 | } 53 | 54 | @Override 55 | public void print(CodePrinter printer) { 56 | printer.beginPrinting(this); 57 | boolean needsParen = expression.getPriority() > ExpressionNode.PRIORITY_MEMBER_ACCESS; 58 | if (needsParen) 59 | printer.print('('); 60 | expression.print(printer); 61 | if (needsParen) 62 | printer.print(')'); 63 | printer.print("." + name); 64 | printer.endPrinting(this); 65 | } 66 | 67 | 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/SwitchFlowBlockJump.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CodePrinter; 20 | 21 | public class SwitchFlowBlockJump extends AbstractCodeNode { 22 | 23 | 24 | private ExpressionNode expression; 25 | private int[] cases; 26 | private FlowBlock[] targets; 27 | private int defaultIndex; 28 | 29 | public SwitchFlowBlockJump(ExpressionNode expr,int[] cases, FlowBlock[] targets, int defaultIndex) { 30 | this.expression = expr; 31 | this.cases = cases; 32 | this.targets = targets; 33 | this.defaultIndex = defaultIndex; 34 | this.write(expr); 35 | expr.setParent(this); 36 | } 37 | 38 | 39 | public ExpressionNode getExpression() { 40 | return expression; 41 | } 42 | 43 | public int[] getCases() { 44 | return cases; 45 | } 46 | 47 | public FlowBlock[] getTargets() { 48 | return targets; 49 | } 50 | 51 | public int getDefaultIndex() { 52 | return defaultIndex; 53 | } 54 | 55 | 56 | 57 | @Override 58 | public void print(CodePrinter printer) { 59 | printer.beginPrinting(this); 60 | printer.print("SWITCH ("); 61 | expression.print(printer); 62 | printer.print(") {"); 63 | printer.tab(); 64 | for (int i = 0; i < cases.length; i++) { 65 | if (i == defaultIndex) 66 | printer.print("\ndefault:\n\t GOTO flow_" + targets[i].getBlockID()); 67 | else 68 | printer.print("\ncase " + cases[i] + ":\n\t GOTO flow_" + targets[i].getBlockID()); 69 | } 70 | 71 | printer.untab(); 72 | printer.print("\n}"); 73 | printer.endPrinting(this); 74 | 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/SwitchNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CodePrinter; 20 | 21 | public class SwitchNode extends AbstractCodeNode implements IBreakableNode { 22 | 23 | /** 24 | * Contains expression which type is boolean. 25 | */ 26 | private ExpressionNode expression; 27 | /** 28 | * Contains scope which should be executed if 29 | * expression finds valid case. 30 | */ 31 | private ScopeNode scope; 32 | /** 33 | * Contains end block of this switch node. 34 | */ 35 | private FlowBlock end; 36 | /** 37 | * Contains label name. 38 | */ 39 | private String labelName; 40 | 41 | public SwitchNode(FlowBlock end, ScopeNode scope, ExpressionNode expr) { 42 | this.end = end; 43 | this.expression = expr; 44 | this.scope = scope; 45 | this.write(expr); 46 | this.write(scope); 47 | expr.setParent(this); 48 | scope.setParent(this); 49 | } 50 | 51 | 52 | @Override 53 | public void print(CodePrinter printer) { 54 | printer.beginPrinting(this); 55 | if (this.labelName != null) 56 | printer.print(this.labelName + " "); 57 | printer.print("switch ("); 58 | expression.print(printer); 59 | printer.print(") "); 60 | scope.print(printer); 61 | printer.endPrinting(this); 62 | } 63 | 64 | public ScopeNode getScope() { 65 | return scope; 66 | } 67 | 68 | public ExpressionNode getExpression() { 69 | return expression; 70 | } 71 | 72 | @Override 73 | public boolean canBreak() { 74 | return this.end != null; 75 | } 76 | 77 | @Override 78 | public FlowBlock getEnd() { 79 | return this.end; 80 | } 81 | 82 | @Override 83 | public void enableLabelName() { 84 | this.labelName = "switch_" + this.hashCode() + ":"; 85 | } 86 | 87 | @Override 88 | public String getLabelName() { 89 | return this.labelName; 90 | } 91 | 92 | 93 | } 94 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/UnconditionalFlowBlockJump.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CodePrinter; 20 | 21 | public class UnconditionalFlowBlockJump extends AbstractCodeNode { 22 | 23 | /** 24 | * Contains target flow block. 25 | */ 26 | private FlowBlock target; 27 | 28 | public UnconditionalFlowBlockJump(FlowBlock target) { 29 | this.target = target; 30 | } 31 | 32 | 33 | public FlowBlock getTarget() { 34 | return target; 35 | } 36 | 37 | @Override 38 | public void print(CodePrinter printer) { 39 | printer.beginPrinting(this); 40 | printer.print("GOTO\t" + "flow_" + target.getBlockID()); 41 | printer.endPrinting(this); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/VariableAssignationNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CS2Type; 20 | import mgi.tools.jagdecs2.CodePrinter; 21 | 22 | public class VariableAssignationNode extends ExpressionNode { 23 | 24 | 25 | private LocalVariable variable; 26 | private ExpressionNode expression; 27 | private boolean isDeclaration = false; 28 | 29 | public VariableAssignationNode(LocalVariable variable,ExpressionNode expr) { 30 | this.variable = variable; 31 | this.expression = expr; 32 | this.write(expr); 33 | expr.setParent(this); 34 | } 35 | 36 | @Override 37 | public int getPriority() { 38 | return ExpressionNode.PRIORITY_ASSIGNMENT; 39 | } 40 | 41 | @Override 42 | public CS2Type getType() { 43 | return this.expression.getType(); 44 | } 45 | 46 | @Override 47 | public ExpressionNode copy() { 48 | return new VariableAssignationNode(this.variable,this.expression.copy()); 49 | } 50 | 51 | public LocalVariable getVariable() { 52 | return variable; 53 | } 54 | 55 | public void setVariable(LocalVariable v) { 56 | this.variable = v; 57 | } 58 | 59 | public ExpressionNode getExpression() { 60 | return expression; 61 | } 62 | 63 | public boolean isDeclaration() { 64 | return isDeclaration; 65 | } 66 | 67 | public void setIsDeclaration(boolean is) { 68 | isDeclaration = is; 69 | } 70 | 71 | @Override 72 | public void print(CodePrinter printer) { 73 | printer.beginPrinting(this); 74 | boolean needsParen = expression.getPriority() > getPriority(); 75 | if (isDeclaration) 76 | printer.print(variable.getType() + " "); 77 | printer.print(variable.getName() + " = "); 78 | if (needsParen) 79 | printer.print('('); 80 | expression.print(printer); 81 | if (needsParen) 82 | printer.print(')'); 83 | printer.endPrinting(this); 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/ast/VariableLoadNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.ast; 18 | 19 | import mgi.tools.jagdecs2.CS2Type; 20 | import mgi.tools.jagdecs2.CodePrinter; 21 | 22 | public class VariableLoadNode extends ExpressionNode { 23 | 24 | 25 | private LocalVariable variable; 26 | 27 | public VariableLoadNode(LocalVariable variable) { 28 | this.variable = variable; 29 | } 30 | 31 | @Override 32 | public CS2Type getType() { 33 | return this.variable.getType(); 34 | } 35 | 36 | @Override 37 | public ExpressionNode copy() { 38 | return new VariableLoadNode(this.variable); 39 | } 40 | 41 | public LocalVariable getVariable() { 42 | return variable; 43 | } 44 | 45 | public void setVariable(LocalVariable v) { 46 | this.variable = v; 47 | } 48 | 49 | @Override 50 | public void print(CodePrinter printer) { 51 | printer.beginPrinting(this); 52 | printer.print(variable.getName()); 53 | printer.endPrinting(this); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/gui/EditorPrinter.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.gui; 18 | 19 | import mgi.tools.jagdecs2.CodePrinter; 20 | import mgi.tools.jagdecs2.ast.AbstractCodeNode; 21 | import mgi.tools.jagdecs2.util.ArrayQueue; 22 | 23 | public class EditorPrinter extends CodePrinter { 24 | 25 | private EditorPanel editor; 26 | private ArrayQueue current; 27 | private AbstractCodeNode[] buffer; 28 | private int length; 29 | 30 | public EditorPrinter(EditorPanel editor) { 31 | this.editor = editor; 32 | this.current = new ArrayQueue(); 33 | this.buffer = new AbstractCodeNode[0]; 34 | } 35 | 36 | @Override 37 | public void beginPrinting(AbstractCodeNode node) { 38 | current.insert(node); 39 | } 40 | 41 | @Override 42 | public void endPrinting(AbstractCodeNode node) { 43 | if (current.take() != node) 44 | throw new RuntimeException("beginPrinting() was not called"); 45 | } 46 | 47 | @Override 48 | public void print(char c) { 49 | out(c); 50 | if (c == '\n') { 51 | String tabs = getTabs(); 52 | for (int i = 0; i < tabs.length(); i++) 53 | out(tabs.charAt(i)); 54 | } 55 | } 56 | 57 | 58 | private void out(char c) { 59 | if (buffer.length <= (length + 1)) 60 | expand(); 61 | buffer[length++] = current.last(); 62 | writer.append(c); 63 | } 64 | 65 | private void expand() { 66 | AbstractCodeNode[] buffer = new AbstractCodeNode[(this.buffer.length + 1) * 2]; 67 | System.arraycopy(this.buffer, 0, buffer, 0, this.buffer.length); 68 | this.buffer = buffer; 69 | } 70 | 71 | 72 | 73 | public EditorPanel getEditor() { 74 | return editor; 75 | } 76 | 77 | public AbstractCodeNode[] getClip() { 78 | if (current.size() > 0) 79 | throw new RuntimeException("Operation not finished!"); 80 | AbstractCodeNode[] clip = new AbstractCodeNode[length]; 81 | System.arraycopy(buffer, 0, clip, 0, length); 82 | return clip; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/gui/Main.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.gui; 18 | 19 | import java.io.File; 20 | import java.io.FileInputStream; 21 | import java.io.FileOutputStream; 22 | 23 | import javax.swing.JOptionPane; 24 | import javax.swing.JPanel; 25 | 26 | import mgi.tools.jagdecs2.CS2; 27 | import mgi.tools.jagdecs2.CS2Decoder; 28 | import mgi.tools.jagdecs2.CS2Decompiler; 29 | import mgi.tools.jagdecs2.ICS2Provider; 30 | import mgi.tools.jagdecs2.ast.FunctionNode; 31 | import mgi.tools.jagdecs2.util.ByteBuffer; 32 | import mgi.tools.jagdecs2.util.ConfigsDatabase; 33 | import mgi.tools.jagdecs2.util.FunctionDatabase; 34 | import mgi.tools.jagdecs2.util.IOUtils; 35 | import mgi.tools.jagdecs2.util.InstructionsDatabase; 36 | import mgi.tools.jagdecs2.util.Options; 37 | import mgi.tools.jagdecs2.util.UnsafeSerializer; 38 | 39 | public class Main { 40 | 41 | private UnsafeSerializer serializer; 42 | private Options options; 43 | private Window window; 44 | 45 | private InstructionsDatabase instructionsDB; 46 | private ConfigsDatabase configsDB; 47 | private FunctionDatabase opcodesDB; 48 | private FunctionDatabase scriptsDB; 49 | private CS2Decompiler decompiler; 50 | 51 | public static void main(String[] args) { 52 | new Main().init(); 53 | } 54 | 55 | public void init() { 56 | serializer = new UnsafeSerializer(); 57 | options = new Options(new File("gui/options.ini")); 58 | reloadDatabases(); 59 | window = new Window(this); 60 | 61 | window.setVisible(true); 62 | } 63 | 64 | public void loadScript(int scriptID) { 65 | if (window.getTabsPanel().getTabCount() > 10) { 66 | message("Error", "Too much tabs!"); 67 | return; 68 | } 69 | 70 | FunctionNode function = decompile(scriptID); 71 | if (function == null) { 72 | message("Error", "Error while loading script!"); 73 | return; 74 | } 75 | 76 | window.getTabsPanel().addTab("Editor - " + function.getName(), new EditorPanel(window, scriptID, function)); 77 | window.getTabsPanel().setSelectedIndex(window.getTabsPanel().getTabCount() - 1); 78 | } 79 | 80 | public void loadScript() { 81 | if (window.getTabsPanel().getTabCount() > 10) { 82 | message("Error", "Too much tabs!"); 83 | return; 84 | } 85 | 86 | File file = IOUtils.selectFile(window, true); 87 | if (file == null || !file.exists()) 88 | return; 89 | 90 | try { 91 | ByteBuffer buffer = new ByteBuffer((int)file.length()); 92 | FileInputStream fis = new FileInputStream(file); 93 | fis.read(buffer.getBuffer()); 94 | fis.close(); 95 | 96 | if (buffer.readInt() != 0xBABECAFE) 97 | throw new RuntimeException("Wrong magic."); 98 | int scriptID = buffer.readInt(); 99 | byte[] data = new byte[buffer.getBuffer().length - 8]; 100 | buffer.readBytes(data, 0, data.length); 101 | 102 | Object o = serializer.readObject(new ByteBuffer(data)); 103 | if (!(o instanceof FunctionNode)) 104 | throw new RuntimeException("Not function node object."); 105 | FunctionNode function = (FunctionNode)o; 106 | 107 | EditorPanel panel = new EditorPanel(window, scriptID, function); 108 | panel.setSaveFile(file); 109 | 110 | window.getTabsPanel().addTab("Editor - " + function.getName(), panel); 111 | window.getTabsPanel().setSelectedIndex(window.getTabsPanel().getTabCount() - 1); 112 | } 113 | catch (Throwable t) { 114 | t.printStackTrace(); 115 | message("Error", "Error while loading script!"); 116 | } 117 | } 118 | 119 | public void saveScript(boolean forceChoose) { 120 | if (getCurrentEditor() == null) { 121 | message("Error", "Please select editor tab first!"); 122 | return; 123 | } 124 | 125 | EditorPanel editor = getCurrentEditor(); 126 | if (editor.getSaveFile() == null || forceChoose) { 127 | File file = IOUtils.selectFile(window, false); 128 | if (file == null) 129 | return; 130 | editor.setSaveFile(file); 131 | } 132 | 133 | try { 134 | byte[] buffer = serializer.writeObject(editor.getFunction()).getBuffer(); 135 | ByteBuffer data = new ByteBuffer(buffer.length + 8); 136 | data.writeInt(0xBABECAFE); 137 | data.writeInt(editor.getScriptID()); 138 | data.writeBytes(buffer, 0, buffer.length); 139 | if (editor.getSaveFile().exists()) 140 | editor.getSaveFile().delete(); 141 | FileOutputStream fos = new FileOutputStream(editor.getSaveFile()); 142 | fos.write(data.getBuffer()); 143 | fos.close(); 144 | } 145 | catch (Throwable t) { 146 | t.printStackTrace(); 147 | message("Error", "Error while saving script!"); 148 | } 149 | } 150 | 151 | public void saveOptions() { 152 | options.save(); 153 | reloadDatabases(); 154 | } 155 | 156 | public void reloadDatabases() { 157 | instructionsDB = new InstructionsDatabase(new File(options.getOption("instructions_db_path"))); 158 | configsDB = new ConfigsDatabase(new File("configs_db.ini"), new File("bitconfigs_db.ini")); // TODO 159 | opcodesDB = new FunctionDatabase(new File(options.getOption("opcodes_db_path"))); 160 | scriptsDB = new FunctionDatabase(new File(options.getOption("scripts_db_path"))); 161 | 162 | decompiler = new CS2Decompiler(instructionsDB, configsDB, opcodesDB, scriptsDB, new ICS2Provider() { 163 | @Override 164 | public CS2 getCS2(InstructionsDatabase idb, ConfigsDatabase cdb, FunctionDatabase sdb, FunctionDatabase odb, int id) { 165 | try { 166 | return CS2Decoder.readScript(idb, cdb, new File(options.getOption("scripts_path") + id)); 167 | } 168 | catch (Throwable t) { 169 | return null; 170 | } 171 | } 172 | }); 173 | 174 | } 175 | 176 | public void openOptions() { 177 | for (int i = 0; i < window.getTabsPanel().getTabCount(); i++) { 178 | if (window.getTabsPanel().getComponentAt(i) instanceof OptionsPanel) { 179 | window.getTabsPanel().setSelectedIndex(i); 180 | return; 181 | } 182 | } 183 | if (window.getTabsPanel().getTabCount() > 10) { 184 | message("Error", "Too much tabs!"); 185 | return; 186 | } 187 | window.getTabsPanel().addTab("Options", new OptionsPanel(window)); 188 | window.getTabsPanel().setSelectedIndex(window.getTabsPanel().getTabCount() - 1); 189 | } 190 | 191 | public FunctionNode decompile(int scriptID) { 192 | try { 193 | return decompiler.decompile(scriptID); 194 | } 195 | catch (Throwable t) { 196 | return null; 197 | } 198 | } 199 | 200 | public EditorPanel getCurrentEditor() { 201 | if (window.getTabsPanel().getSelectedIndex() == -1 || !(window.getTabsPanel().getComponentAt(window.getTabsPanel().getSelectedIndex()) instanceof EditorPanel)) 202 | return null; 203 | return (EditorPanel) window.getTabsPanel().getComponentAt(window.getTabsPanel().getSelectedIndex()); 204 | } 205 | 206 | public void closeCurrentTab() { 207 | if (window.getTabsPanel().getSelectedIndex() == -1) 208 | return; 209 | window.getTabsPanel().removeTabAt(window.getTabsPanel().getSelectedIndex()); 210 | } 211 | 212 | public String inputBox(String title, String message) { 213 | return JOptionPane.showInputDialog(window, message, title, JOptionPane.INFORMATION_MESSAGE); 214 | } 215 | 216 | public String inputBox(String title, String message, String defaultValue) { 217 | return (String) JOptionPane.showInputDialog(window, message, title, JOptionPane.INFORMATION_MESSAGE, null, null, defaultValue); 218 | } 219 | 220 | public void message(String title, String message) { 221 | JOptionPane.showMessageDialog(window, message, title, JOptionPane.INFORMATION_MESSAGE); 222 | } 223 | 224 | public void exit() { 225 | System.exit(0); 226 | } 227 | 228 | 229 | 230 | public Options getOptions() { 231 | return options; 232 | } 233 | 234 | public Window getWindow() { 235 | return window; 236 | } 237 | 238 | public InstructionsDatabase getInstructionsDB() { 239 | return instructionsDB; 240 | } 241 | 242 | public FunctionDatabase getOpcodesDB() { 243 | return opcodesDB; 244 | } 245 | 246 | public FunctionDatabase getScriptsDB() { 247 | return scriptsDB; 248 | } 249 | 250 | } 251 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/gui/OptionsPanel.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.gui; 18 | 19 | import javax.swing.JPanel; 20 | import javax.swing.JTextField; 21 | import javax.swing.JLabel; 22 | import javax.swing.JButton; 23 | 24 | import mgi.tools.jagdecs2.util.Options; 25 | 26 | import com.jgoodies.forms.layout.FormLayout; 27 | import com.jgoodies.forms.layout.ColumnSpec; 28 | import com.jgoodies.forms.layout.RowSpec; 29 | import com.jgoodies.forms.factories.FormFactory; 30 | import java.awt.Font; 31 | import java.awt.event.ActionListener; 32 | import java.awt.event.ActionEvent; 33 | 34 | public class OptionsPanel extends JPanel { 35 | 36 | private Window window; 37 | private JTextField instrPath; 38 | private JTextField opcPath; 39 | private JTextField scrPath; 40 | private JTextField scrsPath; 41 | 42 | 43 | public OptionsPanel(Window window_) { 44 | window = window_; 45 | 46 | setLayout(null); 47 | 48 | JLabel lblInstr = new JLabel("Instructions database path:"); 49 | lblInstr.setFont(new Font("Tahoma", Font.BOLD, 13)); 50 | lblInstr.setBounds(43, 11, 184, 14); 51 | add(lblInstr); 52 | 53 | instrPath = new JTextField(); 54 | instrPath.setText("Loading please wait..."); 55 | instrPath.setBounds(43, 31, 184, 20); 56 | add(instrPath); 57 | instrPath.setColumns(10); 58 | 59 | JLabel lblOpc = new JLabel("Opcodes database path:"); 60 | lblOpc.setFont(new Font("Tahoma", Font.BOLD, 13)); 61 | lblOpc.setBounds(43, 69, 184, 14); 62 | add(lblOpc); 63 | 64 | opcPath = new JTextField(); 65 | opcPath.setText("Loading please wait..."); 66 | opcPath.setColumns(10); 67 | opcPath.setBounds(43, 89, 184, 20); 68 | add(opcPath); 69 | 70 | JLabel lblScr = new JLabel("Scripts database path:"); 71 | lblScr.setFont(new Font("Tahoma", Font.BOLD, 13)); 72 | lblScr.setBounds(43, 135, 184, 14); 73 | add(lblScr); 74 | 75 | scrPath = new JTextField(); 76 | scrPath.setText("Loading please wait..."); 77 | scrPath.setColumns(10); 78 | scrPath.setBounds(43, 155, 184, 20); 79 | add(scrPath); 80 | 81 | JLabel lblScrs = new JLabel("Scripts path:"); 82 | lblScrs.setFont(new Font("Tahoma", Font.BOLD, 13)); 83 | lblScrs.setBounds(256, 11, 184, 14); 84 | add(lblScrs); 85 | 86 | scrsPath = new JTextField(); 87 | scrsPath.setText("Loading please wait..."); 88 | scrsPath.setColumns(10); 89 | scrsPath.setBounds(256, 31, 184, 20); 90 | add(scrsPath); 91 | 92 | JButton btnApply = new JButton("Apply"); 93 | btnApply.addActionListener(new ActionListener() { 94 | public void actionPerformed(ActionEvent e) { 95 | Options options = window.getMain().getOptions(); 96 | options.setOption("instructions_db_path", instrPath.getText()); 97 | options.setOption("opcodes_db_path", opcPath.getText()); 98 | options.setOption("scripts_db_path", scrPath.getText()); 99 | options.setOption("scripts_path", scrsPath.getText()); 100 | window.getMain().saveOptions(); 101 | window.getMain().closeCurrentTab(); 102 | } 103 | }); 104 | btnApply.setBounds(138, 252, 89, 23); 105 | add(btnApply); 106 | 107 | JButton btnClose = new JButton("Cancel"); 108 | btnClose.addActionListener(new ActionListener() { 109 | public void actionPerformed(ActionEvent e) { 110 | window.getMain().closeCurrentTab(); 111 | } 112 | }); 113 | btnClose.setBounds(237, 252, 89, 23); 114 | add(btnClose); 115 | 116 | 117 | loadOptions(); 118 | } 119 | 120 | private void loadOptions() { 121 | instrPath.setText(fetchOption("instructions_db_path")); 122 | opcPath.setText(fetchOption("opcodes_db_path")); 123 | scrPath.setText(fetchOption("scripts_db_path")); 124 | scrsPath.setText(fetchOption("scripts_path")); 125 | } 126 | 127 | private String fetchOption(String key) { 128 | String option = window.getMain().getOptions().getOption(key); 129 | return option != null ? option : "Not set"; 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/gui/Window.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.gui; 18 | 19 | import java.awt.BorderLayout; 20 | import java.awt.EventQueue; 21 | import java.awt.Graphics; 22 | 23 | import javax.imageio.ImageIO; 24 | import javax.swing.JFrame; 25 | import javax.swing.JPanel; 26 | import javax.swing.border.EmptyBorder; 27 | import javax.swing.JMenuBar; 28 | import javax.swing.JMenuItem; 29 | import javax.swing.JOptionPane; 30 | import javax.swing.JPopupMenu; 31 | import java.awt.Component; 32 | import java.awt.event.MouseAdapter; 33 | import java.awt.event.MouseEvent; 34 | import javax.swing.JMenu; 35 | import java.awt.event.ActionListener; 36 | import java.awt.event.ActionEvent; 37 | import javax.swing.JButton; 38 | import javax.swing.KeyStroke; 39 | import java.awt.event.KeyEvent; 40 | import java.awt.event.InputEvent; 41 | import java.awt.image.BufferedImage; 42 | import java.awt.TextField; 43 | import java.io.File; 44 | 45 | import javax.swing.JTabbedPane; 46 | import javax.swing.JTextField; 47 | import javax.swing.event.ChangeListener; 48 | import javax.swing.event.ChangeEvent; 49 | 50 | public class Window extends JFrame { 51 | 52 | private Main main; 53 | private JPanel mainPanel; 54 | private JTabbedPane tabsPanel; 55 | private JTextField textField; 56 | 57 | 58 | private BufferedImage background; 59 | 60 | /** 61 | * Create the frame. 62 | */ 63 | public Window(Main _main) { 64 | this.main = _main; 65 | 66 | try { 67 | background = ImageIO.read(new File("gui/bg.png")); 68 | } 69 | catch (Throwable t) { 70 | background = null; 71 | } 72 | 73 | setTitle("CS2 Decompiler"); 74 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 75 | setBounds(100, 100, 526, 554); 76 | 77 | JMenuBar optionsMenuBar = new JMenuBar(); 78 | setJMenuBar(optionsMenuBar); 79 | 80 | JMenu fileMenu = new JMenu("File"); 81 | optionsMenuBar.add(fileMenu); 82 | 83 | JMenuItem btnExit = new JMenuItem("Exit"); 84 | btnExit.addActionListener(new ActionListener() { 85 | public void actionPerformed(ActionEvent e) { 86 | main.exit(); 87 | } 88 | }); 89 | 90 | JMenuItem btnLoadScript = new JMenuItem("Load Script"); 91 | btnLoadScript.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK)); 92 | btnLoadScript.addActionListener(new ActionListener() { 93 | public void actionPerformed(ActionEvent e) { 94 | main.loadScript(); 95 | } 96 | }); 97 | fileMenu.add(btnLoadScript); 98 | 99 | JMenuItem btnSaveScript = new JMenuItem("Save Script"); 100 | btnSaveScript.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK)); 101 | btnSaveScript.addActionListener(new ActionListener() { 102 | public void actionPerformed(ActionEvent e) { 103 | main.saveScript(false); 104 | } 105 | }); 106 | fileMenu.add(btnSaveScript); 107 | 108 | JMenuItem btnSaveScriptAs = new JMenuItem("Save Script As"); 109 | btnSaveScriptAs.addActionListener(new ActionListener() { 110 | public void actionPerformed(ActionEvent e) { 111 | main.saveScript(true); 112 | } 113 | }); 114 | fileMenu.add(btnSaveScriptAs); 115 | fileMenu.add(btnExit); 116 | 117 | JMenu decompilerMenu = new JMenu("Decompiler"); 118 | optionsMenuBar.add(decompilerMenu); 119 | 120 | JMenuItem btnOptions = new JMenuItem("Options"); 121 | btnOptions.addActionListener(new ActionListener() { 122 | public void actionPerformed(ActionEvent e) { 123 | main.openOptions(); 124 | } 125 | }); 126 | decompilerMenu.add(btnOptions); 127 | 128 | JMenu helpMenu = new JMenu("Help"); 129 | optionsMenuBar.add(helpMenu); 130 | 131 | JMenuItem btnAbout = new JMenuItem("About"); 132 | btnAbout.addActionListener(new ActionListener() { 133 | @Override 134 | public void actionPerformed(ActionEvent e) { 135 | main.message("About", "Made by mgi125 (MangiS)"); 136 | } 137 | }); 138 | helpMenu.add(btnAbout); 139 | 140 | textField = new JTextField(); 141 | optionsMenuBar.add(textField); 142 | textField.setColumns(10); 143 | 144 | JButton btnDecompile = new JButton("Decompile"); 145 | btnDecompile.addActionListener(new ActionListener() { 146 | public void actionPerformed(ActionEvent e) { 147 | int scriptID = -1; 148 | try { 149 | scriptID = Integer.parseInt(textField.getText()); 150 | } 151 | catch (Throwable t) { 152 | main.message("Error", "Invalid script ID!"); 153 | return; 154 | } 155 | main.loadScript(scriptID); 156 | } 157 | }); 158 | optionsMenuBar.add(btnDecompile); 159 | 160 | JButton btnCloseTab = new JButton("Close tab"); 161 | btnCloseTab.addActionListener(new ActionListener() { 162 | public void actionPerformed(ActionEvent e) { 163 | main.closeCurrentTab(); 164 | } 165 | }); 166 | optionsMenuBar.add(btnCloseTab); 167 | mainPanel = new JPanel(); 168 | mainPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); 169 | mainPanel.setLayout(new BorderLayout(0, 0)); 170 | setContentPane(mainPanel); 171 | 172 | tabsPanel = new JTabbedPane(JTabbedPane.TOP) { 173 | @Override 174 | public void paintComponent(Graphics g) { 175 | if (this.getTabCount() > 0 || background == null) { 176 | super.paintComponent(g); 177 | return; 178 | } 179 | 180 | g.drawImage(background, 0, 0, getWidth(), getHeight(), null); 181 | } 182 | }; 183 | mainPanel.add(tabsPanel, BorderLayout.CENTER); 184 | 185 | } 186 | 187 | public JTabbedPane getTabsPanel() { 188 | return tabsPanel; 189 | } 190 | 191 | public Main getMain() { 192 | return main; 193 | } 194 | 195 | 196 | } 197 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/instructions/AbstractInstruction.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.instructions; 18 | 19 | import java.util.Arrays; 20 | 21 | import mgi.tools.jagdecs2.util.InstructionInfo; 22 | 23 | public class AbstractInstruction { 24 | 25 | private InstructionInfo info; 26 | private int address; 27 | 28 | public AbstractInstruction(InstructionInfo info) { 29 | this.info = info; 30 | this.address = -1; 31 | } 32 | 33 | public int getOpcode() { 34 | return info != null ? info.getOpcode() : -1; 35 | } 36 | 37 | public int getScrambledOpcode() { 38 | return info != null ? info.getScrampledOpcode() : -1; 39 | } 40 | 41 | public String getName() { 42 | return info != null ? info.getName() : "N/A"; 43 | } 44 | 45 | public void setAddress(int address) { 46 | this.address = address; 47 | } 48 | 49 | public int getAddress() { 50 | return address; 51 | } 52 | 53 | @Override 54 | public String toString() { 55 | char[] pad = new char[25]; 56 | Arrays.fill(pad, ' '); 57 | char[] name = (info != null ? info.getName() : "N/A").toCharArray(); 58 | System.arraycopy(name, 0, pad, 0, Math.min(pad.length, name.length)); 59 | if (name.length > pad.length) { 60 | pad[pad.length - 3] = '.'; 61 | pad[pad.length - 2] = '.'; 62 | pad[pad.length - 1] = '.'; 63 | } 64 | 65 | return new String(pad); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/instructions/BitConfigInstruction.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.instructions; 18 | 19 | import mgi.tools.jagdecs2.util.BitConfigInfo; 20 | import mgi.tools.jagdecs2.util.InstructionInfo; 21 | 22 | public class BitConfigInstruction extends AbstractInstruction { 23 | 24 | private BitConfigInfo config; 25 | private boolean constant; 26 | 27 | public BitConfigInstruction(InstructionInfo info, BitConfigInfo config, boolean constant) { 28 | super(info); 29 | this.config = config; 30 | this.constant = constant; 31 | } 32 | 33 | public BitConfigInfo getConfig() { 34 | return config; 35 | } 36 | 37 | public boolean getConstant() { 38 | return constant; 39 | } 40 | 41 | public String toString() { 42 | return super.toString() + "\t" + config + "\t" + constant; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/instructions/BooleanInstruction.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.instructions; 18 | 19 | import mgi.tools.jagdecs2.util.InstructionInfo; 20 | 21 | public class BooleanInstruction extends AbstractInstruction { 22 | 23 | private boolean constant; 24 | 25 | public BooleanInstruction(InstructionInfo info, boolean constant) { 26 | super(info); 27 | this.constant = constant; 28 | } 29 | 30 | public boolean getConstant() { 31 | return constant; 32 | } 33 | 34 | public String toString() { 35 | return super.toString() + "\t" + constant; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/instructions/ConfigInstruction.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.instructions; 18 | 19 | import mgi.tools.jagdecs2.util.ConfigInfo; 20 | import mgi.tools.jagdecs2.util.InstructionInfo; 21 | 22 | public class ConfigInstruction extends AbstractInstruction { 23 | 24 | private ConfigInfo config; 25 | private boolean constant; 26 | 27 | public ConfigInstruction(InstructionInfo info, ConfigInfo config, boolean constant) { 28 | super(info); 29 | this.config = config; 30 | this.constant = constant; 31 | } 32 | 33 | public ConfigInfo getConfig() { 34 | return config; 35 | } 36 | 37 | public boolean getConstant() { 38 | return constant; 39 | } 40 | 41 | public String toString() { 42 | return super.toString() + "\t" + config + "\t" + constant; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/instructions/IntInstruction.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.instructions; 18 | 19 | import mgi.tools.jagdecs2.util.InstructionInfo; 20 | 21 | public class IntInstruction extends AbstractInstruction { 22 | 23 | private int constant; 24 | 25 | public IntInstruction(InstructionInfo info, int constant) { 26 | super(info); 27 | this.constant = constant; 28 | } 29 | 30 | public int getConstant() { 31 | return constant; 32 | } 33 | 34 | public String toString() { 35 | return super.toString() + "\t" + constant; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/instructions/JumpInstruction.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.instructions; 18 | 19 | import mgi.tools.jagdecs2.util.InstructionInfo; 20 | 21 | public class JumpInstruction extends AbstractInstruction { 22 | 23 | private Label target; 24 | 25 | public JumpInstruction(InstructionInfo info, Label target) { 26 | super(info); 27 | this.target = target; 28 | } 29 | 30 | public Label getTarget() { 31 | return target; 32 | } 33 | 34 | public String toString() { 35 | return super.toString() + "\t" + target.toString(); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/instructions/Label.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.instructions; 18 | 19 | 20 | public class Label extends AbstractInstruction { 21 | private int labelID; 22 | 23 | public Label() { 24 | super(null); 25 | } 26 | 27 | public int hashCode() { 28 | return labelID; 29 | } 30 | 31 | public String toString() { 32 | return "label_" + labelID + ":"; 33 | } 34 | 35 | public void setLabelID(int labelID) { 36 | this.labelID = labelID; 37 | } 38 | 39 | public int getLabelID() { 40 | return labelID; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/instructions/LongInstruction.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.instructions; 18 | 19 | import mgi.tools.jagdecs2.util.InstructionInfo; 20 | 21 | public class LongInstruction extends AbstractInstruction { 22 | 23 | private long constant; 24 | 25 | public LongInstruction(InstructionInfo info, long constant) { 26 | super(info); 27 | this.constant = constant; 28 | } 29 | 30 | public long getConstant() { 31 | return constant; 32 | } 33 | 34 | public String toString() { 35 | return super.toString() + "\t" + constant; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/instructions/Opcodes.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.instructions; 18 | 19 | import java.lang.reflect.Field; 20 | 21 | public class Opcodes { 22 | 23 | public static final int PUSH_INT = 0; 24 | public static final int PUSH_STR = 1; 25 | public static final int PUSH_LONG = 2; 26 | 27 | public static final int LOAD_INT = 3; 28 | public static final int STORE_INT = 4; 29 | public static final int LOAD_STR = 5; 30 | public static final int STORE_STR = 6; 31 | public static final int LOAD_LONG = 7; 32 | public static final int STORE_LONG = 8; 33 | 34 | public static final int POP_INT = 9; 35 | public static final int POP_STR = 10; 36 | public static final int POP_LONG = 11; 37 | 38 | public static final int NEW_ARRAY = 12; 39 | public static final int ARRAY_LOAD = 13; 40 | public static final int ARRAY_STORE = 14; 41 | 42 | public static final int CALL_CS2 = 15; 43 | public static final int RETURN = 16; 44 | 45 | public static final int SWITCH = 17; 46 | public static final int GOTO = 18; 47 | public static final int INT_EQ = 19; 48 | public static final int INT_NE = 20; 49 | public static final int INT_LT = 21; 50 | public static final int INT_GT = 22; 51 | public static final int INT_LE = 23; 52 | public static final int INT_GE = 24; 53 | public static final int INT_T = 25; 54 | public static final int INT_F = 26; 55 | public static final int LONG_EQ = 27; 56 | public static final int LONG_NE = 28; 57 | public static final int LONG_LT = 29; 58 | public static final int LONG_GT = 30; 59 | public static final int LONG_LE = 31; 60 | public static final int LONG_GE = 32; 61 | 62 | public static final int LOAD_CONFIG = 33; 63 | public static final int STORE_CONFIG = 34; 64 | public static final int LOAD_BITCONFIG = 35; 65 | public static final int STORE_BITCONFIG = 36; 66 | public static final int CONCAT_STRINGS = 37; 67 | 68 | 69 | public static int getOpcode(String name) { 70 | try { 71 | Field[] flds = Opcodes.class.getFields(); 72 | for (Field f : flds) { 73 | if (!f.getName().equals(name)) { 74 | continue; 75 | } 76 | return f.getInt(null); 77 | } 78 | } catch (Exception e) { 79 | e.printStackTrace(); 80 | } 81 | return -1; 82 | } 83 | 84 | public static String getOpcodeName(int opcode) { 85 | try { 86 | Field[] flds = Opcodes.class.getFields(); 87 | for (Field f : flds) { 88 | if (f.getInt(null) != opcode) { 89 | continue; 90 | } 91 | return (f.getName()); 92 | } 93 | } catch (Exception e) { 94 | e.printStackTrace(); 95 | } 96 | return "n/a:" + opcode; 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/instructions/StringInstruction.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.instructions; 18 | 19 | import mgi.tools.jagdecs2.util.InstructionInfo; 20 | 21 | public class StringInstruction extends AbstractInstruction { 22 | 23 | private String constant; 24 | 25 | public StringInstruction(InstructionInfo info, String constant) { 26 | super(info); 27 | this.constant = constant; 28 | } 29 | 30 | public String getConstant() { 31 | return constant; 32 | } 33 | 34 | public String toString() { 35 | return super.toString() + "\t" + constant; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/instructions/SwitchInstruction.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.instructions; 18 | 19 | import mgi.tools.jagdecs2.util.InstructionInfo; 20 | 21 | public class SwitchInstruction extends AbstractInstruction { 22 | 23 | private int[] cases; 24 | private Label[] targets; 25 | private int defaultIndex; 26 | 27 | public SwitchInstruction(InstructionInfo info, int[] cases, Label[] targets) { 28 | super(info); 29 | this.cases = cases; 30 | this.targets = targets; 31 | this.defaultIndex = -1; 32 | } 33 | 34 | public int[] getCases() { 35 | return cases; 36 | } 37 | 38 | public Label[] getTargets() { 39 | return targets; 40 | } 41 | 42 | public int getDefaultIndex() { 43 | return defaultIndex; 44 | } 45 | 46 | 47 | public void attachDefault(Label default_) { 48 | Label[] nTargets = new Label[targets.length + 1]; 49 | int[] nCases = new int[cases.length + 1]; 50 | 51 | System.arraycopy(targets, 0, nTargets, 0, targets.length); 52 | System.arraycopy(cases, 0, nCases, 0, cases.length); 53 | 54 | targets = nTargets; 55 | cases = nCases; 56 | 57 | targets[targets.length - 1] = default_; 58 | cases[cases.length - 1] = Integer.MIN_VALUE; 59 | 60 | defaultIndex = targets.length - 1; 61 | } 62 | 63 | 64 | public void sort() { 65 | int[] sCases = new int[cases.length]; 66 | Label[] sTargets = new Label[targets.length]; 67 | boolean[] usage = new boolean[cases.length]; 68 | boolean defaultAssigned = false; 69 | for (int sWrite = 0; sWrite < sCases.length; sWrite++) { 70 | int lowestAddr = Integer.MAX_VALUE; 71 | int lowestIndex = -1; 72 | for (int i = 0; i < cases.length; i++) 73 | if (!usage[i] && targets[i].getAddress() < lowestAddr) 74 | lowestAddr = targets[lowestIndex = i].getAddress(); 75 | if (!defaultAssigned && defaultIndex == lowestIndex) { 76 | defaultAssigned = true; 77 | defaultIndex = sWrite; 78 | } 79 | usage[lowestIndex] = true; 80 | sCases[sWrite] = cases[lowestIndex]; 81 | sTargets[sWrite] = targets[lowestIndex]; 82 | } 83 | cases = sCases; 84 | targets = sTargets; 85 | } 86 | 87 | 88 | 89 | 90 | 91 | public String toString() { 92 | StringBuilder bld = new StringBuilder(); 93 | bld.append("switch { \n"); 94 | for (int i = 0; i < cases.length; i++) { 95 | bld.append(i == defaultIndex ? "\tdefault: \n" : "\tcase " + cases[i] + ": \n"); 96 | bld.append("\t\t" + Opcodes.getOpcodeName(Opcodes.GOTO) + "\t" + targets[i].toString() + " \n"); 97 | } 98 | bld.append("}"); 99 | return bld.toString(); 100 | } 101 | 102 | } 103 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/util/ArrayQueue.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.util; 18 | 19 | import java.util.Arrays; 20 | 21 | 22 | public class ArrayQueue { 23 | 24 | public static final int INITIAL_CAPACITY = 1000; 25 | 26 | private Object[] queue; 27 | private int top; 28 | 29 | 30 | public ArrayQueue() { 31 | this(INITIAL_CAPACITY); 32 | } 33 | 34 | public ArrayQueue(int initialCapacity) { 35 | this.queue = new Object[initialCapacity]; 36 | } 37 | 38 | 39 | public void insert(T obj) { 40 | if (top >= queue.length) { 41 | Object[] newQueue = new Object[queue.length * 2]; 42 | System.arraycopy(queue, 0, newQueue, 0, queue.length); 43 | queue = newQueue; 44 | } 45 | queue[top++] = obj; 46 | } 47 | 48 | @SuppressWarnings("unchecked") 49 | public T take() { 50 | if (top <= 0) 51 | throw new RuntimeException("Nothing to take."); 52 | return (T)queue[--top]; 53 | } 54 | 55 | 56 | @SuppressWarnings("unchecked") 57 | public T last() { 58 | if (top <= 0) 59 | throw new RuntimeException("No elements."); 60 | return (T)queue[top - 1]; 61 | } 62 | 63 | 64 | @SuppressWarnings("unchecked") 65 | public T first() { 66 | if (top <= 0) 67 | throw new RuntimeException("No elements."); 68 | return (T)queue[0]; 69 | } 70 | 71 | public int size() { 72 | return top; 73 | } 74 | 75 | public void clear() { 76 | top = 0; 77 | Arrays.fill(queue, null); 78 | } 79 | 80 | /** 81 | * Iterate's thru all elements and checks if 82 | * there's at least one element that 83 | * obj.equals(element); 84 | */ 85 | public boolean lookup(T obj) { 86 | for (int i = 0; i < top; i++) 87 | if (queue[i] != null && obj.equals(queue[i])) 88 | return true; 89 | return false; 90 | } 91 | 92 | public void dispose() { 93 | queue = null; 94 | } 95 | 96 | } 97 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/util/BitConfigInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.util; 18 | 19 | public class BitConfigInfo { 20 | 21 | private ConfigInfo base; 22 | private String name; 23 | 24 | public BitConfigInfo(ConfigInfo base, String name) { 25 | this.base = base; 26 | this.name = name; 27 | } 28 | 29 | public ConfigInfo getBase() { 30 | return base; 31 | } 32 | 33 | public String getName() { 34 | return name; 35 | } 36 | 37 | @Override 38 | public String toString() { 39 | return "[" + base + "," + name + "]"; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/util/ConfigInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.util; 18 | 19 | import mgi.tools.jagdecs2.CS2Type; 20 | 21 | public class ConfigInfo { 22 | 23 | private String domainName; 24 | private String configName; 25 | private CS2Type type; 26 | 27 | public ConfigInfo(String domainName, String configName, CS2Type type) { 28 | this.domainName = domainName; 29 | this.configName = configName; 30 | this.type = type; 31 | } 32 | 33 | public String getDomainName() { 34 | return domainName; 35 | } 36 | 37 | public String getConfigName() { 38 | return configName; 39 | } 40 | 41 | public CS2Type getType() { 42 | return type; 43 | } 44 | 45 | @Override 46 | public String toString() { 47 | return "[" + domainName + "," + configName + "," + type + "]"; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/util/ConfigsDatabase.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.util; 18 | 19 | import java.io.BufferedReader; 20 | import java.io.File; 21 | import java.io.FileReader; 22 | 23 | import mgi.tools.jagdecs2.CS2Type; 24 | 25 | public class ConfigsDatabase { 26 | 27 | private File configsFile; 28 | private File bitConfigsFile; 29 | private ConfigInfo[][] configsInfo; 30 | private BitConfigInfo[] bitConfigsInfo; 31 | 32 | public ConfigsDatabase(File configsFile, File bitConfigsFile) { 33 | this.configsFile = configsFile; 34 | this.bitConfigsFile = bitConfigsFile; 35 | this.configsInfo = new ConfigInfo[100][40000]; 36 | this.bitConfigsInfo = new BitConfigInfo[40000]; 37 | this.readConfigsDatabase(); 38 | this.readBitConfigsDatabase(); 39 | } 40 | 41 | public ConfigsDatabase() { 42 | this.configsInfo = new ConfigInfo[100][40000]; 43 | this.bitConfigsInfo = new BitConfigInfo[40000]; 44 | } 45 | 46 | 47 | 48 | 49 | private void readConfigsDatabase() { 50 | try { 51 | BufferedReader reader = new BufferedReader(new FileReader(configsFile)); 52 | int linesCount = 0; 53 | for (String line = reader.readLine(); line != null; line = reader.readLine(),linesCount++) { 54 | if (line.length() <= 0 || line.startsWith(" ") || line.startsWith("//") || line.startsWith("#")) 55 | continue; 56 | try { 57 | String[] split = line.split("\\ "); 58 | int domainType = Integer.parseInt(split[0]); 59 | int id = Integer.parseInt(split[1]); 60 | String domainName = split[2]; 61 | String configName = split[3]; 62 | String tstring = split[4]; 63 | CS2Type type; 64 | if (!tstring.startsWith(".")) 65 | type = CS2Type.forJagexChar(tstring.charAt(0)); 66 | else 67 | type = CS2Type.forJagexChar((char)Short.parseShort(tstring.substring(1))); 68 | configsInfo[domainType][id] = new ConfigInfo(domainName, configName, type); 69 | } 70 | catch (Exception ex) { 71 | reader.close(); 72 | throw new RuntimeException("Error parsing configs database file " + this.configsFile + " on line:" + (linesCount + 1), ex); 73 | } 74 | } 75 | reader.close(); 76 | } 77 | catch (Throwable t) { 78 | throw new RuntimeException(t); 79 | } 80 | } 81 | 82 | private void readBitConfigsDatabase() { 83 | try { 84 | BufferedReader reader = new BufferedReader(new FileReader(bitConfigsFile)); 85 | int linesCount = 0; 86 | for (String line = reader.readLine(); line != null; line = reader.readLine(),linesCount++) { 87 | if (line.length() <= 0 || line.startsWith(" ") || line.startsWith("//") || line.startsWith("#")) 88 | continue; 89 | try { 90 | String[] split = line.split("\\ "); 91 | int baseDomainId = Integer.parseInt(split[0]); 92 | int baseId = Integer.parseInt(split[1]); 93 | int id = Integer.parseInt(split[2]); 94 | String name = split[3]; 95 | 96 | ConfigInfo base = getConfigInfo(baseDomainId, baseId); 97 | if (base == null) 98 | throw new RuntimeException("Can't find base config " + baseDomainId + "," + baseId); 99 | 100 | bitConfigsInfo[id] = new BitConfigInfo(base, name); 101 | } 102 | catch (Exception ex) { 103 | reader.close(); 104 | throw new RuntimeException("Error parsing bitconfigs database file " + this.bitConfigsFile + " on line:" + (linesCount + 1), ex); 105 | } 106 | } 107 | reader.close(); 108 | } 109 | catch (Throwable t) { 110 | throw new RuntimeException(t); 111 | } 112 | } 113 | 114 | 115 | 116 | public ConfigInfo getConfigInfo(int domainType, int id) { 117 | if (domainType < 0 || id < 0 || domainType >= configsInfo.length || id >= configsInfo[domainType].length) 118 | return null; 119 | return configsInfo[domainType][id]; 120 | } 121 | 122 | public BitConfigInfo getBitConfigInfo(int id) { 123 | if (id < 0 || id >= bitConfigsInfo.length) 124 | return null; 125 | return bitConfigsInfo[id]; 126 | } 127 | 128 | 129 | public int configsSize() { 130 | int total = 0; 131 | for (int i = 0; i < configsInfo.length; i++) { 132 | if (configsInfo[i] != null) 133 | total++; 134 | } 135 | return total; 136 | } 137 | 138 | public int bitConfigsSize() { 139 | int total = 0; 140 | for (int i = 0; i < bitConfigsInfo.length; i++) { 141 | if (bitConfigsInfo[i] != null) 142 | total++; 143 | } 144 | return total; 145 | } 146 | 147 | } 148 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/util/DecompilerUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.util; 18 | 19 | import java.util.ArrayList; 20 | import java.util.List; 21 | 22 | import mgi.tools.jagdecs2.ast.CaseAnnotation; 23 | import mgi.tools.jagdecs2.ast.FlowBlock; 24 | import mgi.tools.jagdecs2.ast.SwitchFlowBlockJump; 25 | 26 | public class DecompilerUtils { 27 | 28 | public static class SwitchCase { 29 | private CaseAnnotation[] annotations; 30 | private FlowBlock block; 31 | 32 | public SwitchCase(CaseAnnotation[] annotations,FlowBlock block) { 33 | this.annotations = annotations; 34 | this.block = block; 35 | } 36 | 37 | public CaseAnnotation[] getAnnotations() { 38 | return annotations; 39 | } 40 | 41 | public FlowBlock getBlock() { 42 | return block; 43 | } 44 | 45 | public void setBlock(FlowBlock block) { 46 | this.block = block; 47 | } 48 | 49 | public String toString() { 50 | StringBuilder bld = new StringBuilder(); 51 | for (int i = 0; i < annotations.length; i++) { 52 | bld.append(annotations[i]); 53 | if ((i + 1) < annotations.length) 54 | bld.append(" AND "); 55 | } 56 | bld.append("\t GOTO flow_" + block.getBlockID()); 57 | return bld.toString(); 58 | } 59 | 60 | } 61 | 62 | public static SwitchCase[] makeSwitchCases(SwitchFlowBlockJump sbj) { 63 | SwitchCase[] buff = new SwitchCase[sbj.getCases().length]; 64 | FlowBlock lastBlock = null; 65 | List annotations = new ArrayList(); 66 | int count = 0; 67 | for (int i = 0; i < sbj.getCases().length; i++) { 68 | if (sbj.getTargets()[i] == lastBlock) 69 | annotations.add(sbj.getDefaultIndex() == i ? new CaseAnnotation() : new CaseAnnotation(sbj.getCases()[i])); 70 | else { 71 | if (lastBlock != null) { 72 | CaseAnnotation[] ann = new CaseAnnotation[annotations.size()]; 73 | int aWrite = 0; 74 | for (CaseAnnotation a : annotations) 75 | ann[aWrite++] = a; 76 | buff[count++] = new SwitchCase(ann,lastBlock); 77 | } 78 | lastBlock = sbj.getTargets()[i]; 79 | annotations.clear(); 80 | annotations.add(sbj.getDefaultIndex() == i ? new CaseAnnotation() : new CaseAnnotation(sbj.getCases()[i])); 81 | } 82 | } 83 | if (lastBlock != null) { 84 | CaseAnnotation[] ann = new CaseAnnotation[annotations.size()]; 85 | int aWrite = 0; 86 | for (CaseAnnotation a : annotations) 87 | ann[aWrite++] = a; 88 | buff[count++] = new SwitchCase(ann,lastBlock); 89 | } 90 | 91 | if (count == buff.length) 92 | return buff; 93 | 94 | SwitchCase[] full = new SwitchCase[count]; 95 | System.arraycopy(buff, 0, full, 0, count); 96 | return full; 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/util/FunctionDatabase.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.util; 18 | 19 | import java.io.BufferedReader; 20 | import java.io.BufferedWriter; 21 | import java.io.File; 22 | import java.io.FileReader; 23 | import java.io.FileWriter; 24 | 25 | import mgi.tools.jagdecs2.CS2Type; 26 | 27 | public class FunctionDatabase { 28 | 29 | private File file; 30 | private FunctionInfo[] info; 31 | 32 | public FunctionDatabase(File file) { 33 | this.file = file; 34 | this.info = new FunctionInfo[50000]; 35 | this.readDatabase(); 36 | } 37 | 38 | public FunctionDatabase() { 39 | this.info = new FunctionInfo[50000]; 40 | } 41 | 42 | 43 | public void updateFile(int functionChanged, String newDescriptor) { 44 | if (getInfo(functionChanged) == null) 45 | throw new RuntimeException("Cannot delete definitions..."); 46 | 47 | try { 48 | BufferedReader reader = new BufferedReader(new FileReader(file)); 49 | String[] buffer = new String[50000]; 50 | int linesCount = 0; 51 | for (String line = reader.readLine(); line != null; line = reader.readLine()) 52 | buffer[linesCount++] = line; 53 | reader.close(); 54 | 55 | for (int i = 0; i < linesCount; i++) { 56 | String line = buffer[i]; 57 | if (line.length() <= 0 || line.startsWith(" ") || line.startsWith("//") || line.startsWith("#")) 58 | continue; 59 | 60 | String[] split = line.split("\\ "); 61 | int opcode = Integer.parseInt(split[0]); 62 | if (opcode == functionChanged) { 63 | line = newDescriptor; 64 | } 65 | buffer[i] = line; 66 | 67 | { 68 | split = line.split("\\ "); 69 | opcode = Integer.parseInt(split[0]); 70 | String name = split[1]; 71 | CS2Type returnType = CS2Type.forDesc(split[2]); 72 | CS2Type[] argTypes = new CS2Type[(split.length - 2) / 2]; 73 | String[] argNames = new String[(split.length - 2) / 2]; 74 | int write = 0; 75 | for (int x = 3; x < split.length; x += 2) { 76 | argTypes[write] = CS2Type.forDesc(split[x]); 77 | argNames[write++] = split[x + 1]; 78 | } 79 | info[opcode] = new FunctionInfo(name,argTypes,returnType,argNames); 80 | } 81 | } 82 | 83 | BufferedWriter writer = new BufferedWriter(new FileWriter(file)); 84 | for (int i = 0; i < linesCount; i++) 85 | writer.write(buffer[i] + "\r\n"); 86 | writer.close(); 87 | } 88 | catch (Throwable t) { 89 | throw new RuntimeException(t); 90 | } 91 | } 92 | 93 | 94 | private void readDatabase() { 95 | try { 96 | BufferedReader reader = new BufferedReader(new FileReader(file)); 97 | int linesCount = 0; 98 | for (String line = reader.readLine(); line != null; line = reader.readLine(),linesCount++) { 99 | if (line.length() <= 0 || line.startsWith(" ") || line.startsWith("//") || line.startsWith("#")) 100 | continue; 101 | try { 102 | String[] split = line.split("\\ "); 103 | int opcode = Integer.parseInt(split[0]); 104 | String name = split[1]; 105 | CS2Type returnType = CS2Type.forDesc(split[2]); 106 | CS2Type[] argTypes = new CS2Type[(split.length - 2) / 2]; 107 | String[] argNames = new String[(split.length - 2) / 2]; 108 | int write = 0; 109 | for (int i = 3; i < split.length; i += 2) { 110 | argTypes[write] = CS2Type.forDesc(split[i]); 111 | argNames[write++] = split[i + 1]; 112 | } 113 | info[opcode] = new FunctionInfo(name,argTypes,returnType,argNames); 114 | } 115 | catch (Exception ex) { 116 | ex.printStackTrace(); 117 | reader.close(); 118 | throw new RuntimeException("Error parsing function database file " + this.file + " on line:" + (linesCount + 1)); 119 | } 120 | } 121 | reader.close(); 122 | } 123 | catch (Throwable t) { 124 | throw new RuntimeException(t); 125 | } 126 | } 127 | 128 | 129 | 130 | public FunctionInfo getInfo(int opcode) { 131 | if (opcode < 0 || opcode >= info.length) 132 | return null; 133 | return info[opcode]; 134 | } 135 | 136 | public int bufferSize() { 137 | return info.length; 138 | } 139 | 140 | public int size() { 141 | int total = 0; 142 | for (int i = 0; i < info.length; i++) { 143 | if (info[i] != null) 144 | total++; 145 | } 146 | return total; 147 | } 148 | 149 | } 150 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/util/FunctionInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.util; 18 | 19 | import mgi.tools.jagdecs2.CS2Type; 20 | 21 | public class FunctionInfo { 22 | 23 | private String name; 24 | private CS2Type returnType; 25 | private CS2Type[] argumentTypes; 26 | private String[] argumentNames; 27 | 28 | 29 | public FunctionInfo(String name,CS2Type[] argTypes,CS2Type returnType, String[] argNames) { 30 | this.name = name; 31 | this.argumentTypes = argTypes; 32 | this.returnType = returnType; 33 | this.argumentNames = argNames; 34 | } 35 | 36 | 37 | 38 | public String getName() { 39 | return name; 40 | } 41 | 42 | public CS2Type[] getArgumentTypes() { 43 | return argumentTypes; 44 | } 45 | 46 | public CS2Type getReturnType() { 47 | return returnType; 48 | } 49 | 50 | public String[] getArgumentNames() { 51 | return argumentNames; 52 | } 53 | 54 | public String toString() { 55 | StringBuilder bld = new StringBuilder(); 56 | bld.append(returnType); 57 | bld.append(' '); 58 | bld.append(name); 59 | bld.append('('); 60 | for (int i = 0; i < argumentTypes.length; i++) { 61 | bld.append(argumentTypes[i].toString() + " " + argumentNames[i]); 62 | if ((i + 1) < argumentTypes.length) 63 | bld.append(','); 64 | } 65 | bld.append(')'); 66 | bld.append(';'); 67 | return bld.toString(); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/util/GenericsUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.util; 18 | 19 | public class GenericsUtils { 20 | 21 | 22 | public static boolean isi(Object cst) { 23 | return cst instanceof Integer; 24 | } 25 | 26 | public static boolean iss(Object cst) { 27 | return cst instanceof String; 28 | } 29 | 30 | public static boolean isl(Object cst) { 31 | return cst instanceof Long; 32 | } 33 | 34 | public static Integer asi(Object cst) { 35 | return as(cst); 36 | } 37 | 38 | public static String ass(Object cst) { 39 | return as(cst); 40 | } 41 | 42 | public static Long asl(Object cst) { 43 | return as(cst); 44 | } 45 | 46 | 47 | public static int shrn(int val, int shr, int n) { 48 | return (val >>> shr) & n; 49 | } 50 | 51 | public static long shrn(long val, int shr, long n) { 52 | return (val >>> shr) & n; 53 | } 54 | 55 | 56 | @SuppressWarnings("unchecked") 57 | public static T as(Class t, Object cst) { 58 | return (T)cst; 59 | } 60 | 61 | @SuppressWarnings("unchecked") 62 | public static T as(Object cst) { 63 | return (T)cst; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/util/IOUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.util; 18 | 19 | import java.awt.Component; 20 | import java.io.File; 21 | import java.io.PrintWriter; 22 | import java.io.StringWriter; 23 | 24 | import javax.swing.JFileChooser; 25 | 26 | public class IOUtils { 27 | 28 | public static String getStackTrace(Throwable t) { 29 | StringWriter sw = new StringWriter(); 30 | PrintWriter pw = new PrintWriter(sw, true); 31 | t.printStackTrace(pw); 32 | pw.flush(); 33 | sw.flush(); 34 | return sw.toString(); 35 | } 36 | 37 | 38 | public static File selectFolder(Component parent, boolean isOpen) { 39 | JFileChooser chooser = new JFileChooser(); 40 | chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 41 | int option = isOpen ? chooser.showOpenDialog(parent) : chooser.showSaveDialog(parent); 42 | if (option != JFileChooser.APPROVE_OPTION) 43 | return null; 44 | java.io.File folder = chooser.getSelectedFile(); 45 | if (folder == null) 46 | return null; 47 | return folder; 48 | } 49 | 50 | public static File selectFile(Component parent, boolean isOpen) { 51 | JFileChooser chooser = new JFileChooser(); 52 | chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 53 | int option = isOpen ? chooser.showOpenDialog(parent) : chooser.showSaveDialog(parent); 54 | if (option != JFileChooser.APPROVE_OPTION) 55 | return null; 56 | java.io.File folder = chooser.getSelectedFile(); 57 | if (folder == null) 58 | return null; 59 | return folder; 60 | } 61 | 62 | public static File selectAny(Component parent, boolean isOpen) { 63 | JFileChooser chooser = new JFileChooser(); 64 | chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 65 | int option = isOpen ? chooser.showOpenDialog(parent) : chooser.showSaveDialog(parent); 66 | if (option != JFileChooser.APPROVE_OPTION) 67 | return null; 68 | java.io.File selected = chooser.getSelectedFile(); 69 | if (selected == null) 70 | return null; 71 | return selected; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/util/InstructionInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.util; 18 | 19 | public class InstructionInfo { 20 | 21 | private String name; 22 | private int scrampledOpcode; 23 | private int originalOpcode; 24 | private boolean hasIntConstant; 25 | 26 | public InstructionInfo(String name, int scrampledop, int originalop, boolean hasIntConstant) { 27 | this.name = name; 28 | this.scrampledOpcode = scrampledop; 29 | this.originalOpcode = originalop; 30 | this.hasIntConstant = hasIntConstant; 31 | } 32 | 33 | public String getName() { 34 | return name; 35 | } 36 | 37 | public int getScrampledOpcode() { 38 | return scrampledOpcode; 39 | } 40 | 41 | public int getOpcode() { 42 | return originalOpcode; 43 | } 44 | 45 | public boolean hasIntConstant() { 46 | return hasIntConstant; 47 | } 48 | 49 | @Override 50 | public String toString() { 51 | return name + "[" + originalOpcode + "," + scrampledOpcode + ":" + hasIntConstant + "]"; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/util/InstructionsDatabase.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.util; 18 | 19 | import java.io.BufferedReader; 20 | import java.io.File; 21 | import java.io.FileReader; 22 | 23 | public class InstructionsDatabase { 24 | 25 | private File file; 26 | private InstructionInfo[] info1; 27 | private InstructionInfo[] info2; 28 | 29 | public InstructionsDatabase(File file) { 30 | this.file = file; 31 | this.info1 = new InstructionInfo[40000]; 32 | this.info2 = new InstructionInfo[40000]; 33 | this.readDatabase(); 34 | } 35 | 36 | public InstructionsDatabase() { 37 | this.info1 = new InstructionInfo[40000]; 38 | this.info2 = new InstructionInfo[40000]; 39 | } 40 | 41 | 42 | 43 | 44 | private void readDatabase() { 45 | try { 46 | BufferedReader reader = new BufferedReader(new FileReader(file)); 47 | int linesCount = 0; 48 | for (String line = reader.readLine(); line != null; line = reader.readLine(),linesCount++) { 49 | if (line.length() <= 0 || line.startsWith(" ") || line.startsWith("//") || line.startsWith("#")) 50 | continue; 51 | try { 52 | String[] split = line.split("\\ "); 53 | String name = split[0]; 54 | int scrampled = Integer.parseInt(split[1]); 55 | int unscrampled = Integer.parseInt(split[2]); 56 | boolean big = Boolean.parseBoolean(split[3]); 57 | if (info1[scrampled] != null || info2[unscrampled] != null) 58 | throw new RuntimeException("redefinition: " + scrampled + "," + unscrampled); 59 | info1[scrampled] = info2[unscrampled] = new InstructionInfo(name, scrampled, unscrampled, big); 60 | } 61 | catch (Exception ex) { 62 | ex.printStackTrace(); 63 | reader.close(); 64 | throw new RuntimeException("Error parsing instructions database file " + this.file + " on line:" + (linesCount + 1)); 65 | } 66 | } 67 | reader.close(); 68 | } 69 | catch (Throwable t) { 70 | throw new RuntimeException(t); 71 | } 72 | } 73 | 74 | 75 | 76 | public InstructionInfo getByScrampled(int opcode) { 77 | if (opcode < 0 || opcode >= info1.length) 78 | return null; 79 | return info1[opcode]; 80 | } 81 | 82 | public InstructionInfo getByUnscrampled(int opcode) { 83 | if (opcode < 0 || opcode >= info2.length) 84 | return null; 85 | return info2[opcode]; 86 | } 87 | 88 | 89 | public int size() { 90 | int total = 0; 91 | for (int i = 0; i < info1.length; i++) { 92 | if (info1[i] != null) 93 | total++; 94 | } 95 | return total; 96 | } 97 | 98 | } 99 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/util/OpcodeUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.util; 18 | 19 | import mgi.tools.jagdecs2.instructions.Opcodes; 20 | 21 | public class OpcodeUtils { 22 | 23 | 24 | public static int getTwoConditionsJumpStackType(int opcode) { 25 | if (opcode >= Opcodes.INT_EQ && opcode <= Opcodes.INT_GE) 26 | return 0; 27 | else if (opcode >= Opcodes.LONG_EQ && opcode <= Opcodes.LONG_GE) 28 | return 2; 29 | else 30 | return -1; 31 | } 32 | 33 | public static int getTwoConditionsJumpConditional(int opcode) { 34 | switch (opcode) { 35 | case Opcodes.INT_NE: 36 | case Opcodes.LONG_NE: 37 | return 0; // != 38 | case Opcodes.INT_EQ: 39 | case Opcodes.LONG_EQ: 40 | return 1; // == 41 | case Opcodes.INT_LT: 42 | case Opcodes.LONG_LT: 43 | return 3; // < 44 | case Opcodes.INT_GT: 45 | case Opcodes.LONG_GT: 46 | return 2; // > 47 | case Opcodes.INT_LE: 48 | case Opcodes.LONG_LE: 49 | return 5; // <= 50 | case Opcodes.INT_GE: 51 | case Opcodes.LONG_GE: 52 | return 4; // >= 53 | default: 54 | return -1; 55 | } 56 | } 57 | 58 | public static int getOneConditionJumpStackType(int opcode) { 59 | if (opcode == Opcodes.INT_T || opcode == Opcodes.INT_F) 60 | return 0; 61 | else 62 | return -1; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/util/Options.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.util; 18 | 19 | import java.io.BufferedReader; 20 | import java.io.BufferedWriter; 21 | import java.io.File; 22 | import java.io.FileReader; 23 | import java.io.FileWriter; 24 | import java.util.HashMap; 25 | 26 | public class Options { 27 | 28 | private File file; 29 | private HashMap options; 30 | 31 | public Options(File file) { 32 | this.file = file; 33 | this.options = new HashMap(); 34 | reload(); 35 | } 36 | 37 | /** 38 | * Save's options to file. 39 | */ 40 | public void save() { 41 | try { 42 | BufferedWriter writer = new BufferedWriter(new FileWriter(file)); 43 | 44 | writer.write("# Auto generated options - DO NOT EDIT\n\n\n\n"); 45 | 46 | for (String key : options.keySet()) { 47 | writer.write(key + " " + options.get(key) + "\n\n"); 48 | } 49 | 50 | writer.close(); 51 | } 52 | catch (Throwable t) { 53 | t.printStackTrace(); 54 | } 55 | } 56 | 57 | /** 58 | * Read's options from options file. 59 | */ 60 | public void reload() { 61 | try { 62 | BufferedReader reader = new BufferedReader(new FileReader(file)); 63 | 64 | int linesCount = 0; 65 | for (String line = reader.readLine(); line != null; line = reader.readLine(), linesCount++) { 66 | if (line.length() <= 0 || line.startsWith(" ") || line.startsWith("//") || line.startsWith("#")) 67 | continue; 68 | try { 69 | int space = line.indexOf(' '); 70 | if (space <= 0) { 71 | reader.close(); 72 | throw new RuntimeException("Bad syntax!"); 73 | } 74 | String key = line.substring(0, space); 75 | String value = line.substring(space + 1); 76 | setOption(key, value); 77 | } 78 | catch (Exception ex) { 79 | reader.close(); 80 | throw new RuntimeException("Error parsing options file " + file + " on line:" + (linesCount + 1)); 81 | } 82 | } 83 | 84 | reader.close(); 85 | } 86 | catch (Throwable t) { 87 | t.printStackTrace(); 88 | } 89 | } 90 | 91 | /** 92 | * Get's option. 93 | * Return's null if it's not found. 94 | */ 95 | public String getOption(String key) { 96 | if (options.containsKey(key)) 97 | return options.get(key); 98 | return null; 99 | } 100 | 101 | /** 102 | * Set's option. 103 | */ 104 | public void setOption(String key, String value) { 105 | if (options.containsKey(key)) 106 | options.remove(key); 107 | options.put(key, value); 108 | } 109 | 110 | 111 | 112 | } 113 | -------------------------------------------------------------------------------- /src/mgi/tools/jagdecs2/util/TextUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package mgi.tools.jagdecs2.util; 18 | 19 | public class TextUtils { 20 | 21 | public static String quote(String str) { 22 | StringBuffer result = new StringBuffer("\""); 23 | for (int i = 0; i < str.length(); i++) { 24 | char c; 25 | switch (c = str.charAt(i)) { 26 | case '\0' : 27 | result.append("\\0"); 28 | break; 29 | case '\t' : 30 | result.append("\\t"); 31 | break; 32 | case '\n' : 33 | result.append("\\n"); 34 | break; 35 | case '\r' : 36 | result.append("\\r"); 37 | break; 38 | case '\\' : 39 | result.append("\\\\"); 40 | break; 41 | case '\"' : 42 | result.append("\\\""); 43 | break; 44 | default : 45 | if (c < 32) { 46 | String oct = Integer.toOctalString(c); 47 | result.append("\\000".substring(0, 4 - oct.length())).append(oct); 48 | } else if (c >= 32 && c < 127) 49 | result.append(str.charAt(i)); 50 | else { 51 | String hex = Integer.toHexString(c); 52 | result.append("\\u0000".substring(0, 6 - hex.length())).append(hex); 53 | } 54 | } 55 | } 56 | return result.append("\"").toString(); 57 | } 58 | 59 | public static String quote(char c) { 60 | switch (c) { 61 | case '\0' : 62 | return "\'\\0\'"; 63 | case '\t' : 64 | return "\'\\t\'"; 65 | case '\n' : 66 | return "\'\\n\'"; 67 | case '\r' : 68 | return "\'\\r\'"; 69 | case '\\' : 70 | return "\'\\\\\'"; 71 | case '\"' : 72 | return "\'\\\"\'"; 73 | case '\'' : 74 | return "\'\\\'\'"; 75 | } 76 | if (c < 32) { 77 | String oct = Integer.toOctalString(c); 78 | return "\'\\000".substring(0, 5 - oct.length()) + oct + "\'"; 79 | } 80 | if (c >= 32 && c < 127) 81 | return "\'" + c + "\'"; 82 | else { 83 | String hex = Integer.toHexString(c); 84 | return "\'\\u0000".substring(0, 7 - hex.length()) + hex + "\'"; 85 | } 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/tests/.gitignore: -------------------------------------------------------------------------------- 1 | /Main.java 2 | -------------------------------------------------------------------------------- /src/tests/DecompileAll.java: -------------------------------------------------------------------------------- 1 | package tests; 2 | 3 | import java.io.BufferedWriter; 4 | import java.io.ByteArrayOutputStream; 5 | import java.io.File; 6 | import java.io.FileWriter; 7 | import java.io.PrintStream; 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | 11 | import mgi.tools.jagdecs2.CS2Decompiler; 12 | import mgi.tools.jagdecs2.CodePrinter; 13 | import mgi.tools.jagdecs2.ast.FunctionNode; 14 | import mgi.tools.jagdecs2.util.ConfigsDatabase; 15 | import mgi.tools.jagdecs2.util.FunctionDatabase; 16 | import mgi.tools.jagdecs2.util.InstructionsDatabase; 17 | 18 | public class DecompileAll { 19 | 20 | 21 | 22 | public static void main(String[] args) throws Throwable { 23 | InstructionsDatabase insDatabase = new InstructionsDatabase(new File("instructions_db.ini")); 24 | ConfigsDatabase cfgDatabase = new ConfigsDatabase(new File("configs_db.ini"), new File("bitconfigs_db.ini")); 25 | FunctionDatabase opcodesDatabase = new FunctionDatabase(new File("opcodes_db.ini")); 26 | FunctionDatabase scriptsDatabase = new FunctionDatabase(new File("scripts_db.ini")); 27 | CS2Decompiler decompiler = new CS2Decompiler(insDatabase, cfgDatabase, opcodesDatabase, scriptsDatabase, new TestProvider()); 28 | 29 | 30 | 31 | List scriptsde = new ArrayList(); 32 | List scriptsfe = new ArrayList(); 33 | int count = 12346; 34 | System.setErr(new PrintStream(new ByteArrayOutputStream())); 35 | int flowErrors = 0; 36 | int decompilerErrors = 0; 37 | for (int i = 0; i < count; i++) { 38 | if (new File("scripts/" + i).exists()) { 39 | FunctionNode func = decompiler.decompile(i); 40 | BufferedWriter writer = new BufferedWriter(new FileWriter(new File("sources/" + i + ".cs2"))); 41 | CodePrinter printer = new CodePrinter(); 42 | func.print(printer); 43 | String str = printer.toString(); 44 | writer.append(str); 45 | writer.newLine(); 46 | writer.close(); 47 | boolean flowError = str.contains("flow_"); 48 | boolean decompilerError = str.contains("DecompilerException"); 49 | if (decompilerError) 50 | scriptsde.add(i); 51 | if (flowError) 52 | scriptsfe.add(i); 53 | System.out.println(i + "/" + (count - 1) + ", flow errors:" + (flowError ? ++flowErrors : flowErrors) + ", decompiler errors:" + (decompilerError ? ++decompilerErrors : decompilerErrors)); 54 | } 55 | else { 56 | System.out.println(i + "/" + (count - 1) + ", not existing"); 57 | } 58 | } 59 | 60 | for (Integer i : scriptsde) 61 | System.out.println("Decompiler error on:" + i); 62 | 63 | for (Integer i : scriptsfe) 64 | System.out.println("Flow error on:" + i); 65 | } 66 | 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/tests/TestProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | This program is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program. If not, see . 14 | */ 15 | 16 | 17 | package tests; 18 | 19 | import java.io.File; 20 | import java.io.IOException; 21 | 22 | import mgi.tools.jagdecs2.CS2; 23 | import mgi.tools.jagdecs2.CS2Decoder; 24 | import mgi.tools.jagdecs2.ICS2Provider; 25 | import mgi.tools.jagdecs2.util.ConfigsDatabase; 26 | import mgi.tools.jagdecs2.util.FunctionDatabase; 27 | import mgi.tools.jagdecs2.util.InstructionsDatabase; 28 | 29 | public class TestProvider implements ICS2Provider { 30 | 31 | 32 | @Override 33 | public CS2 getCS2(InstructionsDatabase idb, ConfigsDatabase cdb, FunctionDatabase sdb, FunctionDatabase odb, int id) { 34 | try { 35 | return CS2Decoder.readScript(idb, cdb, new File("scripts/" + id)); 36 | } 37 | catch (IOException ex) { 38 | ex.printStackTrace(); 39 | return null; 40 | } 41 | } 42 | 43 | } 44 | --------------------------------------------------------------------------------