CoreDSL2JSON/com.minres.coredsl.json.tests/src/com/minres/coredsl/json/tests/CoreDslGenerationUnitTest.x...

118 lines
3.8 KiB
Plaintext

package com.minres.coredsl.json.tests
import com.google.inject.Inject
import com.minres.coredsl.coreDsl.DescriptionContent
import org.eclipse.xtext.generator.GeneratorContext
import org.eclipse.xtext.generator.IGenerator2
import org.eclipse.xtext.generator.InMemoryFileSystemAccess
import org.eclipse.xtext.testing.InjectWith
import org.eclipse.xtext.testing.XtextRunner
import org.eclipse.xtext.testing.util.ParseHelper
import org.eclipse.xtext.testing.validation.ValidationTestHelper
import org.eclipse.xtext.util.CancelIndicator
import org.junit.Test
import org.junit.runner.RunWith
import static org.junit.Assert.*
import org.json.JSONObject
import org.json.JSONTokener
import org.json.JSONArray
@RunWith(XtextRunner)
@InjectWith(CoreDslInjectorProvider)
class CoreDslGenerationUnitTest{
@Inject IGenerator2 generator
@Inject extension ParseHelper<DescriptionContent> parseHelper
@Inject ValidationTestHelper validator
def CharSequence addInstructionContext(CharSequence str)'''
InstructionSet TestISA {
architectural_state {
unsigned XLEN;
const unsigned FLEN=32;
register unsigned<32> PC [[is_pc]];
register unsigned<32> X[32];
}
instructions {
«str»
}
}
Core TestCore provides TestISA {
architectural_state {
XLEN=32;
}
}
'''
@Test
def void genDisabledInstr() {
val content = '''
CLI {
encoding: 0b010 :: imm[5:5] :: rd[4:0] :: imm[4:0] :: 0b01;
behavior: {
if(rd == 0) //rd==0 is a hint, so no trap
X[rd] = (unsigned<32>)(signed)imm;
}
}
'''.addInstructionContext.parse
validator.assertNoErrors(content)
val ref = '''
void CLI(){
{
if(rd == 0) *(X+rd) = (uint32_t)(int32_t)sext<6>(imm);
}
}
'''
val fsa = new InMemoryFileSystemAccess()
generator.doGenerate(content.eResource, fsa, new GeneratorContext => [
cancelIndicator = CancelIndicator.NullImpl
])
val res = fsa.textFiles
res.forEach[name, text|
val jsonTokener = new JSONTokener(text.toString)
val json = new JSONObject(jsonTokener);
assertNotNull(json.get("instructions"))
assertTrue(json.get("instructions") instanceof JSONArray)
val instructions = json.get("instructions") as JSONArray
assertEquals(0, instructions.length)
]
}
@Test
def void genEnabledInstr() {
val content = '''
CLI [[hls]]{
encoding: 0b010 :: imm[5:5] :: rd[4:0] :: imm[4:0] :: 0b01;
behavior: {
if(rd == 0) //rd==0 is a hint, so no trap
X[rd] = (unsigned<32>)(signed)imm;
}
}
'''.addInstructionContext.parse
validator.assertNoErrors(content)
val ref = '''
void CLI(){
{
if(rd == 0) *(X+rd) = (uint32_t)(int32_t)sext<6>(imm);
}
}
'''
val fsa = new InMemoryFileSystemAccess()
generator.doGenerate(content.eResource, fsa, new GeneratorContext => [
cancelIndicator = CancelIndicator.NullImpl
])
val res = fsa.textFiles
res.forEach[name, text|
val jsonTokener = new JSONTokener(text.toString)
val json = new JSONObject(jsonTokener);
assertNotNull(json.get("instructions"))
assertTrue(json.get("instructions") instanceof JSONArray)
val instructions = json.get("instructions") as JSONArray
assertEquals(1, instructions.length)
]
}
}