Skip to content

Extract the assembler into a reusable xasm library - #15

Open
epi wants to merge 10 commits into
pfusik:masterfrom
epi:epi/libxasm
Open

Extract the assembler into a reusable xasm library#15
epi wants to merge 10 commits into
pfusik:masterfrom
epi:epi/libxasm

Conversation

@epi

@epi epi commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Summary

Refactors xasm so the assembler core can be used as a library (libxasm) from any D program, not just through the xasm command-line tool. xebin is going to be its first user.
All the assembly logic previously living in source/app.d now sits behind an Assembler class in a new source/xasm/package.d module. app.d becomes a thin CLI frontend that drives that class.

What changed

  • New xasm module (source/xasm/package.d): the assembler implementation, moved out of app.d verbatim and wrapped in an Assembler class with a private implementation and a small public surface.
  • I/O is fully decoupled via delegates — the library performs no direct file access or console output. Callers supply:
    • SourceReader / BinaryReader — read source and binary-include files
    • DiagnosticSink — receive warnings and errors as structured Diagnostic values (Severity, filename, line, source line, message)
    • ListingSink — receive the assembly listing and label table line by line
  • Object code is staged through a buffer, exposed via assembler.object, instead of being written straight to a file.
  • Public API documented with ddoc; make libxasm.html generates the library reference. Label is now a struct, and the module carries a documented usage example (runnable as a unittest).

Notes

  • No changes to the assembler's behavior or output for CLI users; this is a structural refactor only.
  • Man page HISTORY section notes that xasm can now be used as a library, without assigning a concrete version number.

@pfusik pfusik left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for doing this! Generally LGTM with a few nits, and I think -i got broken.

Comment thread source/app.d Outdated
return options[letter - 'a'];
}

enum Severity { warning, error }

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only makes the code more verbose than a bool.

Comment thread source/app.d Outdated

enum Severity { warning, error }

struct Diagnostic {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this struct and not pass directly?

Comment thread source/app.d Outdated
Comment on lines +189 to +190
int line;
string sourceLine;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use lineNo/line consistently.

Comment thread source/app.d Outdated
}

alias SourceReader = immutable(ubyte)[] delegate(string path);
alias BinaryReader = immutable(ubyte)[] delegate(string path, long offset, long length);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int offset, int length

Comment thread source/app.d Outdated
}

File openInputFile(string filename) {
void recordSource(string filename) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

registerSource

Comment thread source/app.d

void writeLabelTable() {
string filename = optionParameters['t' - 'a'];
if (filename !is null && listingStream.isOpen && listingStream != stdout)

@pfusik pfusik Sep 1, 2026

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-l - -t YOU_JUST_IGNORE_ME

Comment thread source/app.d Outdated
}

version (unittest) ubyte[] objectBuffer;
Appender!(ubyte[]) objectBuffer;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's Appender ?

Comment thread source/app.d Outdated
if (inFalseCondition() && !listFalseConditionals)
return;
if (!getOption('i') && includeLevel > 0)
if (!listIncludedFiles && includeLevel > 0)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other two uses of listIncludedFiles already do the logical negation.

Comment thread source/xasm/package.d
int value; /// The value assigned to the label.
bool unused = true; /// True until the label is referenced somewhere.
bool unknownInPass1 = false; /// True if the value was not yet known during pass 1.
bool passed = false; /// True once the label's definition has been processed.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"was encountered in the current pass"

Comment thread source/xasm/package.d

}

///

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank doc comment?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants