Beispiel #1
0
        public override bool Visit(ProgramSection node)
        {
            Visit((TopLevelDeclarationSection)node);

            outputCode("int main( int argc, const char* argv[] )", false, true);
            outputCode("{", false, true);
            PushIdent();
            traverse(node.block);
            outputCode("return 0;", true, true);
            PopIdent();
            outputCode("}", false, true);
            return(true);
        }
Beispiel #2
0
        public override Value Visit(ProgramSection node)
        {
            traverse(node.decls);
            evalCtx.inTopLevel = false;

            Function func = new Function(module, "main", TypeRef.CreateInt32(), new TypeRef[0]);

            func.SetLinkage(LLVMLinkage.CommonLinkage);
            // set 'func' and 'main' instance vars (current function and main function)
            evalCtx.main = evalCtx.func = func;

            // Create a new basic block to start insertion into.
            BasicBlock bb = func.AppendBasicBlock("entry");

            builder.SetInsertPoint(bb);

            traverse(node.block);

            //	Value ret = builder.BuildLoad(values.ElementAt(values.Count - 1).Value);
            //	return builder.BuildReturn(Value.CreateConstInt32(0));
            Value ret = Value.CreateConstInt32(0);

            return(builder.BuildReturn(ret));
        }
 public virtual T Visit(ProgramSection node)
 {
     Visit((TopLevelDeclarationSection)node);
     return(traverse(node.block));
 }
Beispiel #4
0
 public override bool Visit(ProgramSection node)
 {
     Visit((TopLevelDeclarationSection)node);
     TraversePrint(node.block);
     return(true);
 }
Beispiel #5
0
        private bool ResolveAssemblerInstruction(AssemblyRow row)
        {
            try
            {
                if (row.Instruction.Mnemonic == AssemblerConstans.AssemblerInstructions.Org)
                {
                    var parser = new OrgInstructionParser(row, AssembledProgram.SymbolTable);
                    var result = parser.Parse();
                    if (result.ResultCode == ParseResultCode.Error)
                    {
                        throw new Z80AssemblerException(result.Message);
                    }

                    if (result.ResultCode == ParseResultCode.Ok)
                    {
                        ushort orgOperandValue = result.ResultValue;
                        if (row.State != AssemblyRowState.Interpreted)
                        {
                            if (m_CurrentProgramSection == null)
                            {
                                m_CurrentProgramSection = new ProgramSection {
                                    ProgramStartAddress = orgOperandValue
                                };
                            }
                            else
                            {
                                m_CurrentProgramSection.SectionLength =
                                    (ushort)(AssembledProgram.SymbolTable[AssemblerConstans.LocationCounterSymbol].Value -
                                             m_CurrentProgramSection.ProgramStartAddress);
                                AssembledProgram.ProgramSections.Add(m_CurrentProgramSection);
                                m_CurrentProgramSection = new ProgramSection {
                                    ProgramStartAddress = orgOperandValue
                                };
                            }
                        }

                        AssembledProgram.SymbolTable[AssemblerConstans.LocationCounterSymbol].Value = orgOperandValue;
                        if (!string.IsNullOrEmpty(row.Label))
                        {
                            AssembledProgram.SymbolTable[row.Label] = new Symbol {
                                Name = row.Label, Value = orgOperandValue, LineNumber = row.RowNumber
                            };
                        }

                        row.State = AssemblyRowState.Interpreted;
                    }

                    return(true);
                }

                if (row.Instruction.Mnemonic == AssemblerConstans.AssemblerInstructions.End)
                {
                    if (row.State != AssemblyRowState.Interpreted)
                    {
                        if (m_CurrentProgramSection == null)
                        {
                            throw new Z80AssemblerException(" Hiányzó 'ORG' utasítás!");
                        }

                        m_CurrentProgramSection.SectionLength =
                            (ushort)(AssembledProgram.SymbolTable[AssemblerConstans.LocationCounterSymbol].Value - m_CurrentProgramSection.ProgramStartAddress);
                        AssembledProgram.ProgramSections.Add(m_CurrentProgramSection);
                        m_CurrentProgramSection = null;
                    }
                    row.State = AssemblyRowState.Interpreted;
                    return(true);
                }

                if (row.State == AssemblyRowState.Interpreted || row.State == AssemblyRowState.Assembled)
                {
                    return(true);
                }

                if (row.Instruction.Mnemonic == AssemblerConstans.AssemblerInstructions.Equ ||
                    row.Instruction.Mnemonic == AssemblerConstans.AssemblerInstructions.EqualSymbol)
                {
                    var parser = new EquInstructionParser(row, AssembledProgram.SymbolTable);

                    var parseResult = parser.Parse();
                    if (parseResult.ResultCode == ParseResultCode.Error)
                    {
                        throw new Z80AssemblerException(parseResult.Message);
                    }

                    if (parseResult.ResultCode == ParseResultCode.Ok)
                    {
                        var symbol = AssembledProgram.SymbolTable.First(s => s.Key == row.Label.ToUpper());
                        symbol.Value.Value = parseResult.ResultValue;
                        row.State          = AssemblyRowState.Interpreted;
                    }
                    else
                    {
                        row.State = AssemblyRowState.ContainsFutureSymbol;
                    }

                    return(true);
                }

                row.InstructionBytes.Clear();
                var resolver = AssemblerInstructionResolver.Create(row.Instruction.Mnemonic,
                                                                   AssembledProgram.SymbolTable,
                                                                   IncludeDirectories);
                var resolverResult = resolver.Resolve(row);

                if (resolverResult.ResultCode == ParseResultCode.Error)
                {
                    throw new Z80AssemblerException(resolverResult.Message);
                }

                row.InstructionBytes.AddRange(resolver.InstructionBytes);
                row.State = resolverResult.ResultCode == ParseResultCode.Ok
                    ? AssemblyRowState.Assembled
                    : AssemblyRowState.ContainsFutureSymbol;
            }
            catch (Z80AssemblerException exception)
            {
                m_StatusMessage.AppendLine($"{exception.Message} Sor:{row.RowNumber}");
                WrongLineNumber = row.RowNumber;
                return(false);
            }
            return(true);
        }