File: runtime.txt

package info (click to toggle)
harec 0.25.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,420 kB
  • sloc: ansic: 19,905; asm: 247; makefile: 116; lisp: 80; sh: 45
file content (77 lines) | stat: -rw-r--r-- 3,058 bytes parent folder | download
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
harec expects the runtime to provide some features under the "rt" namespace.

@symbol("rt.abort") fn _abort(path: *str, line: u64, col: u64, msg: str) void;
	Print a diagnostic message and terminate the program.

fn abort_fixed(path: *str, line: u64, col: u64, reason: u64) void;
	Print a diagnostic message from a list of pre-determined abort reasons,
	and terminate the program. The list of reasons are:

	0: Slice or array access out-of-bounds
	1: Type assertion failed
	2: Unreachable code
	3: Slice allocation capacity smaller than initializer
	4: Generic assertion failure without a message
	5: Error assertion failed

fn memcpy(dest: *opaque, src: *opaque, n: size) void;
	Copy "n" bytes from "src" to "dest". The memory areas shall not
	overlap.

fn memmove(dest: *opaque, src: *opaque, n: size) void;
	Copy "n" bytes from "src" to "dest". The memory areas may overlap.

fn memset(dest: *opaque, val: u8, n: size) void;
	Set "n" bytes, starting from the address at "dest", to "val".

fn strcmp(a: str, b: str) bool;
	Compare strings "a" and "b", returning true of they are equal.

"ensure" and "unensure" are called when slices are expanded or deleted. The
"length" field of the slice will have been updated, and ensure should allocate
or re-allocate the data field to have sufficient space, and update the capacity
field accordingly and return true. in the case that the re-allocation was not
successful, false should be returned and the slice should not be modified.
"unensure" is called when the space is no longer needed, and should shrink the
allocation as appropriate, if possible.

	type slice = struct {
		data: nullable *opaque,
		length: size,
		capacity: size,
	};

	fn ensure(s: *slice, membsz: size) bool;
	fn unensure(s: *slice, membsz: size) void;

"malloc" and "free" are required to support the "alloc" and "free" built-ins.

	fn malloc(n: size) nullable *opaque;

	@symbol("rt.free") fn free_(_p: nullable *opaque) void;

The runtime is also expected to provide startup code. A list of function
pointers of type `fn() void` is provided in the __init_array_start and
__fini_array_start globals, which are respectively terminated by
__init_array_end and __fini_array_end. The following Hare code will make these
globals available to the current unit:

	const @symbol("__init_array_start") init_start: [*]*fn() void;
	const @symbol("__init_array_end") init_end: [*]*fn() void;
	const @symbol("__fini_array_start") fini_start: [*]*fn() void;
	const @symbol("__fini_array_end") fini_end: [*]*fn() void;

When building with +test (harec -T), @test functions will be emitted, and an ELF
section, .test_array, will be populated similarly to init_array. The startup
code can enumerate the available tests like so:

	type test = struct {
		name: str,
		func: *fn() void,
	};

	const @symbol("__test_array_start") test_start: [*]test;
	const @symbol("__test_array_end") test_end: [*]test;

In order to use these symbols, a custom linker script must be used. A sample is
provided in rt/hare.sc which is compatible with GNU's ld and LLVM's lld.