-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:specs-feup/specs-java-libs
- Loading branch information
Showing
12 changed files
with
249 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?xml version="1.0" encoding="ISO-8859-1"?> | ||
<!-- | ||
Licensed to the Apache Software Foundation (ASF) under one | ||
or more contributor license agreements. See the NOTICE file | ||
distributed with this work for additional information | ||
regarding copyright ownership. The ASF licenses this file | ||
to you under the Apache License, Version 2.0 (the | ||
"License"); you may not use this file except in compliance | ||
with the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, | ||
software distributed under the License is distributed on an | ||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations | ||
under the License. | ||
--> | ||
<ivy-module version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd"> | ||
<info | ||
organisation="specs.fe.up.pt" | ||
module="AsmParser" | ||
status="release"> | ||
</info> | ||
|
||
<dependencies> | ||
<dependency org="com.google.code.gson" name="gson" rev="2.4"/> | ||
</dependencies> | ||
</ivy-module> |
104 changes: 104 additions & 0 deletions
104
AsmParser/src/pt/up/fe/specs/asmparser/Isa32bitParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package pt.up.fe.specs.asmparser; | ||
|
||
import com.google.gson.Gson; | ||
import pt.up.fe.specs.asmparser.ast.FieldNode; | ||
import pt.up.fe.specs.asmparser.ast.RuleNode; | ||
import pt.up.fe.specs.asmparser.parser32bit.Asm32bitParser; | ||
import pt.up.fe.specs.util.SpecsIo; | ||
|
||
import java.util.*; | ||
|
||
public class Isa32bitParser { | ||
|
||
public static void main(String[] args) { | ||
var isaParser = newInstance(SpecsIo.getResource(() -> "pt/up/fe/specs/binarytranslation/asm/parsing/asm_test.json")); | ||
|
||
//"0110_xx_registerd(5)_1000_opcode(1)_0_imm(15)" | ||
// 31, 1, 10925 | ||
var decoded = isaParser.parse(Long.parseLong("01100011111100010010101010101101", 2)); | ||
System.out.println(Arrays.toString(decoded)); | ||
} | ||
|
||
|
||
private final List<Asm32bitParser> parsers; | ||
private final Map<String, Integer> fieldsMap; | ||
|
||
private Isa32bitParser(List<Asm32bitParser> parsers, Map<String, Integer> fieldsMap) { | ||
this.parsers = parsers; | ||
this.fieldsMap = fieldsMap; | ||
} | ||
|
||
|
||
public static Isa32bitParser newInstance(String jsonContents) { | ||
var isa = new Gson().fromJson(jsonContents, Map.class); | ||
|
||
//var fields = new LinkedHashSet<>((List<String>) isa.get("fields")); | ||
|
||
// Maps a field to an index | ||
var fieldsMap = new HashMap<String, Integer>(); | ||
|
||
// Index 0 is id of format | ||
var fieldId = 1; | ||
for (var field : (List<String>) isa.get("fields")) { | ||
fieldsMap.put(field, fieldId); | ||
fieldId++; | ||
} | ||
|
||
// System.out.println("FIELDS MAP: " + fieldsMap); | ||
|
||
int id = 0; | ||
var parsers = new ArrayList<Asm32bitParser>(); | ||
for (var format : (List<String>) isa.get("formats")) { | ||
|
||
// Parse format | ||
var rule = new InstructionFormatParser().parse(format); | ||
|
||
// Verify rule | ||
verify(rule, format, fieldsMap.keySet()); | ||
|
||
// Create parser | ||
var parser = Asm32bitParser.build(id, rule, fieldsMap); | ||
parsers.add(parser); | ||
id++; | ||
} | ||
|
||
|
||
return new Isa32bitParser(parsers, fieldsMap); | ||
} | ||
|
||
private static void verify(RuleNode rule, String format, Set<String> fields) { | ||
// Go to all fields, check they are declared | ||
var invalidField = rule.getDescendants(FieldNode.class).stream() | ||
.filter(node -> !fields.contains(node.getField())) | ||
.findFirst() | ||
.orElse(null); | ||
|
||
|
||
if (invalidField != null) { | ||
throw new RuntimeException("Found undeclared field '" + invalidField.getField() + "' in format rule '" + format + "'"); | ||
} | ||
|
||
} | ||
|
||
public Map<String, Integer> getFieldsMap() { | ||
return fieldsMap; | ||
} | ||
|
||
public int[] parse(long instruction) { | ||
// Iterate over all parsers, looking for one that accepts the instruction | ||
for (var parser : parsers) { | ||
var decoded = parser.parse(instruction); | ||
|
||
// Could not decode, skip | ||
if (decoded == null) { | ||
continue; | ||
} | ||
|
||
// Decoded, return | ||
return decoded; | ||
} | ||
|
||
throw new RuntimeException("Could not decode instruction 0x" + Long.toString(instruction, 16)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,4 +71,9 @@ public void test1() { | |
*/ | ||
} | ||
|
||
@Test | ||
public void testFromJson() { | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.