Skip to content

Instantly share code, notes, and snippets.

@hikari-no-yume
Created May 14, 2021 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hikari-no-yume/e4e814d8846b37d9e224b024385c906c to your computer and use it in GitHub Desktop.
Save hikari-no-yume/e4e814d8846b37d9e224b024385c906c to your computer and use it in GitHub Desktop.
Rust no_std “hello world” compiled to C!

Requires rustc and llvm-cbe.

  • rustc hello-world-no_std.rs -O --emit=llvm-ir -C panic=abort -o hello-world-no_std-O.ll
  • llvm-cbe ../testfiles/hello-world-no_std-O.ll
#![no_std]
#![no_main]
extern "C" {
fn puts(s: *const u8);
fn abort() -> !;
}
#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! {
unsafe {
abort();
}
}
#[no_mangle]
pub fn main() -> isize {
unsafe {
puts("hello, world!\0".as_ptr());
};
0
}
; ModuleID = 'hello_world_no_std.7rcbfp3g-cgu.0'
source_filename = "hello_world_no_std.7rcbfp3g-cgu.0"
target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx10.7.0"
%"core::panic::PanicInfo" = type { [0 x i64], { {}*, [3 x i64]* }, [0 x i64], i64*, [0 x i64], %"core::panic::Location"*, [0 x i64] }
%"core::panic::Location" = type { [0 x i64], { [0 x i8]*, i64 }, [0 x i32], i32, [0 x i32], i32, [0 x i32] }
@alloc3 = private unnamed_addr constant <{ [14 x i8] }> <{ [14 x i8] c"hello, world!\00" }>, align 1
; Function Attrs: noreturn nounwind
define hidden void @rust_begin_unwind(%"core::panic::PanicInfo"* noalias nocapture readonly align 8 dereferenceable(32) %_1) unnamed_addr #0 {
start:
tail call void @abort()
unreachable
}
; Function Attrs: nofree nounwind
define i64 @main() unnamed_addr #1 {
start:
tail call void @puts(i8* nonnull dereferenceable(1) getelementptr inbounds (<{ [14 x i8] }>, <{ [14 x i8] }>* @alloc3, i64 0, i32 0, i64 0))
ret i64 0
}
; Function Attrs: noreturn nounwind
declare void @abort() unnamed_addr #0
; Function Attrs: nofree nounwind
declare void @puts(i8* nocapture readonly) unnamed_addr #1
attributes #0 = { noreturn nounwind "frame-pointer"="all" "probe-stack"="__rust_probestack" "target-cpu"="core2" }
attributes #1 = { nofree nounwind "frame-pointer"="all" "probe-stack"="__rust_probestack" "target-cpu"="core2" }
!llvm.module.flags = !{!0, !1}
!0 = !{i32 7, !"PIC Level", i32 2}
!1 = !{i32 7, !"PIE Level", i32 2}
/* Provide Declarations */
#include <stdint.h>
#ifndef __cplusplus
typedef unsigned char bool;
#endif
#ifdef _MSC_VER
#define __builtin_unreachable() __assume(0)
#endif
#ifdef _MSC_VER
#define __noreturn __declspec(noreturn)
#else
#define __noreturn __attribute__((noreturn))
#endif
#if defined(__GNUC__)
#define __HIDDEN__ __attribute__((visibility("hidden")))
#endif
#if defined(__GNUC__)
#define __ATTRIBUTELIST__(x) __attribute__(x)
#else
#define __ATTRIBUTELIST__(x)
#endif
#ifdef _MSC_VER /* Can only support "linkonce" vars with GCC */
#define __attribute__(X)
#endif
/* Global Declarations */
/* Types Declarations */
struct l_unnamed_2;
struct l_unnamed_3;
struct l_struct_core_KD__KD_panic_KD__KD_Location;
struct l_struct_core_KD__KD_panic_KD__KD_PanicInfo;
struct l_unnamed_1;
/* Function definitions */
/* Types Definitions */
struct l_array_3_uint64_t {
uint64_t array[3];
};
struct l_unnamed_2 {
void* field0;
struct l_array_3_uint64_t* field1;
};
struct l_unnamed_3 {
void* field0;
uint64_t field1;
};
struct l_struct_core_KD__KD_panic_KD__KD_Location {
/* void field0 */ struct l_unnamed_3 field1;
/* void field2 */ uint32_t field3;
/* void field4 */ uint32_t field5;
/* void field6 */};
struct l_struct_core_KD__KD_panic_KD__KD_PanicInfo {
/* void field0 */ struct l_unnamed_2 field1;
/* void field2 */ uint64_t* field3;
/* void field4 */ struct l_struct_core_KD__KD_panic_KD__KD_Location* field5;
/* void field6 */};
struct l_array_14_uint8_t {
uint8_t array[14];
};
#ifdef _MSC_VER
#pragma pack(push, 1)
#endif
struct l_unnamed_1 {
struct l_array_14_uint8_t field0;
} __attribute__ ((packed));
#ifdef _MSC_VER
#pragma pack(pop)
#endif
/* External Global Variable Declarations */
/* Function Declarations */
__noreturn void rust_begin_unwind(struct l_struct_core_KD__KD_panic_KD__KD_PanicInfo*) __ATTRIBUTELIST__((nothrow)) __HIDDEN__;
int main(void) __ATTRIBUTELIST__((nothrow));
__noreturn void abort(void) __ATTRIBUTELIST__((nothrow));
void puts(uint8_t*) __ATTRIBUTELIST__((nothrow));
/* Global Variable Definitions and Initialization */
static struct l_unnamed_1 alloc3 = { { "hello, world!" } };
/* LLVM Intrinsic Builtin Function Bodies */
/* Function Bodies */
__noreturn void rust_begin_unwind(struct l_struct_core_KD__KD_panic_KD__KD_PanicInfo* llvm_cbe__1) {
/*tail*/ abort();
__builtin_unreachable();
}
int main(void) {
/*tail*/ puts(((&alloc3.field0.array[((int64_t)0)])));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment