Posts

Showing posts from September, 2022

Investigating a compiler bug

 On rare occasions we find a bug in the compiler. exploring this one was instructive. A user reported that the simple instruction... print(Float(Int32(60000.00)) was printing 33003.00! Confirmed this is not expected on a macOS playground, then dug into where this came from. It's well documented how the S4A compiler produces AVR machine code... Swift code is compiled to bitcode, then an llvm AVR assembler compiles the bitcode for AVR, so one of these steps is at fault, right? There's actually a hidden subtlety in the first step, swift compilation. Because most of the time when you write Swift code, without realising it you're actually using a swift library and non primitive types... the standard library! So in fact there's effectively hidden Swift code inside the above simple swift code. While the micro swift standard library is not open source, it's very closely based on the common, open source swift library. From that we can see a constructor for Float from Int32 t