Skip to content

Commit

Permalink
add/fix some intrinsics and prelude stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
p7g committed Sep 2, 2023
1 parent ad06d27 commit 59cf2bc
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
7 changes: 6 additions & 1 deletion joe/intrinsics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from joe.codegen import CompileContext
from joe.eval import BoundMethod, BoundTypeConstructor
from joe.intrinsics import file, pointer
from joe.intrinsics import file, pointer, string

_Factories: TypeAlias = Mapping[
str, Mapping[str, Callable[[CompileContext, BoundMethod], ir.Function]]
Expand All @@ -21,6 +21,11 @@
},
"joe.prelude.Pointer": {
"deref<>": pointer.make_pointer_deref,
"asArray<>": pointer.make_pointer_as_array,
},
"joe.prelude.String": {
"asPointer<>": string.make_string_as_pointer,
"length<>": string.make_string_length,
},
}

Expand Down
36 changes: 34 additions & 2 deletions joe/intrinsics/pointer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

def make_pointer_deref(ctx: CompileContext, deref_method: BoundMethod) -> ir.Function:
assert isinstance(deref_method.self_type, BoundType)
file_llvm_type = _type_to_llvm(ctx.ir_module, deref_method.self_type)
pointer_llvm_type = _type_to_llvm(ctx.ir_module, deref_method.self_type)

deref_ty = _method_to_llvm_type(ctx.ir_module, file_llvm_type, deref_method, True)
deref_ty = _method_to_llvm_type(
ctx.ir_module, pointer_llvm_type, deref_method, False
)
deref = ir.Function(ctx.ir_module, deref_ty, name=deref_method.name())
this_arg, offset_arg = deref.args

Expand All @@ -17,3 +19,33 @@ def make_pointer_deref(ctx: CompileContext, deref_method: BoundMethod) -> ir.Fun
ir_builder.ret(ir_builder.load(ptr))

return deref


def make_pointer_as_array(
ctx: CompileContext, as_array_method: BoundMethod
) -> ir.Function:
assert isinstance(as_array_method.self_type, BoundType)
pointer_llvm_type = _type_to_llvm(ctx.ir_module, as_array_method.self_type)

as_array_ty = _method_to_llvm_type(
ctx.ir_module, pointer_llvm_type, as_array_method, False
)
as_array = ir.Function(ctx.ir_module, as_array_ty, name=as_array_method.name())
this_arg, length_arg = as_array.args

ir_builder = ir.IRBuilder(as_array.append_basic_block(name="entry"))
array_ty = _type_to_llvm(ctx.ir_module, as_array_method.get_return_type())
malloc = ctx.get_malloc()
array = ir_builder.bitcast(
ir_builder.call(
malloc, [ir.IntType(64)(array_ty.get_abi_size(ctx.get_target_data()))]
),
array_ty,
)
data_ptr = ir_builder.gep(array, [ir.IntType(32)(0), ir.IntType(32)(0)])
ir_builder.store(this_arg, data_ptr)
length_ptr = ir_builder.gep(array, [ir.IntType(32)(0), ir.IntType(32)(1)])
ir_builder.store(length_arg, length_ptr)
ir_builder.ret(array)

return as_array
18 changes: 16 additions & 2 deletions lib/joe/prelude.joe
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,19 @@ class Long implements IInteger {}
class Unsigned implements IInteger {}
class UnsignedLong implements IInteger {}

class String {}
class String {
UnsignedLong length() {}
Pointer<Byte> asPointer() {}

class Pointer<T> {}
Array<Byte> asByteArray() {
return asPointer().asArray(length() + 1);
}
}

class Pointer<T> {
T _deref(UnsignedLong index) {}
Array<T> asArray(UnsignedLong length) {}
}

class Array<T> {
Pointer<T> _elements;
Expand All @@ -35,6 +45,10 @@ class ArrayList<T> {
return _array[index];
}

void set(Long index, T newValue) {
_array[index] = newValue;
}

Long push(T value) {
_maybeGrow();
_array[length] = value;
Expand Down

0 comments on commit 59cf2bc

Please sign in to comment.