Skip to content

Commit 42aa514

Browse files
committed
Add a minimal baremetal testsuite to Phobos
1 parent b3ac37c commit 42aa514

3 files changed

Lines changed: 119 additions & 2 deletions

File tree

posix.mak

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,14 +427,42 @@ unittest/%.run : $(ROOT)/unittest/test_runner
427427
# Run separate -betterC tests
428428
################################################################################
429429

430-
test/betterC/%.run: test/betterC/%.d $(DMD) $(LIB)
430+
test/betterC/%.run: test/betterC/%.d $(DMD) $(DRUNTIME)
431431
mkdir -p $(ROOT)/unittest/betterC
432432
$(DMD) $(DFLAGS) -of$(ROOT)/unittest/betterC/$(notdir $(basename $<)) -betterC $(UDFLAGS) \
433433
-defaultlib= -debuglib= $(LINKDL) $<
434434
./$(ROOT)/unittest/betterC/$(notdir $(basename $<))
435435

436436
betterC: $(subst .d,.run,$(wildcard test/betterC/*.d))
437437

438+
################################################################################
439+
# Run separate baremetal tests (for now only on x86_32 and x86_64 Linux)
440+
################################################################################
441+
442+
MINIMAL_RUNTIME=$(ROOT)/unittest/baremetal/runtime.a
443+
444+
$(MINIMAL_RUNTIME): test/baremetal/runtime/runtime.d $(DMD) $(DRUNTIME)
445+
mkdir -p $(ROOT)/unittest/baremetal
446+
$(DMD) -betterC -c $(DFLAGS) -lib -of$@ $<
447+
448+
test/baremetal/%.run: test/baremetal/%.d $(DMD) $(MINIMAL_RUNTIME)
449+
mkdir -p $(ROOT)/unittest/baremetal
450+
$(DMD) $(DFLAGS) -c -betterC -of$(ROOT)/unittest/baremetal/$(notdir $(basename $<)).o -betterC $(UDFLAGS) $<
451+
ld $(ROOT)/unittest/baremetal/$(notdir $(basename $<)).o $(MINIMAL_RUNTIME) \
452+
-o $(ROOT)/unittest/baremetal/$(notdir $(basename $<))
453+
./$(ROOT)/unittest/baremetal/$(notdir $(basename $<))
454+
455+
ifneq (linux, $(OS))
456+
baremetal:
457+
@echo "Baremetal tests are currently only run on Linux x86_32 and x86_64"
458+
else
459+
ifeq (,$(findstring $(MODEL),32 64))
460+
baremetal:
461+
@echo "Baremetal tests are currently only run on Linux x86_32 and x86_64"
462+
endif
463+
baremetal: $(subst .d,.run,$(wildcard test/baremetal/*.d))
464+
endif
465+
438466
################################################################################
439467
# More stuff
440468
################################################################################
@@ -634,6 +662,6 @@ $(TESTS_EXTRACTOR): $(TOOLS_DIR)/tests_extractor.d | $(LIB)
634662
auto-tester-build: all checkwhitespace
635663

636664
.PHONY : auto-tester-test
637-
auto-tester-test: unittest betterC
665+
auto-tester-test: unittest betterC baremetal
638666

639667
.DELETE_ON_ERROR: # GNU Make directive (delete output files on error)

test/baremetal/hello.d

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
extern(C) int main(int argc, char** argv)
2+
{
3+
import std.algorithm, std.range;
4+
assert(100.iota.stride(2).take(10).sum == 90);
5+
return 0;
6+
}

test/baremetal/runtime/runtime.d

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
extern(C) void __d_sys_exit(size_t arg1)
2+
{
3+
version(linux)
4+
{
5+
version(X86_64)
6+
{
7+
asm
8+
{
9+
mov RAX, 60;
10+
mov RDI, arg1;
11+
syscall;
12+
}
13+
}
14+
else version(X86)
15+
{
16+
asm
17+
{
18+
mov EAX, 2;
19+
mov EDI, arg1;
20+
syscall;
21+
}
22+
}
23+
}
24+
}
25+
26+
extern(C) void __assert(const(char)* exp, const(char)* file, uint line)
27+
{
28+
version(linux)
29+
print("ASSERT ERROR\n");
30+
assert(0);
31+
}
32+
33+
extern extern(C) int main();
34+
private extern(C) void _start()
35+
{
36+
__d_sys_exit(main());
37+
}
38+
39+
void print(const char* c)
40+
{
41+
print(c, c.strlen);
42+
}
43+
44+
void print(const char* c, size_t len)
45+
{
46+
version(linux)
47+
{
48+
version(X86_64)
49+
{
50+
asm
51+
{
52+
mov RDX, len;
53+
mov RSI, c;
54+
mov RAX, 1; // write
55+
mov RDI, RAX;
56+
syscall;
57+
}
58+
}
59+
else version(X86)
60+
{
61+
asm
62+
{
63+
mov EDX, len;
64+
mov ESI, c;
65+
mov EAX, 1; // write
66+
mov EDI, EAX;
67+
syscall;
68+
}
69+
}
70+
}
71+
}
72+
73+
size_t strlen(const char* c)
74+
{
75+
auto cptr = cast(char*) c;
76+
size_t i;
77+
while (*cptr != 0)
78+
{
79+
cptr++;
80+
i++;
81+
}
82+
return i;
83+
}

0 commit comments

Comments
 (0)