Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions posix.mak
Original file line number Diff line number Diff line change
Expand Up @@ -427,14 +427,42 @@ unittest/%.run : $(ROOT)/unittest/test_runner
# Run separate -betterC tests
################################################################################

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

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

################################################################################
# Run separate baremetal tests (for now only on x86_64 Linux)
################################################################################

MINIMAL_RUNTIME=$(ROOT)/unittest/baremetal/runtime.a

$(MINIMAL_RUNTIME): test/baremetal/runtime/runtime.d $(DMD) $(DRUNTIME)
mkdir -p $(ROOT)/unittest/baremetal
$(DMD) -betterC -c $(DFLAGS) -lib -of$@ $<

test/baremetal/%.run: test/baremetal/%.d $(DMD) $(MINIMAL_RUNTIME)
mkdir -p $(ROOT)/unittest/baremetal
$(DMD) $(DFLAGS) -c -betterC -of$(ROOT)/unittest/baremetal/$(notdir $(basename $<)).o -betterC $(UDFLAGS) $<
ld $(ROOT)/unittest/baremetal/$(notdir $(basename $<)).o $(MINIMAL_RUNTIME) \
-o $(ROOT)/unittest/baremetal/$(notdir $(basename $<))
./$(ROOT)/unittest/baremetal/$(notdir $(basename $<))

ifneq (linux, $(OS))
baremetal:
@echo "Baremetal tests are currently only run on Linux x86_32 and x86_64"
else
ifeq (,$(findstring $(MODEL),64))
baremetal:
@echo "Baremetal tests are currently only run on Linux x86_32 and x86_64"
endif
baremetal: $(subst .d,.run,$(wildcard test/baremetal/*.d))
endif

################################################################################
# More stuff
################################################################################
Expand Down Expand Up @@ -634,6 +662,6 @@ $(TESTS_EXTRACTOR): $(TOOLS_DIR)/tests_extractor.d | $(LIB)
auto-tester-build: all checkwhitespace

.PHONY : auto-tester-test
auto-tester-test: unittest betterC
auto-tester-test: unittest betterC baremetal

.DELETE_ON_ERROR: # GNU Make directive (delete output files on error)
6 changes: 6 additions & 0 deletions test/baremetal/hello.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
extern(C) int main()
{
import std.algorithm, std.range;
assert(100.iota.stride(2).take(10).sum == 90);
return 0;
}
83 changes: 83 additions & 0 deletions test/baremetal/runtime/runtime.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
extern(C) void __d_sys_exit(size_t arg1)
{
version(linux)
{
version(X86_64)
{
asm
{
mov RAX, 60;
mov RDI, arg1;
syscall;
}
}
else version(X86)
{
asm
{
mov EAX, 2;
mov EDI, arg1;
syscall;
}
}
}
}

extern(C) void __assert(const(char)* exp, const(char)* file, uint line)
{
version(linux)
print("ASSERT ERROR\n");
assert(0);
}

extern extern(C) int main();
private extern(C) void _start()
{
__d_sys_exit(main());
}

void print(const char* c)
{
print(c, c.strlen);
}

void print(const char* c, size_t len)
{
version(linux)
{
version(X86_64)
{
asm
{
mov RDX, len;
mov RSI, c;
mov RAX, 1; // write
mov RDI, RAX;
syscall;
}
}
else version(X86)
{
asm
{
mov EDX, len;
mov ESI, c;
mov EAX, 1; // write
mov EDI, EAX;
syscall;
}
}
}
}

size_t strlen(const char* c)
{
auto cptr = cast(char*) c;
size_t i;
while (*cptr != 0)
{
cptr++;
i++;
}
return i;
}