예제 #1
0
파일: facade.cs 프로젝트: possientis/Prog
 public static void Main(string[] args)
 {
     InputStream input = new InputStream("source.c");
     BytecodeStream output = new BytecodeStream();
     Compiler compiler = new Compiler();
     compiler.Compile(input, output);
 }
예제 #2
0
 public override void Assemble(CodeGenerator cgen, Instruction ins, BytecodeStream str)
 {
     if (ins.Operands.Count != 2)
     {
         cgen.CreateError("{0} does not take {1} arguments!", ins.Name, ins.Operands.Count);
     }
     else if (!(ins.Operands[0] is TokenIntLiteral || ins.Operands[0] is TokenRegister) && !(ins.Operands[1] is TokenRegister))
     {
         cgen.CreateError("Unsupported addressing mode for instruction '{0}'", ins.Name);
     }
     else
     {
         TokenRegister op2    = ins.Operands[1] as TokenRegister;
         Opcode        opcode = Opcode.OUT;
         if (ins.Operands[0] is TokenRegister)
         {
             TokenRegister op1 = ins.Operands[0] as TokenRegister;
             str.Emit(new QuasarInstruction(opcode, new RegisterOperand(op1.Register), new RegisterOperand(op2.Register)));
         }
         else if (ins.Operands[0] is TokenIntLiteral)
         {
             TokenIntLiteral op1 = ins.Operands[0] as TokenIntLiteral;
             str.Emit(new QuasarInstruction(opcode, new IntegerOperand((int)op1.Value), new RegisterOperand(op2.Register)));
         }
     }
 }
예제 #3
0
 public override void Assemble(CodeGenerator cgen, Instruction ins, BytecodeStream str)
 {
     if (ins.Operands.Count != 2)
     {
         cgen.CreateError("{0} does not take {1} arguments!", ins.Name, ins.Operands.Count);
     }
     else if (!(ins.Operands[0] is TokenRegister) || (!(ins.Operands[1] is TokenRegister) && !(ins.Operands[1] is TokenIntLiteral) && !(ins.Operands[1] is TokenIdentifier)))
     {
         cgen.CreateError("Unsupported addressing mode for instruction '{0}'", ins.Name);
     }
     else
     {
         TokenRegister op1    = ins.Operands[0] as TokenRegister;
         Opcode        opcode = Opcode.ADD;
         if (ins.Operands[1] is TokenRegister)
         {
             TokenRegister op2 = ins.Operands[1] as TokenRegister;
             str.Emit(new QuasarInstruction(opcode, new RegisterOperand(op1.Register), new RegisterOperand(op2.Register)));
         }
         else if (ins.Operands[1] is TokenIntLiteral)
         {
             TokenIntLiteral op2 = ins.Operands[1] as TokenIntLiteral;
             Console.WriteLine("Add with " + op1.Register);
             str.Emit(new QuasarInstruction(opcode, new RegisterOperand(op1.Register), new IntegerOperand((int)op2.Value)));
         }
         else if (ins.Operands[1] is TokenIdentifier)
         {
             TokenIdentifier ident = ins.Operands[1] as TokenIdentifier;
             str.Emit(new QuasarInstruction(opcode, new RegisterOperand(op1.Register), new SymbolReferenceOperand(ident.Value)));
         }
     }
 }
예제 #4
0
 public override void Assemble(CodeGenerator cgen, Instruction ins, BytecodeStream str)
 {
     if (ins.Operands.Count != 2)
     {
         cgen.CreateError("Instruction {0} does not take {1} operand(s)!", ins.Name, ins.Operands.Count);
     }
 }
예제 #5
0
파일: Compiler.cs 프로젝트: Helen1987/edu
 public void Compile(FileStream input, BytecodeStream output)
 {
     Scanner scanner = new Scanner(input);
     ProgramNodeBuilder builder = new ProgramNodeBuilder();
     new Parser().Parse(scanner, builder);
     RISCCodeGenerator generator = new RISCCodeGenerator(output);
     builder.GetRootNode().Traverse(generator);
 }
예제 #6
0
        static void Main(string[] args)
        {
            FileStream     input  = new FileStream("../../Program.cs", FileMode.Open);
            BytecodeStream output = new BytecodeStream();

            CompilerFacade compiler = new CompilerFacade();

            //Do all complier Job.
            compiler.Compile(input, output);
        }
예제 #7
0
파일: Vm.cs 프로젝트: MuranagaTamura/LessVm
        public bool OpJmp(BytecodeStream code)
        {
            ushort address = code.ReadChar();

            if (code.TrySetPosition(address))
            {
                return(true);
            }

            return(PushError("参照不可能な箇所にジャンプしようとしました"));
        }
예제 #8
0
파일: Vm.cs 프로젝트: MuranagaTamura/LessVm
        public bool OpMoveID(BytecodeStream code)
        {
            int  regId = code.ReadByte();
            uint imm   = (uint)code.ReadInt();

            if (!TrySetRegUi32(regId, imm))
            {
                return(PushRegError(regId));
            }
            return(true);
        }
예제 #9
0
파일: Vm.cs 프로젝트: MuranagaTamura/LessVm
        public bool OpMoveIW(BytecodeStream code)
        {
            int    regId = code.ReadByte();
            ushort imm   = code.ReadChar();

            if (!TrySetRegUi16(regId, imm))
            {
                return(PushRegError(regId));
            }
            return(true);
        }
예제 #10
0
파일: Vm.cs 프로젝트: MuranagaTamura/LessVm
        public bool OpMoveIB(BytecodeStream code)
        {
            int  regId = code.ReadByte();
            byte imm   = code.ReadByte();

            if (!TrySetRegUi8(regId, imm))
            {
                return(PushRegError(regId));
            }
            return(true);
        }
예제 #11
0
파일: Vm.cs 프로젝트: MuranagaTamura/LessVm
        public bool OpStoreD(BytecodeStream code)
        {
            int memPtr = code.ReadChar();
            int regId  = code.ReadByte();

            if (TryGetReg(regId, out uint val))
            {
                return(_memory.TrySetUi32(memPtr, val));
            }
            return(PushRegError(regId));
        }
예제 #12
0
파일: Vm.cs 프로젝트: MuranagaTamura/LessVm
        public bool OpLoadD(BytecodeStream code)
        {
            int regId  = code.ReadByte();
            int memPtr = code.ReadChar();

            if (_memory.TryGetUi32(memPtr, out uint val32))
            {
                return(TrySetRegUi32(regId, val32));
            }
            return(PushError($"メモリ範囲外を指定しました => ptr: {memPtr}"));
        }
예제 #13
0
 public override void Assemble(CodeGenerator cgen, Instruction ins, BytecodeStream str)
 {
     if (ins.Operands.Count != 0)
     {
         cgen.CreateError("{0} does not take {1} arguments!", ins.Name, ins.Operands.Count);
     }
     else
     {
         str.Emit(new QuasarInstruction(Opcode.IRTN));
     }
 }
예제 #14
0
        public virtual void Compile(FileStream sourceFile, BytecodeStream byteStream)
        {
            Scanner            scanner = new Scanner(sourceFile);
            ProgramNodeBuilder builder = new ProgramNodeBuilder();
            Parser             parser  = new Parser();

            parser.Parse(scanner, builder);
            RISCCodeGenerator generator = new RISCCodeGenerator(byteStream);
            ProgramNode       parseTree = builder.GetRootNode();

            parseTree.Traverse(generator);
        }
    public virtual void Compile(StreamReader input, BytecodeStream output)
    {
        Scanner scanner = new Scanner(input);
        var     builder = new ProgramNodeBuilder();
        Parser  parser  = new Parser();

        parser.Parse(scanner, builder);

        RISCCodeGenerator generator = new RISCCodeGenerator(output);
        ProgramNode       parseTree = builder.GetRootNode();

        parseTree.Traverse(generator);
    }
예제 #16
0
        public override void Assemble(CodeGenerator cgen, Instruction ins, BytecodeStream str)
        {
            if (ins.Operands.Count != 1)
            {
                cgen.CreateError("Instruction {0} does not take {1} operand(s)!", ins.Name, ins.Operands.Count);
            }
            else if (ins.Operands[0] is TokenIdentifier)
            {
                TokenIdentifier label = ins.Operands[0] as TokenIdentifier;

                str.Emit(new QuasarInstruction(Opcode.BSR, new SymbolReferenceOperand(label.Value, true)));
            }
        }
예제 #17
0
파일: Vm.cs 프로젝트: MuranagaTamura/LessVm
        public bool OpSysCall(BytecodeStream code)
        {
            ushort methodId = code.ReadChar();

            if (methodId < 0 || methodId > _methods.Count)
            {
                return(PushError("組み込み関数IDが不正です"));
            }

            CreateStatus();
            _itr = _methods[methodId].Invoke(_instance, new object[] { _status }) as IEnumerator <int>;

            return(true);
        }
예제 #18
0
파일: Vm.cs 프로젝트: MuranagaTamura/LessVm
        private bool OpCalcHelper(
            BytecodeStream code,
            Func <uint, uint, uint> calc)
        {
            uint a, b;
            int  rsId;

            if (!GetRegVal(code, out rsId, out a, out b))
            {
                return(false);
            }

            return(TrySetRegUi32(rsId, calc(a, b)));
        }
예제 #19
0
 public override void Assemble(CodeGenerator cgen, Instruction ins, BytecodeStream str)
 {
     if (ins.Operands.Count != 1)
     {
         cgen.CreateError("Directive {0} does not take {1} operands!", ins.Name, ins.Operands.Count);
     }
     else if (!(ins.Operands[0] is TokenIntLiteral))
     {
         cgen.CreateError("Integer size expected!");
     }
     else
     {
         str.Emit(new QuasarData(new byte[((TokenIntLiteral)ins.Operands[0]).Value]));
     }
 }
예제 #20
0
 public override void Assemble(CodeGenerator cgen, Instruction ins, BytecodeStream str)
 {
     if (ins.Operands.Count != 1)
     {
         cgen.CreateError("{0} does not take {1} arguments!", ins.Name, ins.Operands.Count);
     }
     else if (!(ins.Operands[0] is TokenIntLiteral))
     {
         cgen.CreateError("Unsupported addressing mode for instruction '{0}'", ins.Name);
     }
     else
     {
         TokenIntLiteral il = ins.Operands[0] as TokenIntLiteral;
         str.Emit(new OrgDirective((uint)il.Value));
     }
 }
예제 #21
0
        public override void Assemble(CodeGenerator cgen, Instruction ins, BytecodeStream str)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                BinaryWriter bw = new BinaryWriter(ms);
                foreach (AbstractToken tok in ins.Operands)
                {
                    if (tok is TokenIntLiteral)
                    {
                        bw.Write((byte)((TokenIntLiteral)tok).Value);
                    }
                }

                str.Emit(new QuasarData(ms.ToArray()));
            }
        }
예제 #22
0
파일: Vm.cs 프로젝트: MuranagaTamura/LessVm
        public bool OpMoveD(BytecodeStream code)
        {
            int  rsId = code.ReadByte();
            int  rtId = code.ReadByte();
            uint a;

            if (!TryGetReg(rsId, out a))
            {
                return(PushRegError(rsId));
            }
            if (!TrySetRegUi32(rtId, a))
            {
                return(PushRegError(rtId));
            }
            return(true);
        }
예제 #23
0
 public override void Assemble(CodeGenerator cgen, Instruction ins, BytecodeStream str)
 {
     if (ins.Operands.Count != 1)
     {
         cgen.CreateError("{0} does not take {1} arguments!", ins.Name, ins.Operands.Count);
     }
     else if (!(ins.Operands[0] is TokenRegister))
     {
         cgen.CreateError("Unsupported addressing mode for instruction '{0}'", ins.Name);
     }
     else
     {
         TokenRegister src = ins.Operands[0] as TokenRegister;
         str.Emit(new QuasarInstruction(Opcode.LCTL, new RegisterOperand(src.Register)));
     }
 }
예제 #24
0
파일: Vm.cs 프로젝트: MuranagaTamura/LessVm
        public bool InitMemory(BytecodeStream mem, int start, int end)
        {
            int length = end - start;

            if (length < 0)
            {
                return(PushError("メモリの初期化失敗しました"));
            }
            for (int i = 0; i < length && i < mem.Size; ++i)
            {
                if (!_memory.TrySetUi8(start + i, mem.ReadByte()))
                {
                    return(PushError("メモリの初期化に失敗しました"));
                }
            }
            return(true);
        }
예제 #25
0
파일: Vm.cs 프로젝트: MuranagaTamura/LessVm
        public void Init(BytecodeStream code, object instance = null)
        {
            _code   = code;
            _memory = new MemoryObj(MEM_SIZE);
            _errors.Clear();

            _registers = new Register[REG_SIZE];
            for (int i = 0; i < REG_SIZE; ++i)
            {
                _registers[i] = new Register();
            }
            _registers[BASE_PTR][0]  = MEM_SIZE;
            _registers[STACK_PTR][0] = MEM_SIZE;

            _instance = instance;
            _methods.Clear();
            RegisterBuildMethods();
        }
예제 #26
0
파일: facade.cs 프로젝트: possientis/Prog
 public void Compile(InputStream input, BytecodeStream output)
 {
     // creating scanner from InputStream
     Scanner scanner = new Scanner(input);
     // creating builder for abstract syntax tree
     ProgramNodeBuilder builder = new ProgramNodeBuilder();
     // creating Parser
     Parser parser = new Parser();
     // parsing using scanner and builder, hence creating AST
     parser.Parse(scanner, builder);
     // creating target code generator
     RISCCodeGenerator generator = new RISCCodeGenerator(output);
     // retrieving abstract syntax tree from builder
     ProgramNode parseTree = builder.GetRootNode();
     // generating target code from AST and generator
     parseTree.Traverse(generator);
     Console.WriteLine("compilation complete");
 }
예제 #27
0
파일: Vm.cs 프로젝트: MuranagaTamura/LessVm
        private bool GetRegVal(
            BytecodeStream code,
            out int rsId,
            out uint a,
            out uint b)
        {
            rsId = code.ReadByte();
            int rtId = code.ReadByte();

            b = 0;
            if (!TryGetReg(rsId, out a))
            {
                return(PushRegError(rsId));
            }
            if (!TryGetReg(rtId, out b))
            {
                return(PushRegError(rtId));
            }
            return(true);
        }
예제 #28
0
파일: Vm.cs 프로젝트: MuranagaTamura/LessVm
        private bool OpCmpHelper(
            BytecodeStream code,
            Func <uint, uint, bool> isSign)
        {
            int  rsId = code.ReadByte();
            int  rtId = code.ReadByte();
            uint a, b;

            if (!TryGetReg(rsId, out a))
            {
                return(PushRegError(rsId));
            }
            if (!TryGetReg(rtId, out b))
            {
                return(PushRegError(rtId));
            }

            IsZero = (a - b) == 0;
            IsSign = isSign(a, b);

            return(true);
        }
 public RISCCodeGenerator(BytecodeStream output)
 {
 }
예제 #30
0
 public Compiler()
 {
     _parser         = new Parser();
     _scanner        = new Scanner();
     _bytecodeStream = new BytecodeStream();
 }
예제 #31
0
 public StackMachineCodeGenerator(BytecodeStream stream) : base(stream)
 {
 }
예제 #32
0
 public RISCCodeGenerator(BytecodeStream output) : base(output)
 {
 }
예제 #33
0
        public override void Assemble(CodeGenerator cgen, Instruction ins, BytecodeStream str)
        {
            if (ins.Operands.Count != 1)
            {
                cgen.CreateError("Instruction {0} does not take {1} operand(s)!", ins.Name, ins.Operands.Count);
            }
            else if (ins.Operands[0] is TokenIdentifier)
            {
                TokenIdentifier     label = ins.Operands[0] as TokenIdentifier;
                QuasarConditionCode code  = QuasarConditionCode.NONE;
                switch (ins.Name)
                {
                case "bc":
                    code = QuasarConditionCode.C;
                    break;

                case "bs":
                    code = QuasarConditionCode.N;
                    break;

                case "bv":
                    code = QuasarConditionCode.V;
                    break;

                case "bz":
                    code = QuasarConditionCode.Z;
                    break;

                case "be":
                    code = QuasarConditionCode.EQ;
                    break;

                case "bne":
                    code = QuasarConditionCode.NE;
                    break;

                case "ba":
                    code = QuasarConditionCode.AB;
                    break;

                case "bb":
                    code = QuasarConditionCode.BL;
                    break;

                case "bl":
                    code = QuasarConditionCode.LT;
                    break;

                case "bg":
                    code = QuasarConditionCode.GT;
                    break;

                case "ble":
                    code = QuasarConditionCode.LE;
                    break;

                case "bge":
                    code = QuasarConditionCode.GE;
                    break;
                }
                str.Emit(new QuasarInstruction(Opcode.BR, new ConditionCodeOperand(code), new SymbolReferenceOperand(label.Value, true)));
            }
        }
예제 #34
0
파일: facade.cs 프로젝트: possientis/Prog
 public RISCCodeGenerator(BytecodeStream output)
 {
     Console.WriteLine("creating target code generator");
 }
 protected CodeGenerator(BytecodeStream bytecodeStream)
 {
     this.bytecodeStream = bytecodeStream;
 }
 public RiscCodeGenerator(BytecodeStream output)
 {
 }
예제 #37
0
 protected CodeGenerator(BytecodeStream stream)
 {
     this.output = stream;
 }