munificent / magpie
The Magpie programming language.
Clone this repository (size: 897.6 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/munificent/magpie/
| commit 36: | 46d09f708b11 |
| parent 35: | d78a20a8a165 |
| branch: | default |
Random intrinsic.
| r36:46d09f708b11 | 365 loc | 10.2 KB | embed / history / annotate / raw / |
|---|
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 | using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Magpie.Compilation
{
/// <summary>
/// Expression visitor that compiles expressions down to bytecode.
/// </summary>
public class BytecodeGenerator : IBoundExprVisitor<bool>
{
public int Position { get { return (int)mWriter.BaseStream.Position; } }
private BytecodeGenerator(Compiler compiler, BinaryWriter writer,
OffsetTable functionPatcher, StringTable stringTable)
{
mCompiler = compiler;
mWriter = writer;
mFunctionPatcher = functionPatcher;
mStrings = stringTable;
mJumpTable = new JumpTable(this);
}
public static void Generate(Compiler compiler, BinaryWriter writer,
OffsetTable functionPatcher, StringTable stringTable, Function function)
{
BytecodeGenerator generator = new BytecodeGenerator(compiler,
writer, functionPatcher, stringTable);
functionPatcher.DefineOffset(function.UniqueName());
writer.Write(function.NumLocals);
function.Body.Bound.Accept(generator);
generator.TranslateTailCall();
generator.Write(OpCode.Return);
}
public void SeekTo(int position)
{
mWriter.Seek(position, SeekOrigin.Begin);
}
public void SeekToEnd()
{
mWriter.Seek(0, SeekOrigin.End);
}
#region IBoundExprVisitor Members
bool IBoundExprVisitor<bool>.Visit(UnitExpr expr) { return true; } // do nothing
bool IBoundExprVisitor<bool>.Visit(BoolExpr expr) { Write(OpCode.PushBool, expr.Value ? (byte)1 : (byte)0); return true; }
bool IBoundExprVisitor<bool>.Visit(IntExpr expr) { Write(OpCode.PushInt, expr.Value); return true; }
bool IBoundExprVisitor<bool>.Visit(StringExpr expr)
{
Write(OpCode.PushString);
mStrings.InsertOffset(expr.Value);
return true;
}
bool IBoundExprVisitor<bool>.Visit(BoundFuncRefExpr expr)
{
Write(OpCode.PushInt);
mFunctionPatcher.InsertOffset(expr.Function.UniqueName());
return true;
}
bool IBoundExprVisitor<bool>.Visit(ForeignCallExpr expr)
{
// evaluate the arg
expr.Arg.Accept(this);
// add the foreign call
OpCode op;
switch (expr.Function.FuncType.Parameter.Bound.Expand().Length)
{
case 0: op = OpCode.ForeignCall0; break;
case 1: op = OpCode.ForeignCall1; break;
default: op = OpCode.ForeignCallN; break;
}
Write(op, expr.Function.ID);
return true;
}
bool IBoundExprVisitor<bool>.Visit(BoundTupleExpr tuple)
{
// must visit in forward order to ensure that function arguments are
// evaluated left to right
foreach (var field in tuple.Fields)
{
field.Accept(this);
}
// create the structure
Write(OpCode.Alloc, tuple.Fields.Count);
return true;
}
bool IBoundExprVisitor<bool>.Visit(IntrinsicExpr expr)
{
expr.Arg.Accept(this);
expr.Intrinsic.OpCodes.ForEach(op => Write(op));
return true;
}
bool IBoundExprVisitor<bool>.Visit(BoundCallExpr expr)
{
expr.Arg.Accept(this);
expr.Target.Accept(this);
// add the call
OpCode op;
switch (expr.Arg.Type.Expand().Length)
{
case 0: op = OpCode.Call0; break;
case 1: op = OpCode.Call1; break;
default: op = OpCode.CallN; break;
}
mLastCallPosition = Position;
mLastCall = op;
Write(op);
return true;
}
bool IBoundExprVisitor<bool>.Visit(BoundArrayExpr expr)
{
// the count is the first element
Write(OpCode.PushInt, expr.Elements.Count);
// must visit in forward order to ensure that array elements are
// evaluated left to right
foreach (var element in expr.Elements)
{
element.Accept(this);
}
// create the structure (+1 for the size)
Write(OpCode.Alloc, expr.Elements.Count + 1);
return true;
}
bool IBoundExprVisitor<bool>.Visit(BoundBlockExpr block)
{
block.Exprs.ForEach(expr => expr.Accept(this));
return true;
}
bool IBoundExprVisitor<bool>.Visit(BoundIfThenExpr expr)
{
// evaluate the condition
expr.Condition.Accept(this);
mJumpTable.JumpIfFalse("end");
// execute the body
expr.Body.Accept(this);
// jump past it
mJumpTable.PatchJump("end");
return true;
}
bool IBoundExprVisitor<bool>.Visit(BoundIfThenElseExpr expr)
{
// evaluate the condition
expr.Condition.Accept(this);
mJumpTable.JumpIfFalse("else");
// thenBody
expr.ThenBody.Accept(this);
// jump to end
mJumpTable.Jump("end");
// elseBody
mJumpTable.PatchJump("else");
expr.ElseBody.Accept(this);
// end
mJumpTable.PatchJump("end");
return true;
}
bool IBoundExprVisitor<bool>.Visit(BoundReturnExpr expr)
{
expr.Value.Accept(this);
TranslateTailCall();
Write(OpCode.Return);
return true;
}
bool IBoundExprVisitor<bool>.Visit(BoundWhileExpr expr)
{
mJumpTable.PatchJumpBack("while");
// evaluate the condition
expr.Condition.Accept(this);
mJumpTable.JumpIfFalse("end");
// body
expr.Body.Accept(this);
// jump back to loop
mJumpTable.JumpBack("while");
// exit loop
mJumpTable.PatchJump("end");
return true;
}
bool IBoundExprVisitor<bool>.Visit(LoadExpr expr)
{
expr.Struct.Accept(this);
Write(OpCode.Load, (byte)expr.Index);
return true;
}
bool IBoundExprVisitor<bool>.Visit(StoreExpr expr)
{
expr.Value.Accept(this);
expr.Struct.Accept(this);
Write(OpCode.Store, expr.Field.Index);
return true;
}
bool IBoundExprVisitor<bool>.Visit(LocalsExpr expr)
{
Write(OpCode.PushLocals);
return true;
}
bool IBoundExprVisitor<bool>.Visit(ConstructExpr expr)
{
expr.Arg.Accept(this);
// if the struct has only one field, the arg tuple will just be a
// value. in that case, we need to hoist it into a struct to make
// it properly a reference type.
//### opt: this is really only needed for mutable single-field
// structs. for immutable ones, pass by reference and pass by value
// are indistinguishable.
if (expr.Struct.Fields.Count == 1)
{
Write(OpCode.Alloc, 1);
}
else if (expr.Struct.Fields.Count == 0)
{
// for an empty struct, just include a dummy value
//### bob: this is gross
Write(OpCode.PushInt, 0);
}
// note that if there is more than one field, this evaluates to
// nothing: the arg tuple for the structure *is* the structure
// so the work is already done.
return true;
}
bool IBoundExprVisitor<bool>.Visit(ConstructUnionExpr expr)
{
// push the case tag
Write(OpCode.PushInt, expr.Case.Index);
// evaluate the value
expr.Arg.Accept(this);
// one slot for the case tag
int numSlots = 1;
// load the value (if any)
if (expr.Case.ValueType.Bound != Decl.Unit)
{
// add a slot for the value
numSlots++;
}
// create the structure
Write(OpCode.Alloc, numSlots);
return true;
}
#endregion
public void Write(OpCode op)
{
mWriter.Write((byte)op);
}
public void Write(int value)
{
mWriter.Write(value);
}
public void Write(OpCode op, byte operand)
{
Write(op);
Write(operand);
}
public void Write(OpCode op, int operand)
{
Write(op);
Write(operand);
}
private void Write(byte value)
{
mWriter.Write(value);
}
private void TranslateTailCall()
{
// if there is a call at the end, translate it to a tail call
if (mLastCallPosition == Position - 1)
{
SeekTo(mLastCallPosition);
OpCode op;
switch (mLastCall)
{
case OpCode.Call0: op = OpCode.TailCall0; break;
case OpCode.Call1: op = OpCode.TailCall1; break;
default: op = OpCode.TailCallN; break;
}
Write(op);
SeekToEnd();
}
}
private Compiler mCompiler;
private BinaryWriter mWriter;
private OffsetTable mFunctionPatcher;
private StringTable mStrings;
private JumpTable mJumpTable;
private int mLastCallPosition;
private OpCode mLastCall;
}
}
|
