Ejemplo n.º 1
0
        public override void Compile(ByteCode bc)
        {
            m_Function.Compile(bc);

            var argslen = m_Arguments.Count;

            if (!string.IsNullOrEmpty(m_Name))
            {
                bc.Emit_Copy(0);
                bc.Emit_Index(DynValue.NewString(m_Name), true);
                bc.Emit_Swap(0, 1);
                ++argslen;
            }

            for (var i = 0; i < m_Arguments.Count; i++)
                m_Arguments[i].Compile(bc);

            if (!string.IsNullOrEmpty(m_Name))
            {
                bc.Emit_ThisCall(argslen, m_DebugErr);
            }
            else
            {
                bc.Emit_Call(argslen, m_DebugErr);
            }
        }
Ejemplo n.º 2
0
		internal static int LoadChunk(Script script, SourceCode source, ByteCode bytecode)
		{
			ScriptLoadingContext lcontext = CreateLoadingContext(script, source);
			try
			{
				Statement stat;

				using (script.PerformanceStats.StartStopwatch(Diagnostics.PerformanceCounter.AstCreation))
					stat = new ChunkStatement(lcontext);

				int beginIp = -1;

				//var srcref = new SourceRef(source.SourceID);

				using (script.PerformanceStats.StartStopwatch(Diagnostics.PerformanceCounter.Compilation))
				using (bytecode.EnterSource(null))
				{
					bytecode.Emit_Nop(string.Format("Begin chunk {0}", source.Name));
					beginIp = bytecode.GetJumpPointForLastInstruction();
					stat.Compile(bytecode);
					bytecode.Emit_Nop(string.Format("End chunk {0}", source.Name));
				}

				//Debug_DumpByteCode(bytecode, source.SourceID);

				return beginIp;
			}
			catch (SyntaxErrorException ex)
			{
				ex.DecorateMessage(script);
				throw;
			}
		}
Ejemplo n.º 3
0
		internal static int LoadFunction(Script script, SourceCode source, ByteCode bytecode, bool usesGlobalEnv)
		{
			ScriptLoadingContext lcontext = CreateLoadingContext(script, source);

			try
			{
				FunctionDefinitionExpression fnx;

				using (script.PerformanceStats.StartStopwatch(Diagnostics.PerformanceCounter.AstCreation))
					fnx = new FunctionDefinitionExpression(lcontext, usesGlobalEnv);

				int beginIp = -1;

				//var srcref = new SourceRef(source.SourceID);

				using (script.PerformanceStats.StartStopwatch(Diagnostics.PerformanceCounter.Compilation))
				using (bytecode.EnterSource(null))
				{
					bytecode.Emit_Nop(string.Format("Begin function {0}", source.Name));
					beginIp = fnx.CompileBody(bytecode, source.Name);
					bytecode.Emit_Nop(string.Format("End function {0}", source.Name));
				}

				//Debug_DumpByteCode(bytecode, source.SourceID);

				return beginIp;
			}
			catch (SyntaxErrorException ex)
			{
				ex.DecorateMessage(script);
				throw;
			}

		}
Ejemplo n.º 4
0
        public override void Compile(ByteCode bc)
        {
            var L = new Loop
            {
                Scope = m_StackFrame
            };

            bc.PushSourceRef(m_Repeat);

            bc.LoopTracker.Loops.Push(L);

            var start = bc.GetJumpPointForNextInstruction();

            bc.Emit_Enter(m_StackFrame);
            m_Block.Compile(bc);

            bc.PopSourceRef();
            bc.PushSourceRef(m_Until);
            bc.Emit_Debug("..end");

            m_Condition.Compile(bc);
            bc.Emit_Leave(m_StackFrame);
            bc.Emit_Jump(OpCode.Jf, start);

            bc.LoopTracker.Loops.Pop();

            var exitpoint = bc.GetJumpPointForNextInstruction();

            foreach (var i in L.BreakJumps)
                i.NumVal = exitpoint;

            bc.PopSourceRef();
        }
		public override void Compile(ByteCode bc)
		{
			using (bc.EnterSource(m_FunctionCallExpression.SourceRef))
			{
				m_FunctionCallExpression.Compile(bc);
				RemoveBreakpointStop(bc.Emit_Pop());
			}
		}
Ejemplo n.º 6
0
        public override void Compile(ByteCode bc)
        {
            foreach (var exp in expressions)
                exp.Compile(bc);

            if (expressions.Count > 1)
                bc.Emit_MkTuple(expressions.Count);
        }
Ejemplo n.º 7
0
        public override void Compile(ByteCode bc)
        {
            bc.Emit_Clean(m_StackFrame);

            Address = bc.GetJumpPointForLastInstruction();

            foreach (var gotostat in m_Gotos)
                gotostat.SetAddress(Address);
        }
		private Processor(Processor parentProcessor)
		{
			m_Debug = parentProcessor.m_Debug;
			m_RootChunk = parentProcessor.m_RootChunk;
			m_GlobalTable = parentProcessor.m_GlobalTable;
			m_Script = parentProcessor.m_Script;
			m_Parent = parentProcessor;
			m_State = CoroutineState.NotStarted;
		}
Ejemplo n.º 9
0
        public override void Compile(ByteCode bc)
        {
            using (bc.EnterSource(m_Do))
                bc.Emit_Enter(m_StackFrame);

            m_Block.Compile(bc);

            using (bc.EnterSource(m_End))
                bc.Emit_Leave(m_StackFrame);
        }
Ejemplo n.º 10
0
 public override void Compile(ByteCode bc)
 {
     if (m_Statements != null)
     {
         foreach (var s in m_Statements)
         {
             s.Compile(bc);
         }
     }
 }
Ejemplo n.º 11
0
		/// <summary>
		/// Initializes a new instance of the <see cref="Script"/> class.
		/// </summary>
		/// <param name="coreModules">The core modules to be pre-registered in the default global table.</param>
		public Script(CoreModules coreModules)
		{
			Options = new ScriptOptions(DefaultOptions);
			PerformanceStats = new PerformanceStatistics();
			Registry = new Table(this);

			m_ByteCode = new ByteCode(this);
			m_GlobalTable = new Table(this).RegisterCoreModules(coreModules);
			m_MainProcessor = new Processor(this, m_GlobalTable, m_ByteCode);
		}
Ejemplo n.º 12
0
		public Processor(Script script, Table globalContext, ByteCode byteCode)
		{
			m_CoroutinesStack = new List<Processor>();

			m_Debug = new DebugContext();
			m_RootChunk = byteCode;
			m_GlobalTable = globalContext;
			m_Script = script;
			m_State = CoroutineState.Main;
			DynValue.NewCoroutine(new Coroutine(this)); // creates an associated coroutine for the main processor
		}
Ejemplo n.º 13
0
		public override void Compile(ByteCode bc)
		{
			using (bc.EnterSource(m_Ref))
			{
				if (bc.LoopTracker.Loops.Count == 0)
					throw new SyntaxErrorException(this.Script, m_Ref, "<break> at line {0} not inside a loop", m_Ref.FromLine);

				ILoop loop = bc.LoopTracker.Loops.Peek();

				if (loop.IsBoundary())
					throw new SyntaxErrorException(this.Script, m_Ref, "<break> at line {0} not inside a loop", m_Ref.FromLine);

				loop.CompileBreak(bc);
			}
		}
Ejemplo n.º 14
0
 public override void Compile(ByteCode bc)
 {
     using (bc.EnterSource(m_Ref))
     {
         if (m_Expression != null)
         {
             m_Expression.Compile(bc);
             bc.Emit_Ret(1);
         }
         else
         {
             bc.Emit_Ret(0);
         }
     }
 }
Ejemplo n.º 15
0
		public override void Compile(ByteCode bc)
		{
			bc.PushSourceRef(m_RefFor);

			Loop L = new Loop()
			{
				Scope = m_StackFrame
			};

			bc.LoopTracker.Loops.Push(L);

			m_End.Compile(bc);
			bc.Emit_ToNum(3);
			m_Step.Compile(bc);
			bc.Emit_ToNum(2);
			m_Start.Compile(bc);
			bc.Emit_ToNum(1);

			int start = bc.GetJumpPointForNextInstruction();
			var jumpend = bc.Emit_Jump(OpCode.JFor, -1);
			bc.Emit_Enter(m_StackFrame);
			//bc.Emit_SymStorN(m_VarName);

			bc.Emit_Store(m_VarName, 0, 0);

			m_InnerBlock.Compile(bc);

			bc.PopSourceRef();
			bc.PushSourceRef(m_RefEnd);

			bc.Emit_Debug("..end");
			bc.Emit_Leave(m_StackFrame);
			bc.Emit_Incr(1);
			bc.Emit_Jump(OpCode.Jump, start);

			bc.LoopTracker.Loops.Pop();

			int exitpoint = bc.GetJumpPointForNextInstruction();

			foreach (Instruction i in L.BreakJumps)
				i.NumVal = exitpoint;

			jumpend.NumVal = exitpoint;
			bc.Emit_Pop(3);

			bc.PopSourceRef();
		}
Ejemplo n.º 16
0
        public override void Compile(ByteCode bc)
        {
            var meta = bc.Emit_FuncMeta("<chunk-root>");
            var metaip = bc.GetJumpPointForLastInstruction();

            bc.Emit_BeginFn(m_StackFrame);
            bc.Emit_Args(m_VarArgs);

            bc.Emit_Literal(DynValue.NewTable(m_GlobalEnv));
            bc.Emit_Store(m_Env, 0, 0);
            bc.Emit_Pop();

            m_Block.Compile(bc);
            bc.Emit_Ret(0);

            meta.NumVal = bc.GetJumpPointForLastInstruction() - metaip;
        }
Ejemplo n.º 17
0
        public override void Compile(ByteCode bc)
        {
            m_BaseExp.Compile(bc);

            if (m_Name != null)
            {
                bc.Emit_Index(DynValue.NewString(m_Name), true);
            }
            else if (m_IndexExp is LiteralExpression)
            {
                var lit = (LiteralExpression) m_IndexExp;
                bc.Emit_Index(lit.Value);
            }
            else
            {
                m_IndexExp.Compile(bc);
                bc.Emit_Index(isExpList: (m_IndexExp is ExprListExpression));
            }
        }
Ejemplo n.º 18
0
        public void CompileAssignment(ByteCode bc, int stackofs, int tupleidx)
        {
            m_BaseExp.Compile(bc);

            if (m_Name != null)
            {
                bc.Emit_IndexSet(stackofs, tupleidx, DynValue.NewString(m_Name), true);
            }
            else if (m_IndexExp is LiteralExpression)
            {
                var lit = (LiteralExpression) m_IndexExp;
                bc.Emit_IndexSet(stackofs, tupleidx, lit.Value);
            }
            else
            {
                m_IndexExp.Compile(bc);
                bc.Emit_IndexSet(stackofs, tupleidx, isExpList: (m_IndexExp is ExprListExpression));
            }
        }
Ejemplo n.º 19
0
        public override void Compile(ByteCode bc)
        {
            m_Exp.Compile(bc);

            switch (m_OpText)
            {
                case "not":
                    bc.Emit_Operator(OpCode.Not);
                    break;
                case "#":
                    bc.Emit_Operator(OpCode.Len);
                    break;
                case "-":
                    bc.Emit_Operator(OpCode.Neg);
                    break;
                default:
                    throw new InternalErrorException("Unexpected unary operator '{0}'", m_OpText);
            }
        }
Ejemplo n.º 20
0
        public override void Compile(ByteCode bc)
        {
            var endJumps = new List<Instruction>();

            Instruction lastIfJmp = null;

            foreach (var ifblock in m_Ifs)
            {
                using (bc.EnterSource(ifblock.Source))
                {
                    if (lastIfJmp != null)
                        lastIfJmp.NumVal = bc.GetJumpPointForNextInstruction();

                    ifblock.Exp.Compile(bc);
                    lastIfJmp = bc.Emit_Jump(OpCode.Jf, -1);
                    bc.Emit_Enter(ifblock.StackFrame);
                    ifblock.Block.Compile(bc);
                }

                using (bc.EnterSource(m_End))
                    bc.Emit_Leave(ifblock.StackFrame);

                endJumps.Add(bc.Emit_Jump(OpCode.Jump, -1));
            }

            lastIfJmp.NumVal = bc.GetJumpPointForNextInstruction();

            if (m_Else != null)
            {
                using (bc.EnterSource(m_Else.Source))
                {
                    bc.Emit_Enter(m_Else.StackFrame);
                    m_Else.Block.Compile(bc);
                }

                using (bc.EnterSource(m_End))
                    bc.Emit_Leave(m_Else.StackFrame);
            }

            foreach (var endjmp in endJumps)
                endjmp.NumVal = bc.GetJumpPointForNextInstruction();
        }
Ejemplo n.º 21
0
		public override void Compile(ByteCode bc)
		{
			Loop L = new Loop()
			{
				Scope = m_StackFrame
			};


			bc.LoopTracker.Loops.Push(L);

			bc.PushSourceRef(m_Start);

			int start = bc.GetJumpPointForNextInstruction();

			m_Condition.Compile(bc);
			var jumpend = bc.Emit_Jump(OpCode.Jf, -1);

			bc.Emit_Enter(m_StackFrame);

			m_Block.Compile(bc);

			bc.PopSourceRef();
			bc.Emit_Debug("..end");
			bc.PushSourceRef(m_End);
	
			bc.Emit_Leave(m_StackFrame);
			bc.Emit_Jump(OpCode.Jump, start);
			
			bc.LoopTracker.Loops.Pop();

			int exitpoint = bc.GetJumpPointForNextInstruction();

			foreach (Instruction i in L.BreakJumps)
				i.NumVal = exitpoint;

			jumpend.NumVal = exitpoint;

			bc.PopSourceRef();
		}
Ejemplo n.º 22
0
 public SourceCodeStackGuard(SourceRef sref, ByteCode bc)
 {
     m_Bc = bc;
     m_Bc.PushSourceRef(sref);
 }
Ejemplo n.º 23
0
        public override void Compile(ByteCode bc)
        {
            bc.Emit_NewTable();

            foreach (var kvp in m_CtorArgs)
            {
                kvp.Key.Compile(bc);
                kvp.Value.Compile(bc);
                bc.Emit_TblInitN();
            }

            for (var i = 0; i < m_PositionalValues.Count; i++)
            {
                m_PositionalValues[i].Compile(bc);
                bc.Emit_TblInitI(i == m_PositionalValues.Count - 1);
            }
        }
Ejemplo n.º 24
0
 public override void Compile(ByteCode bc)
 {
     bc.Emit_Literal(Value);
 }
		public int CompileBody(ByteCode bc, string friendlyName)
		{
			string funcName = friendlyName ?? ("<" + this.m_Begin.FormatLocation(bc.Script, true) + ">");

			bc.PushSourceRef(m_Begin);

			Instruction I = bc.Emit_Jump(OpCode.Jump, -1);

			Instruction meta = bc.Emit_Meta(funcName, OpCodeMetadataType.FunctionEntrypoint);
			int metaip = bc.GetJumpPointForLastInstruction();

			bc.Emit_BeginFn(m_StackFrame);

			bc.LoopTracker.Loops.Push(new LoopBoundary());

			int entryPoint = bc.GetJumpPointForLastInstruction();

			if (m_UsesGlobalEnv)
			{
				bc.Emit_Load(SymbolRef.Upvalue(WellKnownSymbols.ENV, 0));
				bc.Emit_Store(m_Env, 0, 0);
				bc.Emit_Pop();
			}

			if (m_ParamNames.Length > 0)
				bc.Emit_Args(m_ParamNames);

			m_Statement.Compile(bc);

			bc.PopSourceRef();
			bc.PushSourceRef(m_End);

			bc.Emit_Ret(0);

			bc.LoopTracker.Loops.Pop();

			I.NumVal = bc.GetJumpPointForNextInstruction();
			meta.NumVal = bc.GetJumpPointForLastInstruction() - metaip;

			bc.PopSourceRef();

			return entryPoint;
		}
Ejemplo n.º 26
0
 public override void Compile(ByteCode bc)
 {
     m_Jump = bc.Emit_Jump(OpCode.Jump, m_LabelAddress);
 }
 public override void Compile(ByteCode bc)
 {
     Compile(bc, () => 0, null);
 }
        public int Compile(ByteCode bc, Func<int> afterDecl, string friendlyName)
        {
            using (bc.EnterSource(m_Begin))
            {
                var symbs = m_Closure
                    //.Select((s, idx) => s.CloneLocalAndSetFrame(m_ClosureFrames[idx]))
                    .ToArray();

                m_ClosureInstruction = bc.Emit_Closure(symbs, bc.GetJumpPointForNextInstruction());
                var ops = afterDecl();

                m_ClosureInstruction.NumVal += 2 + ops;
            }

            return CompileBody(bc, friendlyName);
        }
Ejemplo n.º 29
0
		public override void Compile(ByteCode bc)
		{
			//for var_1, ···, var_n in explist do block end

			bc.PushSourceRef(m_RefFor);

			Loop L = new Loop()
			{
				Scope = m_StackFrame
			};
			bc.LoopTracker.Loops.Push(L);

			// get iterator tuple
			m_RValues.Compile(bc);

			// prepares iterator tuple - stack : iterator-tuple
			bc.Emit_IterPrep();

			// loop start - stack : iterator-tuple
			int start = bc.GetJumpPointForNextInstruction();
			bc.Emit_Enter(m_StackFrame);

			// expand the tuple - stack : iterator-tuple, f, var, s
			bc.Emit_ExpTuple(0);

			// calls f(s, var) - stack : iterator-tuple, iteration result
			bc.Emit_Call(2, "for..in");

			// perform assignment of iteration result- stack : iterator-tuple, iteration result
			for (int i = 0; i < m_NameExps.Length; i++)
				m_NameExps[i].CompileAssignment(bc, 0, i);

			// pops  - stack : iterator-tuple
			bc.Emit_Pop();

			// repushes the main iterator var - stack : iterator-tuple, main-iterator-var
			bc.Emit_Load(m_Names[0]);

			// updates the iterator tuple - stack : iterator-tuple, main-iterator-var
			bc.Emit_IterUpd();

			// checks head, jumps if nil - stack : iterator-tuple, main-iterator-var
			var endjump = bc.Emit_Jump(OpCode.JNil, -1);

			// executes the stuff - stack : iterator-tuple
			m_Block.Compile(bc);

			bc.PopSourceRef();
			bc.PushSourceRef(m_RefEnd);

			// loop back again - stack : iterator-tuple
			bc.Emit_Leave(m_StackFrame);
			bc.Emit_Jump(OpCode.Jump, start);

			bc.LoopTracker.Loops.Pop();

			int exitpointLoopExit = bc.GetJumpPointForNextInstruction();
			bc.Emit_Leave(m_StackFrame);

			int exitpointBreaks = bc.GetJumpPointForNextInstruction();

			bc.Emit_Pop();

			foreach (Instruction i in L.BreakJumps)
				i.NumVal = exitpointBreaks;

			endjump.NumVal = exitpointLoopExit;

			bc.PopSourceRef();
		}
        public int CompileBody(ByteCode bc, string friendlyName)
        {
            var funcName = friendlyName ?? ("<" + m_Begin.FormatLocation(bc.Script, true) + ">");

            bc.PushSourceRef(m_Begin);

            var I = bc.Emit_Jump(OpCode.Jump, -1);

            var meta = bc.Emit_FuncMeta(funcName);
            var metaip = bc.GetJumpPointForLastInstruction();

            bc.Emit_BeginFn(m_StackFrame);

            bc.LoopTracker.Loops.Push(new LoopBoundary());

            var entryPoint = bc.GetJumpPointForLastInstruction();

            if (m_GlobalEnv != null)
            {
                bc.Emit_Literal(DynValue.NewTable(m_GlobalEnv));
                bc.Emit_Store(m_Env, 0, 0);
                bc.Emit_Pop();
            }

            if (m_ParamNames.Length > 0)
                bc.Emit_Args(m_ParamNames);

            m_Statement.Compile(bc);

            bc.PopSourceRef();
            bc.PushSourceRef(m_End);

            bc.Emit_Ret(0);

            bc.LoopTracker.Loops.Pop();

            I.NumVal = bc.GetJumpPointForNextInstruction();
            meta.NumVal = bc.GetJumpPointForLastInstruction() - metaip;

            bc.PopSourceRef();

            return entryPoint;
        }
Ejemplo n.º 31
0
 public SourceCodeStackGuard(SourceRef sref, ByteCode bc)
 {
     m_Bc = bc;
     m_Bc.PushSourceRef(sref);
 }