Skip to content

byteshiftlabs/gates

Repository files navigation

Gates

License: GPL v3 Language: C11 Build: CMake

Minimal C subset → VHDL translator

Quick Start

git clone https://github.com/byteshiftlabs/gates.git
cd gates && mkdir build && cd build
cmake .. && make
./gates input.c output.vhdl
./gates --version

Example

Given this C input:

int add(int a, int b) {
    int sum = a + b;
    return sum;
}

Gates generates:

-- VHDL generated by gates

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;

-- Function: add
entity add is
  port (
    clk   : in  std_logic;
    reset : in  std_logic;
    a : in std_logic_vector(31 downto 0);
    b : in std_logic_vector(31 downto 0);
    result : out std_logic_vector(31 downto 0)
  );
end entity;

architecture behavioral of add is
  signal sum : std_logic_vector(31 downto 0);
begin
  process(clk, reset)
  begin
    if reset = '1' then
      sum <= (others => '0');
    elsif rising_edge(clk) then
      sum <= std_logic_vector(unsigned(a) + unsigned(b));
      result <= sum;
    end if;
  end process;
end architecture;

Each C function becomes a VHDL entity with clock/reset ports, input parameters as std_logic_vector ports, and a result output port. Local variables become signals, and control flow maps to synchronous process logic.

Features

  • Recursive-descent parser with full operator precedence
  • Control flow: if/else, while, for, break, continue
  • Self-contained functions with return value propagation, plus structs and arrays
  • VHDL entity/architecture generation with clock/reset, signals, and synchronous processes
  • Multi-level error diagnostics (error/warning/note across 5 categories)
  • GoogleTest-based unit, integration, structural validation, and edge case coverage

Requirements

  • Platform: Ubuntu 24.04 x86_64
  • CMake 3.14+
  • C11-compatible compiler (tested on GCC 13.3.0)
  • C++17 for GoogleTest

Optional Tools

  • cppcheck 2.13+ — static analysis (cmake --build build --target cppcheck)
  • Sphinx + sphinx_rtd_theme — documentation (./build_docs.sh)

Testing

./run_tests.sh    # Build and run all tests
./build_docs.sh   # Build documentation

Docker

Build an interactive development image:

docker build -t gates-dev .
docker run --rm -it -v "$PWD":/workspace gates-dev

Run full validation during image build:

docker build --build-arg RUN_VALIDATION=1 -t gates-validated .

Inside the container, use the existing project scripts:

cmake -S . -B build -DENABLE_TESTING=ON
cmake --build build --target gates -j"$(nproc)"
./run_tests.sh
./build_docs.sh
./build/gates examples/example.c output.vhdl

Project Structure

src/
  app/         — Entry point (gates.c)
  parser/      — Tokenizer, recursive-descent parser, AST construction
  codegen/     — VHDL entity/architecture/process generation
  core/        — AST node definitions, utility functions
  symbols/     — Symbol tables for arrays and structs
tests/         — GoogleTest unit, integration, and edge case tests
docs/          — Sphinx documentation
examples/      — Sample C input files

Known Limitations

  • No global variables
  • No nested structs or arrays of structs
  • No pointer support
  • No switch/case or do-while
  • Function calls are parsed, but cross-function hardware wiring is not yet synthesized. Multi-function files emit independent entities, so keep hardware-generating examples self-contained.
  • Generated VHDL targets the currently supported C subset. Structural well-formedness is verified by self-contained validation tests (balanced constructs, declared signals, proper type wrapping). Current output does not perform resource sharing, pipelining, or constant folding (see ROADMAP.md for planned future work)

Documentation

See ROADMAP.md for planned features and docs/ for full documentation.

License

GPL v3 — see LICENSE.

Disclosure: This software was developed with AI assistance under human supervision.