CoreDSL2JSON/com.minres.coredsl.json/src/com/minres/coredsl/json/CoreDslJsonGenerator.xtend

112 lines
4.0 KiB
Plaintext
Raw Normal View History

2021-09-25 16:22:15 +02:00
/*
* generated by Xtext 2.13.0
*/
package com.minres.coredsl.json
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.AbstractGenerator
import org.eclipse.xtext.generator.IFileSystemAccess2
import org.eclipse.xtext.generator.IGeneratorContext
import com.minres.coredsl.coreDsl.CoreDef
import com.minres.coredsl.coreDsl.Instruction
import com.minres.coredsl.coreDsl.InstructionSet
import org.apache.log4j.Logger
import com.minres.coredsl.coreDsl.Encoding
import com.minres.coredsl.coreDsl.BitField
import com.minres.coredsl.coreDsl.BitValue
import java.util.List
import com.minres.coredsl.coreDsl.ISA
import org.json.JSONObject
import org.json.JSONArray
import com.minres.coredsl.util.BigIntegerWithRadix
2021-09-26 15:27:52 +02:00
import com.minres.coredsl.coreDsl.Statement
2022-03-06 15:00:14 +01:00
import org.eclipse.xtext.resource.XtextResource
2021-09-25 16:22:15 +02:00
/**
* Generates code from your model files on save.
*
* See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#code-generation
*/
class CoreDslJsonGenerator extends AbstractGenerator {
2021-09-26 15:27:52 +02:00
2021-09-25 16:22:15 +02:00
val logger = Logger.getLogger(typeof(CoreDslJsonGenerator));
override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
for (e : resource.allContents.toIterable.filter(CoreDef)) {
val root = new JSONObject()
root.put('instructions', e.compile)
2022-03-06 15:00:14 +01:00
fsa.generateFile(e.name + ".json", root.toString(2))
2021-09-25 16:22:15 +02:00
}
}
def Boolean isHls(Instruction inst){
val instrSet = inst.eContainer as ISA;
2022-03-06 15:00:14 +01:00
!(inst.attributes.filter[it.type=='hls'].isEmpty &&
instrSet.commonInstructionAttributes.filter[it.type=='hls'].isEmpty)
2021-09-25 16:22:15 +02:00
}
def JSONArray compile(CoreDef coreDef) {
val insts = coreDef.allInstr
val enabled_insts = insts.filter[it.isHls]
val res = enabled_insts.map[ inst | inst.jsonDescr ].toList
return res.length>0?new JSONArray(res) :new JSONArray()
}
def JSONObject jsonDescr(Instruction inst){
val ret = new JSONObject();
2022-03-06 15:00:14 +01:00
ret.put("decoding", inst.encoding.fields.map[it.asString].join('|'))
2021-09-25 16:22:15 +02:00
ret.put("name", inst.name);
2022-03-06 15:00:14 +01:00
ret.put('disassembly', inst.assembly !== null? inst.name.toLowerCase + ' ' + inst.assembly.toLowerCase : inst.name)
2021-09-26 15:27:52 +02:00
ret.put('execution', inst.behavior.source)
2021-09-25 16:22:15 +02:00
ret.put('restrictions', '')
}
2021-09-26 15:27:52 +02:00
def String getSource(Statement stmt){
2022-03-06 15:00:14 +01:00
(stmt.eResource as XtextResource).serializer.serialize(stmt)
2021-09-26 15:27:52 +02:00
}
2021-09-25 16:22:15 +02:00
def Iterable<Instruction> allInstr(CoreDef core) {
val unique = newLinkedHashMap
2022-05-08 13:54:27 +02:00
val instrList = if (core.providedInstructionSets.size == 0)
2021-09-25 16:22:15 +02:00
core.instructions
else {
2022-05-08 13:54:27 +02:00
val instrSets = core.providedInstructionSets?.map[InstructionSet i|i.allInstructionSets].flatten
2021-09-25 16:22:15 +02:00
val seen = newLinkedHashSet
seen.addAll(instrSets)
seen.map[InstructionSet i|i.instructions].flatten
}
for (Instruction i : instrList) {
if (i.eContainer instanceof InstructionSet)
logger.trace("adding instruction " + i.name + " of " + (i.eContainer as InstructionSet).name)
if (i.eContainer instanceof CoreDef)
logger.trace("adding instruction " + i.name + " of " + (i.eContainer as CoreDef).name)
unique.put(i.name, i)
}
val instLut = newLinkedHashMap()
for (Instruction i : unique.values) {
logger.trace("adding encoding " + i.encoding.bitEncoding + " for instruction " + i.name)
instLut.put(i.encoding.bitEncoding, i)
}
return instLut.values
}
def List<InstructionSet> allInstructionSets(InstructionSet core) {
val s = if(core.superType !== null) core.superType.allInstructionSets else newLinkedList
s.add(core)
return s
}
def String getBitEncoding(Encoding encoding) '''«FOR field : encoding.fields»«field.regEx»«ENDFOR»'''
2022-05-08 13:54:27 +02:00
def dispatch getRegEx(BitField i) '''«FOR idx : i.startIndex.value.intValue .. i.endIndex.value.intValue».«ENDFOR»'''
2021-09-25 16:22:15 +02:00
def dispatch getRegEx(BitValue i) '''«i.value.toString(2)»'''
2022-05-08 13:54:27 +02:00
def dispatch asString(BitField i) '''«i.name»[«i.startIndex.value.intValue»:«i.endIndex.value.intValue»]'''
2021-09-25 16:22:15 +02:00
def dispatch asString(BitValue i) {
(i.value as BigIntegerWithRadix).toCString(2)
}
}