Permalink
Browse files

First commit for major rewrite effort.

* Implemented new lexer, parser, ast.
* Implemented archive file format.
* Work begun on new assembler.
* Removed existing commands and packages as they will be replaced by
  new implementations.
  • Loading branch information...
1 parent 9d455a9 commit ae08d6c4368106d079f2c1b56c87a978684e5b56 @jteeuwen committed Nov 8, 2012
Showing with 3,064 additions and 6,732 deletions.
  1. +25 −47 README.md
  2. +66 −0 archive/README.md
  3. +141 −0 archive/archive.go
  4. +108 −0 archive/archive_test.go
  5. +5 −0 archive/doc.go
  6. +139 −0 archive/read.go
  7. +37 −0 archive/symbol.go
  8. +108 −0 archive/write.go
  9. +2 −111 asm/README.md
  10. +17 −369 asm/asm.go
  11. +48 −176 asm/asm_test.go
  12. +0 −87 asm/const.go
  13. +0 −81 asm/debug.go
  14. +5 −0 asm/doc.go
  15. +0 −24 asm/error.go
  16. +0 −253 asm/funcs.go
  17. +0 −71 asm/lang.go
  18. +0 −22 dcpu-asm/OPTIMIZATIONS.md
  19. +0 −136 dcpu-asm/README.md
  20. +0 −44 dcpu-asm/binarywriter.go
  21. +0 −23 dcpu-asm/debugwriter.go
  22. +0 −233 dcpu-asm/main.go
  23. +0 −76 dcpu-asm/postprocessor.go
  24. +0 −78 dcpu-asm/pre_opt_shorthand.go
  25. +0 −56 dcpu-asm/pre_scrambler.go
  26. +0 −41 dcpu-asm/pre_stripper.go
  27. +0 −76 dcpu-asm/preprocessor.go
  28. +0 −35 dcpu-asm/sourcewriter.go
  29. +0 −30 dcpu-asm/version.go
  30. +0 −33 dcpu-data/README.md
  31. +0 −107 dcpu-data/main.go
  32. +0 −30 dcpu-data/version.go
  33. +0 −56 dcpu-data/wordwriter.go
  34. +0 −163 dcpu-prof/README.md
  35. +0 −33 dcpu-prof/ast.go
  36. +0 −46 dcpu-prof/command.go
  37. +0 −38 dcpu-prof/input.go
  38. +0 −100 dcpu-prof/list.go
  39. +0 −130 dcpu-prof/main.go
  40. +0 −60 dcpu-prof/source.go
  41. +0 −66 dcpu-prof/top.go
  42. +0 −30 dcpu-prof/version.go
  43. +0 −44 dcpu-run
  44. +0 −119 dcpu-test/README.md
  45. +0 −162 dcpu-test/main.go
  46. +0 −284 dcpu-test/test.go
  47. +0 −30 dcpu-test/version.go
  48. +7 −5 dcpu.lang
  49. +0 −121 lib/bootstrap/bootstrap.dasm
  50. +69 −0 lib/dcpu/bootstrap.dasm
  51. +15 −0 lib/dcpu/clock.dasm
  52. +16 −0 lib/dcpu/lem1802.dasm
  53. +6 −0 lib/dcpu/m35fd.dasm
  54. +35 −0 lib/dcpu/spc2000.dasm
  55. +27 −0 lib/dcpu/sped3.dasm
  56. +141 −0 parser/LANGUAGE.md
  57. +2 −1 parser/README.md
  58. +399 −349 parser/ast.go
  59. +61 −0 parser/ast_test.go
  60. +4 −23 parser/block.go
  61. +7 −36 parser/char.go
  62. +6 −14 parser/comment.go
  63. +16 −0 parser/constant.go
  64. +6 −0 parser/doc.go
  65. +162 −0 parser/dump.go
  66. +0 −24 parser/error.go
  67. +0 −118 parser/escape.go
  68. +4 −23 parser/expression.go
  69. +6 −24 parser/function.go
  70. +16 −0 parser/import.go
  71. +6 −24 parser/instruction.go
  72. +7 −15 parser/label.go
  73. +0 −67 parser/lang.go
  74. +0 −282 parser/lex.go
  75. +230 −0 parser/lexer.go
  76. +581 −0 parser/lexrules.go
  77. +0 −310 parser/lexstates.go
  78. +6 −14 parser/name.go
  79. +41 −33 parser/node.go
  80. +7 −38 parser/number.go
  81. +6 −14 parser/operator.go
  82. +15 −0 parser/package.go
  83. +0 −26 parser/parser_test.go
  84. +7 −16 parser/string.go
  85. +73 −47 parser/token.go
  86. +57 −0 parser/tree.go
  87. +0 −15 parser/util/README.md
  88. +0 −89 parser/util/astwriter.go
  89. +0 −123 parser/util/common.go
  90. +0 −221 parser/util/sourcereader.go
  91. +0 −214 parser/util/sourcewriter.go
  92. +0 −108 parser/verify.go
  93. +0 −20 prof/README.md
  94. +0 −55 prof/block.go
  95. +0 −147 prof/profile.go
  96. +0 −123 prof/profile_test.go
  97. +0 −85 prof/profiledata.go
  98. +0 −121 prof/reader.go
  99. +0 −187 prof/writer.go
  100. BIN testdata/hw.a
  101. +160 −0 testdata/hw.ast
  102. +39 −0 testdata/hw.dasm
  103. +123 −0 testdata/hw.tokens
View
@@ -1,64 +1,42 @@
## DCPU
-This repository contains [DCPU assembly](http://dcpu.com) utilities and code.
-Mostly commonly used library bits and bobs, along with a comprehensive
-assembler and unit testing framework.
-
-
-Commandline tools:
-
-* **dcpu-asm**: This is a commandline assembler with a wide range of options,
- including pre- and post-processors.
-* **dcpu-emu**: This is an experimental, graphical emulator with all known
- hardware components implemented.
-* **dcpu-data**: This is a small tool which generates DCPU assembly source
- from any input file. Useful if you want to embed binary data in your
- programs.
-* **dcpu-prof**: This is an interactive commandline tool that can analyze
- profiling data generated by the `prof` package.
-* **dcpu-test**: This program runs unit tests as defined in the `lib`
- directory. We use this to verify newly written code does what we
- want it to do.
-
-Packages:
-
-* **parser**: This holds a package that parses assembly source and turns it
- into an Abstract Syntax Tree.
-* **parser/util**: This package contains some parser related utility
- bits and bobs.
-* **asm**: This holds an assembler. It turns an AST into a compiled
- program, ready to be passed to the CPU for execution.
-* **cpu**: A CPU emulator implementation. It adds the necessary instructions
- to make unit tests behave properly. As such, it may not be ideal to use
- as a standalone emulator.
-* **prof**:This package holds a profiler for DASM code. It maintains
- information like cycle costs for a currently executing program.
- An emulator can use it to generate a profile file which can then be examined
- by the `dcpu-prof` tool for performance bottlenecks.
-
-DCPU asssembly:
-
-* **lib/**: This directory holds often used assembly code and unit tests.
-
-Misc stuff:
+This repository contains [DCPU](http://dcpu.com) utilities and code.
+Among wich are a parser, assembler and linker for a custom DCPU assembly
+language. Along with a graphical emulator/debugger/profiler.
+
+The toolchain defined in this project, closely mimics the build setup
+for the Go programming language. The Assembly source itself borrows a number
+of aspects from Go, among which is the way it defines packages and imports.
+
+For more information about the language itself, refer to `parser/LANGUAGE.md`.
+For examples, refer to the `testdata` directory, or the code in the `lib` folder.
+
+
+### Commandline tools:
+
+* **dcpu-emu**: This is a graphical emulator with all known hardware components
+ implemented. It doubles as a debugger and code profiler.
+
+
+### DCPU asssembly:
+
+* **lib**: This directory holds often used assembly code and unit tests.
+
+
+### Misc stuff:
* **dcpu.lang**: This file is a DCPU syntax file for GtkSourceView
compatible editors (like Gedit). It should be installed in the
language-specs directory.
For me this is at: `/usr/share/gtksourceview-3.0/language-specs/`.
-* **dcpu-run**: This shell script ties `dcpu-test` and `dcpu-prof` together
- to run unit tests on a given input source file. It generates profiling
- data and then displays a focused overview of this data.
* **fontedit**: This is a font editor for LEM1802 fonts.
Refer to the README of each individual tool for more info.
### Usage
-To install one or more of the tools, do the following:
-
- $ go get github.com/jteeuwen/dcpu/<pkg|cmd>
+ $ go get github.com/jteeuwen/dcpu/...
The commandline programs will be installed where ever your `$GOBIN` points to.
They are now ready for use.
View
@@ -0,0 +1,66 @@
+## DCPU Archive
+
+This package defines the archive file format.
+
+An archive contains all compiled binary code for a given package, along with
+debug symbols and source context.
+
+These files are generated by the assembler and used by the linker
+to construct the final binary program which can be run on a DCPU emulator.
+
+Additional uses for these archives cover profiling and debugging. Along with
+source file context for each instruction (filename, line- and column numbers),
+it can define debugger break-points and other things in the form of bit flags
+in the `Flags` field of the `Symbol` type.
+
+
+### Encoding
+
+* An archive file is gzip compressed.
+* All multi-byte data is encoded as **Little Endian**.
+* Strings are UTF-8 encoded, written out as raw bytes.
+ They are preceeded by a 16-bit unsigned integer, defining the string length.
+
+
+### File structure
+
+* The file starts with a 4-byte header, defining the following fields:
+ * 3-byte magic spelling: `DAR`.
+ * 1-byte version number, which should be `<= archive.Version`.
+
+* It then defines the package import path associated with the code in this
+ archive. This is encoded as a string.
+
+* Now follows a 16-bit unsigned integer, defining the number of file paths.
+* N number of source file paths associated with the code in this archive.
+ The order of these is important and should not be altered, as will be
+ explained later. These are encoded as strings.
+
+* 16-bit unsigned integer defining the length of the symbol list and
+ instruction list. These are two separate lists, which must always have the
+ same length. This archive format defines 1 symbol for every instruction.
+
+* N number of 8-byte Symbol structures. Each structure has the following fields:
+ * `uint16 File`: The index of the file path to which the associated
+ instruction belongs. It is an index into the File path list previously read.
+ This field is why the order of the file path list must be retained.
+ * `uint16 Line`: The line number at which the associated instruction was
+ defined in the original source.
+ * `uint16 Col`: The column number at which the associated instruction was
+ defined in the original source.
+ * `uint16 Flags`: Bit flags for this symbol. These flags are as follows:
+
+ * **Breakpoint** (`0x01`): Symbol has a debug breakpoint associated with it.
+ This is a signal to a debugger to halt execution at the instruction
+ associated with this symbol.
+
+* N number of 16-bit unsigned integer instructions. These are the words
+ that make up the actual program which can be run on the DCPU.
+
+
+### License
+
+DCPU, 0x10c and related materials are Copyright 2012 Mojang.
+
+Unless otherwise stated, all of the work in this project is subject to a
+1-clause BSD license. Its contents can be found in the enclosed LICENSE file.
View
@@ -0,0 +1,141 @@
+// This file is subject to a 1-clause BSD license.
+// Its contents can be found in the enclosed LICENSE file.
+
+package archive
+
+import (
+ "compress/gzip"
+ "fmt"
+ "io"
+)
+
+const (
+ Magic = "DAR"
+ Version = 1
+)
+
+// validArchive returns true if the given header data matches our expectations.
+// This means the magic matches and the version is less than or equal to the
+// current package version.
+func validArchive(d []byte) bool {
+ return d[0] == Magic[0] &&
+ d[1] == Magic[1] &&
+ d[2] == Magic[2] &&
+ d[3] <= Version
+}
+
+// Archive represents a package archive file.
+type Archive struct {
+ Package string // Package name for this archive.
+ Files []string // List of file names associated with this package.
+ Symbols []*Symbol // List of debug symbols.
+ Code []uint16 // List of instructions.
+}
+
+// New creates a new, empty archive instance.
+func New() *Archive {
+ a := new(Archive)
+ return a
+}
+
+// Read reads archive contents from the given stream.
+func (a *Archive) Read(r io.Reader) (err error) {
+ defer func() {
+ x := recover()
+ if x != nil {
+ err = fmt.Errorf("Archive.Read: %v", x)
+ }
+ }()
+
+ gz, err := gzip.NewReader(r)
+ if err != nil {
+ return
+ }
+
+ defer gz.Close()
+
+ // Read archive header.
+ var data [4]uint8
+ read(gz, data[:])
+
+ // Validate header.
+ if !validArchive(data[:]) {
+ panic("Invalid archive.")
+ }
+
+ // Read package name.
+ a.Package = readString(gz)
+
+ // Read file names.
+ a.Files = make([]string, readU16(gz))
+ for i := range a.Files {
+ a.Files[i] = readString(gz)
+ }
+
+ // Read size of Symbol and instruction lists.
+ size := readU16(gz)
+
+ // Read symbols.
+ a.Symbols = make([]*Symbol, size)
+ for i := range a.Symbols {
+ a.Symbols[i] = new(Symbol)
+ a.Symbols[i].read(gz)
+ }
+
+ // Read instructions.
+ a.Code = make([]uint16, size)
+ for i := range a.Code {
+ a.Code[i] = readU16(gz)
+ }
+
+ return
+}
+
+// Write writes the archive contents to the given stream.
+func (a *Archive) Write(w io.Writer) (err error) {
+ defer func() {
+ x := recover()
+ if x != nil {
+ err = fmt.Errorf("Archive.Write: %v", x)
+ }
+ }()
+
+ // Size of symbol and code lists must be equal.
+ if len(a.Symbols) != len(a.Code) {
+ return fmt.Errorf("Archive.Write: Symbol and Code lists must be of equal length.")
+ }
+
+ gz := gzip.NewWriter(w)
+ if err != nil {
+ return
+ }
+
+ defer gz.Close()
+
+ // Write archive header.
+ write(gz, []byte{Magic[0], Magic[1], Magic[2], Version})
+
+ // Write package name.
+ writeString(gz, a.Package)
+
+ // Write file names.
+ writeU16(gz, uint16(len(a.Files)))
+ for _, file := range a.Files {
+ writeString(gz, file)
+ }
+
+ // Write size of symbol and instruction lists.
+ writeU16(gz, uint16(len(a.Symbols)))
+
+ // Write symbols.
+ for _, sym := range a.Symbols {
+ sym.write(gz)
+ }
+
+ // Write instructions.
+ for _, word := range a.Code {
+ writeU16(gz, word)
+ }
+
+ return
+}
Oops, something went wrong.

0 comments on commit ae08d6c

Please sign in to comment.