Семь бэкендов и t27b
Вы узнаете
Одна спека, много целевых языков и нативный компилятор без LLVM.
Одна спека t27 может стать Zig, C, Rust, JavaScript, TypeScript или Verilog; Verilog получается двумя путями, прямо из спеки и через HIR, — вот и семь бэкендов. Плеер показывает Rust, который t27c пишет для hello_world. t27b — второй компилятор: нативный машинный код AArch64, без LLVM, JIT для тестов и объектные файлы Mach-O. Он не закончен, и мы говорим, насколько: в запуске 26c447ad8 он прошёл 307 из 449 спек, которые эталонный компилятор проходит с тестами, 68.4%.
Попробуйте
Переключите плеер на JavaScript в «Ещё попробуйте», сгенерируйте модуль и запустите его.

A terminal in your browser running the t27 compiler as WebAssembly: t27c gen rust prints the Rust it emits for hello_world.t27, and t27c test runs the spec's tests.
specs/tutorial/08_modules_and_arrays.t27
// SPDX-License-Identifier: Apache-2.0
; 08 — Modules, visibility and arrays
; Lesson 8, the last one. How a spec names itself, what it exposes, how it
; pulls in another spec, and the array/index syntax used by every lookup table
; in the corpus.
; .
; After this, open specs/numeric/gf16.t27 -- it is a real spec built entirely
; out of what these eight lessons cover.
; phi^2 + 1/phi^2 = 3 | TRINITY
module tutorial-08-modules;
// ============================================================================
// use
//
// use path::to::spec;
//
// Pulls in another spec. Paths are `::`-separated and rooted at the corpus,
// not at the filesystem.
// ============================================================================
use base::types;
// ============================================================================
// pub
//
// `pub` makes a name part of this spec's surface. Without it the name is
// internal: usable here, invisible elsewhere. Default to leaving things
// private -- a spec's exported surface is a promise to whoever imports it.
// ============================================================================
pub const EXPORTED_WIDTH : u8 = 16;
// No `pub`: an implementation detail of this module.
const INTERNAL_SCALE : u8 = 4;
pub fn scaled_width() u8 {
return EXPORTED_WIDTH / INTERNAL_SCALE;
}
// ============================================================================
// Arrays
//
// [_]TYPE{ a, b, c }
//
// The `_` means "as many as I wrote". Fixed tables like this are how the
// numeric specs carry their constants.
// ============================================================================
pub const TRIT_VALUES = [_]i8{ -1, 0, 1 };
pub const POWERS_OF_TWO = [_]u16{ 1, 2, 4, 8, 16, 32, 64, 128 };
pub const EXPONENT_BIAS = [_]u8{ 15, 31, 63, 127 };
// ============================================================================
// Indexing
//
// `array[i]`, zero-based.
// ============================================================================
pub fn power_of_two(index: u8) u16 {
return POWERS_OF_TWO[index];
}
pub fn bias_for_width(index: u8) u8 {
return EXPONENT_BIAS[index];
}
// ============================================================================
// Tests
// ============================================================================
test "the power table matches its own definition" {
try std.testing.expectEqual(@as(u16, 1), power_of_two(0));
try std.testing.expectEqual(@as(u16, 128), power_of_two(7));
}
test "bias table carries the four standard widths" {
try std.testing.expectEqual(@as(u8, 15), bias_for_width(0));
try std.testing.expectEqual(@as(u8, 127), bias_for_width(3));
}
test "a private constant is still usable inside its own module" {
try std.testing.expectEqual(@as(u8, 4), scaled_width());
}
invariant exported_width_is_a_power_of_two
assert EXPORTED_WIDTH == 16
Все уроки
Модуль 1 · Чип
Что такое FPGA, какой чип у нас и как его выводы встречаются с платой.
Модуль 2 · Числа в железе
Биты, триты и форматы чисел, и сколько стоит арифметика в логике.
Модуль 3 · Ваша программа на t27
Напишите спеку, проверьте её и увидьте, почему «компилируется» не значит «верно».
Модуль 4 · Внутри t27c
Как компилятор читает спеку и что он пишет, включая нативный t27b.
Модуль 5 · От спеки к железу
Verilog, который пишет t27c, ячейки, в которые он превращается, и что делает одна LUT.
Модуль 6 · Читаем синтез
Что yosys сообщает о вашем дизайне и какие предупреждения важны.
Модуль 7 · Размещение, трассировка, тайминг
Где ячейки оказываются на кристалле и успевает ли тактовая частота.
Модуль 8 · Битстрим
Как оттрассированный дизайн становится битами, которые загружает чип, без Vivado.
Модуль 9 · На плате
Спросите чип, кто он, загрузите биты и проверьте их.