Ejemplo n.º 1
0
 public void AddChild( DASM16Builder child )
 {
     if ( child.Length > 0 )
     {
         myChildren.Add( Length, child );
         InstructionCount += child.InstructionCount;
         Length += child.Length;
     }
 }
Ejemplo n.º 2
0
 public void AddMethod( String identifier, ValType[] paramTypes, ValType returnType, bool inline, DASM16Builder method )
 {
     if ( inline )
         myMethods.Add( new MethodInfo( identifier, paramTypes, returnType ), method );
     else
     {
         String label = "__" + Identifier + "__" + identifier;
         foreach ( ValType type in paramTypes )
             label += "__" + type.Identifier;
         stStaticASM.AddLabel( label, false );
         stStaticASM.AddChild( method );
         AddMethod( identifier, paramTypes, returnType, true, "JSR " + label );
     }
 }
Ejemplo n.º 3
0
        public static DASM16Builder Parse( String str )
        {
            DASM16Builder builder = new DASM16Builder();

            int offset = 0;
            while ( offset < str.Length )
            {
                SkipWhitespace( str, ref offset );
                if ( IsNextCommand( str, offset ) )
                {
                    int startOffset = offset;
                    DASM16CommandInfo cmd = ReadCommand( str, ref offset );
                    switch ( cmd.Command )
                    {
                        case DASM16Command.Define:
                        case DASM16Command.LDefine:
                            ushort val;
                            try
                            {
                                val = ParseLiteral( cmd.Args[ 1 ] );
                            }
                            catch ( AssemblerException )
                            {
                                throw new AssemblerException( str, startOffset, "Invalid second argument for definition, expected literal value" );
                            }
                            builder.AddConstant( cmd.Args[ 0 ], val, cmd.Command == DASM16Command.LDefine );
                            break;
                        case DASM16Command.Include:
                            String path = cmd.Args[ 0 ];
                            if ( !File.Exists( path ) )
                                throw new AssemblerException( str, startOffset, "Cannot include file at \"" + path + "\", file does not exist" );
                            String code = File.ReadAllText( path );
                            try
                            {
                                builder.AddChild( Parse( code ) );
                            }
                            catch ( AssemblerException e )
                            {
                                throw new AssemblerException( str, startOffset, e.Message + "\n  in included file \"" + path + "\"" );
                            }
                            break;
                    }
                    continue;
                }
                if ( IsNextLabelDefinition( str, offset ) )
                {
                    int startOffset = offset;
                    if ( ++offset >= str.Length )
                        throw new AssemblerException( str, offset, "Unexpected end of file" );

                    bool local = str[ offset ] == ':';
                    if ( local )
                        ++offset;
                    SkipWhitespace( str, ref offset );

                    if ( offset >= str.Length )
                        throw new AssemblerException( str, offset, "Unexpected end of file" );

                    String label = ReadLabel( str, ref offset );

                    if ( label.Length == 0 )
                        throw new AssemblerException( str, offset, "Label identifier expected" );

                    builder.AddLabel( label, local );
                    continue;
                }
                if ( IsNextOpcode( str, offset ) )
                {
                    builder.AddInstruction( ReadInstruction( str, ref offset ) );
                    continue;
                }
                if ( offset < str.Length )
                    throw new AssemblerException( str, offset, "Expected .command, :label or opcode" );
            }
            return builder;
        }