MysoreScript
runtime.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdint.h>
4 #include <assert.h>
5 #include <string>
6 #include "gc.h"
7 
8 static_assert(sizeof(void*) == 8,
9  "MysoreScript only supports 64-bit platforms currently");
10 
11 namespace {
17 template<typename T>
18 T* gcAlloc(size_t extraBytes=0)
19 {
20  size_t size = sizeof(T) + extraBytes;
21  return reinterpret_cast<T*>(GC_MALLOC(size));
22 }
23 }
24 namespace AST
25 {
26  struct ClosureDecl;
27 }
28 
29 namespace MysoreScript
30 {
31 
32 struct Object;
33 struct Closure;
34 
39 typedef Object* Obj;
43 inline bool isInteger(Obj o)
44 {
45  return (reinterpret_cast<intptr_t>(o) & 7) == 1;
46 }
51 inline intptr_t getInteger(Obj o)
52 {
53  assert(isInteger(o));
54  return reinterpret_cast<intptr_t>(o) >> 3;
55 }
59 inline Obj createSmallInteger(intptr_t i)
60 {
61  // Small integers are 61-bits, assert that we won't overflow.
62  assert((i<<3)>>3 == i);
63  return reinterpret_cast<Obj>(((i << 3) | 1));
64 }
65 
70 typedef uint32_t Selector;
75 typedef Object *(*CompiledMethod)(Object*,Selector,...);
80 typedef Object *(*ClosureInvoke)(Closure*,...);
81 
85 struct Method
86 {
94  int32_t args;
98  CompiledMethod function;
103 };
104 
109 struct Class
110 {
115  struct Class *superclass;
119  const char *className;
123  int32_t methodCount;
136  const char **indexedIVarNames;
137 };
138 
144 struct Object
145 {
150  struct Class *isa;
151 };
152 
156 struct Array
157 {
174 };
175 
179 struct String
180 {
193  char characters[0];
194 };
195 
199 struct Closure
200 {
221  Obj boundVars[0];
222 };
223 
227 extern struct Class StringClass;
231 extern struct Class SmallIntClass;
235 extern struct Class ClosureClass;
239 void registerClass(const std::string &name, struct Class *cls);
243 struct Class* lookupClass(const std::string &name);
244 
245 
246 
247 extern "C"
248 {
253 Obj newObject(struct Class *cls);
258 Obj mysoreScriptAdd(Obj lhs, Obj rhs);
263 Obj mysoreScriptSub(Obj lhs, Obj rhs);
268 Obj mysoreScriptMul(Obj lhs, Obj rhs);
273 Obj mysoreScriptDiv(Obj lhs, Obj rhs);
281 }
282 
287 Selector lookupSelector(const std::string &);
297 Obj callCompiledMethod(CompiledMethod m, Obj receiver, Selector sel, Obj *args,
298  int argCount);
302 Obj callCompiledClosure(ClosureInvoke m, Closure *receiver, Obj *args,
303  int argCount);
304 
305 }
struct Class * isa
The pointer to the class of this object.
Definition: runtime.hh:150
Object *(* CompiledMethod)(Object *, Selector,...)
A compiled method is a function that takes an object (the receiver) and the selector as implicit argu...
Definition: runtime.hh:75
Object * Obj
Object pointer.
Definition: runtime.hh:33
Obj mysoreScriptAdd(Obj lhs, Obj rhs)
Helper function called by compiled code for the + operator on objects that are not small (embedded in...
Definition: runtime.cc:681
Obj callCompiledClosure(ClosureInvoke m, Closure *receiver, Obj *args, int argCount)
Calls a compiled closure from the specified argument list.
Definition: runtime.cc:656
Obj mysoreScriptDiv(Obj lhs, Obj rhs)
Helper function called by compiled code for the / operator on objects that are not small (embedded in...
Definition: runtime.cc:693
uint32_t Selector
Selectors are unique identifiers for methods.
Definition: runtime.hh:70
AST::ClosureDecl * AST
The AST for this closure.
Definition: runtime.hh:217
Obj bufferSize
The size of the buffer.
Definition: runtime.hh:169
const char ** indexedIVarNames
The names of the instance variables.
Definition: runtime.hh:136
A closure declaration.
Definition: ast.hh:473
ClosureInvoke invoke
The function that is used to invoke this closure.
Definition: runtime.hh:213
Class * isa
Class pointer.
Definition: runtime.hh:161
struct Class SmallIntClass
The SmallInt (Number) class structure.
Definition: runtime.cc:531
Class * isa
Class pointer.
Definition: runtime.hh:184
struct Method * methodList
An array of methodCount elements describing the methods that this class implements.
Definition: runtime.hh:132
Selector lookupSelector(const std::string &str)
Looks up the selector for a specified string value, registering a new value if this is the first time...
Definition: runtime.cc:553
Obj newObject(struct Class *cls)
Instantiate an object.
Definition: runtime.cc:606
Object *(* ClosureInvoke)(Closure *,...)
A compiled closure invoke function.
Definition: runtime.hh:80
Obj mysoreScriptMul(Obj lhs, Obj rhs)
Helper function called by compiled code for the * operator on objects that are not small (embedded in...
Definition: runtime.cc:689
void registerClass(const std::string &name, struct Class *cls)
Register a newly constructed class.
Definition: runtime.cc:596
struct Class * lookupClass(const std::string &name)
Look up an existing class.
Definition: runtime.cc:601
A generic MysoreScript object.
Definition: runtime.hh:144
AST::ClosureDecl * AST
The AST for this method.
Definition: runtime.hh:102
int32_t args
The number of arguments that this method takes.
Definition: runtime.hh:94
struct Class StringClass
The String class structure.
Definition: runtime.cc:492
struct Class ClosureClass
The Closure class structure.
Definition: runtime.cc:543
Method * methodForSelector(Class *cls, Selector sel)
Looks up the Method that should be invoked for the specified selector on this class.
Definition: runtime.cc:615
T * gcAlloc(size_t extraBytes=0)
Typesafe helper function for allocating garbage-collected memory.
Definition: runtime.hh:18
CompiledMethod compiledMethodForSelector(Obj obj, Selector sel)
Look up the compiled method to call for a specific selector.
Definition: runtime.cc:697
Class * isa
Class pointer.
Definition: runtime.hh:204
Obj callCompiledMethod(CompiledMethod m, Obj receiver, Selector sel, Obj *args, int argCount)
Calls a compiled method, constructing the correct argument frame based on the arguments.
Definition: runtime.cc:632
The layout of the primitive Array class in MysoreScript.
Definition: runtime.hh:156
struct Class * superclass
The superclass of this class, if it has one, or a null pointer if it is a root class.
Definition: runtime.hh:115
intptr_t getInteger(Obj o)
Assuming that o is a small integer (an integer embedded in a pointer), return it as a C integer...
Definition: runtime.hh:51
int32_t methodCount
The number of methods that this class implements.
Definition: runtime.hh:123
The primitive String class in MysoreScript.
Definition: runtime.hh:179
int32_t indexedIVarCount
The number of indexed instance variables that this class has.
Definition: runtime.hh:127
Definition: ast.hh:17
const char * className
The name of this class.
Definition: runtime.hh:119
Obj createSmallInteger(intptr_t i)
Construct a small integer object from the given integer.
Definition: runtime.hh:59
Obj * buffer
The buffer storing the values in this array.
Definition: runtime.hh:173
Obj mysoreScriptSub(Obj lhs, Obj rhs)
Helper function called by compiled code for the - operator on objects that are not small (embedded in...
Definition: runtime.cc:685
Obj length
Length of the array (number of elements in it).
Definition: runtime.hh:165
Obj length
The number of characters in the string.
Definition: runtime.hh:188
Struct holding metadata about a class.
Definition: runtime.hh:109
Methods in a class&#39;s method list.
Definition: runtime.hh:85
bool isInteger(Obj o)
Is this object a small integer (lowest bit is 1, next two bits are 0).
Definition: runtime.hh:43
Obj parameters
The number of parameters that this object has.
Definition: runtime.hh:208
The layout of all closures in MysoreScript.
Definition: runtime.hh:199
Selector selector
The selector that this method applies to.
Definition: runtime.hh:90