c799ef0bf6acd41deebf820c73f00ac6e1635aefbdeec6f4e6ac5ca80a1dfc193409d677e28a8811837dcc981918cd397c5ba0339b27b3374e2a55e34cbcc1 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // TODO(sven): add flow in here
  2. import { isSignature, isNumberLiteral } from "@webassemblyjs/ast";
  3. import { assert } from "mamacro";
  4. export function moduleContextFromModuleAST(m) {
  5. const moduleContext = new ModuleContext();
  6. assert(m.type === "Module");
  7. m.fields.forEach(field => {
  8. switch (field.type) {
  9. case "Start": {
  10. moduleContext.setStart(field.index);
  11. break;
  12. }
  13. case "TypeInstruction": {
  14. moduleContext.addType(field);
  15. break;
  16. }
  17. case "Func": {
  18. moduleContext.addFunction(field);
  19. break;
  20. }
  21. case "Global": {
  22. moduleContext.defineGlobal(field);
  23. break;
  24. }
  25. case "ModuleImport": {
  26. switch (field.descr.type) {
  27. case "GlobalType": {
  28. moduleContext.importGlobal(
  29. field.descr.valtype,
  30. field.descr.mutability
  31. );
  32. break;
  33. }
  34. case "Memory": {
  35. moduleContext.addMemory(
  36. field.descr.limits.min,
  37. field.descr.limits.max
  38. );
  39. break;
  40. }
  41. case "FuncImportDescr": {
  42. moduleContext.importFunction(field.descr);
  43. break;
  44. }
  45. case "Table": {
  46. // FIXME(sven): not implemented yet
  47. break;
  48. }
  49. default:
  50. throw new Error(
  51. "Unsupported ModuleImport of type " +
  52. JSON.stringify(field.descr.type)
  53. );
  54. }
  55. break;
  56. }
  57. case "Memory": {
  58. moduleContext.addMemory(field.limits.min, field.limits.max);
  59. break;
  60. }
  61. }
  62. });
  63. return moduleContext;
  64. }
  65. /**
  66. * Module context for type checking
  67. */
  68. export class ModuleContext {
  69. constructor() {
  70. this.funcs = [];
  71. this.funcsOffsetByIdentifier = [];
  72. this.types = [];
  73. this.globals = [];
  74. this.globalsOffsetByIdentifier = [];
  75. this.mems = [];
  76. // Current stack frame
  77. this.locals = [];
  78. this.labels = [];
  79. this.return = [];
  80. this.debugName = "unknown";
  81. this.start = null;
  82. }
  83. /**
  84. * Set start segment
  85. */
  86. setStart(index) {
  87. this.start = index.value;
  88. }
  89. /**
  90. * Get start function
  91. */
  92. getStart() {
  93. return this.start;
  94. }
  95. /**
  96. * Reset the active stack frame
  97. */
  98. newContext(debugName, expectedResult) {
  99. this.locals = [];
  100. this.labels = [expectedResult];
  101. this.return = expectedResult;
  102. this.debugName = debugName;
  103. }
  104. /**
  105. * Functions
  106. */
  107. addFunction(func /*: Func*/) {
  108. // eslint-disable-next-line prefer-const
  109. let { params: args = [], results: result = [] } = func.signature || {};
  110. args = args.map(arg => arg.valtype);
  111. this.funcs.push({ args, result });
  112. if (typeof func.name !== "undefined") {
  113. this.funcsOffsetByIdentifier[func.name.value] = this.funcs.length - 1;
  114. }
  115. }
  116. importFunction(funcimport) {
  117. if (isSignature(funcimport.signature)) {
  118. // eslint-disable-next-line prefer-const
  119. let { params: args, results: result } = funcimport.signature;
  120. args = args.map(arg => arg.valtype);
  121. this.funcs.push({ args, result });
  122. } else {
  123. assert(isNumberLiteral(funcimport.signature));
  124. const typeId = funcimport.signature.value;
  125. assert(this.hasType(typeId));
  126. const signature = this.getType(typeId);
  127. this.funcs.push({
  128. args: signature.params.map(arg => arg.valtype),
  129. result: signature.results
  130. });
  131. }
  132. if (typeof funcimport.id !== "undefined") {
  133. // imports are first, we can assume their index in the array
  134. this.funcsOffsetByIdentifier[funcimport.id.value] = this.funcs.length - 1;
  135. }
  136. }
  137. hasFunction(index) {
  138. return typeof this.getFunction(index) !== "undefined";
  139. }
  140. getFunction(index) {
  141. if (typeof index !== "number") {
  142. throw new Error("getFunction only supported for number index");
  143. }
  144. return this.funcs[index];
  145. }
  146. getFunctionOffsetByIdentifier(name) {
  147. assert(typeof name === "string");
  148. return this.funcsOffsetByIdentifier[name];
  149. }
  150. /**
  151. * Labels
  152. */
  153. addLabel(result) {
  154. this.labels.unshift(result);
  155. }
  156. hasLabel(index) {
  157. return this.labels.length > index && index >= 0;
  158. }
  159. getLabel(index) {
  160. return this.labels[index];
  161. }
  162. popLabel() {
  163. this.labels.shift();
  164. }
  165. /**
  166. * Locals
  167. */
  168. hasLocal(index) {
  169. return typeof this.getLocal(index) !== "undefined";
  170. }
  171. getLocal(index) {
  172. return this.locals[index];
  173. }
  174. addLocal(type) {
  175. this.locals.push(type);
  176. }
  177. /**
  178. * Types
  179. */
  180. addType(type) {
  181. assert(type.functype.type === "Signature");
  182. this.types.push(type.functype);
  183. }
  184. hasType(index) {
  185. return this.types[index] !== undefined;
  186. }
  187. getType(index) {
  188. return this.types[index];
  189. }
  190. /**
  191. * Globals
  192. */
  193. hasGlobal(index) {
  194. return this.globals.length > index && index >= 0;
  195. }
  196. getGlobal(index) {
  197. return this.globals[index].type;
  198. }
  199. getGlobalOffsetByIdentifier(name) {
  200. assert(typeof name === "string");
  201. return this.globalsOffsetByIdentifier[name];
  202. }
  203. defineGlobal(global /*: Global*/) {
  204. const type = global.globalType.valtype;
  205. const mutability = global.globalType.mutability;
  206. this.globals.push({ type, mutability });
  207. if (typeof global.name !== "undefined") {
  208. this.globalsOffsetByIdentifier[global.name.value] =
  209. this.globals.length - 1;
  210. }
  211. }
  212. importGlobal(type, mutability) {
  213. this.globals.push({ type, mutability });
  214. }
  215. isMutableGlobal(index) {
  216. return this.globals[index].mutability === "var";
  217. }
  218. isImmutableGlobal(index) {
  219. return this.globals[index].mutability === "const";
  220. }
  221. /**
  222. * Memories
  223. */
  224. hasMemory(index) {
  225. return this.mems.length > index && index >= 0;
  226. }
  227. addMemory(min, max) {
  228. this.mems.push({ min, max });
  229. }
  230. getMemory(index) {
  231. return this.mems[index];
  232. }
  233. }