File size: 10,295 Bytes
3ce1a40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**
 * Recursive-descent parser for the CUDA-like kernel language.
 * Produces the AST in ../ast/ast.js. Every failure raises ParseError with a line.
 */

import { tokenize, TokenType } from './lexer.js';

export class ParseError extends Error {
  constructor(message, line) {
    super(`${message} (line ${line})`);
    this.name = 'ParseError';
    this.line = line;
  }
}

const SCALARS = new Set(['int', 'uint', 'float', 'double', 'void']);
const BUILTINS = new Set([
  'blockIdx', 'blockDim', 'threadIdx', 'gridDim',
  'global_id', 'local_id', 'block_id', 'global_id_x', 'local_id_x',
]);

/** Binary operator precedence, higher binds tighter. Follows C. */
const PRECEDENCE = {
  '||': 1, '&&': 2,
  '|': 3, '^': 4, '&': 5,
  '==': 6, '!=': 6,
  '<': 7, '>': 7, '<=': 7, '>=': 7,
  '<<': 8, '>>': 8,
  '+': 9, '-': 9,
  '*': 10, '/': 10, '%': 10,
};

const ASSIGN_OPS = new Set(['=', '+=', '-=', '*=', '/=', '%=']);

/** Maps CUDA-style builtins onto the backend-neutral IR names. */
const BUILTIN_MEMBERS = {
  blockIdx: { x: 'block_id_x', y: 'block_id_y', z: 'block_id_z' },
  blockDim: { x: 'block_dim_x', y: 'block_dim_y', z: 'block_dim_z' },
  threadIdx: { x: 'local_id_x', y: 'local_id_y', z: 'local_id_z' },
  gridDim: { x: 'grid_dim_x', y: 'grid_dim_y', z: 'grid_dim_z' },
};

export function parse(source) {
  const tokens = tokenize(source);
  let pos = 0;

  const peek = (offset = 0) => tokens[Math.min(pos + offset, tokens.length - 1)];
  const at = (value, type = TokenType.PUNCT) => peek().type === type && peek().value === value;
  const atKeyword = (value) => peek().type === TokenType.KEYWORD && peek().value === value;
  const next = () => tokens[pos++];
  const expect = (value) => {
    if (!at(value)) throw new ParseError(`Expected '${value}' but found '${peek().value}'`, peek().line);
    return next();
  };
  const atScalar = () => peek().type === TokenType.KEYWORD && SCALARS.has(peek().value) && peek().value !== 'void';
  const expectIdent = () => {
    if (peek().type !== TokenType.IDENT) throw new ParseError(`Expected an identifier but found '${peek().value}'`, peek().line);
    return next().value;
  };

  // ---- parameter type: `const float* A` or `int N` ----
  function parseParamType() {
    const isConst = atKeyword('const');
    if (isConst) next();
    const typeToken = next();
    if (typeToken.type !== TokenType.KEYWORD || !SCALARS.has(typeToken.value)) {
      throw new ParseError(`'${typeToken.value}' is not a supported parameter type`, typeToken.line);
    }
    const scalar = typeToken.value;
    let isBuffer = false;
    if (at('*')) {
      next();
      isBuffer = true;
    }
    return { kind: isBuffer ? 'buffer' : 'scalar', scalar, isConst };
  }

  function parseParams() {
    expect('(');
    const params = [];
    if (at(')')) {
      next();
      return params;
    }
    for (;;) {
      const type = parseParamType();
      const name = expectIdent();
      params.push({ name, type });
      if (at(',')) {
        next();
        if (at(')')) break;
        continue;
      }
      break;
    }
    expect(')');
    return params;
  }

  // ---- primary expressions ----
  function parsePrimary() {
    const token = peek();

    if (at('(')) {
      next();
      const inner = parseExpr();
      expect(')');
      return inner;
    }
    if (token.type === TokenType.INT) {
      next();
      const value = token.value.startsWith('0x') || token.value.startsWith('0X')
        ? parseInt(token.value, 16)
        : parseInt(token.value, 10);
      return { kind: 'int', value };
    }
    if (token.type === TokenType.FLOAT) {
      next();
      // As in C, an unsuffixed literal is a double and `f` makes it a float.
      const single = /[fF]$/.test(token.value);
      return { kind: 'float', value: Number.parseFloat(token.value), single };
    }
    // Functional cast: int(x), float(x).
    if (atScalar() && peek(1).type === TokenType.PUNCT && peek(1).value === '(') {
      const to = next().value;
      next();
      const inner = parseExpr();
      expect(')');
      return { kind: 'cast', to, operand: inner };
    }
    if (atKeyword('true')) {
      next();
      return { kind: 'bool', value: true };
    }
    if (atKeyword('false')) {
      next();
      return { kind: 'bool', value: false };
    }
    if (token.type === TokenType.IDENT) {
      next();
      if (BUILTINS.has(token.value)) return { kind: 'builtin', name: token.value };
      if (at('(')) throw new ParseError(`Unknown function '${token.value}'`, token.line);
      return { kind: 'var', name: token.value };
    }
    throw new ParseError(`Unexpected '${token.value}' in expression`, token.line);
  }

  function parsePostfix() {
    let left = parsePrimary();
    for (;;) {
      if (at('[')) {
        next();
        const index = parseExpr();
        expect(']');
        left = { kind: 'index', base: left, index };
        continue;
      }
      if (at('.') && left.kind === 'builtin') {
        next();
        const member = expectIdent();
        const mapped = BUILTIN_MEMBERS[left.name];
        if (!mapped || !(member in mapped)) {
          throw new ParseError(`'${left.name}' has no member '${member}'`, peek().line);
        }
        left = { kind: 'call', callee: mapped[member], args: [] };
        continue;
      }
      if (at('++') || at('--')) {
        const op = next().value;
        left = { kind: 'assign', op: op === '++' ? '+=' : '-=', target: left, value: { kind: 'int', value: 1 } };
        continue;
      }
      break;
    }
    return left;
  }

  function parseUnary() {
    if (at('!') || at('-') || at('~')) {
      const op = next().value;
      return { kind: 'unary', op, operand: parseUnary() };
    }
    // C-style cast: (float)x.
    if (at('(') && peek(1).type === TokenType.KEYWORD && SCALARS.has(peek(1).value) && peek(1).value !== 'void'
        && peek(2).type === TokenType.PUNCT && peek(2).value === ')') {
      next();
      const to = next().value;
      next();
      return { kind: 'cast', to, operand: parseUnary() };
    }
    return parsePostfix();
  }

  function parseBinary(minPrecedence) {
    let left = parseUnary();
    for (;;) {
      const token = peek();
      if (token.type !== TokenType.PUNCT) break;
      const precedence = PRECEDENCE[token.value];
      if (precedence === undefined || precedence < minPrecedence) break;
      next();
      const right = parseBinary(precedence + 1);
      left = { kind: 'binary', op: token.value, left, right };
    }
    return left;
  }

  function parseExpr() {
    const left = parseBinary(1);
    if (ASSIGN_OPS.has(peek().value) && peek().type === TokenType.PUNCT) {
      const op = next().value;
      const value = parseExpr();
      return { kind: 'assign', op, target: left, value };
    }
    return left;
  }

  // ---- statements ----
  function parseBlock() {
    expect('{');
    const body = [];
    while (!at('}')) {
      if (peek().type === TokenType.EOF) throw new ParseError("Unterminated block: expected '}'", peek().line);
      body.push(parseStmt());
    }
    expect('}');
    return body;
  }

  function parseStmt() {
    if (at('{')) return { kind: 'block', body: parseBlock() };

    if (atKeyword('if')) {
      next();
      expect('(');
      const cond = parseExpr();
      expect(')');
      // A braceless body is valid CUDA and is used by the reference kernel, so a single
      // statement is accepted and wrapped rather than rejected.
      const then = at('{') ? parseBlock() : [parseStmt()];
      let otherwise = [];
      if (atKeyword('else')) {
        next();
        otherwise = at('{') ? parseBlock() : [parseStmt()];
      }
      return { kind: 'if', cond, then, otherwise };
    }

    if (atKeyword('while')) {
      next();
      expect('(');
      const cond = parseExpr();
      expect(')');
      const body = at('{') ? parseBlock() : [parseStmt()];
      return { kind: 'while', cond, body };
    }

    if (atKeyword('for')) {
      next();
      expect('(');
      let init = null;
      if (at(';')) next();
      else init = parseSimpleStmt();
      const cond = at(';') ? null : parseExpr();
      expect(';');
      const step = at(')') ? null : parseExpr();
      expect(')');
      const body = at('{') ? parseBlock() : [parseStmt()];
      return { kind: 'for', init, cond, step, body };
    }

    if (atKeyword('break') || atKeyword('continue')) {
      const kind = next().value;
      expect(';');
      return { kind };
    }

    if (atKeyword('return')) {
      next();
      if (at(';')) {
        next();
        return { kind: 'return', value: null };
      }
      const value = parseExpr();
      expect(';');
      return { kind: 'return', value };
    }

    return parseSimpleStmt();
  }

  /** A declaration or expression statement, including its ';'. Also the init clause of `for`. */
  function parseSimpleStmt() {
    // Local scalar declaration: int i = 0; (a functional cast like int(x) is an expression)
    if (atScalar() && !(peek(1).type === TokenType.PUNCT && peek(1).value === '(')) {
      const type = next().value;
      const name = expectIdent();
      let init = null;
      if (at('=')) {
        next();
        init = parseExpr();
      }
      expect(';');
      return { kind: 'decl', name, type, init };
    }

    const expr = parseExpr();
    expect(';');
    // An assignment statement keeps the assign node itself, matching the Stmt kinds in ast.js.
    if (expr.kind === 'assign') return expr;
    return { kind: 'expr', value: expr };
  }

  // ---- top level: a single __global__ kernel ----
  if (!atKeyword('__global__')) {
    throw new ParseError(`A kernel must be declared __global__ (found '${peek().value}')`, peek().line);
  }
  next();
  const returnToken = next();
  if (returnToken.type !== TokenType.KEYWORD || !SCALARS.has(returnToken.value)) {
    throw new ParseError(`'${returnToken.value}' is not a supported return type`, returnToken.line);
  }
  const returnType = returnToken.value;
  const name = expectIdent();
  const params = parseParams();
  const body = parseBlock();

  if (peek().type !== TokenType.EOF) {
    throw new ParseError(`Unexpected trailing input '${peek().value}'`, peek().line);
  }
  return { name, returnType, params, body, isGlobal: true };
}