Implement and test strlen
This commit is contained in:
parent
68611b4df3
commit
ac516d55a2
9
3-lang0ll/.gitignore
vendored
9
3-lang0ll/.gitignore
vendored
@ -1,7 +1,12 @@
|
||||
/lang0ll*.bc
|
||||
/lang0ll*.c
|
||||
/lang0ll*.exe
|
||||
/lang0ll*.ll
|
||||
/lang0ll*.o
|
||||
/lang0ll*.s
|
||||
/foo.exe
|
||||
/foo.ll
|
||||
/runtimetest.bc
|
||||
/runtimetest.c
|
||||
/runtimetest.exe
|
||||
/runtimetest.ll
|
||||
/runtimetest.o
|
||||
/runtimetest.s
|
||||
|
||||
@ -9,10 +9,13 @@ LANG0C=$(CURDIR)/../2-lang0c/lang0c.exe
|
||||
all: lang0ll.exe
|
||||
|
||||
%.exe: %.s
|
||||
clang $^ -o $@
|
||||
clang -g $^ -o $@
|
||||
|
||||
%.s: %.ll
|
||||
llc --relocation-model=pic $^
|
||||
%.s: %.bc
|
||||
llc --relocation-model=pic $^ -o $@
|
||||
|
||||
%.bc: %.ll
|
||||
llvm-as $^ -o $@
|
||||
|
||||
lang0ll0.c: lang0ll.lang0 $(LANG0C)
|
||||
cat $< | $(LANG0C) > $@
|
||||
|
||||
@ -1,19 +0,0 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void print(char * x)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int sum(int a, int b)
|
||||
{
|
||||
return a + b;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
char * foo = "Hello, world!";
|
||||
printf("This is a test.");
|
||||
|
||||
return sum(7, 15);
|
||||
}
|
||||
@ -6,6 +6,7 @@ target triple = "x86_64-pc-linux-gnu"
|
||||
|
||||
declare void @exit(i32)
|
||||
declare i32 @fputs(i8*, %FILE*)
|
||||
declare i8* @malloc(i32)
|
||||
declare %FILE* @fdopen(i32, i8*)
|
||||
declare void @fclose(%FILE*)
|
||||
|
||||
@ -157,9 +158,111 @@ define i8* @__mapsetkey(i8* %0, i8* %1, i8* %2)
|
||||
{
|
||||
ret i8* %0 ; STUB
|
||||
}
|
||||
define i8* @__str_reverse(i8* %0, i64 %1)
|
||||
{
|
||||
%input_empty = icmp ule i64 %1, 1
|
||||
br i1 %input_empty, label %lbl_done, label %lbl_work_to_do
|
||||
lbl_work_to_do:
|
||||
|
||||
%arg1_sub1 = sub i64 %1, 1
|
||||
%rgidx_reg = alloca i64, align 8
|
||||
store i64 %arg1_sub1, i64* %rgidx_reg
|
||||
|
||||
%lfidx_reg = alloca i64, align 8
|
||||
store i64 0, i64* %lfidx_reg
|
||||
br label %loop
|
||||
|
||||
loop:
|
||||
%rgidx = load i64, i64* %rgidx_reg
|
||||
%lfidx = load i64, i64* %lfidx_reg
|
||||
|
||||
%rgidx_le_lfidx = icmp ule i64 %rgidx, %lfidx
|
||||
br i1 %rgidx_le_lfidx, label %lbl_done, label %lbl_flip_one
|
||||
lbl_flip_one:
|
||||
%rgadr = getelementptr i8, i8* %0, i64 %rgidx
|
||||
%rgchr = load i8, i8* %rgadr
|
||||
%lfadr = getelementptr i8, i8* %0, i64 %lfidx
|
||||
%lfchr = load i8, i8* %lfadr
|
||||
|
||||
store i8 %lfchr, i8* %rgadr
|
||||
store i8 %rgchr, i8* %lfadr
|
||||
|
||||
%new_rgidx = sub i64 %rgidx, 1
|
||||
%new_lfidx = add i64 %lfidx, 1
|
||||
|
||||
store i64 %new_rgidx, i64* %rgidx_reg
|
||||
store i64 %new_lfidx, i64* %lfidx_reg
|
||||
|
||||
br label %loop
|
||||
|
||||
lbl_done:
|
||||
ret i8* %0;
|
||||
}
|
||||
define i8* @__i64_to_str(i64 %i)
|
||||
{
|
||||
%res = call i8* @malloc(i64 32)
|
||||
store i8 48, i8* %res ; Base case
|
||||
|
||||
%idx_reg = alloca i64, align 8
|
||||
store i64 0, i64* %idx_reg
|
||||
|
||||
%rem_idx = alloca i64, align 8
|
||||
store i64 %i, i64* %rem_idx
|
||||
|
||||
br label %loop
|
||||
loop:
|
||||
%rem = load i64, i64* %rem_idx
|
||||
%rem.eq0 = icmp eq i64 %rem, 0
|
||||
br i1 %rem.eq0, label %lbl_done, label %lbl_addchar
|
||||
lbl_addchar:
|
||||
%digit = urem i64 %rem, 10
|
||||
%char.0 = trunc i64 %digit to i8
|
||||
%char.1 = add i8 48, %char.0
|
||||
|
||||
%idx = load i64, i64* %idx_reg
|
||||
%char.adr = getelementptr i8, i8* %res, i64 %idx
|
||||
store i8 %char.1, i8* %char.adr
|
||||
|
||||
%newrem.0 = sub i64 %rem, %digit
|
||||
%newrem.1 = udiv i64 %newrem.0, 10
|
||||
store i64 %newrem.1, i64* %rem_idx
|
||||
%newidx = add i64 %idx, 1
|
||||
store i64 %newidx, i64* %idx_reg
|
||||
|
||||
br label %loop
|
||||
|
||||
lbl_done:
|
||||
; null terminator
|
||||
%nt_idx = load i64, i64* %idx_reg
|
||||
%nt_adr = getelementptr i8, i8* %res, i64 %nt_idx
|
||||
store i8 0, i8* %nt_adr
|
||||
|
||||
; By not increasing the index, the index is the length
|
||||
|
||||
%len = load i64, i64* %idx_reg
|
||||
%rev = call i8* @__str_reverse(i8* %res, i64 %len)
|
||||
ret i8 *%rev
|
||||
}
|
||||
define i8* @__strlen(i8* %0)
|
||||
{
|
||||
ret i8* %0 ; STUB
|
||||
%i = alloca i64, align 8
|
||||
|
||||
store i64 0, i64* %i
|
||||
br label %loop
|
||||
loop:
|
||||
%index = load i64, i64* %i
|
||||
%char0_adr = getelementptr i8, i8* %0, i64 %index
|
||||
%char0 = load i8, i8* %char0_adr
|
||||
%char0_eol = icmp eq i8 %char0, 0
|
||||
br i1 %char0_eol, label %lbl_char0_eol, label %lbl_char0_not_eol
|
||||
lbl_char0_not_eol:
|
||||
%next_index = add i64 %index, 1
|
||||
store i64 %next_index, i64* %i
|
||||
br label %loop
|
||||
lbl_char0_eol:
|
||||
%len = load i64, i64* %i
|
||||
%res = call i8* @__i64_to_str(i64 %len)
|
||||
ret i8* %res;
|
||||
}
|
||||
define i8* @__intinc(i8* %0)
|
||||
{
|
||||
@ -208,6 +311,12 @@ define void @test_not(i8* %0)
|
||||
call void @test_result_1(i8* @str.not, i8* %0, i8* %result)
|
||||
ret void
|
||||
}
|
||||
define void @test_strlen(i8* %0)
|
||||
{
|
||||
%result = call i8* @__strlen(i8* %0)
|
||||
call void @test_result_1(i8* @str.strlen, i8* %0, i8* %result)
|
||||
ret void
|
||||
}
|
||||
|
||||
|
||||
define i64 @main()
|
||||
@ -248,6 +357,16 @@ define i64 @main()
|
||||
call void @test_not(i8* @str.0)
|
||||
call void @test_not(i8* @str.1)
|
||||
|
||||
call void @test_strlen(i8* @str.)
|
||||
call void @test_strlen(i8* @str.a)
|
||||
call void @test_strlen(i8* @str.eq)
|
||||
call void @test_strlen(i8* @str.not)
|
||||
call void @test_strlen(i8* @str.left)
|
||||
call void @test_strlen(i8* @str.right)
|
||||
call void @test_strlen(i8* @str.strlen)
|
||||
call void @test_strlen(i8* @str.explanation)
|
||||
call void @test_strlen(i8* @str.cats_paragraph)
|
||||
|
||||
|
||||
|
||||
call i32 @fclose(%FILE* %stdout_ptr)
|
||||
@ -258,6 +377,11 @@ define i64 @main()
|
||||
@str. = internal constant [1 x i8] c"\00"
|
||||
@str.0 = internal constant [2 x i8] c"0\00"
|
||||
@str.1 = internal constant [2 x i8] c"1\00"
|
||||
@str.2 = internal constant [2 x i8] c"2\00"
|
||||
@str.3 = internal constant [2 x i8] c"3\00"
|
||||
@str.4 = internal constant [2 x i8] c"4\00"
|
||||
@str.5 = internal constant [2 x i8] c"5\00"
|
||||
@str.6 = internal constant [2 x i8] c"6\00"
|
||||
@str.a = internal constant [2 x i8] c"a\00"
|
||||
@str.b = internal constant [2 x i8] c"b\00"
|
||||
@str.c = internal constant [2 x i8] c"c\00"
|
||||
@ -269,4 +393,8 @@ define i64 @main()
|
||||
@str.lt = internal constant [3 x i8] c"lt\00"
|
||||
@str.not = internal constant [4 x i8] c"not\00"
|
||||
@str.right = internal constant [6 x i8] c"right\00"
|
||||
@str.strlen = internal constant [7 x i8] c"strlen\00"
|
||||
@str.w = internal constant [2 x i8] c"w\00"
|
||||
|
||||
@str.explanation = internal constant [135 x i8] c"Bitwise AND Operation: The and instruction performs a bitwise AND between the input value and the mask to isolate the smallest 8 bits.\00"
|
||||
@str.cats_paragraph = internal constant [1193 x i8] c"Cats are fascinating creatures that have captivated humans for thousands of years. Belonging to the family Felidae, they are known for their agility, sharp senses, and playful behavior. Cats communicate through a variety of vocalizations, body language, and purring. One remarkable feature is their keen hunting skills; they possess sharp retractable claws and excellent night vision, enabling them to stalk and capture prey effectively. Domesticated cats, often kept as pets, bring joy to many households. They exhibit a range of personalities, from solitary and aloof to affectionate and social. Cats also enjoy playing, often engaging in activities that mimic hunting, such as chasing toys or pouncing on moving objects. Additionally, their grooming habits help keep their fur clean and contain calming pheromones that promote bonding with their human companions. Studies suggest that having a cat can reduce stress and anxiety levels. This unique relationship invites appreciation for their independence and charm, as they provide companionship while often maintaining a mysterious aura. Overall, cats are beloved pets that enrich our lives with their unique attributes and companionship.\00"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user