Example #1
0
        private OpCode OpcodeFromInstruction(AtlasParser.InstructionContext context)
        {
            AtlasParser.InstructionCodeContext         instructionCode = context.instructionCode();
            AtlasParser.InstructionCodeNeedsArgContext instructionNeedsArgumentCode = context.instructionCodeNeedsArg();

            string s = instructionCode != null?instructionCode.GetText() : (instructionNeedsArgumentCode != null ? instructionNeedsArgumentCode.GetText() : "MISSINGINSTRUCTIONERROR");

            return(OpcodeFromString(s));
        }
Example #2
0
        public override void ExitInstruction(AtlasParser.InstructionContext context)
        {
            OpCode opCode   = OpcodeFromInstruction(context);
            int    argSize  = AtlasCPU.ArgSizeFromOpCode(opCode);
            bool   needsArg = argSize != 0;

            if (m_currentPass == AssemblerSemanticAnalysisPass.RecordLabels)
            {
                //space for opcode
                m_fileSize++;

                //does this instruction take an argument?
                if (!needsArg)
                {
                    return;
                }

                //space for arg
                m_fileSize += argSize;
            }
            else if (m_currentPass == AssemblerSemanticAnalysisPass.CodeGen)
            {
                EmitOpCode(opCode);

                //does this instruction take an argument?
                if (!needsArg)
                {
                    return;
                }

                //if it does, emit that value

                int argVal = 0;

                //argument is a label
                if (context.ID() != null)
                {
                    //check that the label exists
                    if (!m_labels.ContainsKey(context.ID().Symbol.Text))
                    {
                        SematicError(context, "Label " + context.ID().GetText() + " is undefined");
                    }

                    argVal = m_labels[context.ID().Symbol.Text];
                }
                //argument is a literal
                else if (context.literal() != null)
                {
                    argVal = GetLiteralValue(context.literal());
                }

                EmitWord(argVal);
            }
        }