Example #1
0
 public static void Main(string[] args)
 {
     InputStream input = new InputStream("source.c");
     BytecodeStream output = new BytecodeStream();
     Compiler compiler = new Compiler();
     compiler.Compile(input, output);
 }
Example #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)));
         }
     }
 }
Example #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)));
         }
     }
 }
Example #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);
     }
 }
Example #5
0
 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);
 }
Example #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);
        }
Example #7
0
        public bool OpJmp(BytecodeStream code)
        {
            ushort address = code.ReadChar();

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

            return(PushError("参照不可能な箇所にジャンプしようとしました"));
        }
Example #8
0
        public bool OpMoveID(BytecodeStream code)
        {
            int  regId = code.ReadByte();
            uint imm   = (uint)code.ReadInt();

            if (!TrySetRegUi32(regId, imm))
            {
                return(PushRegError(regId));
            }
            return(true);
        }
Example #9
0
        public bool OpMoveIW(BytecodeStream code)
        {
            int    regId = code.ReadByte();
            ushort imm   = code.ReadChar();

            if (!TrySetRegUi16(regId, imm))
            {
                return(PushRegError(regId));
            }
            return(true);
        }
Example #10
0
        public bool OpMoveIB(BytecodeStream code)
        {
            int  regId = code.ReadByte();
            byte imm   = code.ReadByte();

            if (!TrySetRegUi8(regId, imm))
            {
                return(PushRegError(regId));
            }
            return(true);
        }
Example #11
0
        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));
        }
Example #12
0
        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}"));
        }
Example #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));
     }
 }
        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);
    }
Example #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)));
            }
        }
Example #17
0
        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);
        }
Example #18
0
        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)));
        }
Example #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]));
     }
 }
Example #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));
     }
 }
Example #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()));
            }
        }
Example #22
0
        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);
        }
Example #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)));
     }
 }
Example #24
0
        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);
        }
Example #25
0
        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();
        }
Example #26
0
 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");
 }
Example #27
0
        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);
        }
Example #28
0
        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)
 {
 }
Example #30
0
 public Compiler()
 {
     _parser         = new Parser();
     _scanner        = new Scanner();
     _bytecodeStream = new BytecodeStream();
 }
 public StackMachineCodeGenerator(BytecodeStream stream) : base(stream)
 {
 }
Example #32
0
 public RISCCodeGenerator(BytecodeStream output) : base(output)
 {
 }
Example #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)));
            }
        }
Example #34
0
 public RISCCodeGenerator(BytecodeStream output)
 {
     Console.WriteLine("creating target code generator");
 }
 protected CodeGenerator(BytecodeStream bytecodeStream)
 {
     this.bytecodeStream = bytecodeStream;
 }
 public RiscCodeGenerator(BytecodeStream output)
 {
 }
Example #37
0
 protected CodeGenerator(BytecodeStream stream)
 {
     this.output = stream;
 }