forked from dllaurence/Nil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
system.llh
182 lines (139 loc) · 5.11 KB
/
system.llh
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#ifndef SYSTEM_LLH
#define SYSTEM_LLH
;**********************************************************************
; system.llh
;
; Public declarations of external objects--mainly C library functions
; and helper functions defined in system.c.
;
; Many of these are here for debugging.
;
; Copyright 2009-2010 by Dustin Laurence. Distributed under the terms of
; the LGPLv3.
;
;**********************************************************************
#include "c_defs.llh"
; ASCII codes (very handy since we don't have C's char constants)
; Whitespace
#define ASCII_TAB 9
#define ASCII_NEWLINE 10
#define ASCII_VTAB 11
#define ASCII_FORMFEED 12
#define ASCII_CR 13
#define ASCII_SPACE 32
; Symbols
#define ASCII_BANG 33
#define ASCII_DQUOTE 34
#define ASCII_HASH 35
#define ASCII_SQUOTE 39
#define ASCII_LPAREN 40
#define ASCII_RPAREN 41
#define ASCII_PLUS 43
#define ASCII_MINUS 45
#define ASCII_PERIOD 46
; Digits
#define ASCII_0 48
#define ASCII_1 49
#define ASCII_2 50
#define ASCII_3 51
#define ASCII_4 52
#define ASCII_5 53
#define ASCII_6 54
#define ASCII_7 55
#define ASCII_8 56
#define ASCII_9 57
; More Symbols
#define ASCII_COLON 58
#define ASCII_SEMICOLON 59
#define ASCII_LANGLE 60
#define ASCII_LT 60
#define ASCII_EQUAL 61
#define ASCII_RANGLE 62
#define ASCII_GT 62
; Majescules
#define ASCII_A 65
#define ASCII_B 66
#define ASCII_C 67
#define ASCII_D 68
#define ASCII_E 69
#define ASCII_L 76
#define ASCII_M 77
#define ASCII_N 78
#define ASCII_O 79
#define ASCII_P 80
#define ASCII_Q 81
#define ASCII_R 82
#define ASCII_S 83
; Miniscules
#define ASCII_a 97
#define ASCII_b 98
#define ASCII_c 99
#define ASCII_DEL 127
; Assertions
declare ccc void @abort() noreturn
; Convenience, and ensures we don't forget 'noreturn'
#define Abort() call ccc void @abort() noreturn
; These are defined in system_ll.llm
; Usage note: this facility is much less useful than in C, because there is
; no expression language. You must assign the i1 result of a test to a
; register manually, then pass it to assert(). We can't provide any help
; because the "non-hygenic macros" problem is worse than in C, and anything
; that uses a register will capture from the environment.
; Because of this, unlike in C the asserts do *not* go away if NDEBUG
; is defined. At each usage point you will have to do the #ifndef
; guard yourself, because you'll almost certainly have to put something
; to compute the test predicate anyway. Enjoy life without nested
; expressions.
declare ccc void @cant_happen_impl(%c_int %line) noreturn
#define cant_happen() call ccc void @cant_happen_impl(%c_int __LINE__) noreturn
declare ccc void @assert_impl(i1 %test, %c_int %line)
#define assert(test) call ccc void @assert_impl(i1 test, %c_int __LINE__)
declare ccc void @assert_false_impl(i1 %test, %c_int %line)
#define assert_false(test) \
call ccc void @assert_false_impl(i1 test, %c_int __LINE__)
; stdio
; libc
declare ccc %c_int @fputs(%c_char*, %c_char*)
declare ccc %c_int @putchar(%c_int)
declare ccc %c_int @puts(%c_char *)
declare ccc %c_int @getchar()
declare ccc %c_int @ungetc(%c_int, %c_char*)
declare ccc %c_int @feof(%c_char*)
declare ccc %c_int @ferror(%c_char*)
declare ccc %c_int @printf(%c_char*, ...)
; Additional I/O code in system_c.c
declare ccc %c_char* @getstdfileptr(%c_int)
; Additional I/O code in system_ll.llm
declare ccc %c_int @putstring(%c_char*) ; puts(), sans newline
; Print an int
declare ccc void @puti(%c_int)
; Print a long
declare ccc void @putl(%c_long)
; Print a pointer in hexadecimal
declare ccc void @putptr(i8*)
#if C_INTPTR_T_BITSOF == 64
#define PutWord(word) putl(word)
#elif C_INTPTR_T_BITSOF == 32
#define PutWord(word) puti(word)
#else
Die!!!
#endif
; We define our own codes, whether they match or not, to decouple
; our code from libc and because they aren't 1-1 anyway (IO_ERROR
; is covering anything reported by errno, for example)
#define IO_EOF -1
#define IO_ERROR -10
declare ccc void @ungetchar_asserted(%c_int %char)
declare ccc %c_int @getchar_checked()
; Malloc & friends
declare ccc i8* @malloc(%c_size_t %size)
; Strings
declare ccc %c_char* @strdup(%c_char*)
declare ccc %c_char* @strndup(%c_char*, %c_size_t %len)
declare ccc %c_int @strcmp(%c_char*, %c_char*)
declare ccc %c_size_t @strlen(%c_char* %string)
declare ccc %c_long @strtol(%c_char* %str, %c_char** %end, %c_int %base)
; Other libc
declare ccc void @exit(%c_int %status)
; Useful Macros
#endif