16 bit support in swift compiler

Solved!

OK thanks for all the help on this and other questions guys. :slight_smile:

This is the patch I used to add support for avr to swift (built on swift 4.2 source code)...

diff --git a/lib/Basic/LangOptions.cpp b/lib/Basic/LangOptions.cpp
index 146bee5e5b..6491413b8f 100644
--- a/lib/Basic/LangOptions.cpp
+++ b/lib/Basic/LangOptions.cpp
@@ -48,7 +48,8 @@ static const StringRef SupportedConditionalCompilationArches[] = {
   "x86_64",
   "powerpc64",
   "powerpc64le",
-  "s390x"
+  "s390x",
+  "avr"
 };
 
 static const StringRef SupportedConditionalCompilationEndianness[] = {
@@ -221,7 +222,9 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
   case llvm::Triple::ArchType::systemz:
     addPlatformConditionValue(PlatformConditionKind::Arch, "s390x");
     break;
-  default:
+  case llvm::Triple::ArchType::avr:
+    addPlatformConditionValue(PlatformConditionKind::Arch, "avr");
+    break;  default:
     UnsupportedArch = true;
   }
 
@@ -252,6 +255,9 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
   case llvm::Triple::ArchType::systemz:
     addPlatformConditionValue(PlatformConditionKind::Endianness, "big");
     break;
+  case llvm::Triple::ArchType::avr:
+    addPlatformConditionValue(PlatformConditionKind::Endianness, "little");
+    break;
   default:
     llvm_unreachable("undefined architecture endianness");
   }

Even then, it won't build normally. The standard library won't build and LLVM does not include AVR target support by default. To build, use this command to make ninja files...

utils/build-script -S -R --clean --debug-llvm --debug-swift --extra-cmake-options="-DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=AVR" --build-swift-static-stdlib TRUE

(you don't need the debug versions if you don't want them)

Then build cmark, followed by llvm, followed by swift something like this...

ninja -C ../build/Ninja-DebugAssert+stdlib-ReleaseAssert/cmark-macosx-x86_64/
ninja -C ../build/Ninja-DebugAssert+stdlib-ReleaseAssert/llvm-macosx-x86_64/
ninja -C ../build/Ninja-DebugAssert+stdlib-ReleaseAssert/swift-macosx-x86_64/

(adjusted accordingly)

Note, we are not building the standard library.

With this patched version of swift I am able to compile LLVM IR targeted for AVR...

swiftc -target avr-atmel-linux-gnueabihf -Xfrontend -emit-ir -S -O -parse-as-library -I uSwift a.swift

...where a.swift is a simple swift file with import Swift at the top as the first directive.

This outputs LLVM IR with the correct pointer sizes and other target datalayout (yay).

target datalayout = "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8"
target triple = "avr-atmel-linux-gnueabihf"

The source code for my micro stdlib is the subject of another post and is a bit esoteric!

9 Likes