modus
Menu
On this page

The module c_compiler

The C compiler as a module: chibicc, ported. It reads a C source and writes a text of assembly that the module assemble takes, into memory that the caller lends.

What it is for. A machine with no host compiles C. The console calls it in a call host, gives its text to the assembler, and isolates the module that the assembler wrote:

> isolate("c_compiler")
> isolate("assemble")
> t = buffer(1)
> n = compile("int add(int a, int b) { return a + b; }", t)
> f = buffer(4)
> m = assemble(bytes(t, 0, n), f)
> isolate_memory("adder", f, m)
> add(2, 3)
5

The C part is not in this folder: the flake fetches chibicc at one commit with the patches of the system and the thin libc, and the tool links its objects into this module. This package states the export, gives the C part an arena of the budget, and copies the text into the memory of the caller.

The export compile_unit compiles one C source of a program of several sources: the source names its headers, which the caller gives as a file list, and the caller names the exports of the program. Each unit is the one source of a fresh call host: the module assemble joins the texts.

It has no entry: it runs in the call host of its caller, with no grant. Its memory is an arena of the budget of the call host, made at each call and ended at its return.

See: Module contract: The C compiler, An outside source in a module: C at a pinned commit in the repository

c_compiler module with no entry

Hash
e8812e81ee2510ded10c1a09b8d94af6a0451c77499f98bb22866cbe09fd336c
Size
178171 bytes
Exports
2
Imports
0
Imported by
0
License
MIT

The C compiler as a module: chibicc, ported

FactValue
KindModules with no entry
Sourcesrc/modules/system/c_compiler
Boot imageyes: in the boot image, 178171 bytes in this build
Build settingsnone: the defaults of the source
Importsnone
Imported bynone

Neighbours

It imports no module, and no module imports it. The module graph shows it with the other modules that have no edge.

Exports

ExportWhat it does
compile(source: Bytes(Read), output: Handle(Read, Write)) -> Integerwrites the text of assembly of a C source into a memory object, and gives the bytes of the text
compile_unit(name: Text, source: Bytes(Read), files: Bytes(Read), exports: Text, output: Handle(Read, Write)) -> Integerwrites the text of assembly of one C source of a program into a memory object, and gives the bytes of the text

compile

compile(source: (Read), output: (Read, Write)) -> 

compile writes the text of assembly of a C source into a memory object, and gives the bytes of the text.

source is the C source, 1 byte to 256 KiB, with no byte 0. output is a memory object that the caller lends to write; the text starts at its first byte, and the module assemble takes it. Each function that is not static becomes an export of the text: its int and long parameters and result are Integers. A call host compiles one source: a second call that reaches the compiler fails with Refused, and unload then isolate give a new one. A source that chibicc refuses fails with -1 and the error record Bad_Byte, whose value is the offset of the byte that its diagnostic names, or Refused when the diagnostic names no byte of the source; the log has the diagnostic. A memory object smaller than the text fails with Too_Small, and the limit is the bytes of the text.

Parameters
  • source: (Read)
  • output: (Read, Write)
Result
When it fails
a negative result, and the error record
Source
src/modules/system/c_compiler/c_compiler.odin:510

compile_unit

compile_unit(name: , source: (Read), files: (Read), exports: , output: (Read, Write)) -> 

compile_unit writes the text of assembly of one C source of a program into a memory object, and gives the bytes of the text.

name is the name of the source, 1 to 255 bytes: its folder is the folder of its quoted includes, and the diagnostic names it. source is the C source, as for compile. files is the file list of the other files of the program, its headers, at most 256 files and 2 MiB: records of a name size and a content size of 4 bytes each, little endian, then the name and the content; an empty list has no file. exports names the exports of the program, separated by spaces or new lines, at most 32; an empty text makes each function that is not static an export. The text starts with unit, and each function and variable that is not static is global, so the module assemble joins the texts of the units into one module. A call host compiles one unit, as for compile. A file list or an export list that breaks its form fails before the C part runs: Out_Of_Range for a record that passes the end of the list, Too_Long or Empty for a name, Bad_Byte for a byte 0, Exists for a name twice, Over_Limit for too many files or exports. The memory of the C part is an arena of the budget of the call host: the largest source of chibicc, parse.c, takes 13 MiB.

Parameters
  • name:
  • source: (Read)
  • files: (Read)
  • exports:
  • output: (Read, Write)
Result
When it fails
a negative result, and the error record
Source
src/modules/system/c_compiler/c_compiler.odin:528

Read from the source of the module, as the build reads it to describe the exports (tools/modustool/build/describe.odin).

Bytes wire form

A wire form of the module contract: one word of a call. Bytes with no description.

Handle wire form

A wire form of the module contract: one word of a call. A handle that the caller lends for the time of the call, in a register.

Integer wire form

A wire form of the module contract: one word of a call. A signed integer of 64 bits, in a register.

Error_Record record of a module

A record of the module c_compiler: 56 bytes, aligned on 8 bytes, 0 bytes of padding.

  • kindi6408 bytes
  • uniti6488 bytes
  • operationi64168 bytes
  • argumenti64248 bytes
  • valuei64328 bytes
  • limiti64408 bytes
  • foundi64488 bytes

Text wire form

A wire form of the module contract: one word of a call. The address of a slice record {address, count} of UTF-8 bytes.