-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcc
executable file
·51 lines (35 loc) · 1.3 KB
/
dcc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
TMP="$(mktemp)"
cat "core/core.asm" >> "${TMP}.asm"
cat "core/builtins.asm" >> "${TMP}.asm"
cat "core/sys.asm" >> "${TMP}.asm"
echo "; BEGIN PROGRAM ;" >> "${TMP}.asm"
echo "___header" >> "${TMP}.asm"
cat - <(cat "core/prelude.dk") | \
sed "s/#!.*//g" | \
m4 -P | \
tr -s '[:space:]' '\n' | \
while read -r word; do
if [[ "$word" =~ ^[[:digit:]]+$ ]] ; then
sed 's/^/___push /' <<< "$word"
elif [[ "$word" =~ ^\&\.[[:graph:]]+$ ]] ; then
printf '%s' "${word:2}" | xxd -p | sed 's/^/___push .d_/'
elif [[ "$word" =~ ^\&[[:graph:]]+$ ]] ; then
printf '%s' "${word:1}" | xxd -p | sed 's/^/___push d_/'
elif [[ "$word" =~ ^@\.[[:graph:]]+$ ]] ; then
printf '%s' "${word:2}" | xxd -p | sed 's/^/___label .d_/'
elif [[ "$word" =~ ^@[[:graph:]]+$ ]] ; then
printf '%s' "${word:1}" | xxd -p | sed 's/^/___label d_/'
elif [[ "$word" =~ ^\.[[:graph:]]+$ ]] ; then
printf '%s' "${word}" | xxd -p | sed 's/^/___call .d_/'
elif [[ "$word" =~ ^(here|go|count|word|byte)$ ]] ; then
printf '%s\n' "${word}" | sed 's/^/___/'
elif [[ "$word" =~ ^[[:graph:]]+$ ]] ; then
printf '%s' "${word}" | xxd -p | sed 's/^/___call d_/'
fi
done >> "${TMP}.asm"
nasm -O3 -felf64 "${TMP}.asm" -o "${TMP}.o"
ld -o "$TMP" "${TMP}.o" -s -n --gc-sections
mv "${TMP}" "$1"