annotate Jint.Runtime/VM/FunctionBuilder.cs @ 6:a6329b092499

Added scopes, function builder
author cin
date Wed, 30 Oct 2013 17:38:35 +0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
1 using System;
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
2 using System.Collections.Generic;
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
3
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
4 namespace Jint.Runtime.VM
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
5 {
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
6 class FunctionBuidler
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
7 {
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
8 Scope m_parentScope;
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
9
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
10 /// <summary>
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
11 /// maps local variable names to the registers
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
12 /// </summary>
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
13 Dictionary<string,int> m_locals;
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
14
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
15 /// <summary>
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
16 /// maps formal parameters to local variables
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
17 /// </summary>
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
18 int[] m_formalParameters;
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
19
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
20 int m_frameSize;
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
21
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
22 IInstruction m_code;
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
23
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
24 RuntimeContext m_runtime;
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
25
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
26 public FunctionBuilder (string[] argumentNames, RuntimeContext runtime, Scope parentScope)
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
27 {
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
28 m_parentScope;
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
29 m_frameSize = 2; // reserve for this and scope
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
30
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
31 m_locals = new Dictionary<string,int>();
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
32
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
33 if (argumentNames!= null) {
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
34 m_formalParameters = new int[argumentNames.Length];
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
35 for(int i = 0; i < argumentNames.Length; i++)
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
36 m_formalParameters[i] = AllocVariable(argumentNames[i]);
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
37 }
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
38 }
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
39
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
40 // all vars should be allocated before temp registers
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
41 public int AllocVariable(string name) {
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
42 int id;
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
43
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
44 if (m_locals.TryGetValue (name, out id))
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
45 return id;
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
46
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
47 id = m_frameSize++;
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
48 m_locals [name] = id;
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
49 return id;
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
50 }
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
51
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
52 public int AllocateRegister() {
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
53 return m_frameSize++;
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
54 }
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
55
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
56 public void Invoke (object that, object[] args) {
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
57 var frame = new Frame (m_frameSize, m_runtime);
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
58 var scope = new Scope (m_locals, frame, m_declaringScope);
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
59 frame.SetValue (Frame.ThisRegister, that);
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
60 frame.SetValue (Frame.ScopeRegister, scope);
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
61
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
62 var paramLen = Math.Min (m_formalParameters.Length, args.Length);
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
63
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
64 for (int i=0; i< paramLen; i++)
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
65 frame.SetValue (m_formalParameters [i], args [i]);
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
66 }
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
67
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
68 }
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
69 }
a6329b092499 Added scopes, function builder
cin
parents:
diff changeset
70