summaryrefslogtreecommitdiff
path: root/Build/source/utils/asymptote/frame.h
blob: 804e89baeaeee06ec61df609869fd8c14fc9eee4 (plain)
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
/*****
 * frame.h
 * Andy Hammerlindl 2002/07/22
 *
 * Describes the frame levels for the functions of the language.
 * Also creates accesses for the variable for automated loading
 * and saving of variables.
 *****/

#ifndef FRAME_H
#define FRAME_H

#include <cassert>

#include "access.h"

namespace trans {

class frame : public gc {
  frame *parent;
  size_t numFormals;
  Int numLocals;

  // With the SIMPLE_FRAME flag, the size of frames cannot be changed at
  // runtime.  This is an issue for runnable-at-a-time mode, where global
  // variables can be continually added.  To handle this, the frame for
  // global variables is an "indirect" frame.  It holds one variable, which is
  // a link to another frame.  When the subframe is too small, a larger
  // runtime array is allocated, and the link is changed.
  enum {DIRECT_FRAME, INDIRECT_FRAME} style;

#ifdef DEBUG_FRAME
  string name;
#endif

  frame(string name)
    : parent(new frame("<subframe of " + name + ">", 0, 0)),
      numFormals(0), numLocals(1), style(INDIRECT_FRAME)
#ifdef DEBUG_FRAME
      , name(name)
#endif
  {}

public:
  frame(string name, frame *parent, size_t numFormals)
    : parent(parent), numFormals(numFormals), numLocals(0),
      style(DIRECT_FRAME)
#ifdef DEBUG_FRAME
      , name(name)
#endif
  {}

  static frame *indirect_frame(string name) {
    return new frame(name);
  }

  frame *getParent() {
    return parent;
  }

  // Which variable stores the link to the parent frame.
  Int parentIndex() {
    return numFormals;
  }

  Int size() {
    if (style == DIRECT_FRAME)
      return (Int) (1+numFormals+numLocals);
    else
      return parent->size();
  }

  access *accessFormal(size_t index) {
    assert(index < numFormals);
    assert(style == DIRECT_FRAME);
    return new localAccess((Int) (index), this);
  }

  access *allocLocal() {
    if (style == DIRECT_FRAME)
      return new localAccess((Int) (1 + numFormals + numLocals++), this);
    else
      return parent->allocLocal();
  }
};

inline void print(ostream& out, frame *f) {
  out << f;
  if (f != 0) {
    out << " -> ";
    print(out, f->getParent());
  }
}

} // namespace trans

#endif