Repository navigation
Output Formats
Besides the binary image, BespokeASM can write a human- or tool-readable version of an assembly, called a pretty-print format. Select one with -p (--pretty-print) and -t (--pretty-print-format):
bespokeasm compile -c isa.yaml -n -p -t intel_hex program.asm
bespokeasm compile -c isa.yaml -n -p -t symbols --pretty-print-output program.json program.asmThe text goes to stdout unless --pretty-print-output names a file. Without -t, the format is listing.
BespokeASM has several built-in formats. An instruction set configuration can also define its own formats, which exist only for that configuration; several of the example configurations in the BespokeASM repository do. Generated ISA documentation (bespokeasm docs) lists every format an instruction set accepts, and shell completion offers them for -t.
-
-tnames are checked after the configuration loads. A name the configuration does not define is an error that lists the available formats. - Built-in names match without regard to case (
-t Intel_Hex); names a configuration defines must match exactly. - Giving
-twithout-pis a warning.
| Format | Output |
|---|---|
listing |
The source listing: line numbers, addresses, assembled bytes, source and comments. #annotate listing shapes it. |
hex |
A text hex dump of the assembled bytes. |
intel_hex |
Intel HEX encoding of the assembled bytes. |
symbols |
Every label and constant as JSON, with its annotations. |
hex and intel_hex are defined formats, written in the same schema a configuration uses (builtin_formats.yaml). Both need 8-bit words.
minhex was a built-in format before BespokeASM 0.9.0. It is now defined by the Minimal CPU example configuration.
symbols writes JSON for other tools. Symbols that the configuration predefines come first, naming the configuration file and with a null line; then every label and constant in source, in the order they are defined.
{
"format": "bespokeasm-symbols",
"format_version": 1,
"bespokeasm_version": "0.9.0",
"isa": { "name": "my-cpu", "version": "1.0.0", "word_size": 8, "address_size": 16 },
"source_file": "program.asm",
"symbols": [
{ "name": "STACK_TOP", "kind": "constant", "scope": "global", "value": 61440,
"file": "isa.yaml", "line": null, "annotations": {} },
{ "name": "pa", "kind": "label", "scope": "global", "value": 275,
"file": "program.asm", "line": 27,
"annotations": { "xmin": { "arguments": { "kind": "var", "type": "int" }, "resolved_symbols": {} } },
"data": { "size": 2, "words": [0, 0] } }
]
}-
kindislabelorconstant;scopeisglobal,fileorlocal;valueis a label's address or a constant's value. -
fileis relative to the main source file's directory. -
annotationsholds each namespace's arguments, with defaults applied, and the resolved value of eachsymbol-typed argument. An argument that was not given and has no default is left out. - An annotated label also has
data: the size, in words, and the words of its data run. -
verbatimlists every verbatim block in source order, each with itsnamespace,part(nullwhen it names none),file,lineandtext.
A top-level output_formats section declares formats. Each becomes a -t value for that configuration. A format is a template with named sections; each section runs one generator and sets its options.
The examples on this page come from the Minimal 64x4 configuration's xmin format, which writes an Extended Min script: a declaration for each symbol carrying an xmin annotation, then the assembled code as Intel HEX.
output_formats:
xmin:
documentation:
description: Extended Min source file with exported symbols and an asm block.
requires_word_size: 8
template: |
# {source_file}: generated by BespokeASM {bespokeasm_version} ({isa_name} {isa_version})
{?{header}
}{symbols}{?
{script}}
asm
{code}
sections:
code:
generator: intel_hex
symbols:
generator: exports
annotation: xmin
order: address
lines_key: kind
lines:
addr: "int {name} := 0x{?{address:04x}}{?{value:04x}}"
var: "{type} {name}{?[{count}]} @ 0x{address:04x}"
const: "{type} {name} := {value}"
header:
generator: verbatim
namespace: xmin
part: header
script:
generator: verbatim
namespace: xminFor a routine and three int variables annotated with #annotate xmin, this writes:
# addw.min64x4: generated by BespokeASM 0.9.0 (slu4-min64x4-asm 1.1.0)
int AddW := 0x0100
int pa @ 0x0113
int pb @ 0x0115
int pr @ 0x0117
asm
:10010000761301A415017E1701761401F316017E02
:0901100018016900000000000064
:00000001FF
| Key | Meaning |
|---|---|
documentation.description |
Shown in the generated ISA documentation. |
requires_word_size |
Selecting the format on an instruction set with another word size is an error. |
template |
The text of the output, with fields. |
sections |
The named sections the template places, each a generator and its options. |
shared |
A shared set to start from (see Shared definitions). |
predefined |
Memory zones and constants that exist only while the format is selected. |
A format may not have the name of a built-in format. A configuration that declares formats should set general.min_version to 0.9.0 or later.
A template is text with {field} or {field:spec} placeholders, where spec is a Python format spec such as 04x, >8 or 03o. Fields are fixed names, not expressions.
- The template's fields are
source_file,isa_name,isa_versionandbespokeasm_version, plus one field per section, which takes no format spec. A section may not be named like a template field. - A section's text never starts or ends with a blank line; the template decides the spacing around it.
-
{?...}is an optional group: it is written only when every field inside it has a value.{name}{?[{count}]}givesbuf[16]whencountis 16, andpawhen there is no count. Groups do not nest. A group can hold a section, which has a value when its text is not empty: thexmintemplate above writes the blank line before{script}only when the source has#verbatim xminblocks, and the one after{header}only when it has#verbatim xmin headerblocks. - A field with no value prints as nothing, and its format spec is not applied.
- Literal braces are written
{{and}}.
Template mistakes, such as an unknown field or a format spec that cannot apply to it, are configuration errors.
| Generator | Output | Options |
|---|---|---|
intel_hex |
Intel HEX records, as -t intel_hex. Needs 8-bit words. |
none |
hex |
A text hex dump, as -t hex. Needs 8-bit words. |
none |
byte_list |
Comma-separated 0x.. bytes, for a format that embeds a blob. Needs 8-bit words. |
none |
listing |
The same text as -t listing, with #annotate listing applied. |
none |
rows |
Rows of bytes, with an address line at each .org. Needs 8-bit words. |
bytes_per_row, row_prefix, byte, org_line
|
byte_lines |
A table with one row per assembled byte. Needs 8-bit words. |
columns, then separator, header_rule, rule_separator
|
exports |
One line or table row per symbol that carries an annotation. | see below |
verbatim |
The text of verbatim blocks. |
namespace, part, separator
|
Using a generator that needs 8-bit words on another word size, leaving out a required option, or giving an option the generator does not take is a configuration error.
All four options are required.
| Option | Meaning |
|---|---|
bytes_per_row |
The most bytes in a row. |
row_prefix |
Text at the start of each row. |
byte |
A template for each byte, with the field byte. |
org_line |
A template written on its own line at each .org, with the field address. The current row ends first. |
Muted lines (#mute) are skipped. For example:
code:
generator: rows
bytes_per_row: 16
row_prefix: ":"
byte: "{byte:02x} "
org_line: "{address:04x}"8000
:0e fe 16 ff ff 0e 13 36 0e 80 36 38 24 80 37 37
:14 05 80 48 65 6c 6c 6f 2c 20 57 6f 72 6c 64 21
One table row per assembled byte, in address order, using the table options. Its fields are:
| Field | Value |
|---|---|
label |
The labels at that address, separated by spaces, on the line's first byte. |
address |
The byte's address. |
byte |
The byte. |
source |
The line's instruction text, on its first byte only. |
comment |
The line's comment, on its first byte only. |
Muted lines are skipped but keep their addresses, so the table has a gap there. Data that the configuration predefines (predefined.data) is not listed.
One line, or one table row, per symbol that carries an annotation.
| Option | Meaning |
|---|---|
annotation |
Required. The annotation namespace to export. |
order |
source (default): in the order symbols are defined. address: labels in address order, then constants in the order they are defined. |
unique |
name: two exports with one name is an error. |
name_pattern |
A regular expression every export name must match. |
reserved_names |
Names no export may have. |
comment_field and comment_prefix
|
With line templates: when the named field (such as a description argument) has a value, each of its lines is written above the export's line, starting with comment_prefix (such as "# "). Not with columns. |
blank_line_after |
With line templates: names a boolean argument. An export that sets it is followed by a blank line (except the last), so the source groups the declarations. The Minimal 64x4 xmin format uses its gap argument: #annotate xmin kind=const type=char gap. Not with columns. |
value_map |
Rewrites enum argument values in the output, as { type: { str: char } }. Lines and table cases are still chosen by the source value. |
lines_key and lines
|
Line templates: lines_key names an enum argument, and lines gives a template for each of its values. |
columns, separator, header_rule, rule_separator
|
A table instead of line templates. |
An exports section has either lines_key and lines, or columns, never both. Each export has these fields:
| Field | Value |
|---|---|
name |
The annotation's name argument, if it has one, else the symbol's name. Checked against name_pattern, reserved_names and unique. |
symbol |
The symbol's name. |
address |
A label's address; empty for a constant. |
value |
A constant's value; empty for a label. |
size |
The size of a label's data run, in words; empty when it has none. |
words |
The words of a label's data run, in hex, separated by spaces. |
| every argument | Each annotation argument. A symbol-typed argument is its resolved value; a count that the data set reads like a written one. An argument that was not given and has no default is empty. |
An annotation argument may not be named like one of these fields, except name.
Every #verbatim <namespace> block of one namespace and part, in source order, with included files' blocks where they are included.
| Option | Meaning |
|---|---|
namespace |
Required. An annotation namespace that accepts verbatim blocks (verbatim in its declaration). |
part |
A part the namespace declares: the section writes the blocks that name it, such as #verbatim xmin header. Without part, the blocks that name no part. |
separator |
The text between two blocks. Default: "\n\n", one blank line. |
Each block is written as it is in the source, except that trailing spaces are removed and the blank lines at its start and end are dropped. A source with no blocks gives an empty section.
byte_lines, and exports with columns, write a table.
columns:
- { field: address, header: Loc, format: "03o" }
- { field: byte, header: Data, format: "03o" }
- { field: label, header: Label, min_width: 8 }
- { field: source, header: Contents, min_width: 16 }
- { field: comment, header: Comments }
separator: " | "
header_rule: "="Loc | Data | Label | Contents | Comments
==================================================================================
004 | 023 | init | ld a, 1 |
005 | 001 | | |
006 | 034 | | ld [OUTPUT], a | init the screen display
007 | 200 | | |
010 | 367 | .loop | jm left_loop | call left_loop function
Each column names a field, or a list of fields, of which the cell shows the first that has a value (field: [address, value]). A column can also set:
| Key | Meaning |
|---|---|
header |
The column's header. Default: empty. The header row is written unless every header is empty. |
format |
A Python format spec for the value, such as 03o for octal. |
align |
left (default) or right. |
min_width |
The column's least width. Default 0. |
In an exports table, a column can instead choose its field per row, the table counterpart of lines_key: by names an enum argument, and cases maps its values to a field and format. A row whose value has no case gets an empty cell.
- header: value
by: kind
cases:
addr: { field: address, format: "#06x" }
const: { field: value }Layout:
- Each column is as wide as the largest of its
min_width, its header and its widest formatted value. The last column is not padded, and trailing spaces are removed. -
separatorgoes between columns. Default: two spaces. -
header_rule(one character) draws a line under the header row. -
rule_separatorgoes between columns in that rule, such as-+-under a" | "separator. - When only one of
separatorandrule_separatoris set, the other gap takes its width: spaces in the rows, or rule characters in the rule.
Several formats often need the same definitions. A top-level output_format_shared section holds named sets; a format names one with shared and adds or overrides items for itself.
output_format_shared:
xmin:
requires_word_size: 8
predefined:
memory_zones:
- name: XMIN_USER_CODE
start: 0x0100
end: 0x0FFF
sections:
symbols:
generator: exports
annotation: xmin
unique: name
output_formats:
xmin:
shared: xmin
template: ...
sections:
code:
generator: intel_hex
symbols:
order: address
lines_key: kind
lines: ...
xmin_listing:
shared: xmin
template: ...
sections:
code:
generator: listing
symbols:
columns: ...- A format is its set with the format's own keys applied over it.
predefinedmerges by item name: the same name replaces the set's item, a new name adds one.sectionsmerges by section name, and then by option key. - A set can hold any format key except
template,documentationandshared. - A section of the set that a format's template does not use is not part of that format; it is not checked there.
- An error in a merged value names the key where it was written, in the set or in the format.
A format's or set's predefined section declares memory_zones and constants, with the same keys as the top-level predefined ones. They exist only while a format that defines them is selected: the assembler adds them before it reads the source, and then they act like top-level ones.
Using one when no such format is selected is an error that names the formats that define it. So source that uses a format-scoped name compiles only with one of those formats, and not with a built-in one. That is one reason to define a listing format that shares the set, like xmin_listing above.
Source that should also build with other formats can choose its lines with the __OUTPUT_FORMAT__ preprocessor symbol, which names the selected format: for example, #if __OUTPUT_FORMAT__ == xmin around the .memzone XMIN_USER_CODE line, with an .org for other builds after #else.
- A format-scoped name may not reuse a top-level predefined name, name the
GLOBALzone, or appear twice in one list. - Two formats may define the same constant with different values, so their builds can assemble different bytes. Keeping them consistent is up to the configuration.
Configuration problems are errors, reported by key path when the configuration loads. These are warnings instead, and become errors with -W:
-
-tgiven without-p. - A section the template never uses.
- A shared set that no format uses.
-
header_rulewhen no column has a header. -
separatorandrule_separatorof different widths, so the rule does not line up with the columns. - An
exportssection whose annotation no symbol carries.