This repository has been archived by the owner on Sep 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Leon Rische
committed
Jan 23, 2017
1 parent
2ac6a4b
commit 673a57d
Showing
13 changed files
with
202 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
(define (escape str) | ||
(let ((parts (map ->string (string->list (->string str))))) | ||
(string-join | ||
(cons "prim_" | ||
(map | ||
(lambda (part) | ||
(cond | ||
((equal? part "+") "_plus_") | ||
((equal? part ">") "_greater_") | ||
((equal? part "<") "_less_") | ||
((equal? part "=") "_equal_") | ||
((equal? part "*") "_times_") | ||
((equal? part "?") "_questionmark_") | ||
(else part))) | ||
parts))))) | ||
|
||
(define (tagged-list? expr tag) | ||
(and (pair? expr) | ||
(eq? (car expr) tag))) | ||
|
||
(define (string-join lst) (foldl string-append "" lst)) | ||
(define (show . args) (string-join (map ->string args))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,81 @@ | ||
declare i8* @calloc(i32, i32) | ||
declare void @free(i8*) | ||
declare void @print_ptr(i64, i8*) | ||
declare void @puts_ptr(i64, i8*) | ||
declare i8 @putchar(i8) | ||
|
||
@heap_base = global i64* zeroinitializer, align 8 | ||
@raw_heap_base = global i8* zeroinitializer, align 8 | ||
declare void @print_ptr(i64) | ||
declare void @puts_ptr(i64) | ||
|
||
@heap_base = global i8* zeroinitializer, align 8 | ||
@heap_index = global i64 0, align 8 | ||
|
||
define i64 @prim_print(i64 %a) { | ||
%base = load i8*, i8** @raw_heap_base | ||
call void @print_ptr(i64 %a, i8* %base) | ||
define i64 @prim_inspect(i64 %a) { | ||
call void @print_ptr(i64 %a) | ||
; TODO: Return empty list or undefined | ||
ret i64 0 | ||
} | ||
|
||
define i64 @prim_puts(i64 %a) { | ||
%base = load i8*, i8** @raw_heap_base | ||
call void @puts_ptr(i64 %a, i8* %base) | ||
define i64 @prim_newline() { | ||
call i8 @putchar(i8 10) | ||
; TODO: Return empty list or undefined | ||
ret i64 0 | ||
} | ||
|
||
define i64 @prim_putchar(i64 %a) { | ||
%raw_value = lshr i64 %a, 3 | ||
%char = trunc i64 %raw_value to i8 | ||
|
||
%raw_res = call i8 @putchar(i8 %char) | ||
%res = zext i8 %raw_res to i64 | ||
%res2 = shl i64 %res, 3 | ||
|
||
ret i64 %res2 | ||
} | ||
|
||
define i64 @internal_heap-store(i64 %val) { | ||
%heap_base = load i8*, i8** @heap_base | ||
%heap_index = load i64, i64* @heap_index | ||
|
||
%raw_heap_ptr = getelementptr i8, i8* %heap_base, i64 %heap_index | ||
%heap_ptr = bitcast i8* %raw_heap_ptr to i64* | ||
|
||
store i64 %val, i64* %heap_ptr | ||
%int_ptr = ptrtoint i8* %raw_heap_ptr to i64 | ||
|
||
%new_heap_index = add i64 %heap_index, 8 | ||
store i64 %new_heap_index, i64* @heap_index, align 8 | ||
|
||
ret i64 %int_ptr | ||
} | ||
|
||
define i64 @internal_heap-store-byte(i8 %val) { | ||
%heap_base = load i8*, i8** @heap_base | ||
%heap_index = load i64, i64* @heap_index | ||
|
||
%raw_heap_ptr = getelementptr i8, i8* %heap_base, i64 %heap_index | ||
|
||
store i8 %val, i8* %raw_heap_ptr | ||
%int_ptr = ptrtoint i8* %raw_heap_ptr to i64 | ||
|
||
%new_heap_index = add i64 %heap_index, 1 | ||
store i64 %new_heap_index, i64* @heap_index, align 8 | ||
|
||
ret i64 %int_ptr | ||
} | ||
|
||
define void @internal_heap-align-index() { | ||
%heap_index = load i64, i64* @heap_index | ||
; Get the last 3 bytes of the index | ||
%rem = and i64 %heap_index, 7 | ||
; If it is a multiple of 8, those bytes are 0b000 | ||
|
||
%test = icmp eq i64 %rem, 0 | ||
br i1 %test, label %true, label %false | ||
true: | ||
ret void | ||
|
||
false: | ||
%tmp = sub i64 %heap_index, %rem | ||
%new_heap_index = add i64 %tmp, 8 | ||
store i64 %new_heap_index, i64* @heap_index, align 8 | ||
ret void | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
(defn emit-string (var str) | ||
(let ((len (string-length str)) | ||
(tmp (generate-var))) | ||
(emit (format " ~A = call i64 @internal_heap-store(i64 ~A)" tmp len)) | ||
(begin | ||
(defn helper (str idx len) | ||
(if (< idx len) | ||
(let* ((char (string-ref str idx)) | ||
(ord (char->integer char))) | ||
(emit (format " call i64 @internal_heap-store-byte(i8 ~A)" ord)) | ||
(helper str (add1 idx) len)) | ||
(emit (format " call i64 @internal_heap-store-byte(i8 0)")))) | ||
(helper str 0 len)) | ||
(emit (format " call void @internal_heap-align-index()")) | ||
(emit (format "~A = or i64 ~A, 5" var tmp)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters