Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
This repository was archived by the owner on Feb 4, 2025. It is now read-only.
/saltwaterPublic archive

A C compiler written in Rust, with a focus on good error messages.

License

NotificationsYou must be signed in to change notification settings

jyn514/saltwater

Repository files navigation

Build StatusJoin us on Discord

saltwater: the part of the sea causing lots of rust

A C compiler written in Rust, with a focus on good error messages.


This project is no longer maintained.


Running

swcc reads from standard in by default, so you can type in code directly.It's not interactive though, you have to hit Ctrl+D to indicate end of file (Ctrl+Z on Windows).

Useswcc --help for all options (or seebelow).

Running on Windows

You need to havecc on your PATH. You can either install mingw + gcc or MSVC.Other than that, it should work exactly the same as on Linux.

Homebrew

brew install saltwater

Unimplemented features

  • Defining functions taking variadic arguments. Note that calling variadic functions (likeprintf) is already supported.
  • Variable-length arrays (int a[n])
  • Multiple translation units (files)
  • Bitfields
  • Compiling on non-x86 platforms
  • Cross-compilation

Examples

$cattests/runner-tests/readme.c// output: j is 6intprintf(constchar*, ...);typedefstructs*sp;inti=1;inta[3]= {1,2,3};floatf=2.5;structs {intouter;}my_struct;intg(int);intmain(void) {spmy_struct_pointer=&my_struct;constintc=my_struct_pointer->outer=4;// should return 6intj=i+f*a[2]-c/g(1);printf("j is %d\n",j);returnj;}intg(inti) {if (i<0||i >=3) {return0;  }returna[i];}$swcctests/runner-tests/readme.c$️ ./a.outjis6

Debug output

$cattests/runner-tests/cpp/if/defined.c// code: 2#definea#defineb#if defined(a)inti=2;#endif#ifndefbsyntaxerror#endif# if definedb&& defined(a)intmain() {returni; }#endif$swcc-Etests/runner-tests/cpp/if/defined.cinti=2 ;intmain ( ) {returni ; }
$echo'int i = 1 + 2 ^ 3 % 5 / 2 & 1; int main(){}' |swcc--debug-astast:inti= ((1)+ (2)) ^ ((((3) % (5)) / (2))& (1));ast:intmain(){}
$cattests/runner-tests/hello_world.c#include<stdio.h>intmain() {puts("Hello, world!");}$swcc--debug-irtests/runner-tests/hello_world.cfunctionu0:0()->i32system_v {gv0=symbolcolocatedu1:3sig0= (i64)->i32system_vfn0=u0:26sig0block0:v0=global_value.i64gv0v1=callfn0(v0)v2=iconst.i320returnv2}$ ./a.outHello,world!

All options

$ swcc --helpswcc 0.9.0Jynn Nelson <github@jyn.dev>A C compiler written in Rust, with a focus on good error messages.Homepage: https://github.com/jyn514/rcc/usage: swcc [FLAGS] [OPTIONS] [<file>]FLAGS:        --debug-ast        If set, print the parsed abstract syntax tree (AST) in addition to compiling.                            The AST does no type checking or validation, it only parses.        --debug-hir        If set, print the high intermediate representation (HIR) in addition to compiling.                            This does type checking and validation and also desugars various expressions.        --debug-ir         If set, print the intermediate representation (IR) of the program in addition to compiling.        --debug-lex        If set, print all tokens found by the lexer in addition to compiling.        --jit              If set, will use JIT compilation for C code and instantly run compiled code (No files produced).                            NOTE: this option only works if swcc was compiled with the `jit` feature.    -h, --help             Prints help information    -c, --no-link          If set, compile and assemble but do not link. Object file is machine-dependent.    -E, --preprocess-only  If set, preprocess only, but do not do anything else.                            Note that preprocessing discards whitespace and comments.                            There is not currently a way to disable this behavior.    -V, --version          Prints version informationOPTIONS:        --color <when>       When to use color. May be "never", "auto", or "always". [default: auto]    -o, --output <output>    The output file to use. [default: a.out]        --max-errors <max>   The maximum number of errors to allow before giving up.                             Use 0 to allow unlimited errors. [default: 10]    -I, --include <dir>      Add a directory to the local include path (`#include "file.h"`).                              Can be specified multiple times to add multiple directories.    -D, --define <id[=val]>  Define an object-like macro.                              Can be specified multiple times to add multiple macros.                              `val` defaults to `1`.ARGS:    <file>    The file to read C source from. "-" means stdin (use ./- to read a file called '-').              Only one file at a time is currently accepted. [default: -]

Testing

cargotest# optionally, you can fuzz the compiler# it may be more helpful to just `grep -R unimplemented src`, though# libFuzzer/AFLtests/fuzz.sh# Honggfuzz:# Running Honggfuzz locally requires some parameters to use it at its full potential,# so it is probably a good idea to have a look here: https://github.com/rust-fuzz/honggfuzz-rs/blob/master/README.md# and here: https://github.com/google/honggfuzz/blob/master/docs/USAGE.md# we suggest the following:HFUZZ_RUN_ARGS="--tmout_sigvtalrm --exit_upon_crash" tests/hfuzz.sh

FAQ

SeeFAQ.md

Implementation Defined Behavior

SeeIMPLEMENTATION_DEFINED.md

Contributing

SeeCONTRIBUTING.md.This also includes reporting bugs.

About

A C compiler written in Rust, with a focus on good error messages.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp