mirror of
https://github.com/Minres/RDL-Editor.git
synced 2025-07-01 21:43:26 +02:00
Fixed NPE in generator
This commit is contained in:
@ -0,0 +1,157 @@
|
||||
package com.minres.rdl
|
||||
|
||||
import com.minres.rdl.rdl.ComponentInstance
|
||||
import com.minres.rdl.rdl.ComponentDefinition
|
||||
import com.minres.rdl.rdl.ExplicitPropertyAssignment
|
||||
import com.minres.rdl.rdl.PropertyAssignment
|
||||
import com.minres.rdl.rdl.Instantiation
|
||||
import com.minres.rdl.rdl.ComponentDefinitionType
|
||||
import com.minres.rdl.rdl.PropertyEnum
|
||||
import com.minres.rdl.rdl.PropertyAssignmentRhs
|
||||
import com.minres.rdl.rdl.RValue
|
||||
import com.minres.rdl.rdl.RValueConstant
|
||||
import com.minres.rdl.rdl.InstancePropertyRef
|
||||
|
||||
class RdlUtil {
|
||||
|
||||
static def IntegerWithRadix addressValue(ComponentInstance instance) {
|
||||
if (instance.address !== null)
|
||||
return instance.address as IntegerWithRadix
|
||||
else
|
||||
return new IntegerWithRadix(0)
|
||||
}
|
||||
|
||||
def long accessWidth(ComponentDefinition definition){
|
||||
var size = 32L
|
||||
val pa = definition.propertyAssignments.findFirst[PropertyAssignment pa |
|
||||
pa instanceof ExplicitPropertyAssignment && (pa as ExplicitPropertyAssignment).name==PropertyEnum.ACCESSWIDTH
|
||||
]
|
||||
if(pa !== null){
|
||||
val sz = new IntegerWithRadix((pa as ExplicitPropertyAssignment).rhs.effectiveValue)
|
||||
size=sz.value
|
||||
}
|
||||
return size
|
||||
}
|
||||
|
||||
static def long regWidth(ComponentDefinition definition){
|
||||
var size = 32L
|
||||
val pa = definition.propertyAssignments.findFirst[PropertyAssignment pa |
|
||||
pa instanceof ExplicitPropertyAssignment && (pa as ExplicitPropertyAssignment).name==PropertyEnum.REGWIDTH
|
||||
]
|
||||
if(pa !== null){
|
||||
val sz = new IntegerWithRadix((pa as ExplicitPropertyAssignment).rhs.effectiveValue)
|
||||
size=sz.value
|
||||
}
|
||||
return size
|
||||
}
|
||||
|
||||
static def long getSize(Instantiation instantiation){
|
||||
val componentDef= instantiation.definingComponent
|
||||
switch (componentDef.type) {
|
||||
case ComponentDefinitionType.REG: {
|
||||
val pa = componentDef.propertyAssignments.findFirst[PropertyAssignment pa |
|
||||
pa instanceof ExplicitPropertyAssignment && (pa as ExplicitPropertyAssignment).name==PropertyEnum.REGWIDTH
|
||||
]
|
||||
if(pa !== null){
|
||||
val sz = new IntegerWithRadix((pa as ExplicitPropertyAssignment).rhs.effectiveValue)
|
||||
return sz.value
|
||||
}
|
||||
return 32L
|
||||
}
|
||||
case ComponentDefinitionType.FIELD:{
|
||||
val pa = componentDef.propertyAssignments.findFirst[PropertyAssignment pa |
|
||||
pa instanceof ExplicitPropertyAssignment && (pa as ExplicitPropertyAssignment).name==PropertyEnum.FIELDWIDTH
|
||||
]
|
||||
if(pa !== null){
|
||||
val sz = new IntegerWithRadix((pa as ExplicitPropertyAssignment).rhs.effectiveValue)
|
||||
return sz.value
|
||||
}
|
||||
return 1L
|
||||
}
|
||||
default: {
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static def String effectiveName(ComponentDefinition definition){
|
||||
if(definition.name!==null){
|
||||
return definition.name.replaceAll('\\s+', '_')
|
||||
} else{
|
||||
val pa = definition.propertyAssignments.findFirst[PropertyAssignment pa |
|
||||
pa instanceof ExplicitPropertyAssignment && (pa as ExplicitPropertyAssignment).name==PropertyEnum.NAME
|
||||
]
|
||||
if(pa !== null)
|
||||
return (pa as ExplicitPropertyAssignment).rhs.effectiveValue.replaceAll('\\s+', '_')
|
||||
else
|
||||
return ('unnamed_'+definition.type).replaceAll('\\s+', '_')
|
||||
}
|
||||
}
|
||||
|
||||
static def String effectiveValue(PropertyAssignmentRhs rhs){
|
||||
if(rhs.value!== null)
|
||||
rhs.value.effectiveValue
|
||||
else if(rhs.instPropRef!==null)
|
||||
rhs.instPropRef.effectiveValue
|
||||
else if(rhs.enumRef!==null)
|
||||
rhs.enumRef.name
|
||||
}
|
||||
|
||||
static def String effectiveValue(RValue rvalue){
|
||||
if(rvalue.str!==null){
|
||||
rvalue.str
|
||||
} else if(rvalue.^val!=RValueConstant.UNDEFINED)
|
||||
rvalue.^val.literal
|
||||
else if(rvalue.num!==null){
|
||||
val num = rvalue.num as IntegerWithRadix
|
||||
num.toString
|
||||
}
|
||||
}
|
||||
|
||||
static def String effectiveValue(InstancePropertyRef ref){
|
||||
throw new RuntimeException()
|
||||
}
|
||||
|
||||
static def ComponentDefinition definingComponent(Instantiation instantiation){
|
||||
if(instantiation.componentRef!==null) instantiation.componentRef else instantiation.component
|
||||
}
|
||||
|
||||
static def int instanceCount(ComponentDefinition definition, ComponentDefinitionType type){
|
||||
definition.instantiationsOfType(type).map[it.componentInstances.size].reduce[p1, p2| p1+p2]
|
||||
}
|
||||
|
||||
static def instantiationsOfType(ComponentDefinition definition, ComponentDefinitionType type){
|
||||
definition.instantiations.filter[it.definingComponent.type == type]
|
||||
}
|
||||
|
||||
static def long byteSize(Instantiation instantiation, long start){
|
||||
val componentDefinition = instantiation.definingComponent
|
||||
var long componentSize=0;
|
||||
if(instantiation.definingComponent.type == ComponentDefinitionType.REG){
|
||||
componentSize=instantiation.definingComponent.regWidth/8
|
||||
} else
|
||||
for(subInstantiation: componentDefinition.instantiations)
|
||||
componentSize = subInstantiation.byteSize(componentSize)
|
||||
|
||||
var long lastTopAddress = start
|
||||
var long topAddress=start
|
||||
for(componentInstance: instantiation.componentInstances ){
|
||||
val byteSize = if(componentInstance.address !== null) (componentInstance.address as IntegerWithRadix).value+componentSize else componentSize + lastTopAddress
|
||||
topAddress = Math.max(topAddress, byteSize)
|
||||
lastTopAddress = byteSize
|
||||
}
|
||||
return topAddress
|
||||
}
|
||||
|
||||
static def long byteSize(Instantiation instantiation){
|
||||
val componentDefinition = instantiation.definingComponent
|
||||
var long componentSize=0;
|
||||
if(instantiation.definingComponent.type == ComponentDefinitionType.REG){
|
||||
componentSize=instantiation.definingComponent.regWidth/8
|
||||
} else
|
||||
for(subInstantiation: componentDefinition.instantiations)
|
||||
componentSize = subInstantiation.byteSize(componentSize)
|
||||
return componentSize
|
||||
}
|
||||
|
||||
}
|
@ -4,6 +4,10 @@ import com.minres.rdl.generator.RdlBaseGenerator
|
||||
import com.minres.rdl.rdl.ComponentDefinition
|
||||
import com.minres.rdl.IntegerWithRadix
|
||||
import com.minres.rdl.rdl.ComponentDefinitionType
|
||||
import com.minres.rdl.rdl.ComponentInstance
|
||||
import com.minres.rdl.rdl.Instantiation
|
||||
|
||||
import static extension com.minres.rdl.RdlUtil.*
|
||||
|
||||
class AddrmapGenerator extends RdlBaseGenerator {
|
||||
|
||||
@ -14,21 +18,21 @@ class AddrmapGenerator extends RdlBaseGenerator {
|
||||
}
|
||||
|
||||
override generateHeader() {'''
|
||||
#ifndef _E300_PLAT_MAP_H_
|
||||
#define _E300_PLAT_MAP_H_
|
||||
#ifndef _«componentDefinition.effectiveName.toUpperCase»_MAP_H_
|
||||
#define _«componentDefinition.effectiveName.toUpperCase»_MAP_H_
|
||||
// need double braces, see https://stackoverflow.com/questions/6893700/how-to-construct-stdarray-object-with-initializer-list#6894191
|
||||
const std::array<sysc::target_memory_map_entry<32>, «componentDefinition.instanceCount(ComponentDefinitionType.REGFILE)»> e300_plat_map = {{
|
||||
const std::array<sysc::target_memory_map_entry<32>, «componentDefinition.instanceCount(ComponentDefinitionType.REGFILE)»> «componentDefinition.effectiveName»_map = {{
|
||||
«FOR instantiation : componentDefinition.instantiationsOfType(ComponentDefinitionType.REGFILE)»
|
||||
«FOR instance : instantiation.componentInstances»
|
||||
{&i_«instance.name», 0x«Long.toHexString((instance.address as IntegerWithRadix).value)», 0x«Long.toHexString(instantiation.byteSize)»},
|
||||
{&i_«instance.name», «instance.addressValue», 0x«Long.toHexString(instantiation.byteSize)»},
|
||||
«ENDFOR»
|
||||
«ENDFOR»
|
||||
}};
|
||||
|
||||
#endif /* _E300_PLAT_MAP_H_ */
|
||||
#endif /* _«componentDefinition.effectiveName.toUpperCase»_MAP_H_ */
|
||||
'''
|
||||
}
|
||||
|
||||
|
||||
override generateSource() {
|
||||
''
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ 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 static extension com.minres.rdl.RdlUtil.*
|
||||
|
||||
/**
|
||||
* Generates code from your model files on save.
|
||||
@ -22,13 +23,13 @@ class RDLGenerator extends AbstractGenerator {
|
||||
val gen = it.fileGenerator
|
||||
if(gen!==null){
|
||||
val header = gen.generateHeader
|
||||
if(header!==null && header.length>0) fsa.generateFile(it.name+'.h', fsa.outputConfig('incl-out'), header)
|
||||
if(header!==null && header.length>0) fsa.generateFile(it.effectiveName+'.h', fsa.outputConfig('incl-out'), header)
|
||||
val source = gen.generateSource
|
||||
if(source!==null && source.length>0) fsa.generateFile(it.name+'.cpp', fsa.outputConfig('src-out'), source)
|
||||
if(source!==null && source.length>0) fsa.generateFile(it.effectiveName+'.cpp', fsa.outputConfig('src-out'), source)
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
def RdlBaseGenerator fileGenerator(ComponentDefinition definition){
|
||||
switch(definition.type){
|
||||
case ComponentDefinitionType.REGFILE: new RegfileGenerator(definition)
|
||||
|
@ -13,138 +13,7 @@ import com.minres.rdl.rdl.InstancePropertyRef
|
||||
import com.minres.rdl.rdl.ComponentDefinitionType
|
||||
|
||||
abstract class RdlBaseGenerator {
|
||||
|
||||
def long accessWidth(ComponentDefinition definition){
|
||||
var size = 32L
|
||||
val pa = definition.propertyAssignments.findFirst[PropertyAssignment pa |
|
||||
pa instanceof ExplicitPropertyAssignment && (pa as ExplicitPropertyAssignment).name==PropertyEnum.ACCESSWIDTH
|
||||
]
|
||||
if(pa !== null){
|
||||
val sz = new IntegerWithRadix((pa as ExplicitPropertyAssignment).rhs.effectiveValue)
|
||||
size=sz.value
|
||||
}
|
||||
return size
|
||||
}
|
||||
|
||||
def long regWidth(ComponentDefinition definition){
|
||||
var size = 32L
|
||||
val pa = definition.propertyAssignments.findFirst[PropertyAssignment pa |
|
||||
pa instanceof ExplicitPropertyAssignment && (pa as ExplicitPropertyAssignment).name==PropertyEnum.REGWIDTH
|
||||
]
|
||||
if(pa !== null){
|
||||
val sz = new IntegerWithRadix((pa as ExplicitPropertyAssignment).rhs.effectiveValue)
|
||||
size=sz.value
|
||||
}
|
||||
return size
|
||||
}
|
||||
|
||||
def long getSize(Instantiation instantiation){
|
||||
val componentDef= instantiation.definingComponent
|
||||
switch (componentDef.type) {
|
||||
case ComponentDefinitionType.REG: {
|
||||
val pa = componentDef.propertyAssignments.findFirst[PropertyAssignment pa |
|
||||
pa instanceof ExplicitPropertyAssignment && (pa as ExplicitPropertyAssignment).name==PropertyEnum.REGWIDTH
|
||||
]
|
||||
if(pa !== null){
|
||||
val sz = new IntegerWithRadix((pa as ExplicitPropertyAssignment).rhs.effectiveValue)
|
||||
return sz.value
|
||||
}
|
||||
return 32L
|
||||
}
|
||||
case ComponentDefinitionType.FIELD:{
|
||||
val pa = componentDef.propertyAssignments.findFirst[PropertyAssignment pa |
|
||||
pa instanceof ExplicitPropertyAssignment && (pa as ExplicitPropertyAssignment).name==PropertyEnum.FIELDWIDTH
|
||||
]
|
||||
if(pa !== null){
|
||||
val sz = new IntegerWithRadix((pa as ExplicitPropertyAssignment).rhs.effectiveValue)
|
||||
return sz.value
|
||||
}
|
||||
return 1L
|
||||
}
|
||||
default: {
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def String effectiveName(ComponentDefinition definition){
|
||||
if(definition.name!==null){
|
||||
definition.name
|
||||
} else{
|
||||
val pa = definition.propertyAssignments.findFirst[PropertyAssignment pa |
|
||||
pa instanceof ExplicitPropertyAssignment && (pa as ExplicitPropertyAssignment).name==PropertyEnum.NAME
|
||||
]
|
||||
(pa as ExplicitPropertyAssignment).rhs.effectiveValue
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
def String effectiveValue(PropertyAssignmentRhs rhs){
|
||||
if(rhs.value!== null)
|
||||
rhs.value.effectiveValue
|
||||
else if(rhs.instPropRef!==null)
|
||||
rhs.instPropRef.effectiveValue
|
||||
else if(rhs.enumRef!==null)
|
||||
rhs.enumRef.name
|
||||
}
|
||||
|
||||
def String effectiveValue(RValue rvalue){
|
||||
if(rvalue.str!==null){
|
||||
rvalue.str
|
||||
} else if(rvalue.^val!=RValueConstant.UNDEFINED)
|
||||
rvalue.^val.literal
|
||||
else if(rvalue.num!==null){
|
||||
val num = rvalue.num as IntegerWithRadix
|
||||
num.toString
|
||||
}
|
||||
}
|
||||
|
||||
def String effectiveValue(InstancePropertyRef ref){
|
||||
throw new RuntimeException()
|
||||
}
|
||||
|
||||
def ComponentDefinition definingComponent(Instantiation instantiation){
|
||||
if(instantiation.componentRef!==null) instantiation.componentRef else instantiation.component
|
||||
}
|
||||
|
||||
def int instanceCount(ComponentDefinition definition, ComponentDefinitionType type){
|
||||
definition.instantiationsOfType(type).map[it.componentInstances.size].reduce[p1, p2| p1+p2]
|
||||
}
|
||||
|
||||
def instantiationsOfType(ComponentDefinition definition, ComponentDefinitionType type){
|
||||
definition.instantiations.filter[it.definingComponent.type == type]
|
||||
}
|
||||
|
||||
def long byteSize(Instantiation instantiation, long start){
|
||||
val componentDefinition = instantiation.definingComponent
|
||||
var long componentSize=0;
|
||||
if(instantiation.definingComponent.type == ComponentDefinitionType.REG){
|
||||
componentSize=instantiation.definingComponent.regWidth/8
|
||||
} else
|
||||
for(subInstantiation: componentDefinition.instantiations)
|
||||
componentSize = subInstantiation.byteSize(componentSize)
|
||||
|
||||
var long lastTopAddress = start
|
||||
var long topAddress=start
|
||||
for(componentInstance: instantiation.componentInstances ){
|
||||
val byteSize = if(componentInstance.address !== null) (componentInstance.address as IntegerWithRadix).value+componentSize else componentSize + lastTopAddress
|
||||
topAddress = Math.max(topAddress, byteSize)
|
||||
lastTopAddress = byteSize
|
||||
}
|
||||
return topAddress
|
||||
}
|
||||
|
||||
def long byteSize(Instantiation instantiation){
|
||||
val componentDefinition = instantiation.definingComponent
|
||||
var long componentSize=0;
|
||||
if(instantiation.definingComponent.type == ComponentDefinitionType.REG){
|
||||
componentSize=instantiation.definingComponent.regWidth/8
|
||||
} else
|
||||
for(subInstantiation: componentDefinition.instantiations)
|
||||
componentSize = subInstantiation.byteSize(componentSize)
|
||||
return componentSize
|
||||
}
|
||||
|
||||
|
||||
def String generateHeader()
|
||||
|
||||
def String generateSource()
|
||||
|
@ -8,6 +8,8 @@ import com.minres.rdl.rdl.Instantiation
|
||||
import java.util.Date
|
||||
import com.minres.rdl.rdl.Range
|
||||
|
||||
import static extension com.minres.rdl.RdlUtil.*
|
||||
|
||||
class RegfileGenerator extends RdlBaseGenerator{
|
||||
|
||||
val ComponentDefinition componentDefinition
|
||||
@ -145,7 +147,7 @@ class RegfileGenerator extends RdlBaseGenerator{
|
||||
inline void sysc::«componentDefinition.name»::registerResources(sysc::tlm_target<BUSWIDTH>& target) {
|
||||
«FOR instantiation : componentDefinition.instantiations»
|
||||
«FOR instance : instantiation.componentInstances»
|
||||
target.addResource(«instance.name», 0x«Long.toHexString((instance.address as IntegerWithRadix).value)»UL);
|
||||
target.addResource(«instance.name», «instance.addressValue»UL);
|
||||
«ENDFOR»
|
||||
«ENDFOR»
|
||||
}
|
||||
|
Reference in New Issue
Block a user