1
+ import { initialize as initializeBuiltins } from "./builtins" ;
1
2
import { Target } from "./compiler" ;
2
3
import { GETTER_PREFIX , SETTER_PREFIX , PATH_DELIMITER } from "./constants" ;
3
- import { DiagnosticCode , DiagnosticMessage , DiagnosticEmitter , DiagnosticCategory } from "./diagnostics" ;
4
+ import { DiagnosticCode , DiagnosticMessage , DiagnosticEmitter } from "./diagnostics" ;
4
5
import { Type , typesToString } from "./types" ;
5
6
import { I64 } from "./util" ;
6
7
import {
@@ -57,7 +58,7 @@ class QueuedImport {
57
58
declaration : ImportDeclaration ;
58
59
}
59
60
60
- const reusableTypesStub : Map < string , Type > = new Map ( ) ;
61
+ const noTypesYet : Map < string , Type > = new Map ( ) ;
61
62
62
63
export class Program extends DiagnosticEmitter {
63
64
@@ -70,7 +71,7 @@ export class Program extends DiagnosticEmitter {
70
71
/** Elements by internal name. */
71
72
elements : Map < string , Element > = new Map ( ) ;
72
73
/** Types by internal name. */
73
- types : Map < string , Type > = reusableTypesStub ;
74
+ types : Map < string , Type > = noTypesYet ;
74
75
/** Exports of individual files by internal name. Not global exports. */
75
76
exports : Map < string , Element > = new Map ( ) ;
76
77
@@ -82,6 +83,22 @@ export class Program extends DiagnosticEmitter {
82
83
/** Initializes the program and its elements prior to compilation. */
83
84
initialize ( target : Target = Target . WASM32 ) : void {
84
85
this . target = target ;
86
+ this . types = new Map ( [
87
+ [ "i8" , Type . i8 ] ,
88
+ [ "i16" , Type . i16 ] ,
89
+ [ "i32" , Type . i32 ] ,
90
+ [ "i64" , Type . i64 ] ,
91
+ [ "isize" , target == Target . WASM64 ? Type . isize64 : Type . isize32 ] ,
92
+ [ "u8" , Type . u8 ] ,
93
+ [ "u16" , Type . u16 ] ,
94
+ [ "u32" , Type . u32 ] ,
95
+ [ "u64" , Type . u64 ] ,
96
+ [ "usize" , target == Target . WASM64 ? Type . usize64 : Type . usize32 ] ,
97
+ [ "bool" , Type . bool ] ,
98
+ [ "f32" , Type . f32 ] ,
99
+ [ "f64" , Type . f64 ] ,
100
+ [ "void" , Type . void ]
101
+ ] ) ;
85
102
86
103
initializeBuiltins ( this ) ;
87
104
@@ -1110,120 +1127,3 @@ export class Interface extends Class {
1110
1127
super ( template , internalName , typeArguments , base ) ;
1111
1128
}
1112
1129
}
1113
-
1114
- const builtinIntTypes : Type [ ] = [ Type . i32 , Type . i64 ] ;
1115
- const builtinFloatTypes : Type [ ] = [ Type . f32 , Type . f64 ] ;
1116
-
1117
- function initializeBuiltins ( program : Program ) : void {
1118
-
1119
- // types
1120
-
1121
- program . types = new Map ( [
1122
- [ "i8" , Type . i8 ] ,
1123
- [ "i16" , Type . i16 ] ,
1124
- [ "i32" , Type . i32 ] ,
1125
- [ "i64" , Type . i64 ] ,
1126
- [ "isize" , program . target == Target . WASM64 ? Type . isize64 : Type . isize32 ] ,
1127
- [ "u8" , Type . u8 ] ,
1128
- [ "u16" , Type . u16 ] ,
1129
- [ "u32" , Type . u32 ] ,
1130
- [ "u64" , Type . u64 ] ,
1131
- [ "usize" , program . target == Target . WASM64 ? Type . usize64 : Type . usize32 ] ,
1132
- [ "bool" , Type . bool ] ,
1133
- [ "f32" , Type . f32 ] ,
1134
- [ "f64" , Type . f64 ] ,
1135
- [ "void" , Type . void ]
1136
- ] ) ;
1137
-
1138
- // functions
1139
-
1140
- const usize : Type = program . target == Target . WASM64 ? Type . usize64 : Type . usize32 ;
1141
-
1142
- addGenericUnaryBuiltin ( program , "clz" , builtinIntTypes ) ;
1143
- addGenericUnaryBuiltin ( program , "ctz" , builtinIntTypes ) ;
1144
- addGenericUnaryBuiltin ( program , "popcnt" , builtinIntTypes ) ;
1145
- addGenericBinaryBuiltin ( program , "rotl" , builtinIntTypes ) ;
1146
- addGenericBinaryBuiltin ( program , "rotr" , builtinIntTypes ) ;
1147
-
1148
- addGenericUnaryBuiltin ( program , "abs" , builtinFloatTypes ) ;
1149
- addGenericUnaryBuiltin ( program , "ceil" , builtinFloatTypes ) ;
1150
- addGenericBinaryBuiltin ( program , "copysign" , builtinFloatTypes ) ;
1151
- addGenericUnaryBuiltin ( program , "floor" , builtinFloatTypes ) ;
1152
- addGenericBinaryBuiltin ( program , "max" , builtinFloatTypes ) ;
1153
- addGenericBinaryBuiltin ( program , "min" , builtinFloatTypes ) ;
1154
- addGenericUnaryBuiltin ( program , "nearest" , builtinFloatTypes ) ;
1155
- addGenericUnaryBuiltin ( program , "sqrt" , builtinFloatTypes ) ;
1156
- addGenericUnaryBuiltin ( program , "trunc" , builtinFloatTypes ) ;
1157
-
1158
- addSimpleBuiltin ( program , "current_memory" , [ ] , usize ) ;
1159
- addSimpleBuiltin ( program , "grow_memory" , [ usize ] , usize ) ;
1160
- addSimpleBuiltin ( program , "unreachable" , [ ] , Type . void ) ;
1161
-
1162
- addGenericAnyBuiltin ( program , "load" ) ;
1163
- addGenericAnyBuiltin ( program , "store" ) ;
1164
- addGenericAnyBuiltin ( program , "reinterpret" ) ;
1165
- addGenericAnyBuiltin ( program , "select" ) ;
1166
-
1167
- addGenericAnyBuiltin ( program , "sizeof" ) ;
1168
- addGenericUnaryTestBuiltin ( program , "isNaN" , builtinFloatTypes ) ;
1169
- addGenericUnaryTestBuiltin ( program , "isFinite" , builtinFloatTypes ) ;
1170
- addSimpleBuiltin ( program , "assert" , [ Type . bool ] , Type . void ) ;
1171
- }
1172
-
1173
- /** Adds a simple (non-generic) builtin. */
1174
- function addSimpleBuiltin ( program : Program , name : string , parameterTypes : Type [ ] , returnType : Type ) {
1175
- let prototype : FunctionPrototype = new FunctionPrototype ( program , name , null , null ) ;
1176
- prototype . isGeneric = false ;
1177
- prototype . isBuiltin = true ;
1178
- const k : i32 = parameterTypes . length ;
1179
- const parameters : Parameter [ ] = new Array ( k ) ;
1180
- for ( let i : i32 = 0 ; i < k ; ++ i )
1181
- parameters [ i ] = new Parameter ( "arg" + i , parameterTypes [ i ] , null ) ;
1182
- prototype . instances . set ( "" , new Function ( prototype , name , [ ] , parameters , returnType , null ) ) ;
1183
- program . elements . set ( name , prototype ) ;
1184
- }
1185
-
1186
- /** Adds a generic unary builtin that takes and returns a value of its generic type. */
1187
- function addGenericUnaryBuiltin ( program : Program , name : string , types : Type [ ] ) : void {
1188
- let prototype : FunctionPrototype = new FunctionPrototype ( program , name , null , null ) ;
1189
- prototype . isGeneric = true ;
1190
- prototype . isBuiltin = true ;
1191
- for ( let i : i32 = 0 , k = types . length ; i < k ; ++ i ) {
1192
- const typeName : string = types [ i ] . toString ( ) ;
1193
- prototype . instances . set ( typeName , new Function ( prototype , name + "<" + typeName + ">" , [ types [ i ] ] , [ new Parameter ( "value" , types [ i ] , null ) ] , types [ i ] , null ) ) ;
1194
- }
1195
- program . elements . set ( name , prototype ) ;
1196
- }
1197
-
1198
- /** Adds a generic binary builtin that takes two and returns a value of its generic type. */
1199
- function addGenericBinaryBuiltin ( program : Program , name : string , types : Type [ ] ) : void {
1200
- let prototype : FunctionPrototype = new FunctionPrototype ( program , name , null , null ) ;
1201
- prototype . isGeneric = true ;
1202
- prototype . isBuiltin = true ;
1203
- for ( let i : i32 = 0 , k = types . length ; i < k ; ++ i ) {
1204
- const typeName : string = types [ i ] . toString ( ) ;
1205
- prototype . instances . set ( typeName , new Function ( prototype , name + "<" + typeName + ">" , [ types [ i ] , types [ i ] ] , [ new Parameter ( "left" , types [ i ] , null ) , new Parameter ( "right" , types [ i ] , null ) ] , types [ i ] , null ) ) ;
1206
- }
1207
- program . elements . set ( name , prototype ) ;
1208
- }
1209
-
1210
- /** Adds a generic unary builtin that alwways returns `bool`. */
1211
- function addGenericUnaryTestBuiltin ( program : Program , name : string , types : Type [ ] ) : void {
1212
- let prototype : FunctionPrototype = new FunctionPrototype ( program , name , null , null ) ;
1213
- prototype . isGeneric = true ;
1214
- prototype . isBuiltin = true ;
1215
- for ( let i : i32 = 0 , k = types . length ; i < k ; ++ i ) {
1216
- const typeName : string = types [ i ] . toString ( ) ;
1217
- prototype . instances . set ( typeName , new Function ( prototype , name + "<" + typeName + ">" , [ types [ i ] ] , [ new Parameter ( "value" , types [ i ] , null ) ] , Type . bool , null ) ) ;
1218
- }
1219
- program . elements . set ( name , prototype ) ;
1220
- }
1221
-
1222
- /** Adds a special generic builtin that takes any type argument. */
1223
- function addGenericAnyBuiltin ( program : Program , name : string ) : void {
1224
- let prototype : FunctionPrototype = new FunctionPrototype ( program , name , null , null ) ;
1225
- prototype . isGeneric = true ;
1226
- prototype . isBuiltin = true ;
1227
- program . elements . set ( name , prototype ) ;
1228
- // instances are hard coded in compiler.ts
1229
- }
0 commit comments