Compare commits
8 Commits
b37ab5d008
...
master
Author | SHA1 | Date | |
---|---|---|---|
c742b17fe0 | |||
4305f2ef36 | |||
d9360ce1fd | |||
ed7157aaf3 | |||
72814c5b15 | |||
e935ee319d | |||
bf744c7075 | |||
7add65c8c6 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
/.launch/
|
||||
/.settings/
|
||||
/*.json
|
||||
/.m2
|
||||
|
25
.launch/CoreDSL2JSON change version.launch
Normal file
25
.launch/CoreDSL2JSON change version.launch
Normal file
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<launchConfiguration type="org.eclipse.m2e.Maven2LaunchConfigurationType">
|
||||
<intAttribute key="M2_COLORS" value="0"/>
|
||||
<booleanAttribute key="M2_DEBUG_OUTPUT" value="false"/>
|
||||
<stringAttribute key="M2_GOALS" value="org.eclipse.tycho:tycho-versions-plugin:set-version"/>
|
||||
<booleanAttribute key="M2_NON_RECURSIVE" value="false"/>
|
||||
<booleanAttribute key="M2_OFFLINE" value="false"/>
|
||||
<stringAttribute key="M2_PROFILES" value=""/>
|
||||
<listAttribute key="M2_PROPERTIES">
|
||||
<listEntry value="tycho.mode=maven"/>
|
||||
<listEntry value="newVersion=${string_prompt:version string}"/>
|
||||
<listEntry value="maven.repo.local=${workspace_loc:/com.minres.coredsl.json.parent}/.m2"/>
|
||||
</listAttribute>
|
||||
<stringAttribute key="M2_RUNTIME" value="EMBEDDED"/>
|
||||
<booleanAttribute key="M2_SKIP_TESTS" value="false"/>
|
||||
<intAttribute key="M2_THREADS" value="1"/>
|
||||
<booleanAttribute key="M2_UPDATE_SNAPSHOTS" value="false"/>
|
||||
<stringAttribute key="M2_USER_SETTINGS" value=""/>
|
||||
<booleanAttribute key="M2_WORKSPACE_RESOLUTION" value="false"/>
|
||||
<booleanAttribute key="org.eclipse.debug.core.ATTR_FORCE_SYSTEM_CONSOLE_ENCODING" value="false"/>
|
||||
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_ATTR_USE_ARGFILE" value="false"/>
|
||||
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_SHOW_CODEDETAILS_IN_EXCEPTION_MESSAGES" value="true"/>
|
||||
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_USE_CLASSPATH_ONLY_JAR" value="false"/>
|
||||
<stringAttribute key="org.eclipse.jdt.launching.WORKING_DIRECTORY" value="${workspace_loc:/com.minres.coredsl.json.parent}"/>
|
||||
</launchConfiguration>
|
21
.launch/CoreDSL2JSON clean.launch
Normal file
21
.launch/CoreDSL2JSON clean.launch
Normal file
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<launchConfiguration type="org.eclipse.m2e.Maven2LaunchConfigurationType">
|
||||
<intAttribute key="M2_COLORS" value="0"/>
|
||||
<booleanAttribute key="M2_DEBUG_OUTPUT" value="false"/>
|
||||
<stringAttribute key="M2_GOALS" value="clean"/>
|
||||
<booleanAttribute key="M2_NON_RECURSIVE" value="false"/>
|
||||
<booleanAttribute key="M2_OFFLINE" value="false"/>
|
||||
<stringAttribute key="M2_PROFILES" value=""/>
|
||||
<listAttribute key="M2_PROPERTIES"/>
|
||||
<stringAttribute key="M2_RUNTIME" value="EMBEDDED"/>
|
||||
<booleanAttribute key="M2_SKIP_TESTS" value="false"/>
|
||||
<intAttribute key="M2_THREADS" value="1"/>
|
||||
<booleanAttribute key="M2_UPDATE_SNAPSHOTS" value="false"/>
|
||||
<stringAttribute key="M2_USER_SETTINGS" value=""/>
|
||||
<booleanAttribute key="M2_WORKSPACE_RESOLUTION" value="false"/>
|
||||
<booleanAttribute key="org.eclipse.debug.core.ATTR_FORCE_SYSTEM_CONSOLE_ENCODING" value="false"/>
|
||||
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_ATTR_USE_ARGFILE" value="false"/>
|
||||
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_SHOW_CODEDETAILS_IN_EXCEPTION_MESSAGES" value="true"/>
|
||||
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_USE_CLASSPATH_ONLY_JAR" value="false"/>
|
||||
<stringAttribute key="org.eclipse.jdt.launching.WORKING_DIRECTORY" value="${project_loc:com.minres.coredsl.json.parent}"/>
|
||||
</launchConfiguration>
|
23
.launch/CoreDSL2JSON package.launch
Normal file
23
.launch/CoreDSL2JSON package.launch
Normal file
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<launchConfiguration type="org.eclipse.m2e.Maven2LaunchConfigurationType">
|
||||
<intAttribute key="M2_COLORS" value="0"/>
|
||||
<booleanAttribute key="M2_DEBUG_OUTPUT" value="false"/>
|
||||
<stringAttribute key="M2_GOALS" value="package"/>
|
||||
<booleanAttribute key="M2_NON_RECURSIVE" value="false"/>
|
||||
<booleanAttribute key="M2_OFFLINE" value="false"/>
|
||||
<stringAttribute key="M2_PROFILES" value=""/>
|
||||
<listAttribute key="M2_PROPERTIES">
|
||||
<listEntry value="maven.repo.local=${project_loc:com.minres.coredsl.json.parent}/.m2"/>
|
||||
</listAttribute>
|
||||
<stringAttribute key="M2_RUNTIME" value="EMBEDDED"/>
|
||||
<booleanAttribute key="M2_SKIP_TESTS" value="false"/>
|
||||
<intAttribute key="M2_THREADS" value="1"/>
|
||||
<booleanAttribute key="M2_UPDATE_SNAPSHOTS" value="false"/>
|
||||
<stringAttribute key="M2_USER_SETTINGS" value=""/>
|
||||
<booleanAttribute key="M2_WORKSPACE_RESOLUTION" value="false"/>
|
||||
<booleanAttribute key="org.eclipse.debug.core.ATTR_FORCE_SYSTEM_CONSOLE_ENCODING" value="false"/>
|
||||
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_ATTR_USE_ARGFILE" value="false"/>
|
||||
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_SHOW_CODEDETAILS_IN_EXCEPTION_MESSAGES" value="true"/>
|
||||
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_USE_CLASSPATH_ONLY_JAR" value="false"/>
|
||||
<stringAttribute key="org.eclipse.jdt.launching.WORKING_DIRECTORY" value="${project_loc:com.minres.coredsl.json.parent}"/>
|
||||
</launchConfiguration>
|
@@ -4,7 +4,7 @@
|
||||
<parent>
|
||||
<artifactId>com.minres.coredsl.json.parent</artifactId>
|
||||
<groupId>com.minres.coredsl</groupId>
|
||||
<version>2.0.2</version>
|
||||
<version>2.0.3</version>
|
||||
<relativePath>..</relativePath>
|
||||
</parent>
|
||||
<artifactId>com.minres.coredsl.json.standalone</artifactId>
|
||||
|
@@ -19,9 +19,9 @@
|
||||
<repository location="https://download.eclipse.org/modeling/tmf/xtext/updates/releases/2.26.0/"/>
|
||||
</location>
|
||||
<location includeAllPlatforms="false" includeConfigurePhase="false" includeMode="planner" includeSource="true" type="InstallableUnit">
|
||||
<unit id="com.google.gson" version="2.8.9.v202.0.21-1409"/>
|
||||
<unit id="com.google.gson" version="2.8.9.v20220111-1409"/>
|
||||
<unit id="com.google.inject" version="5.0.1.v20210324-2015"/>
|
||||
<unit id="javax.inject" version="1.0.0.v2.0.2030"/>
|
||||
<unit id="javax.inject" version="1.0.0.v20091030"/>
|
||||
<unit id="org.junit" version="4.13.2.v20211018-1956"/>
|
||||
<unit id="org.apiguardian" version="1.1.2.v20211018-1956"/>
|
||||
<unit id="org.junit.jupiter.api" version="5.8.1.v20211018-1956"/>
|
||||
@@ -32,14 +32,14 @@
|
||||
<unit id="org.junit.platform.runner" version="1.8.1.v20211018-1956"/>
|
||||
<unit id="org.junit.platform.suite.commons" version="1.8.1.v20211018-1956"/>
|
||||
<unit id="org.opentest4j" version="1.2.0.v20211018-1956"/>
|
||||
<unit id="org.objectweb.asm" version="9.2.0.v202.0.23-1119"/>
|
||||
<unit id="org.objectweb.asm.tree" version="9.2.0.v202.0.23-1119"/>
|
||||
<unit id="org.objectweb.asm" version="9.2.0.v20210813-1119"/>
|
||||
<unit id="org.objectweb.asm.tree" version="9.2.0.v20210813-1119"/>
|
||||
<unit id="io.github.classgraph" version="4.8.138.v20211212-1642"/>
|
||||
<repository location="https://download.eclipse.org/tools/orbit/downloads/2022-03"/>
|
||||
<unit id="org.json" version="1.0.0.v201011060100"/>
|
||||
</location>
|
||||
<location includeAllPlatforms="false" includeConfigurePhase="false" includeMode="planner" includeSource="true" type="InstallableUnit">
|
||||
<repository location="https://minres.github.io/CoreDSL/repository/2.0/2.0.28"/>
|
||||
<repository location="https://minres.github.io/CoreDSL/repository/2.0/2.0.21"/>
|
||||
<unit id="com.minres.coredsl.feature.source.feature.group" version="0.0.0"/>
|
||||
<unit id="com.minres.coredsl.feature.feature.group" version="0.0.0"/>
|
||||
</location>
|
||||
|
@@ -4,7 +4,7 @@
|
||||
<parent>
|
||||
<groupId>com.minres.coredsl</groupId>
|
||||
<artifactId>com.minres.coredsl.json.parent</artifactId>
|
||||
<version>2.0.2</version>
|
||||
<version>2.0.3</version>
|
||||
<relativePath>..</relativePath>
|
||||
</parent>
|
||||
<artifactId>com.minres.coredsl.json.target</artifactId>
|
||||
|
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
|
||||
Automatic-Module-Name: com.minres.coredsl.json.dbt_rise.tests
|
||||
Bundle-ManifestVersion: 2
|
||||
Bundle-Vendor: MINRES Technologies GmbH
|
||||
Bundle-Version: 2.0.2
|
||||
Bundle-Version: 2.0.3
|
||||
Bundle-SymbolicName: com.minres.coredsl.json.tests;singleton:=true
|
||||
Bundle-ActivationPolicy: lazy
|
||||
Require-Bundle: com.minres.coredsl.json;bundle-version="1.0.0",
|
||||
|
@@ -4,7 +4,7 @@
|
||||
<parent>
|
||||
<groupId>com.minres.coredsl</groupId>
|
||||
<artifactId>com.minres.coredsl.json.parent</artifactId>
|
||||
<version>2.0.2</version>
|
||||
<version>2.0.3</version>
|
||||
<relativePath>..</relativePath>
|
||||
</parent>
|
||||
<artifactId>com.minres.coredsl.json.tests</artifactId>
|
||||
|
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
|
||||
Automatic-Module-Name: com.minres.coredsl.json
|
||||
Bundle-ManifestVersion: 2
|
||||
Bundle-Vendor: MINRES Technologies GmbH
|
||||
Bundle-Version: 2.0.2
|
||||
Bundle-Version: 2.0.3
|
||||
Bundle-SymbolicName: com.minres.coredsl.json;singleton:=true
|
||||
Bundle-ActivationPolicy: lazy
|
||||
Require-Bundle: com.minres.coredsl;bundle-version="2.0.0",
|
||||
@@ -12,7 +12,7 @@ Require-Bundle: com.minres.coredsl;bundle-version="2.0.0",
|
||||
org.eclipse.equinox.preferences;bundle-version="3.8.0",
|
||||
org.eclipse.xtend.lib;bundle-version="2.14.0",
|
||||
org.eclipse.xtext;bundle-version="2.22.0",
|
||||
org.eclipse.emf.mwe.utils;bundle-version="1.6.2",
|
||||
org.eclipse.emf.mwe.utils,
|
||||
org.apache.commons.logging;bundle-version="1.2.0",
|
||||
org.json;bundle-version="1.0.0"
|
||||
Bundle-RequiredExecutionEnvironment: JavaSE-17
|
||||
|
@@ -4,7 +4,7 @@
|
||||
<parent>
|
||||
<groupId>com.minres.coredsl</groupId>
|
||||
<artifactId>com.minres.coredsl.json.parent</artifactId>
|
||||
<version>2.0.2</version>
|
||||
<version>2.0.3</version>
|
||||
<relativePath>..</relativePath>
|
||||
</parent>
|
||||
<artifactId>com.minres.coredsl.json</artifactId>
|
||||
|
@@ -8,6 +8,8 @@ import org.eclipse.xtext.generator.JavaIoFileSystemAccess
|
||||
import org.eclipse.xtext.generator.IOutputConfigurationProvider
|
||||
import javax.inject.Singleton
|
||||
import com.minres.coredsl.json.GeneratorOutputConfigurationProvider
|
||||
import org.eclipse.xtext.scoping.impl.ImportUriResolver
|
||||
import com.minres.coredsl.scoping.IncludeUriResolver
|
||||
|
||||
/**
|
||||
* Use this class to register components to be used at runtime / without the Equinox extension registry.
|
||||
@@ -21,11 +23,11 @@ import com.minres.coredsl.json.GeneratorOutputConfigurationProvider
|
||||
.in(Singleton);
|
||||
binder.bind(IFileSystemAccess2)
|
||||
.to(JavaIoFileSystemAccess)
|
||||
binder.bind(ImportUriResolver)
|
||||
.to(IncludeUriResolver);
|
||||
}
|
||||
|
||||
def Class<? extends IGenerator2> bindIGenerator2() {
|
||||
// return DummyGenerator.class;
|
||||
return typeof(CoreDslJsonGenerator)
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -38,8 +38,8 @@ class Main implements Callable<Integer> {
|
||||
@Option(names=#["-o", "--output-file"], description="output file")
|
||||
public File outputFile;
|
||||
|
||||
@Option(names=#["-r", "--repository"], description="repository directory")
|
||||
public String repository = "";
|
||||
@Option(names=#["-r", "--repository"], description="one ore more repository directories")
|
||||
public String[] repositories;
|
||||
|
||||
@Option(names=#["-v", "--verbose"], description="verbose output")
|
||||
public Boolean verbose = false;
|
||||
@@ -70,12 +70,12 @@ class GeneratorMain {
|
||||
@Inject JavaIoFileSystemAccess fsa
|
||||
|
||||
def run(Main params) {
|
||||
if (params.repository.length > 0) {
|
||||
params.repositories.forEach[repository, idx|
|
||||
val projectMapping = new ProjectMapping
|
||||
projectMapping.projectName = "CoreDSL Repository"
|
||||
projectMapping.path = params.repository
|
||||
projectMapping.projectName = "Repository:"+idx
|
||||
projectMapping.path = repository
|
||||
new StandaloneSetup().addProjectMapping(projectMapping)
|
||||
}
|
||||
]
|
||||
try {
|
||||
for (file : params.files) {
|
||||
// Load the resource
|
||||
|
@@ -37,7 +37,7 @@ if [ $# -lt 1 ]; then
|
||||
fi
|
||||
|
||||
INPUT_FILE=$1
|
||||
GENERATOR_JAR=${SCRIPTDIR}/com.minres.coredsl.json.repository/target/com.minres.coredsl.json-2.0.0-SNAPSHOT.jar
|
||||
GENERATOR_JAR=${SCRIPTDIR}/com.minres.coredsl.json.repository/target/com.minres.coredsl.json-2.0.3.jar
|
||||
JAVA_OPTS="--add-modules ALL-SYSTEM --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.annotation=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.base/java.lang.module=ALL-UNNAMED --add-opens=java.base/java.lang.ref=ALL-UNNAMED --add-opens=java.base/java.lang.reflect=ALL-UNNAMED --add-opens=java.base/java.math=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=java.base/java.net.spi=ALL-UNNAMED --add-opens=java.base/java.nio=ALL-UNNAMED --add-opens=java.base/java.nio.channels=ALL-UNNAMED --add-opens=java.base/java.nio.channels.spi=ALL-UNNAMED --add-opens=java.base/java.nio.charset=ALL-UNNAMED --add-opens=java.base/java.nio.charset.spi=ALL-UNNAMED --add-opens=java.base/java.nio.file=ALL-UNNAMED --add-opens=java.base/java.nio.file.attribute=ALL-UNNAMED --add-opens=java.base/java.nio.file.spi=ALL-UNNAMED --add-opens=java.base/java.security=ALL-UNNAMED --add-opens=java.base/java.security.acl=ALL-UNNAMED --add-opens=java.base/java.security.cert=ALL-UNNAMED --add-opens=java.base/java.security.interfaces=ALL-UNNAMED --add-opens=java.base/java.security.spec=ALL-UNNAMED --add-opens=java.base/java.text=ALL-UNNAMED --add-opens=java.base/java.text.spi=ALL-UNNAMED --add-opens=java.base/java.time=ALL-UNNAMED --add-opens=java.base/java.time.chrono=ALL-UNNAMED --add-opens=java.base/java.time.format=ALL-UNNAMED --add-opens=java.base/java.time.temporal=ALL-UNNAMED --add-opens=java.base/java.time.zone=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED --add-opens=java.base/java.util.concurrent.locks=ALL-UNNAMED --add-opens=java.base/java.util.function=ALL-UNNAMED --add-opens=java.base/java.util.jar=ALL-UNNAMED --add-opens=java.base/java.util.regex=ALL-UNNAMED --add-opens=java.base/java.util.spi=ALL-UNNAMED --add-opens=java.base/java.util.stream=ALL-UNNAMED --add-opens=java.base/java.util.zip=ALL-UNNAMED --add-opens=java.datatransfer/java.awt.datatransfer=ALL-UNNAMED --add-opens=java.desktop/java.applet=ALL-UNNAMED --add-opens=java.desktop/java.awt=ALL-UNNAMED --add-opens=java.desktop/java.awt.color=ALL-UNNAMED --add-opens=java.desktop/java.awt.desktop=ALL-UNNAMED --add-opens=java.desktop/java.awt.dnd=ALL-UNNAMED --add-opens=java.desktop/java.awt.dnd.peer=ALL-UNNAMED --add-opens=java.desktop/java.awt.event=ALL-UNNAMED --add-opens=java.desktop/java.awt.font=ALL-UNNAMED --add-opens=java.desktop/java.awt.geom=ALL-UNNAMED --add-opens=java.desktop/java.awt.im=ALL-UNNAMED --add-opens=java.desktop/java.awt.im.spi=ALL-UNNAMED --add-opens=java.desktop/java.awt.image=ALL-UNNAMED --add-opens=java.desktop/java.awt.image.renderable=ALL-UNNAMED --add-opens=java.desktop/java.awt.peer=ALL-UNNAMED --add-opens=java.desktop/java.awt.print=ALL-UNNAMED --add-opens=java.desktop/java.beans=ALL-UNNAMED --add-opens=java.desktop/java.beans.beancontext=ALL-UNNAMED --add-opens=java.instrument/java.lang.instrument=ALL-UNNAMED --add-opens=java.logging/java.util.logging=ALL-UNNAMED --add-opens=java.management/java.lang.management=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED --add-opens=java.rmi/java.rmi=ALL-UNNAMED --add-opens=java.rmi/java.rmi.activation=ALL-UNNAMED --add-opens=java.rmi/java.rmi.dgc=ALL-UNNAMED --add-opens=java.rmi/java.rmi.registry=ALL-UNNAMED --add-opens=java.rmi/java.rmi.server=ALL-UNNAMED --add-opens=java.sql/java.sql=ALL-UNNAMED"
|
||||
GENERATOR="java $JAVA_OPTS -jar ${GENERATOR_JAR} "
|
||||
|
||||
|
4
pom.xml
4
pom.xml
@@ -2,7 +2,7 @@
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.minres.coredsl</groupId>
|
||||
<version>2.0.2</version>
|
||||
<version>2.0.3</version>
|
||||
<artifactId>com.minres.coredsl.json.parent</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
@@ -95,7 +95,7 @@
|
||||
<artifact>
|
||||
<groupId>com.minres.coredsl</groupId>
|
||||
<artifactId>com.minres.coredsl.json.target</artifactId>
|
||||
<version>2.0.2</version>
|
||||
<version>2.0.3</version>
|
||||
</artifact>
|
||||
</target>
|
||||
<environments>
|
||||
|
222
process_json.py
Normal file
222
process_json.py
Normal file
@@ -0,0 +1,222 @@
|
||||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import json
|
||||
from abc import abstractmethod
|
||||
from pathlib import Path
|
||||
from typing import Union
|
||||
|
||||
from jsonschema import ValidationError, validate
|
||||
|
||||
INSTRUCTION_SCHEMA = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"instructions": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {"type": "string"},
|
||||
"decoding": {"type": "string"},
|
||||
},
|
||||
"required": ["name", "decoding"],
|
||||
},
|
||||
}
|
||||
},
|
||||
"required": ["instructions"],
|
||||
}
|
||||
|
||||
|
||||
class EncodingPart:
|
||||
pos: int = 0
|
||||
size: int = 0
|
||||
|
||||
@abstractmethod
|
||||
def coredsl(self) -> str:
|
||||
pass
|
||||
|
||||
|
||||
class Field(EncodingPart):
|
||||
def get_bounds(self, s: str) -> tuple[int, int]:
|
||||
if ":" in s:
|
||||
end, start = s[s.index("[") :].strip("[").rstrip("]").split(":")
|
||||
else:
|
||||
start = end = int(s[s.index("[") :].strip("[").rstrip("]"))
|
||||
return int(start), int(end)
|
||||
|
||||
def __init__(self, s: str) -> None:
|
||||
assert "[" in s and "]" in s
|
||||
assert s.count("[") == 1 and s.count("]")
|
||||
self.start, self.end = self.get_bounds(s)
|
||||
self.size = self.end - self.start + 1
|
||||
self.name = s[: s.index("[")]
|
||||
|
||||
def __str__(self) -> str:
|
||||
shift = " << " if self.pos >= self.start else ">>"
|
||||
shamt = str(self.pos)
|
||||
value_str = f"(\\{self.name} & 0b{self.size * '1'})"
|
||||
if self.start:
|
||||
value_str = f"(\\{self.name} & 0b{self.size * '1'}{self.start * '0'})"
|
||||
shamt = str(abs(self.pos - self.start))
|
||||
return value_str + shift + shamt if self.pos - self.start else value_str
|
||||
|
||||
def coredsl(self) -> str:
|
||||
middle = (
|
||||
f"[{self.end}:{self.start}]"
|
||||
if self.size > 1
|
||||
else f"[{self.end}:{self.end}]"
|
||||
)
|
||||
return self.name + middle
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return "Field: " + self.coredsl()
|
||||
|
||||
|
||||
class Literal(EncodingPart):
|
||||
def __init__(self, s: str) -> None:
|
||||
assert len(s.replace("1", "").replace("0", "")) == 0
|
||||
self.value = int(s, 2)
|
||||
self.size = len(s)
|
||||
|
||||
def __str__(self) -> str:
|
||||
shamt = str(self.pos)
|
||||
value_str = format(self.value, f"#0{self.size + 2}b")
|
||||
return value_str + " << " + shamt if self.pos else value_str
|
||||
|
||||
def coredsl(self) -> str:
|
||||
return format(self.value, f"#0{self.size + 2}b")
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return "Literal: " + self.coredsl()
|
||||
|
||||
|
||||
class Encoding:
|
||||
name: str
|
||||
parts: tuple[EncodingPart, ...]
|
||||
|
||||
def __init__(self, d: dict) -> None:
|
||||
s = d["decoding"]
|
||||
parts = s.split("|")
|
||||
idx = 0
|
||||
buf: list[EncodingPart] = []
|
||||
# reverse to determine position easier
|
||||
for part in reversed(parts):
|
||||
typed = Field(part) if "[" in part else Literal(part)
|
||||
typed.pos = idx
|
||||
idx += typed.size
|
||||
buf.append(typed)
|
||||
self.parts = tuple(reversed(buf))
|
||||
self.name = d["name"].upper()
|
||||
|
||||
def _get_masked_enc(self) -> str:
|
||||
masked_enc = "".join(
|
||||
[
|
||||
bin(elem.value)[2:].zfill(elem.size)
|
||||
if isinstance(elem, Literal)
|
||||
else elem.size * "x"
|
||||
for elem in self.parts
|
||||
]
|
||||
)
|
||||
return masked_enc
|
||||
|
||||
def _collapse_literals(self):
|
||||
new_parts: list[EncodingPart] = []
|
||||
for part in self.parts:
|
||||
if (
|
||||
isinstance(part, Literal)
|
||||
and new_parts
|
||||
and isinstance(new_parts[-1], Literal)
|
||||
):
|
||||
new_value = (new_parts[-1].value << part.size) + part.value
|
||||
new_size = new_parts[-1].size + part.size
|
||||
combined_val = format(new_value, f"#0{new_size + 2}b")
|
||||
collapsed = Literal(combined_val[2:])
|
||||
new_parts[-1] = collapsed
|
||||
else:
|
||||
new_parts.append(part)
|
||||
self.parts = tuple(new_parts)
|
||||
|
||||
def valid_size(self, target_size: int = -1):
|
||||
total_size = sum([part.size for part in self.parts])
|
||||
if target_size == -1:
|
||||
enc_str = self._get_masked_enc()
|
||||
if enc_str[-2:] != "11":
|
||||
target_size = 16
|
||||
elif enc_str[-4:-2] == "111":
|
||||
# ILEN >32
|
||||
target_size = -1
|
||||
else:
|
||||
target_size = 32
|
||||
return total_size == target_size
|
||||
|
||||
def create_macro(self) -> str:
|
||||
def riscv_sort_key(name: str) -> tuple[int, Union[int, str]]:
|
||||
if name == "rd":
|
||||
return (0, 0)
|
||||
elif name.startswith("rs") and name[2:].isdigit():
|
||||
return (1, int(name[2:]))
|
||||
else:
|
||||
return (2, name)
|
||||
|
||||
field_names = [elem.name for elem in self.parts if isinstance(elem, Field)]
|
||||
unique_field_names = list(dict.fromkeys(field_names))
|
||||
fields_str = ", ".join(sorted(unique_field_names, key=riscv_sort_key))
|
||||
header = f".macro {self.name}{',' if len(unique_field_names) > 0 else ''} {fields_str}"
|
||||
indent = " "
|
||||
|
||||
comment = "# Encoding parts: " + " ".join(
|
||||
[elem.coredsl() for elem in self.parts]
|
||||
)
|
||||
self._collapse_literals()
|
||||
strs = [str(elem) for elem in self.parts]
|
||||
content = ".word " + " | ".join(strs)
|
||||
|
||||
tail = ".endm"
|
||||
return "\n".join([header, indent + comment, indent + content, tail])
|
||||
|
||||
|
||||
def parse_args():
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Generate assembler macros from CoreDSL2JSON output."
|
||||
)
|
||||
parser.add_argument(
|
||||
"path", type=Path, help="Path to the JSON file generated by CoreDSL2JSON."
|
||||
)
|
||||
parser.add_argument(
|
||||
"--name",
|
||||
type=str,
|
||||
help="Name of the instruction to generate the macro for (optional).",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--size",
|
||||
type=int,
|
||||
default=-1,
|
||||
help="Instruction size in bits. If not set, checks lowest bits of the instruction and determines size according to default RISC-V specification.",
|
||||
)
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def load_and_validate(path: Path):
|
||||
data = json.loads(path.read_text())
|
||||
try:
|
||||
validate(instance=data, schema=INSTRUCTION_SCHEMA)
|
||||
except ValidationError as e:
|
||||
raise ValueError(f"Invalid JSON format: {e.message}") from e
|
||||
return data
|
||||
|
||||
|
||||
def main():
|
||||
args = parse_args()
|
||||
data = load_and_validate(args.path)
|
||||
for instruction in data["instructions"]:
|
||||
if args.name and args.name.upper() != instruction["name"]:
|
||||
continue
|
||||
enc = Encoding(instruction)
|
||||
if not enc.valid_size(args.size):
|
||||
print(f"Invalid size for {enc.name}")
|
||||
continue
|
||||
print(enc.create_macro())
|
||||
print()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@@ -0,0 +1 @@
|
||||
jsonschema
|
Reference in New Issue
Block a user