Skip to content

Commit

Permalink
add pervasive.ml
Browse files Browse the repository at this point in the history
  • Loading branch information
satos---jp committed Sep 5, 2018
1 parent 40fbc01 commit cc924f0
Show file tree
Hide file tree
Showing 25 changed files with 220 additions and 58 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -51,3 +51,6 @@ a.ml
a.mli
objd.txt
tes.sh
*.s
!lib/asm/*.s
syntax_modoki.ml
136 changes: 107 additions & 29 deletions lib/lib.s → lib/asm/lib.s
Expand Up @@ -19,11 +19,83 @@ global read_int
global read_float
global raise_match_failure

extern print_char
extern print_char_err
extern read_char

section .data
fless:
dd fless_p
fless_p:
dd fless_
int_of_float:
dd int_of_float_p
int_of_float_p:
dd int_of_float_
float_of_int:
dd float_of_int_p
float_of_int_p:
dd float_of_int_
fiszero:
dd fiszero_p
fiszero_p:
dd fiszero_
fispos:
dd fispos_p
fispos_p:
dd fispos_
fisneg:
dd fisneg_p
fisneg_p:
dd fisneg_
fabs:
dd fabs_p
fabs_p:
dd fabs_
floor:
dd floor_p
floor_p:
dd floor_
fsqr:
dd fsqr_p
fsqr_p:
dd fsqr_
fneg:
dd fneg_p
fneg_p:
dd fneg_
fhalf:
dd fhalf_p
fhalf_p:
dd fhalf_
sqrt:
dd sqrt_p
sqrt_p:
dd sqrt_
sin:
dd sin_p
sin_p:
dd sin_
cos:
dd cos_p
cos_p:
dd cos_
atan:
dd atan_p
atan_p:
dd atan_
read_int:
dd read_int_p
read_int_p:
dd read_int_
read_float:
dd read_float_p
read_float_p:
dd read_float_
raise_match_failure:
dd raise_match_failure_p
raise_match_failure_p:
dd raise_match_failure_

two:
dd 2.0
half:
Expand All @@ -48,7 +120,7 @@ minusone:

section .text

float_of_int: ; int -> float
float_of_int_: ; int -> float
fild dword [esp+0x4]
fstp dword [esp-0x4]
mov eax,[esp-0x4]
Expand All @@ -61,21 +133,21 @@ float_of_int: ; int -> float

; これ、OCamlと乖離させる。
; またOCamlに合わせ直す
int_of_float: ; float -> int
int_of_float_: ; float -> int
push ebp
mov ebp,esp
sub esp,0x4
mov eax,dword [ebp+0x8]
mov dword [ebp-0x4],eax

push dword [ebp+0x8]
call fiszero
call fiszero_
add esp,0x4
test eax,eax
jne int_of_float_exact_zero

push dword [ebp+0x8]
call fisneg
call fisneg_
add esp,0x4
test eax,eax
jne int_of_float_neg
Expand All @@ -100,8 +172,8 @@ int_of_float_exact_zero:

add esp,0x4
pop ebp
ret
fless:
ret
fless_:
xor eax,eax
fld dword [esp+0x8]
fld dword [esp+0x4]
Expand All @@ -112,7 +184,7 @@ fless:
and eax,1
ret

fiszero:
fiszero_:
xor eax,eax
fld dword [esp+0x4]
ftst
Expand All @@ -122,7 +194,7 @@ fiszero:
and eax,1
ret

fisneg:
fisneg_:
xor eax,eax
fld dword [esp+0x4]
ftst
Expand All @@ -132,15 +204,15 @@ fisneg:
and eax,1
ret

fhalf:
fhalf_:
fld dword [esp+0x4]
fld dword [half]
fmulp
fstp dword [esp-0x4]
mov eax,[esp-0x4]
ret

fispos:
fispos_:
xor eax,eax
fld dword [esp+0x4]
ftst
Expand All @@ -155,51 +227,51 @@ fispos:
ret


fneg:
fneg_:
fld dword [esp+0x4]
fchs
fstp dword [esp-0x4]
mov eax,[esp-0x4]
ret

fsqr:
fsqr_:
fld dword [esp+0x4]
fld dword [esp+0x4]
fmulp
fstp dword [esp-0x4]
mov eax,[esp-0x4]
ret

sqrt:
sqrt_:
fld dword [esp+0x4]
fsqrt
fstp dword [esp-0x4]
mov eax,[esp-0x4]
ret

cos:
cos_:
fld dword [esp+0x4]
fcos
fstp dword [esp-0x4]
mov eax,[esp-0x4]
ret

sin:
sin_:
fld dword [esp+0x4]
fsin
fstp dword [esp-0x4]
mov eax,[esp-0x4]
ret

atan:
atan_:
fld dword [esp+0x4]
fld1
fpatan
fstp dword [esp-0x4]
mov eax,[esp-0x4]
ret

fabs:
fabs_:
fld dword [esp+0x4]
fabs
fstp dword [esp-0x4]
Expand All @@ -211,7 +283,7 @@ fabs:
; これもOcamlと乖離させる。
; OCamlに戻します

floor:
floor_:
push dword [esp+0x4]
call fisneg
add esp,4
Expand Down Expand Up @@ -302,7 +374,8 @@ loop:
mov ebp,esp
sub esp,60
push edi
call read_char
mov edx,[read_char]
call [edx]
mov dword [ebp-20],eax
add esp,0
pop edi
Expand Down Expand Up @@ -378,12 +451,13 @@ loop:
add esp,60
pop ebp
ret
read_int:
read_int_:
push ebp
mov ebp,esp
sub esp,60
push edi
call read_char
mov edx,[read_char]
call [edx]
mov dword [ebp-12],eax
add esp,0
pop edi
Expand Down Expand Up @@ -461,7 +535,7 @@ read_int:
jmp @virtual_label_1000000318
@virtual_label_1000000317:
push edi
call read_int
call read_int_
mov dword [ebp-60],eax
add esp,0
pop edi
Expand Down Expand Up @@ -491,7 +565,8 @@ isnum:
ret

get_until_num:
call read_char
mov edx,[read_char]
call [edx]
cmp eax,0x2d
je ok_get_until_num
cmp eax,0x2e
Expand All @@ -509,15 +584,16 @@ retry_get_until_num:
ret


read_float:
read_float_:
finit
sub esp,0x4
call get_until_num
cmp eax,0x2d
jne isplus
ismunis:
fld dword [minusone]
call read_char
mov edx,[read_char]
call [edx]
jmp read_body_float
isplus:
fld dword [one]
Expand All @@ -541,7 +617,8 @@ read_body_loop:
pop eax
faddp
; top += readc
call read_char
mov edx,[read_char]
call [edx]
jmp read_body_loop
read_dot:
cmp eax,0x2e
Expand All @@ -550,7 +627,8 @@ read_dot:
push dword [one]
fld dword [zero]
read_tenika_loop:
call read_char
mov edx,[read_char]
call [edx]
push eax
call isnum
mov ebx,eax
Expand Down Expand Up @@ -610,7 +688,7 @@ print_string_err_loop:
pop ebp
ret

raise_match_failure:
raise_match_failure_:
push estr
call print_string_err
add esp,4
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
22 changes: 19 additions & 3 deletions lib/libio_linux.s → lib/asm/libio_linux.s
Expand Up @@ -11,6 +11,22 @@ print_char:
print_char_p:
dd print_char_

read_char:
dd read_char_p
read_char_p:
dd read_char_

print_char_err:
dd print_char_err_p
print_char_err_p:
dd print_char_err_

puts_err:
dd puts_err_p
puts_err_p:
dd puts_err_


section .text

print_char_:
Expand All @@ -21,7 +37,7 @@ print_char_:
int 0x80
ret

read_char:
read_char_:
mov edx,1
lea ecx,[esp-0x4]
mov ebx,0
Expand All @@ -31,15 +47,15 @@ read_char:
and eax,0xff
ret

print_char_err:
print_char_err_:
mov edx,1
lea ecx,[esp+0x4]
mov ebx,2
mov eax,4
int 0x80
ret

puts_err:
puts_err_:
mov edx,dword [esp+0x8]
mov ecx,dword [esp+0x4]
mov ebx,2
Expand Down
File renamed without changes.
1 change: 0 additions & 1 deletion lib/lib.mli

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions lib/ml/pervasive.mli
@@ -0,0 +1,2 @@
val print_int : int -> unit

0 comments on commit cc924f0

Please sign in to comment.