Skip to content

Riscv libc

riscv_libc

RiscvLibcFunctions dataclass

Bases: InterpreterFunctions

Source code in xdsl/interpreters/riscv_libc.py
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
62
63
64
@register_impls
class RiscvLibcFunctions(InterpreterFunctions):
    @impl_external("malloc")
    def malloc(
        self, interpreter: Interpreter, op: Operation, args: PythonValues
    ) -> PythonValues:
        assert len(args) == 1
        assert isinstance(args[0], int)
        size = args[0]
        if size % 4 != 0:
            # malloc a bit too much if not word-aligned
            size = ceil(size / 4) * 4

        # set values to 1 to signify uninitialized memory
        return (ptr.RawPtr.zeros(size),)

    @impl_external("calloc")
    def calloc(
        self, interpreter: Interpreter, op: Operation, args: PythonValues
    ) -> PythonValues:
        assert len(args) == 2
        assert isinstance(args[0], int)
        assert isinstance(args[1], int)
        num = args[0]
        size = args[1]

        num_bytes = num * size

        if num_bytes % 4 != 0:
            # malloc a bit too much if not word-aligned
            num_bytes = ceil(num_bytes / 4) * 4

        return (ptr.RawPtr.zeros(size),)

    @impl_external("free")
    def free(
        self, interpreter: Interpreter, op: Operation, args: PythonValues
    ) -> PythonValues:
        assert len(args) == 1
        assert isinstance(args[0], ptr.RawPtr)
        return ()

    @impl_external("putchar")
    def putchar(self, iterpreter: Interpreter, op: Operation, args: PythonValues):
        assert len(args) == 1
        char = args[0]
        if isinstance(char, int):
            iterpreter.print(chr(char), end="")
        else:
            iterpreter.print(char, end="")
        return (char,)

malloc(interpreter: Interpreter, op: Operation, args: PythonValues) -> PythonValues

Source code in xdsl/interpreters/riscv_libc.py
16
17
18
19
20
21
22
23
24
25
26
27
28
@impl_external("malloc")
def malloc(
    self, interpreter: Interpreter, op: Operation, args: PythonValues
) -> PythonValues:
    assert len(args) == 1
    assert isinstance(args[0], int)
    size = args[0]
    if size % 4 != 0:
        # malloc a bit too much if not word-aligned
        size = ceil(size / 4) * 4

    # set values to 1 to signify uninitialized memory
    return (ptr.RawPtr.zeros(size),)

calloc(interpreter: Interpreter, op: Operation, args: PythonValues) -> PythonValues

Source code in xdsl/interpreters/riscv_libc.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
@impl_external("calloc")
def calloc(
    self, interpreter: Interpreter, op: Operation, args: PythonValues
) -> PythonValues:
    assert len(args) == 2
    assert isinstance(args[0], int)
    assert isinstance(args[1], int)
    num = args[0]
    size = args[1]

    num_bytes = num * size

    if num_bytes % 4 != 0:
        # malloc a bit too much if not word-aligned
        num_bytes = ceil(num_bytes / 4) * 4

    return (ptr.RawPtr.zeros(size),)

free(interpreter: Interpreter, op: Operation, args: PythonValues) -> PythonValues

Source code in xdsl/interpreters/riscv_libc.py
48
49
50
51
52
53
54
@impl_external("free")
def free(
    self, interpreter: Interpreter, op: Operation, args: PythonValues
) -> PythonValues:
    assert len(args) == 1
    assert isinstance(args[0], ptr.RawPtr)
    return ()

putchar(iterpreter: Interpreter, op: Operation, args: PythonValues)

Source code in xdsl/interpreters/riscv_libc.py
56
57
58
59
60
61
62
63
64
@impl_external("putchar")
def putchar(self, iterpreter: Interpreter, op: Operation, args: PythonValues):
    assert len(args) == 1
    char = args[0]
    if isinstance(char, int):
        iterpreter.print(chr(char), end="")
    else:
        iterpreter.print(char, end="")
    return (char,)