mirror of
https://github.com/KolibriOS/kolibrios.git
synced 2024-12-15 19:33:59 +03:00
2f54c7de00
git-svn-id: svn://kolibrios.org@8097 a494cfbc-eb01-0410-851d-a64ba20cac60
503 lines
16 KiB
Plaintext
503 lines
16 KiB
Plaintext
(*
|
|
BSD 2-Clause License
|
|
|
|
Copyright (c) 2018-2020, Anton Krotov
|
|
All rights reserved.
|
|
*)
|
|
|
|
MODULE RTL;
|
|
|
|
IMPORT SYSTEM, API;
|
|
|
|
|
|
CONST
|
|
|
|
bit_depth* = 64;
|
|
maxint* = 7FFFFFFFFFFFFFFFH;
|
|
minint* = 8000000000000000H;
|
|
|
|
WORD = bit_depth DIV 8;
|
|
MAX_SET = bit_depth - 1;
|
|
|
|
|
|
VAR
|
|
|
|
name: INTEGER;
|
|
types: INTEGER;
|
|
sets: ARRAY (MAX_SET + 1) * (MAX_SET + 1) OF INTEGER;
|
|
|
|
|
|
PROCEDURE [stdcall64] _move* (bytes, dest, source: INTEGER);
|
|
BEGIN
|
|
SYSTEM.CODE(
|
|
048H, 08BH, 045H, 010H, (* mov rax, qword [rbp + 16] *)
|
|
048H, 085H, 0C0H, (* test rax, rax *)
|
|
07EH, 020H, (* jle L *)
|
|
0FCH, (* cld *)
|
|
057H, (* push rdi *)
|
|
056H, (* push rsi *)
|
|
048H, 08BH, 075H, 020H, (* mov rsi, qword [rbp + 32] *)
|
|
048H, 08BH, 07DH, 018H, (* mov rdi, qword [rbp + 24] *)
|
|
048H, 089H, 0C1H, (* mov rcx, rax *)
|
|
048H, 0C1H, 0E9H, 003H, (* shr rcx, 3 *)
|
|
0F3H, 048H, 0A5H, (* rep movsd *)
|
|
048H, 089H, 0C1H, (* mov rcx, rax *)
|
|
048H, 083H, 0E1H, 007H, (* and rcx, 7 *)
|
|
0F3H, 0A4H, (* rep movsb *)
|
|
05EH, (* pop rsi *)
|
|
05FH (* pop rdi *)
|
|
(* L: *)
|
|
)
|
|
END _move;
|
|
|
|
|
|
PROCEDURE [stdcall64] _arrcpy* (base_size, len_dst, dst, len_src, src: INTEGER): BOOLEAN;
|
|
VAR
|
|
res: BOOLEAN;
|
|
|
|
BEGIN
|
|
IF len_src > len_dst THEN
|
|
res := FALSE
|
|
ELSE
|
|
_move(len_src * base_size, dst, src);
|
|
res := TRUE
|
|
END
|
|
|
|
RETURN res
|
|
END _arrcpy;
|
|
|
|
|
|
PROCEDURE [stdcall64] _strcpy* (chr_size, len_src, src, len_dst, dst: INTEGER);
|
|
BEGIN
|
|
_move(MIN(len_dst, len_src) * chr_size, dst, src)
|
|
END _strcpy;
|
|
|
|
|
|
PROCEDURE [stdcall64] _rot* (VAR A: ARRAY OF INTEGER);
|
|
VAR
|
|
i, n, k: INTEGER;
|
|
|
|
BEGIN
|
|
k := LEN(A) - 1;
|
|
n := A[0];
|
|
i := 0;
|
|
WHILE i < k DO
|
|
A[i] := A[i + 1];
|
|
INC(i)
|
|
END;
|
|
A[k] := n
|
|
END _rot;
|
|
|
|
|
|
PROCEDURE [stdcall64] _set* (b, a: INTEGER): INTEGER;
|
|
BEGIN
|
|
IF (a <= b) & (a <= MAX_SET) & (b >= 0) THEN
|
|
SYSTEM.GET((MIN(b, MAX_SET) * (MAX_SET + 1) + MAX(a, 0)) * WORD + SYSTEM.ADR(sets[0]), a)
|
|
ELSE
|
|
a := 0
|
|
END
|
|
|
|
RETURN a
|
|
END _set;
|
|
|
|
|
|
PROCEDURE [stdcall64] _set1* (a: INTEGER); (* {a} -> rax *)
|
|
BEGIN
|
|
SYSTEM.CODE(
|
|
048H, 031H, 0C0H, (* xor rax, rax *)
|
|
048H, 08BH, 04DH, 010H, (* mov rcx, qword [rbp + 16] *) (* rcx <- a *)
|
|
048H, 083H, 0F9H, 03FH, (* cmp rcx, 63 *)
|
|
077H, 004H, (* ja L *)
|
|
048H, 00FH, 0ABH, 0C8H (* bts rax, rcx *)
|
|
(* L: *)
|
|
)
|
|
END _set1;
|
|
|
|
|
|
PROCEDURE [stdcall64] _divmod* (y, x: INTEGER); (* (x div y) -> rax; (x mod y) -> rdx *)
|
|
BEGIN
|
|
SYSTEM.CODE(
|
|
048H, 08BH, 045H, 018H, (* mov rax, qword [rbp + 24] *) (* rax <- x *)
|
|
048H, 031H, 0D2H, (* xor rdx, rdx *)
|
|
048H, 085H, 0C0H, (* test rax, rax *)
|
|
074H, 022H, (* je L2 *)
|
|
07FH, 003H, (* jg L1 *)
|
|
048H, 0F7H, 0D2H, (* not rdx *)
|
|
(* L1: *)
|
|
049H, 089H, 0C0H, (* mov r8, rax *)
|
|
048H, 08BH, 04DH, 010H, (* mov rcx, qword [rbp + 16] *) (* rcx <- y *)
|
|
048H, 0F7H, 0F9H, (* idiv rcx *)
|
|
048H, 085H, 0D2H, (* test rdx, rdx *)
|
|
074H, 00EH, (* je L2 *)
|
|
049H, 031H, 0C8H, (* xor r8, rcx *)
|
|
04DH, 085H, 0C0H, (* test r8, r8 *)
|
|
07DH, 006H, (* jge L2 *)
|
|
048H, 0FFH, 0C8H, (* dec rax *)
|
|
048H, 001H, 0CAH (* add rdx, rcx *)
|
|
(* L2: *)
|
|
)
|
|
END _divmod;
|
|
|
|
|
|
PROCEDURE [stdcall64] _new* (t, size: INTEGER; VAR ptr: INTEGER);
|
|
BEGIN
|
|
ptr := API._NEW(size);
|
|
IF ptr # 0 THEN
|
|
SYSTEM.PUT(ptr, t);
|
|
INC(ptr, WORD)
|
|
END
|
|
END _new;
|
|
|
|
|
|
PROCEDURE [stdcall64] _dispose* (VAR ptr: INTEGER);
|
|
BEGIN
|
|
IF ptr # 0 THEN
|
|
ptr := API._DISPOSE(ptr - WORD)
|
|
END
|
|
END _dispose;
|
|
|
|
|
|
PROCEDURE [stdcall64] _length* (len, str: INTEGER);
|
|
BEGIN
|
|
SYSTEM.CODE(
|
|
048H, 08BH, 045H, 018H, (* mov rax, qword [rbp + 24] *)
|
|
048H, 08BH, 04DH, 010H, (* mov rcx, qword [rbp + 16] *)
|
|
048H, 0FFH, 0C8H, (* dec rax *)
|
|
(* L1: *)
|
|
048H, 0FFH, 0C0H, (* inc rax *)
|
|
080H, 038H, 000H, (* cmp byte [rax], 0 *)
|
|
074H, 005H, (* jz L2 *)
|
|
0E2H, 0F6H, (* loop L1 *)
|
|
048H, 0FFH, 0C0H, (* inc rax *)
|
|
(* L2: *)
|
|
048H, 02BH, 045H, 018H (* sub rax, qword [rbp + 24] *)
|
|
)
|
|
END _length;
|
|
|
|
|
|
PROCEDURE [stdcall64] _lengthw* (len, str: INTEGER);
|
|
BEGIN
|
|
SYSTEM.CODE(
|
|
048H, 08BH, 045H, 018H, (* mov rax, qword [rbp + 24] *)
|
|
048H, 08BH, 04DH, 010H, (* mov rcx, qword [rbp + 16] *)
|
|
048H, 083H, 0E8H, 002H, (* sub rax, 2 *)
|
|
(* L1: *)
|
|
048H, 083H, 0C0H, 002H, (* add rax, 2 *)
|
|
066H, 083H, 038H, 000H, (* cmp word [rax], 0 *)
|
|
074H, 006H, (* jz L2 *)
|
|
0E2H, 0F4H, (* loop L1 *)
|
|
048H, 083H, 0C0H, 002H, (* add rax, 2 *)
|
|
(* L2: *)
|
|
048H, 02BH, 045H, 018H, (* sub rax, qword [rbp + 24] *)
|
|
048H, 0D1H, 0E8H (* shr rax, 1 *)
|
|
)
|
|
END _lengthw;
|
|
|
|
|
|
PROCEDURE [stdcall64] strncmp (a, b, n: INTEGER): INTEGER;
|
|
BEGIN
|
|
SYSTEM.CODE(
|
|
048H, 08BH, 04DH, 010H, (* mov rcx, qword[rbp + 16]; rcx <- a *)
|
|
048H, 08BH, 055H, 018H, (* mov rdx, qword[rbp + 24]; rdx <- b *)
|
|
04CH, 08BH, 045H, 020H, (* mov r8, qword[rbp + 32]; r8 <- n *)
|
|
04DH, 031H, 0C9H, (* xor r9, r9 *)
|
|
04DH, 031H, 0D2H, (* xor r10, r10 *)
|
|
048H, 0B8H, 000H, 000H,
|
|
000H, 000H, 000H, 000H,
|
|
000H, 080H, (* movabs rax, minint *)
|
|
(* L1: *)
|
|
04DH, 085H, 0C0H, (* test r8, r8 *)
|
|
07EH, 024H, (* jle L3 *)
|
|
044H, 08AH, 009H, (* mov r9b, byte[rcx] *)
|
|
044H, 08AH, 012H, (* mov r10b, byte[rdx] *)
|
|
048H, 0FFH, 0C1H, (* inc rcx *)
|
|
048H, 0FFH, 0C2H, (* inc rdx *)
|
|
049H, 0FFH, 0C8H, (* dec r8 *)
|
|
04DH, 039H, 0D1H, (* cmp r9, r10 *)
|
|
074H, 008H, (* je L2 *)
|
|
04CH, 089H, 0C8H, (* mov rax, r9 *)
|
|
04CH, 029H, 0D0H, (* sub rax, r10 *)
|
|
0EBH, 008H, (* jmp L3 *)
|
|
(* L2: *)
|
|
04DH, 085H, 0C9H, (* test r9, r9 *)
|
|
075H, 0DAH, (* jne L1 *)
|
|
048H, 031H, 0C0H, (* xor rax, rax *)
|
|
(* L3: *)
|
|
05DH, (* pop rbp *)
|
|
0C2H, 018H, 000H (* ret 24 *)
|
|
)
|
|
RETURN 0
|
|
END strncmp;
|
|
|
|
|
|
PROCEDURE [stdcall64] strncmpw (a, b, n: INTEGER): INTEGER;
|
|
BEGIN
|
|
SYSTEM.CODE(
|
|
048H, 08BH, 04DH, 010H, (* mov rcx, qword[rbp + 16]; rcx <- a *)
|
|
048H, 08BH, 055H, 018H, (* mov rdx, qword[rbp + 24]; rdx <- b *)
|
|
04CH, 08BH, 045H, 020H, (* mov r8, qword[rbp + 32]; r8 <- n *)
|
|
04DH, 031H, 0C9H, (* xor r9, r9 *)
|
|
04DH, 031H, 0D2H, (* xor r10, r10 *)
|
|
048H, 0B8H, 000H, 000H,
|
|
000H, 000H, 000H, 000H,
|
|
000H, 080H, (* movabs rax, minint *)
|
|
(* L1: *)
|
|
04DH, 085H, 0C0H, (* test r8, r8 *)
|
|
07EH, 028H, (* jle L3 *)
|
|
066H, 044H, 08BH, 009H, (* mov r9w, word[rcx] *)
|
|
066H, 044H, 08BH, 012H, (* mov r10w, word[rdx] *)
|
|
048H, 083H, 0C1H, 002H, (* add rcx, 2 *)
|
|
048H, 083H, 0C2H, 002H, (* add rdx, 2 *)
|
|
049H, 0FFH, 0C8H, (* dec r8 *)
|
|
04DH, 039H, 0D1H, (* cmp r9, r10 *)
|
|
074H, 008H, (* je L2 *)
|
|
04CH, 089H, 0C8H, (* mov rax, r9 *)
|
|
04CH, 029H, 0D0H, (* sub rax, r10 *)
|
|
0EBH, 008H, (* jmp L3 *)
|
|
(* L2: *)
|
|
04DH, 085H, 0C9H, (* test r9, r9 *)
|
|
075H, 0D6H, (* jne L1 *)
|
|
048H, 031H, 0C0H, (* xor rax, rax *)
|
|
(* L3: *)
|
|
05DH, (* pop rbp *)
|
|
0C2H, 018H, 000H (* ret 24 *)
|
|
)
|
|
RETURN 0
|
|
END strncmpw;
|
|
|
|
|
|
PROCEDURE [stdcall64] _strcmp* (op, len2, str2, len1, str1: INTEGER): BOOLEAN;
|
|
VAR
|
|
res: INTEGER;
|
|
bRes: BOOLEAN;
|
|
c: CHAR;
|
|
|
|
BEGIN
|
|
res := strncmp(str1, str2, MIN(len1, len2));
|
|
IF res = minint THEN
|
|
IF len1 > len2 THEN
|
|
SYSTEM.GET(str1 + len2, c);
|
|
res := ORD(c)
|
|
ELSIF len1 < len2 THEN
|
|
SYSTEM.GET(str2 + len1, c);
|
|
res := -ORD(c)
|
|
ELSE
|
|
res := 0
|
|
END
|
|
END;
|
|
|
|
CASE op OF
|
|
|0: bRes := res = 0
|
|
|1: bRes := res # 0
|
|
|2: bRes := res < 0
|
|
|3: bRes := res <= 0
|
|
|4: bRes := res > 0
|
|
|5: bRes := res >= 0
|
|
END
|
|
|
|
RETURN bRes
|
|
END _strcmp;
|
|
|
|
|
|
PROCEDURE [stdcall64] _strcmpw* (op, len2, str2, len1, str1: INTEGER): BOOLEAN;
|
|
VAR
|
|
res: INTEGER;
|
|
bRes: BOOLEAN;
|
|
c: WCHAR;
|
|
|
|
BEGIN
|
|
res := strncmpw(str1, str2, MIN(len1, len2));
|
|
IF res = minint THEN
|
|
IF len1 > len2 THEN
|
|
SYSTEM.GET(str1 + len2 * 2, c);
|
|
res := ORD(c)
|
|
ELSIF len1 < len2 THEN
|
|
SYSTEM.GET(str2 + len1 * 2, c);
|
|
res := -ORD(c)
|
|
ELSE
|
|
res := 0
|
|
END
|
|
END;
|
|
|
|
CASE op OF
|
|
|0: bRes := res = 0
|
|
|1: bRes := res # 0
|
|
|2: bRes := res < 0
|
|
|3: bRes := res <= 0
|
|
|4: bRes := res > 0
|
|
|5: bRes := res >= 0
|
|
END
|
|
|
|
RETURN bRes
|
|
END _strcmpw;
|
|
|
|
|
|
PROCEDURE PCharToStr (pchar: INTEGER; VAR s: ARRAY OF CHAR);
|
|
VAR
|
|
c: CHAR;
|
|
i: INTEGER;
|
|
|
|
BEGIN
|
|
i := 0;
|
|
REPEAT
|
|
SYSTEM.GET(pchar, c);
|
|
s[i] := c;
|
|
INC(pchar);
|
|
INC(i)
|
|
UNTIL c = 0X
|
|
END PCharToStr;
|
|
|
|
|
|
PROCEDURE IntToStr (x: INTEGER; VAR str: ARRAY OF CHAR);
|
|
VAR
|
|
i, a: INTEGER;
|
|
|
|
BEGIN
|
|
i := 0;
|
|
a := x;
|
|
REPEAT
|
|
INC(i);
|
|
a := a DIV 10
|
|
UNTIL a = 0;
|
|
|
|
str[i] := 0X;
|
|
|
|
REPEAT
|
|
DEC(i);
|
|
str[i] := CHR(x MOD 10 + ORD("0"));
|
|
x := x DIV 10
|
|
UNTIL x = 0
|
|
END IntToStr;
|
|
|
|
|
|
PROCEDURE append (VAR s1: ARRAY OF CHAR; s2: ARRAY OF CHAR);
|
|
VAR
|
|
n1, n2: INTEGER;
|
|
|
|
BEGIN
|
|
n1 := LENGTH(s1);
|
|
n2 := LENGTH(s2);
|
|
|
|
ASSERT(n1 + n2 < LEN(s1));
|
|
|
|
SYSTEM.MOVE(SYSTEM.ADR(s2[0]), SYSTEM.ADR(s1[n1]), n2);
|
|
s1[n1 + n2] := 0X
|
|
END append;
|
|
|
|
|
|
PROCEDURE [stdcall64] _error* (modnum, _module, err, line: INTEGER);
|
|
VAR
|
|
s, temp: ARRAY 1024 OF CHAR;
|
|
|
|
BEGIN
|
|
CASE err OF
|
|
| 1: s := "assertion failure"
|
|
| 2: s := "NIL dereference"
|
|
| 3: s := "bad divisor"
|
|
| 4: s := "NIL procedure call"
|
|
| 5: s := "type guard error"
|
|
| 6: s := "index out of range"
|
|
| 7: s := "invalid CASE"
|
|
| 8: s := "array assignment error"
|
|
| 9: s := "CHR out of range"
|
|
|10: s := "WCHR out of range"
|
|
|11: s := "BYTE out of range"
|
|
END;
|
|
|
|
append(s, API.eol + "module: "); PCharToStr(_module, temp); append(s, temp);
|
|
append(s, API.eol + "line: "); IntToStr(line, temp); append(s, temp);
|
|
|
|
API.DebugMsg(SYSTEM.ADR(s[0]), name);
|
|
|
|
API.exit_thread(0)
|
|
END _error;
|
|
|
|
|
|
PROCEDURE [stdcall64] _isrec* (t0, t1, r: INTEGER): INTEGER;
|
|
BEGIN
|
|
SYSTEM.GET(t0 + t1 + types, t0)
|
|
RETURN t0 MOD 2
|
|
END _isrec;
|
|
|
|
|
|
PROCEDURE [stdcall64] _is* (t0, p: INTEGER): INTEGER;
|
|
BEGIN
|
|
IF p # 0 THEN
|
|
SYSTEM.GET(p - WORD, p);
|
|
SYSTEM.GET(t0 + p + types, p)
|
|
END
|
|
|
|
RETURN p MOD 2
|
|
END _is;
|
|
|
|
|
|
PROCEDURE [stdcall64] _guardrec* (t0, t1: INTEGER): INTEGER;
|
|
BEGIN
|
|
SYSTEM.GET(t0 + t1 + types, t0)
|
|
RETURN t0 MOD 2
|
|
END _guardrec;
|
|
|
|
|
|
PROCEDURE [stdcall64] _guard* (t0, p: INTEGER): INTEGER;
|
|
BEGIN
|
|
SYSTEM.GET(p, p);
|
|
IF p # 0 THEN
|
|
SYSTEM.GET(p - WORD, p);
|
|
SYSTEM.GET(t0 + p + types, p)
|
|
ELSE
|
|
p := 1
|
|
END
|
|
|
|
RETURN p MOD 2
|
|
END _guard;
|
|
|
|
|
|
PROCEDURE [stdcall64] _dllentry* (hinstDLL, fdwReason, lpvReserved: INTEGER): INTEGER;
|
|
RETURN API.dllentry(hinstDLL, fdwReason, lpvReserved)
|
|
END _dllentry;
|
|
|
|
|
|
PROCEDURE [stdcall64] _sofinit*;
|
|
BEGIN
|
|
API.sofinit
|
|
END _sofinit;
|
|
|
|
|
|
PROCEDURE [stdcall64] _exit* (code: INTEGER);
|
|
BEGIN
|
|
API.exit(code)
|
|
END _exit;
|
|
|
|
|
|
PROCEDURE [stdcall64] _init* (modname: INTEGER; tcount, _types: INTEGER; code, param: INTEGER);
|
|
VAR
|
|
t0, t1, i, j: INTEGER;
|
|
|
|
BEGIN
|
|
API.init(param, code);
|
|
|
|
types := API._NEW(tcount * tcount + SYSTEM.SIZE(INTEGER));
|
|
ASSERT(types # 0);
|
|
FOR i := 0 TO tcount - 1 DO
|
|
FOR j := 0 TO tcount - 1 DO
|
|
t0 := i; t1 := j;
|
|
|
|
WHILE (t1 # 0) & (t1 # t0) DO
|
|
SYSTEM.GET(_types + t1 * WORD, t1)
|
|
END;
|
|
|
|
SYSTEM.PUT8(i * tcount + j + types, ORD(t0 = t1))
|
|
END
|
|
END;
|
|
|
|
FOR i := 0 TO MAX_SET DO
|
|
FOR j := 0 TO i DO
|
|
sets[i * (MAX_SET + 1) + j] := LSR(ASR(minint, i - j), MAX_SET - i)
|
|
END
|
|
END;
|
|
|
|
name := modname
|
|
END _init;
|
|
|
|
|
|
END RTL. |