@@ -46,147 +46,6 @@ import {
46
46
ElementKind
47
47
} from "./program" ;
48
48
49
- /** Initializes the specified program with built-in constants and functions. */
50
- export function initialize ( program : Program ) : void {
51
-
52
- // math
53
- addConstant ( program , "NaN" , Type . f64 ) ;
54
- addConstant ( program , "Infinity" , Type . f64 ) ;
55
-
56
- addFunction ( program , "isNaN" , true ) ;
57
- addFunction ( program , "isFinite" , true ) ;
58
- addFunction ( program , "clz" , true ) ;
59
- addFunction ( program , "ctz" , true ) ;
60
- addFunction ( program , "popcnt" , true ) ;
61
- addFunction ( program , "rotl" , true ) ;
62
- addFunction ( program , "rotr" , true ) ;
63
- addFunction ( program , "abs" , true ) ;
64
- addFunction ( program , "max" , true ) ;
65
- addFunction ( program , "min" , true ) ;
66
- addFunction ( program , "ceil" , true ) ;
67
- addFunction ( program , "floor" , true ) ;
68
- addFunction ( program , "copysign" , true ) ;
69
- addFunction ( program , "nearest" , true ) ;
70
- addFunction ( program , "reinterpret" , true ) ;
71
- addFunction ( program , "sqrt" , true ) ;
72
- addFunction ( program , "trunc" , true ) ;
73
-
74
- // memory access
75
- addFunction ( program , "load" , true ) ;
76
- addFunction ( program , "store" , true ) ;
77
- addFunction ( program , "sizeof" , true ) ;
78
-
79
- // control flow
80
- addFunction ( program , "select" , true ) ;
81
- addFunction ( program , "unreachable" ) ;
82
-
83
- // host operations
84
- addFunction ( program , "current_memory" ) ;
85
- addFunction ( program , "grow_memory" ) ;
86
- // addFunction(program, "move_memory");
87
- // addFunction(program, "set_memory");
88
-
89
- // other
90
- addFunction ( program , "changetype" , true ) ;
91
- addFunction ( program , "assert" ) ;
92
-
93
- // abort is special in that it is imported conditionally. for example, when
94
- // compiling with noAssert=true, it isn't necessary that it is present, that
95
- // is if a user doesn't call it manually.
96
- var abortPrototype = addFunction ( program , "abort" ) ;
97
- abortPrototype . set ( ElementFlags . DECLARED ) ;
98
- abortPrototype . instances . set ( "" , new Function ( abortPrototype , "abort" , null , [
99
- new Parameter ( null , program . options . usizeType ) , // message (string)
100
- new Parameter ( null , program . options . usizeType ) , // file name (string)
101
- new Parameter ( null , Type . u32 ) , // line number
102
- new Parameter ( null , Type . u32 ) // column number
103
- ] , Type . void , null ) ) ;
104
-
105
- // conversions and limits
106
- var i32Func : FunctionPrototype ,
107
- u32Func : FunctionPrototype ,
108
- i64Func : FunctionPrototype ,
109
- u64Func : FunctionPrototype ;
110
- addFunction ( program , "i8" ) . members = new Map ( [
111
- [ "MIN_VALUE" , new Global ( program , "MIN_VALUE" , "i8.MIN_VALUE" , null , Type . i8 ) . withConstantIntegerValue ( - 128 , - 1 ) ] ,
112
- [ "MAX_VALUE" , new Global ( program , "MAX_VALUE" , "i8.MAX_VALUE" , null , Type . i8 ) . withConstantIntegerValue ( 127 , 0 ) ]
113
- ] ) ;
114
- addFunction ( program , "i16" ) . members = new Map ( [
115
- [ "MIN_VALUE" , new Global ( program , "MIN_VALUE" , "i16.MIN_VALUE" , null , Type . i16 ) . withConstantIntegerValue ( - 32768 , - 1 ) ] ,
116
- [ "MAX_VALUE" , new Global ( program , "MAX_VALUE" , "i16.MAX_VALUE" , null , Type . i16 ) . withConstantIntegerValue ( 32767 , 0 ) ]
117
- ] ) ;
118
- ( i32Func = addFunction ( program , "i32" ) ) . members = new Map ( [
119
- [ "MIN_VALUE" , new Global ( program , "MIN_VALUE" , "i32.MIN_VALUE" , null , Type . i32 ) . withConstantIntegerValue ( - 2147483648 , - 1 ) ] ,
120
- [ "MAX_VALUE" , new Global ( program , "MAX_VALUE" , "i32.MAX_VALUE" , null , Type . i32 ) . withConstantIntegerValue ( 2147483647 , 0 ) ]
121
- ] ) ;
122
- ( i64Func = addFunction ( program , "i64" ) ) . members = new Map ( [
123
- [ "MIN_VALUE" , new Global ( program , "MIN_VALUE" , "i64.MIN_VALUE" , null , Type . i64 ) . withConstantIntegerValue ( 0 , - 2147483648 ) ] ,
124
- [ "MAX_VALUE" , new Global ( program , "MAX_VALUE" , "i64.MAX_VALUE" , null , Type . i64 ) . withConstantIntegerValue ( - 1 , 2147483647 ) ]
125
- ] ) ;
126
- addFunction ( program , "u8" ) . members = new Map ( [
127
- [ "MIN_VALUE" , new Global ( program , "MIN_VALUE" , "u8.MIN_VALUE" , null , Type . u8 ) . withConstantIntegerValue ( 0 , 0 ) ] ,
128
- [ "MAX_VALUE" , new Global ( program , "MAX_VALUE" , "u8.MAX_VALUE" , null , Type . u8 ) . withConstantIntegerValue ( 255 , 0 ) ]
129
- ] ) ;
130
- addFunction ( program , "u16" ) . members = new Map ( [
131
- [ "MIN_VALUE" , new Global ( program , "MIN_VALUE" , "u16.MIN_VALUE" , null , Type . u16 ) . withConstantIntegerValue ( 0 , 0 ) ] ,
132
- [ "MAX_VALUE" , new Global ( program , "MAX_VALUE" , "u16.MAX_VALUE" , null , Type . u16 ) . withConstantIntegerValue ( 65535 , 0 ) ]
133
- ] ) ;
134
- ( u32Func = addFunction ( program , "u32" ) ) . members = new Map ( [
135
- [ "MIN_VALUE" , new Global ( program , "MIN_VALUE" , "u32.MIN_VALUE" , null , Type . u32 ) . withConstantIntegerValue ( 0 , 0 ) ] ,
136
- [ "MAX_VALUE" , new Global ( program , "MAX_VALUE" , "u32.MAX_VALUE" , null , Type . u32 ) . withConstantIntegerValue ( - 1 , 0 ) ]
137
- ] ) ;
138
- ( u64Func = addFunction ( program , "u64" ) ) . members = new Map ( [
139
- [ "MIN_VALUE" , new Global ( program , "MIN_VALUE" , "u64.MIN_VALUE" , null , Type . u64 ) . withConstantIntegerValue ( 0 , 0 ) ] ,
140
- [ "MAX_VALUE" , new Global ( program , "MAX_VALUE" , "u64.MAX_VALUE" , null , Type . i64 ) . withConstantIntegerValue ( - 1 , - 1 ) ]
141
- ] ) ;
142
- addFunction ( program , "bool" ) . members = new Map ( [
143
- [ "MIN_VALUE" , new Global ( program , "MIN_VALUE" , "bool.MIN_VALUE" , null , Type . bool ) . withConstantIntegerValue ( 0 , 0 ) ] ,
144
- [ "MAX_VALUE" , new Global ( program , "MAX_VALUE" , "bool.MAX_VALUE" , null , Type . bool ) . withConstantIntegerValue ( 1 , 0 ) ]
145
- ] ) ;
146
- addFunction ( program , "f32" ) . members = new Map ( [
147
- [ "MIN_VALUE" , new Global ( program , "MIN_VALUE" , "f32.MIN_VALUE" , null , Type . f32 ) . withConstantFloatValue ( - 3.40282347e+38 ) ] ,
148
- [ "MAX_VALUE" , new Global ( program , "MAX_VALUE" , "f32.MAX_VALUE" , null , Type . f32 ) . withConstantFloatValue ( 3.40282347e+38 ) ] ,
149
- [ "MIN_SAFE_INTEGER" , new Global ( program , "MIN_SAFE_INTEGER" , "f32.MIN_SAFE_INTEGER" , null , Type . f32 ) . withConstantFloatValue ( - 16777215 ) ] ,
150
- [ "MAX_SAFE_INTEGER" , new Global ( program , "MAX_SAFE_INTEGER" , "f32.MAX_SAFE_INTEGER" , null , Type . f32 ) . withConstantFloatValue ( 16777215 ) ] ,
151
- [ "EPSILON" , new Global ( program , "EPSILON" , "f32.EPSILON" , null , Type . f32 ) . withConstantFloatValue ( 1.19209290e-07 ) ]
152
- ] ) ;
153
- addFunction ( program , "f64" ) . members = new Map ( [
154
- [ "MIN_VALUE" , new Global ( program , "MIN_VALUE" , "f64.MIN_VALUE" , null , Type . f64 ) . withConstantFloatValue ( - 1.7976931348623157e+308 ) ] ,
155
- [ "MAX_VALUE" , new Global ( program , "MAX_VALUE" , "f64.MAX_VALUE" , null , Type . f64 ) . withConstantFloatValue ( 1.7976931348623157e+308 ) ] ,
156
- [ "MIN_SAFE_INTEGER" , new Global ( program , "MIN_SAFE_INTEGER" , "f64.MIN_SAFE_INTEGER" , null , Type . f64 ) . withConstantFloatValue ( - 9007199254740991 ) ] ,
157
- [ "MAX_SAFE_INTEGER" , new Global ( program , "MAX_SAFE_INTEGER" , "f64.MAX_SAFE_INTEGER" , null , Type . f64 ) . withConstantFloatValue ( 9007199254740991 ) ] ,
158
- [ "EPSILON" , new Global ( program , "EPSILON" , "f64.EPSILON" , null , Type . f64 ) . withConstantFloatValue ( 2.2204460492503131e-16 ) ]
159
- ] ) ;
160
- if ( program . options . isWasm64 ) {
161
- program . elements . set ( "isize" , i64Func ) ;
162
- program . elements . set ( "usize" , u64Func ) ;
163
- addConstant ( program , "HEAP_BASE" , Type . usize64 ) ;
164
- } else {
165
- program . elements . set ( "isize" , i32Func ) ;
166
- program . elements . set ( "usize" , u32Func ) ;
167
- addConstant ( program , "HEAP_BASE" , Type . usize32 ) ;
168
- }
169
- }
170
-
171
- /** Adds a built-in constant to the specified program. */
172
- function addConstant ( program : Program , name : string , type : Type ) : Global {
173
- var global = new Global ( program , name , name , null , type ) ;
174
- global . set ( ElementFlags . BUILTIN ) ;
175
- global . set ( ElementFlags . CONSTANT ) ;
176
- program . elements . set ( name , global ) ;
177
- return global ;
178
- }
179
-
180
- /** Adds a built-in function to the specified program. */
181
- function addFunction ( program : Program , name : string , isGeneric : bool = false ) : FunctionPrototype {
182
- var prototype = new FunctionPrototype ( program , name , name , null , null ) ;
183
- prototype . set ( ElementFlags . BUILTIN ) ;
184
- if ( isGeneric )
185
- prototype . set ( ElementFlags . GENERIC ) ;
186
- program . elements . set ( name , prototype ) ;
187
- return prototype ;
188
- }
189
-
190
49
/** Compiles a get of a built-in global. */
191
50
export function compileGetConstant ( compiler : Compiler , global : Global , reportNode : Node ) : ExpressionRef {
192
51
switch ( global . internalName ) { // switching on strings should become a compiler optimization eventually
@@ -204,7 +63,8 @@ export function compileGetConstant(compiler: Compiler, global: Global, reportNod
204
63
return compiler . module . createF64 ( Infinity ) ;
205
64
206
65
case "HEAP_BASE" : // constant, but never inlined
207
- return compiler . module . createGetGlobal ( "HEAP_BASE" , ( compiler . currentType = < Type > global . type ) . toNativeType ( ) ) ;
66
+ compiler . currentType = compiler . options . usizeType ;
67
+ return compiler . module . createGetGlobal ( "HEAP_BASE" , compiler . options . nativeSizeType ) ;
208
68
}
209
69
compiler . error ( DiagnosticCode . Operation_not_supported , reportNode . range ) ;
210
70
return compiler . module . createUnreachable ( ) ;
0 commit comments