summaryrefslogtreecommitdiff
path: root/chapter32/src/test/java/com/seaofnodes/simple/AssemblyDumpTest.java
blob: 0e4c230a909cc537ce402290672fa9bc6e149e4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.seaofnodes.simple;

import com.seaofnodes.simple.codegen.CodeGen;
import com.seaofnodes.simple.type.*;
import com.seaofnodes.simple.util.BAOS;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HexFormat;
import org.junit.Test;
import static org.junit.Assert.*;

public class AssemblyDumpTest {
    @Test public void libraryUnitsAndGlyphs() throws Exception {
        Path root=Path.of("build/objs/assembly-dump");
        Files.createDirectories(root);
        Path build=Files.createTempDirectory(root,"sys-");
        CodeGen code=new CodeGen("src/main/smp",build.toString(),null,"sys",null,123,true,TypeInteger.BOT)
            .driver(CodeGen.Phase.Serialize,TestC.CPU_PORT,TestC.CALL_CONVENTION)
            .driver(CodeGen.Phase.Encoding,TestC.CPU_PORT,TestC.CALL_CONVENTION);
        var entry=code._encoding;
        String before=code.asm();
        assertSame("Dumping another unit must not change the active encoding",entry,code._encoding);
        for( var unit : code._compunits.values() )
            if( unit._src!=null ) {
                String heading="=== Compilation unit: "+unit._cname+" ===";
                assertEquals("Exactly one heading for "+unit._cname,2,before.split(heading,-1).length);
            }
        int start=before.indexOf("=== Compilation unit: sys.draw.Canvas ===");
        assertTrue(start>=0);
        int end=before.indexOf("=== Compilation unit:",start+1);
        String canvas=before.substring(start,end<0 ? before.length() : end);
        assertTrue(canvas.contains("Constant Pool"));
        assertTrue(canvas.contains("sys.draw.Canvas._glyphs: []u16"));
        TypeMemPtr ptr=(TypeMemPtr)code._compunits.get("sys/draw/Canvas")._clz.field("_glyphs")._t;
        TypeConAry glyphs=(TypeConAry)ptr._obj.field("[]")._t;
        assertEquals(96,glyphs.len());
        BAOS bytes=new BAOS();
        glyphs.write(bytes);
        assertTrue("The complete font follows its u32 length header",
                   canvas.contains("60000000"+HexFormat.of().formatHex(bytes.toByteArray())));
        code.exportELF(false,false);
        com.seaofnodes.simple.codegen.PrintRegTestSupport.checkDataAliases(
            build.resolve("sys/draw/Canvas.o"),196,"sys.draw.Canvas._glyphs");
        assertSame(entry,code._encoding);
        assertEquals("Export must not truncate the dump to its entry unit",before,code.asm());
        Files.writeString(build.resolve("sys.asm"),before);
    }

    @Test public void combinedEncodingAndEarlierPhase() {
        for( String cpu : new String[]{"x86_64_v2","arm","riscv"} ) {
            CodeGen code=new CodeGen("val a=new u16[~]{5,6,7}; return a[arg&1];")
                .driver(CodeGen.Phase.RegAlloc,cpu,"SystemV");
            assertFalse(code.asm().contains("Constant Pool"));
            code.driver(cpu,"SystemV",true,false);
            String dump=code.asm();
            assertFalse(dump.contains("=== Compilation unit:"));
            assertTrue(dump.contains("Constant Pool"));
            assertTrue(dump.contains("03000000050006000700"));
        }
    }
}