Example #1
0
File: test.go Project: DanB91/dcpu
// compile compiles the given AST and returns a CPU instance ready to run the code.
func (t *Test) compile(ast *dp.AST) (c *cpu.CPU, err error) {
	var bin []cpu.Word

	if bin, t.dbg, err = asm.Assemble(ast); err != nil {
		return
	}

	if !hasExit(bin) {
		return nil, errors.New(fmt.Sprintf(
			"%s: Program has no unconditional EXIT. This means the test will run indefinitely.", t.file))
	}

	t.profile = prof.New(bin, t.dbg)

	c = cpu.New()
	copy(c.Store.Mem[:], bin)

	c.ClockSpeed = time.Duration(time.Duration(*clock))
	c.Trace = func(pc, op, a, b cpu.Word, s *cpu.Storage) {
		t.parseInstruction(pc, op, a, b, s, *trace)
	}

	c.InstructionHandler = func(pc cpu.Word, s *cpu.Storage) {
		t.profile.Update(pc, s)
	}

	c.NotifyBranchSkip = func(pc, cost cpu.Word) {
		t.profile.UpdateCost(pc, cost)
	}

	return
}
Example #2
0
File: main.go Project: DanB91/dcpu
func main() {
	parseArgs()

	// Collect all the source code into the given AST.
	// This takes care of resolving includes and identifying
	// unresolved label references.
	var ast parser.AST

	err := util.ReadSource(&ast, infile, includes)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Source reader: %v\n", err)
		os.Exit(1)
	}

	// Run pre-processors on the generated AST.
	err = PreProcess(&ast, *optimize)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Pre processor: %v\n", err)
		os.Exit(1)
	}

	// Dump AST or source code if necessary.
	if *dumpast || *dumpsrc {
		err = writeSource(&ast, *outfile, *dumpast)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Source writer: %v\n", err)
			os.Exit(1)
		}
		os.Exit(0)
	}

	// Assemble program.
	program, dbg, err := asm.Assemble(&ast)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Assembler: %v\n", err)
		os.Exit(1)
	}

	// Run post-processors on generated binary code and debug symbols.
	err = PostProcess(program, dbg, *optimize)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Post processor: %v\n", err)
		os.Exit(1)
	}

	// Write debug file.
	if err = writeDebug(dbg, *debugfile); err != nil {
		fmt.Fprintf(os.Stderr, "Debug writer: %v\n", err)
		os.Exit(1)
	}

	// Write binary output.
	if err = writeProgram(program, *outfile, *littleendian); err != nil {
		fmt.Fprintf(os.Stderr, "Binary writer: %v\n", err)
		os.Exit(1)
	}
}
Example #3
0
// build builds the given package and its dependencies.
func build(importpath string, flags asm.AsmFlags) (err error) {
	files, _ := path.SourceFiles(importpath)
	if len(files) == 0 {
		return nil // No source files
	}

	var ast parser.AST
	for i := range files {
		err = ast.Parse(files[i], 0)
		if err != nil {
			return
		}
	}

	ar, err := asm.Assemble(&ast, flags)
	if err != nil {
		return
	}

	return writeArchive(ar, importpath)
}