phasm/phasm/build/default.py
Johan B.W. de Vries aa30ead730 Moves the prelude to runtime
Previously, it was hardcoded at 'compile' time (in as much
Python has that). This would make it more difficult to add
stuff to it. Also, in a lot of places we made assumptions
about prelude instead of checking properly.
2025-05-29 13:19:07 +02:00

80 lines
2.6 KiB
Python

"""
The default class for build environments.
Contains the compiler builtins as well as some sane defaults.
# Added types
f32: A 32-bits IEEE 754 float, of 32 bits width.
"""
from ..type3.types import (
Type3,
TypeApplication_Nullary,
)
from ..wasm import (
WasmTypeFloat32,
WasmTypeFloat64,
WasmTypeInt32,
WasmTypeInt64,
)
from ..wasmgenerator import Generator
from .base import BuildBase, TypeInfo
from .typeclasses import bits, eq, natnum, ord
class BuildDefault(BuildBase[Generator]):
def __init__(self) -> None:
super().__init__()
u8 = Type3('u8', TypeApplication_Nullary(None, None))
u16 = Type3('u16', TypeApplication_Nullary(None, None))
u32 = Type3('u32', TypeApplication_Nullary(None, None))
u64 = Type3('u64', TypeApplication_Nullary(None, None))
i8 = Type3('i8', TypeApplication_Nullary(None, None))
i16 = Type3('i16', TypeApplication_Nullary(None, None))
i32 = Type3('i32', TypeApplication_Nullary(None, None))
i64 = Type3('i64', TypeApplication_Nullary(None, None))
f32 = Type3('f32', TypeApplication_Nullary(None, None))
f64 = Type3('f64', TypeApplication_Nullary(None, None))
bytes_ = self.dynamic_array(u8)
self.type_info_map.update({
'u8': TypeInfo('u8', WasmTypeInt32, 'i32.load8_u', 'i32.store8', 1, False),
'u16': TypeInfo('u16', WasmTypeInt32, 'i32.load16_u', 'i32.store16', 2, False),
'u32': TypeInfo('u32', WasmTypeInt32, 'i32.load', 'i32.store', 4, False),
'u64': TypeInfo('u64', WasmTypeInt64, 'i64.load', 'i64.store', 8, False),
'i8': TypeInfo('i8', WasmTypeInt32, 'i32.load8_s', 'i32.store8', 1, True),
'i16': TypeInfo('i16', WasmTypeInt32, 'i32.load16_s', 'i32.store16', 2, True),
'i32': TypeInfo('i32', WasmTypeInt32, 'i32.load', 'i32.store', 4, True),
'i64': TypeInfo('i64', WasmTypeInt64, 'i64.load', 'i64.store', 8, True),
'f32': TypeInfo('f32', WasmTypeFloat32, 'f32.load', 'f32.store', 4, None),
'f64': TypeInfo('f64', WasmTypeFloat64, 'f64.load', 'f64.store', 8, None),
})
self.types.update({
'u8': u8,
'u16': u16,
'u32': u32,
'u64': u64,
'i8': i8,
'i16': i16,
'i32': i32,
'i64': i64,
'f32': f32,
'f64': f64,
'bytes': bytes_,
})
tc_list = [
bits,
eq, ord,
natnum,
]
for tc in tc_list:
tc.load(self)
for tc in tc_list:
tc.wasm(self)