News for hackers. Stuff that matters.
Re:Mrustc: a Rust compiler written in C++ by __david__ on Thursday September 21, 2017 @08:02PM (#15308326)
I don’t see why that’s an issue. Can you name a C compiler that distros use that isn’t written in C?
Re:Mrustc: a Rust compiler written in C++ by raimue on Thursday September 21, 2017 @08:13PM (#15308379)
C compilers are ubiquituous. Name a platform which does not have at least one C compiler. Rust might come into such a position at some later time as well, but until then at least a transitional solution besides cross-compiling would make packaging easier and reproducible.
Re:Mrustc: a Rust compiler written in C++ by andrewflnr on Thursday September 21, 2017 @08:57PM (#15308614)
Really, the solution is just to make cross compiling as easy as any other kind. Compilation is basically a pure function, so by rights compilers with different targets should be drop-in replacements of each other.
Re:Mrustc: a Rust compiler written in C++ by jjoonathan on Thursday September 21, 2017 @09:42PM (#15308804)
> Compilation is basically a pure function

Sure, a pure function taking the universe as an argument.

Re:Mrustc: a Rust compiler written in C++ by alphaalpha101 on Thursday September 21, 2017 @09:44PM (#15308813)
No, a pure function taking the original source code as the only argument.
Re:Mrustc: a Rust compiler written in C++ by andrewflnr on Thursday September 21, 2017 @10:03PM (#15308893)
Exactly. There's no reason a compiler should take any input but the source (maybe some config) nor have any output but the translated code. There's no dependence on running on a particular piece of hardware in this equation. Just run the codegen for platform Y on platform X.
Re:Mrustc: a Rust compiler written in C++ by coldtea on Friday September 22, 2017 @04:15AM (#15310255)
Wouldn't it need to link to native libraries, and thus require you to have all those around too?
Re:Mrustc: a Rust compiler written in C++ by andrewflnr on Friday September 22, 2017 @09:02PM (#15317384)
Sure, I guess in the 10,000 foot view those look like source code :). But there's no fundamental reason you can't have libraries for platform X merely present on a platform Y system, it's just inconvenient today. That's the point I'm really trying to get at.
Re:Mrustc: a Rust compiler written in C++ by eat_veggies on Thursday September 21, 2017 @10:12PM (#15308927)
I'll admit I don't know a whole lot about compilers but don't most of them do optimizations and other changes based on the CPU like its architecture, cache size, and instruction set? That may be what GP is getting at.

Stuff like LLVM, and emscripten exist though so it's probably not as big of a deal as they say.

Re:Mrustc: a Rust compiler written in C++ by andrewflnr on Thursday September 21, 2017 @10:27PM (#15308997)
Sure, based on the target CPU. But there's no fundamental reason I can't do all those optimizations for, say, x86 in a compiler that happens to be running on an Arm CPU. As far as the compiler is concerned it's all just data that gets stuffed in files, just like any other software.
Re:Mrustc: a Rust compiler written in C++ by infogulch on Thursday September 21, 2017 @11:26PM (#15309205)
The super-easy cross compilation in go is one of the best parts. Every install comes with the capacity to compile to every go target with no differences in input except setting it by name.

This is really something more languages should strive for.

Re:Mrustc: a Rust compiler written in C++ by kibwen on Friday September 22, 2017 @12:28AM (#15309391)
> This is really something more languages should strive for.

Most languages these days obviate cross-compilation in the first place by being interpreted. Of the languages that remain, the natively-compiled ones, most haven't gone to Go's lengths of writing a custom libc, which is emphatically not recommended for both Windows and Mac (the syscall interfaces aren't stable, and this has broken Go code in the past: https://github.com/golang/go/issues/16272 ). And gc, the primary Go compiler and the one with out-of-the-box cross-compilation, supports relatively few platforms (I count 11, whereas rustc looks like it supports 50-70 platforms). You can use gccgo to get more platforms, but AFAICT gccgo's cross-compilation story isn't nearly as nice: https://github.com/golang/go/wiki/GccgoCrossCompilation .

For Rust, cross-compilation looks like this:

1. Install the libc for the target system, and, if necessary, a compatible linker.

2. Run `rustup target add foo` where "foo" is one of the target triples on https://forge.rust-lang.org/platform-support.html

3. Add the target triple to your Cargo.toml.

Which isn't too shabby. You can read more about it here: https://github.com/japaric/rust-cross

Re:Mrustc: a Rust compiler written in C++ by falcolas on Friday September 22, 2017 @12:29PM (#15313585)
Well, it's not just data - it's also frequently pointers or labels to code in dynamically imported libraries; things which can't always be calculated without the exact library being used on hand.
Re:Mrustc: a Rust compiler written in C++ by andrewflnr on Friday September 22, 2017 @09:03PM (#15317385)
Dynamic libraries are also made of data. See also my response to coldtea here: https://news.ycombinator.com/item?id=15310255
Re:Mrustc: a Rust compiler written in C++ by kibwen on Friday September 22, 2017 @12:31AM (#15309402)
Be careful, let's not forget the lesson of Ken Thompson's Reflections on Trusting Trust. It's more accurate to say that compilation is a function that takes two arguments: the source code, and the compiler itself; this is how trusting-trust attacks propagate despite total absence from the source code.
Re:Mrustc: a Rust compiler written in C++ by weberc2 on Friday September 22, 2017 @10:58AM (#15312754)
You're being pedantic, but your pedantry is incorrect, so allow me to be pedantic in correcting you :p. The output of all pure functions depends on the function and its inputs; this isn't more true for compilers than, say, addition. The problem with Trusting Trust is that the function isn't practically inspectable, not that the function is impure. A trusting trust compiler is still pure.
Re:Mrustc: a Rust compiler written in C++ by falcolas on Friday September 22, 2017 @12:26PM (#15313565)
A functionally pure compiler would always provide the same output for the given source code. The challenges in creating repeatable builds of highly popular open source projects shows that the average compiler is anything but functionally pure. The global state of the system running the compiler has a huge impact on the output of the compiler.
Re:Mrustc: a Rust compiler written in C++ by weberc2 on Friday September 22, 2017 @12:45PM (#15313737)
I agree, but that's irrelevant to the Trusting Trust topic.
Re:Mrustc: a Rust compiler written in C++ by hasenj on Friday September 22, 2017 @01:44AM (#15309666)
And even if you could manually verify the binary output of a compiler and prove it correct, you still don't know that the CPU itself is not malicious.
Re:Mrustc: a Rust compiler written in C++ by __david__ on Saturday September 23, 2017 @12:50PM (#15320545)
Incidentally, this is the principle that ccache[1] operates on.

[1] https://ccache.samba.org/

Re:Mrustc: a Rust compiler written in C++ by coldtea on Friday September 22, 2017 @04:14AM (#15310251)
>I don’t see why that’s an issue. Can you name a C compiler that distros use that isn’t written in C?

Can you name a distro or environment that doesn't already have a C compiler at arm's reach?

That's not the case with Rust compilers, which is obviously what the issue is.

Re:Mrustc: a Rust compiler written in C++ by __david__ on Saturday September 23, 2017 @12:46PM (#15320511)
No, it's not obvious what the issue is. If you want to compile a C compiler, you need a C compiler binary. If you want to compile a rust compiler, you need a rust compiler binary. It's the exact same issue, but people don't whine about the C compiler, probably because it's been that way for 50 years.
Re:Mrustc: a Rust compiler written in C++ by fooker on Friday September 22, 2017 @03:37AM (#15310113)
Actually, yes. GCC and Clang are both written in C++ now.
Re:Mrustc: a Rust compiler written in C++ by steveklabnik on Thursday September 21, 2017 @08:11PM (#15308370)
gcc and clang are both C++ in my understanding.

Regardless, those compilers are usually grandfathered in; distro policies are different for other languages. It just depends.

Re:Mrustc: a Rust compiler written in C++ by kazinator on Thursday September 21, 2017 @09:21PM (#15308723)
Even the C++ front end in GCC is written in C.

https://gcc.gnu.org/git/?p=gcc.git;a=tree;f=gcc/cp

Re:Mrustc: a Rust compiler written in C++ by readittwice on Friday September 22, 2017 @02:16AM (#15309789)
No, they use C++ even for the C front-end. But you are right, you can still see its C past in the source code.

What's important though is that the GCC devs actively try to avoid the newest C++-features, this allows it to compile current GCC-trunk even with very old GCC-Releases. IIRC even GCC 4.3 (released in March 2008) is able to compile current trunk (GCC 8.0).

This is somewhat different for Rust, where I think the current policy is that master needs to compile with last released version (releases every 6 weeks and just to make it clear: Rust is written in Rust). Before that, the compiler was updated even more frequently. Bootstrapping Rust from 0 therefore is quite hard, since Rust is far from being as ubiquitous as C/C++-compilers. Even if you already have a rustc on your system, it's not unlikely that it is too old for compiling Rust-master. The first Rust-compiler was written in OCaml, so you need to compile that first, then compile Rust commit-for-commit until you reach current master. This could take quite some time. IMHO this is why mrustc is great, since you just compile current master (if it supports all features) with it and then use the generated compiler to compile Rust.

Re:Mrustc: a Rust compiler written in C++ by pjmlp on Friday September 22, 2017 @12:58AM (#15309485)
It looks like C, but actually it is common subset from C++.

GCC has migrated to C++ in 2008.

https://lwn.net/Articles/542457/

Re:Mrustc: a Rust compiler written in C++ by kazinator on Friday September 22, 2017 @10:57AM (#15312743)
Any carefully written C program that doesn't use certain C99 an C11 things can be said to be C++.

My language implementation compiles as C and C++. Every so often I build it as C++ (like before releases) to check for regressions and flush out any issues caught by the C++ compiler.

I don't think of it as "written in C++", though it is not technically a false statement.

All Scheme programs are really written in Common Lisp; they just need a suitable library of macros and functions ...

Re:Mrustc: a Rust compiler written in C++ by microcolonel on Thursday September 21, 2017 @10:40PM (#15309054)
Technically not only C anymore, not sure about the C++ frontend specifically, but there is C++ in GCC since... 5? 4?
Re:Mrustc: a Rust compiler written in C++ by xioxox on Friday September 22, 2017 @12:00AM (#15309301)
Some of those files are C++!
Re:Mrustc: a Rust compiler written in C++ by kazinator on Thursday September 21, 2017 @09:19PM (#15308713)
Can you name a C compiler that distros use that isn't GCC or Clang?
Re:Mrustc: a Rust compiler written in C++ by SAI_Peregrinus on Tuesday September 26, 2017 @01:03PM (#15340516)
MSVC. Yeah, Windows isn't a "distro" but it is a major OS and MSVC is a pretty major compiler. But it's binary only so there's no need to bootstrap it unless you're working at MS.
Re:Mrustc: a Rust compiler written in C++ by xoroshiro on Friday September 22, 2017 @05:11AM (#15310449)
I don't get the issue as well, but maybe from ignorance. Can't I just get the rust compiler in LLVM's IR form and bootstrap from there?
Re:Mrustc: a Rust compiler written in C++ by tmzt on Friday September 22, 2017 @08:56AM (#15311714)
LLVM IR is still very targeted to the architecture it will be run on, it's not intended to be a portable representation.