# All files / src/generator index.js

97.94% Statements 95/97
83.87% Branches 26/31
100% Functions 20/20
97.89% Lines 93/95

    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
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    21x
    21x
    21x
    21x
    21x
    21x
    21x
    21x
    21x
    21x
    21x
    21x
    21x
    21x

    21x

    21x

    133x

    133x
    133x
    133x

    133x

    133x

    83x

    133x
    132x
    132x

    132x

    67x

    67x
    133x

    133x

    264x
    55x

    264x

    264x

    67x
    7x

    67x
    67x

    214x
    47x

    47x
    27x
    27x

    47x

    47x
    47x

    67x
    47x

    125x
    125x

    8x
    8x
    8x

    6x
    6x

    2x
    2x

    157x
    157x
    19x

    9x

    133x
    133x
    133x

    96x
    96x

    37x

    133x

    57x
    57x

    57x

    7x
    7x
    7x
    7x
    7x
    6x
    6x

    7x

    133x
    133x

    133x

    133x

    132x
    13x

    13x
    13x

    6x

    11x

    67x

    66x

    66x

     // @flow
    import invariant from "invariant";
    import Syntax from "../Syntax";
    import mapSyntax from "./map-syntax";
    import mergeBlock from "./merge-block";
    import walkNode from "../utils/walk-node";
    import mapNode from "../utils/map-node";
    import generateElement from "./element";
    import generateExport from "./export";
    import generateMemory from "./memory";
    import generateTable from "./table";
    import generateInitializer from "../generator/initializer";
    import generateImport from "./import";
    import generateType from "./type";
    import { generateValueType } from "./utils";
    import { generateImplicitFunctionType } from "./type";
    import {
      GLOBAL_INDEX,
      FUNCTION_INDEX,
      FUNCTION_METADATA,
      TYPE_INDEX,
    } from "../semantics/metadata";

    import type { NodeType, ConfigType } from "../flow/types";
    import type {
      ProgramType,
      IntermediateOpcodeType,
      IntermediateVariableType,
    } from "./flow/types";

    export const generateCode = (
      func: NodeType
    ): { code: IntermediateOpcodeType[], locals: IntermediateVariableType[] } => {
      // eslint-disable-next-line
      const [argsNode, resultNode, ...body] = func.params;

      const metadata = func.meta[FUNCTION_METADATA];
      invariant(body, "Cannot generate code for function without body");
      invariant(metadata, "Cannot generate code for function without metadata");

      const { locals, argumentsCount } = metadata;

      const block = {
        code: [],
        // On this Episode of ECMAScript Spec: Object own keys traversal!
        // Sometimes it pays to know the spec. Keys are traversed in the order
        // they are added to the object. This includes Object.keys. Because the AST is traversed
        // depth-first we can guarantee that arguments will also be added first
        // to the locals object. We can depend on the spec providing the keys,
        // such that we can slice away the number of arguments and get DECLARED locals _only_.
        locals: Object.keys(locals)
          .slice(argumentsCount)
          .map(key => generateValueType(locals[key])),
        debug: `Function ${func.value}`,
      };

      // NOTE: Declarations have a side-effect of changing the local count
      //       This is why mapSyntax takes a parent argument
      const mappedSyntax = body.map(mapSyntax(block));
      Eif (mappedSyntax) {
        block.code = mappedSyntax.reduce(mergeBlock, []);
      }

      return block;
    };

    export default function generator(
      ast: NodeType,
      config: ConfigType
    ): ProgramType {
      const program: ProgramType = {
        Version: config.version,
        Types: [],
        Code: [],
        Exports: [],
        Imports: [],
        Globals: [],
        Element: [],
        Functions: [],
        Memory: [],
        Table: [],
        Artifacts: [],
        Name: {
          module: config.filename,
          functions: [],
          locals: [],
        },
      };

      const findTypeIndex = (functionNode: NodeType): number => {
        const search = generateImplicitFunctionType(functionNode);

        return program.Types.findIndex(t => {
          const paramsMatch =
            t.params.length === search.params.length &&
            t.params.reduce((a, v, i) => a && v === search.params[i], true);

          const resultMatch = t.result === search.result;

          return paramsMatch && resultMatch;
        });
      };

      const findTableIndex = functionIndex =>
        program.Element.findIndex(n => n.functionIndex === functionIndex);

      const typeMap = {};
      const astWithTypes = mapNode({
        [Syntax.Typedef]: (node, _ignore) => {
          let typeIndex = program.Types.findIndex(({ id }) => id === node.value);
          let typeNode = program.Types[typeIndex];

          if (typeNode == null) {
            typeIndex = program.Types.length;
            program.Types.push(generateType(node));
          }

          typeNode = {
            ...node,
            meta: { ...node.meta, [TYPE_INDEX]: typeIndex },
          };

          typeMap[node.value] = { typeIndex, typeNode };
          return typeNode;
        },
      })(ast);

      const nodeMap = {
        [Syntax.Typedef]: (_, __) => _,
        [Syntax.Export]: node => {
          const [nodeToExport] = node.params;
          program.Exports.push(generateExport(nodeToExport));
        },
        [Syntax.ImmutableDeclaration]: node => {
          const globalMeta = node.meta[GLOBAL_INDEX];
          Eif (globalMeta != null) {
            switch (node.type) {
              case "Memory":
                program.Memory.push(generateMemory(node));
                break;
              case "Table":
                program.Table.push(generateTable(node));
                break;
              default:
                program.Globals.push(generateInitializer(node));
            }
          }
        },
        [Syntax.Declaration]: node => {
          const globalMeta = node.meta[GLOBAL_INDEX];
          if (globalMeta != null) {
            program.Globals.push(generateInitializer(node));
          }
        },
        [Syntax.Import]: node => {
          program.Imports.push(...generateImport(node));
        },
        [Syntax.FunctionDeclaration]: node => {
          const typeIndex = (() => {
            const index = findTypeIndex(node);
            if (index === -1) {
              // attach to a type index
              program.Types.push(generateImplicitFunctionType(node));
              return program.Types.length - 1;
            }

            return index;
          })();

          const patched = mapNode({
            [Syntax.Type]: typeNode => {
              const userDefinedType = typeMap[typeNode.value];
              Iif (userDefinedType != null) {
                return {
                  ...typeNode,
                  meta: { ...typeNode.meta, [TYPE_INDEX]: userDefinedType.index },
                };
              }

              return typeNode;
            },
            [Syntax.FunctionPointer]: pointer => {
              const metaFunctionIndex = pointer.meta[FUNCTION_INDEX];
              Eif (metaFunctionIndex) {
                const functionIndex = metaFunctionIndex;
                let tableIndex = findTableIndex(functionIndex);
                if (tableIndex < 0) {
                  tableIndex = program.Element.length;
                  program.Element.push(generateElement(functionIndex));
                }
              }
              return pointer;
            },
          })(node);

          // Quick fix for shifting around function indices. These don't necessarily
          // get written in the order they appear in the source code.
          const index = node.meta[FUNCTION_INDEX];
          invariant(index != null, "Function index must be set");

          program.Functions[index] = typeIndex;
          // We will need to filter out the empty slots later
          program.Code[index] = generateCode(patched);

          if (config.encodeNames) {
            program.Name.functions.push({
              index,
              name: node.value,
            });
            const functionMetadata = node.meta[FUNCTION_METADATA];
            if (
              functionMetadata != null &&
              Object.keys(functionMetadata.locals).length
            ) {
              program.Name.locals[index] = {
                index,
                locals: Object.entries(functionMetadata.locals).map(
                  ([name, local]: [string, any]) => {
                    return {
                      name,
                      index: Number(local.meta["local/index"]),
                    };
                  }
                ),
              };
            }
          }
        },
      };

      walkNode(nodeMap)(astWithTypes);

      // Unlike function indexes we need function bodies to be exact
      program.Code = program.Code.filter(Boolean);

      return program;
    }