File: Insn.h

package info (click to toggle)
openjade 1.4devel1-21.3
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 5,328 kB
  • ctags: 15,078
  • sloc: cpp: 90,082; sh: 7,479; ansic: 1,793; lisp: 894; perl: 604; makefile: 393; sed: 93
file content (565 lines) | stat: -rw-r--r-- 12,146 bytes parent folder | download | duplicates (9)
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
// Copyright (c) 1996 James Clark
// See the file copying.txt for copying permission.

#ifndef Insn_INCLUDED
#define Insn_INCLUDED 1

#include "ELObj.h"
#include <OpenSP/Resource.h>
#include <OpenSP/Ptr.h>
#include <OpenSP/Location.h>
#include <OpenSP/Message.h>

#ifdef DSSSL_NAMESPACE
namespace DSSSL_NAMESPACE {
#endif

class VM;
class EvalContext;
class Interpreter;

class Insn : public Resource {
public:
  virtual ~Insn();
  virtual const Insn *execute(VM &) const = 0;
  virtual bool isReturn(int &nArgs) const;
  virtual bool isPopBindings(int &n, ConstPtr<Insn> &) const;
};

typedef ConstPtr<Insn> InsnPtr;

class ErrorInsn : public Insn {
public:
  const Insn *execute(VM &) const;
};

class CondFailInsn : public ErrorInsn {
public:
  CondFailInsn(const Location &loc);
  const Insn *execute(VM &) const;
private:
  Location loc_;
};

class CaseFailInsn : public ErrorInsn {
public:
  CaseFailInsn(const Location &loc);
  const Insn *execute(VM &) const;
private:
  Location loc_;
};

class ConstantInsn : public Insn {
public:
  ConstantInsn(ELObj *, InsnPtr);
  const Insn *execute(VM &) const;
private:
  ELObj *value_;
  InsnPtr next_;
};

class ResolveQuantitiesInsn : public Insn {
public:
  ResolveQuantitiesInsn(const Location &, InsnPtr);
  const Insn *execute(VM &) const;
private:
  Location loc_;
  InsnPtr next_;
};

class TestInsn : public Insn {
public:
  TestInsn(InsnPtr, InsnPtr);
  const Insn *execute(VM &) const;
private:
  InsnPtr consequent_;
  InsnPtr alternative_;
};

class OrInsn : public Insn {
public:
  OrInsn(InsnPtr nextTest, InsnPtr next);
  const Insn *execute(VM &) const;
private:
  InsnPtr nextTest_;
  InsnPtr next_;
};

class AndInsn : public Insn {
public:
  AndInsn(InsnPtr nextTest, InsnPtr next);
  const Insn *execute(VM &) const;
private:
  InsnPtr nextTest_;
  InsnPtr next_;
};

class CaseInsn : public Insn {
public:
  CaseInsn(ELObj *, InsnPtr match, InsnPtr fail);
  const Insn *execute(VM &) const;
private:
  ELObj *obj_;
  InsnPtr match_;
  InsnPtr fail_;
};

class PopInsn : public Insn {
public:
  PopInsn(InsnPtr next);
  const Insn *execute(VM &) const;
private:
  InsnPtr next_;
};

class ConsInsn : public Insn {
public:
  ConsInsn(InsnPtr next);
  const Insn *execute(VM &) const;
private:
  InsnPtr next_;
};

class AppendInsn : public Insn {
public:
  AppendInsn(const Location &, InsnPtr next);
  const Insn *execute(VM &) const;
private:
  Location loc_;
  InsnPtr next_;
};

class ApplyBaseInsn : public Insn {
public:
  ApplyBaseInsn(int nArgs, const Location &);
protected:
  FunctionObj *decodeArgs(VM &) const;
  Location loc_;
  int nArgs_;
};

class ApplyInsn : public ApplyBaseInsn {
public:
  ApplyInsn(int nArgs, const Location &, InsnPtr);
  const Insn *execute(VM &) const;
private:
  InsnPtr next_;
};

class TailApplyInsn : public ApplyBaseInsn {
public:
  TailApplyInsn(int nCallerArgs, int nArgs, const Location &);
  const Insn *execute(VM &) const;
private:
  int nCallerArgs_;
};

class FrameRefInsn : public Insn {
public:
  FrameRefInsn(int index, InsnPtr next);
  const Insn *execute(VM &vm) const;
private:
  int index_;
  InsnPtr next_;
};

class StackRefInsn : public Insn {
public:
  StackRefInsn(int index, int frameIndex, InsnPtr next);
  const Insn *execute(VM &vm) const;
private:
  int index_;			// always negative
  int frameIndex_;
  InsnPtr next_;
};

class ClosureRefInsn : public Insn {
public:
  ClosureRefInsn(int index, InsnPtr next);
  const Insn *execute(VM &vm) const;
private:
  int index_;
  InsnPtr next_;
};

class StackSetBoxInsn : public Insn {
public:
  StackSetBoxInsn(int index, int frameIndex, const Location &loc, InsnPtr next);
  const Insn *execute(VM &vm) const;
private:
  int index_;			// always negative
  int frameIndex_;
  Location loc_;
  InsnPtr next_;
};

class StackSetInsn : public Insn {
public:
  StackSetInsn(int index, int frameIndex, InsnPtr next);
  const Insn *execute(VM &vm) const;
private:
  int index_;			// always negative
  int frameIndex_;
  InsnPtr next_;
};

class ClosureSetBoxInsn : public Insn {
public:
  ClosureSetBoxInsn(int index, const Location &loc, InsnPtr next);
  const Insn *execute(VM &vm) const;
private:
  int index_;
  Location loc_;
  InsnPtr next_;
};

class TopRefInsn : public Insn {
public:
  TopRefInsn(const Identifier *var, InsnPtr next);
  const Insn *execute(VM &vm) const;
private:
  const Identifier *var_;
  InsnPtr next_;
  Location loc_;
};

class PopBindingsInsn : public Insn {
public:
  const Insn *execute(VM &vm) const;
  bool isPopBindings(int &n, InsnPtr &) const;
  static InsnPtr make(int n, InsnPtr next);
private:
  PopBindingsInsn(int n, InsnPtr next);
  int n_;
  InsnPtr next_;
};

class PrimitiveObj;

class PrimitiveCallInsn : public Insn {
public:
  PrimitiveCallInsn(int nArgs, PrimitiveObj *, const Location &, InsnPtr);
  const Insn *execute(VM &) const;
private:
  int nArgs_;
  PrimitiveObj *prim_;
  Location loc_;
  InsnPtr next_;
};

struct Signature {
  int nRequiredArgs;
  int nOptionalArgs;
  bool restArg;
  int nKeyArgs;
  const Identifier *const *keys;
};

// This Insn constructs a ClosureObj.

class ClosureInsn : public Insn {
public:
  ClosureInsn(const Signature *, InsnPtr code, int displayLength, InsnPtr next);
  const Insn *execute(VM &) const;
private:
  const Signature *sig_;
  InsnPtr code_;
  int displayLength_;
  InsnPtr next_;
};

class ClosureObj;

class FunctionCallInsn : public Insn {
public:
  FunctionCallInsn(int nArgs, FunctionObj *, const Location &, InsnPtr);
  const Insn *execute(VM &) const;
private:
  int nArgs_;
  FunctionObj *function_;		// must be permanent
  Location loc_;
  InsnPtr next_;
};

class FunctionTailCallInsn : public Insn {
public:
  FunctionTailCallInsn(int nArgs, FunctionObj *, const Location &,
		       int nCallerArgs);
  const Insn *execute(VM &) const;
private:
  int nArgs_;
  FunctionObj *function_;		// must be permanent
  Location loc_;
  int nCallerArgs_;
};

class VarargsInsn : public Insn {
public:
  VarargsInsn(const Signature &, Vector<InsnPtr> &entryPoints,
	      const Location &);
  const Insn *execute(VM &) const;
private:
  const Signature *sig_;
  Vector<InsnPtr> entryPoints_;
  Location loc_;
};

class SetKeyArgInsn : public Insn {
public:
  SetKeyArgInsn(int offset, InsnPtr);
  const Insn *execute(VM &) const;
private:
  int offset_;
  InsnPtr next_;
};

class TestNullInsn : public Insn {
public:
  TestNullInsn(int offset, InsnPtr ifNull, InsnPtr ifNotNull);
  const Insn *execute(VM &) const;
private:
  int offset_;
  InsnPtr ifNull_;
  InsnPtr ifNotNull_;
  InsnPtr next_;
};

class ReturnInsn : public Insn {
public:
  ReturnInsn(int totalArgs);
  const Insn *execute(VM &) const;
  bool isReturn(int &nArgs) const;
private:
  int totalArgs_;
};

class FunctionObj : public ELObj {
public:
  FunctionObj(const Signature *sig) : sig_(sig) { }
  int totalArgs();
  int nRequiredArgs();
  int nOptionalArgs();
  int nKeyArgs();
  bool restArg();
  virtual const Insn *call(VM &vm, const Location &, const Insn *next) = 0;
  virtual const Insn *tailCall(VM &vm, const Location &, int nCallerArgs) = 0;
  virtual InsnPtr makeCallInsn(int nArgs, Interpreter &, const Location &,
			       InsnPtr next);
  virtual InsnPtr makeTailCallInsn(int nArgs, Interpreter &,
				   const Location &, int nCallerArgs);
  const Signature &signature() const { return *sig_; }
  FunctionObj *asFunction();
  virtual void setArgToCC(VM &);
private:
  const Signature *sig_;
};

class PrimitiveObj : public FunctionObj {
public:
  PrimitiveObj(const Signature *sig) : FunctionObj(sig) { }
  const Insn *call(VM &vm, const Location &, const Insn *next);
  const Insn *tailCall(VM &vm, const Location &, int nCallerArgs);
  InsnPtr makeCallInsn(int nArgs, Interpreter &, const Location &, InsnPtr next);
  virtual ELObj *primitiveCall(int nArgs, ELObj **args, EvalContext &, Interpreter &,
			       const Location &) = 0;
  void setIdentifier(const Identifier *ident);
protected:
  ELObj *argError(Interpreter &, const Location &,
		  const MessageType3 &, unsigned, ELObj *) const;
  ELObj *noCurrentNodeError(Interpreter &, const Location &) const;
private:
  const Identifier *ident_;
};

class ApplyPrimitiveObj : public FunctionObj {
public:
  ApplyPrimitiveObj();
  const Insn *call(VM &vm, const Location &, const Insn *next);
  const Insn *tailCall(VM &vm, const Location &, int nCallerArgs);
private:
  bool shuffle(VM &vm, const Location &loc);
  static const Signature signature_;
};

class CallWithCurrentContinuationPrimitiveObj : public FunctionObj {
public:
  CallWithCurrentContinuationPrimitiveObj();
  const Insn *call(VM &vm, const Location &, const Insn *next);
  const Insn *tailCall(VM &vm, const Location &, int nCallerArgs);
private:
  static const Signature signature_;
};

class ClosureObj : public FunctionObj {
public:
  void *operator new(size_t, Collector &c) {
    return c.allocateObject(1);
  }
  ClosureObj(const Signature *, InsnPtr, ELObj **display);
  ~ClosureObj() { delete [] display_; }
  ELObj **display();
  const Insn *call(VM &, const Location &, const Insn *);
  const Insn *tailCall(VM &, const Location &, int nCallerArgs);
  void traceSubObjects(Collector &) const;
  ELObj *display(int) const;
  void setArgToCC(VM &);
private:
  InsnPtr code_;
  // terminated by null pointer.
  // null if empty.
  ELObj **display_;
};

class ContinuationObj : public FunctionObj {
public:
  ContinuationObj();
  const Insn *call(VM &, const Location &, const Insn *);
  const Insn *tailCall(VM &, const Location &, int nCallerArgs);
  void set(size_t stackSize, size_t controlStackSize) {
    stackSize_ = stackSize;
    controlStackSize_ = controlStackSize;
  }
  void kill() { controlStackSize_ = 0; }
  bool live() const { return controlStackSize_ > 0; }
private:
  size_t stackSize_;
  size_t controlStackSize_;
  static const Signature signature_;
};

class BoxObj : public ELObj {
public:
  BoxObj();
  BoxObj(ELObj *);
  BoxObj *asBox();
  void traceSubObjects(Collector &) const;
  ELObj *value;
};

class SetBoxInsn : public Insn {
public:
  SetBoxInsn(int n, InsnPtr next);
  const Insn *execute(VM &vm) const;
private:
  int n_;
  InsnPtr next_;
};

class SetImmediateInsn : public Insn {
public:
  SetImmediateInsn(int n, InsnPtr next);
  const Insn *execute(VM &vm) const;
private:
  int n_;
  InsnPtr next_;
};

class UnboxInsn : public Insn {
public:
  UnboxInsn(InsnPtr next);
  const Insn *execute(VM &vm) const;
private:
  InsnPtr next_;
};

class CheckInitInsn : public Insn {
public:
  CheckInitInsn(const Identifier *ident, const Location &, InsnPtr next);
  const Insn *execute(VM &vm) const;
private:
  const Identifier *ident_;
  Location loc_;
  InsnPtr next_;
};

class BoxInsn : public Insn {
public:
  BoxInsn(InsnPtr next);
  const Insn *execute(VM &vm) const;
private:
  InsnPtr next_;
};

class BoxArgInsn : public Insn {
public:
  BoxArgInsn(int n, InsnPtr next);
  const Insn *execute(VM &vm) const;
private:
  int n_;
  InsnPtr next_;
};

class BoxStackInsn : public Insn {
public:
  BoxStackInsn(int n, InsnPtr next);
  const Insn *execute(VM &vm) const;
private:
  int n_;
  InsnPtr next_;
};

class VectorInsn : public Insn {
public:
  VectorInsn(size_t n, InsnPtr next);
  const Insn *execute(VM &vm) const;
private:
  size_t n_;
  InsnPtr next_;
};

class ListToVectorInsn : public Insn {
public:
  ListToVectorInsn(InsnPtr next);
  const Insn *execute(VM &vm) const;
private:
  InsnPtr next_;
};

inline
int FunctionObj::nRequiredArgs()
{
  return signature().nRequiredArgs;
}

inline
int FunctionObj::nOptionalArgs()
{
  return signature().nOptionalArgs;
}

inline
bool FunctionObj::restArg()
{
  return signature().restArg;
}

inline
int FunctionObj::nKeyArgs()
{
  return signature().nKeyArgs;
}

inline
int FunctionObj::totalArgs()
{
  const Signature &sig = signature();
  return sig.nRequiredArgs + sig.nOptionalArgs + sig.nKeyArgs + sig.restArg;
}

inline
void PrimitiveObj::setIdentifier(const Identifier *ident)
{
  ident_ = ident;
}

inline
ELObj *ClosureObj::display(int i) const
{
  return display_[i];
}

#ifdef DSSSL_NAMESPACE
}
#endif

#endif /* not Insn_INCLUDED */