aboutsummaryrefslogtreecommitdiff
path: root/snug-clean/src/Snug/Compile/ABI.icl
blob: 225f09c9a6dfa3403c22750316df4a6fa27be433 (plain) (blame)
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
implementation module Snug.Compile.ABI

import StdEnv

import Text

import MIPS.MIPS32
import Snug.Compile

constructorLabel :: !Namespace !ConstructorIdent -> Label
constructorLabel "" id = "_c" +++ id // for built-in constructors
constructorLabel ns id = concat4 "__" (escapeLabel ns) "_c" (escapeLabel id)

functionLabel :: !Namespace !EntryPoint !SymbolIdent -> Label
functionLabel ns entry_point id
	| size ns == 0
		= {#'_',e} +++ escapeLabel id
		= concat4 "__" (escapeLabel ns) {#'_',e} (escapeLabel id)
where
	e = case entry_point of
		NodeEntry -> 'n'

closureLabel :: !Namespace !SymbolIdent !Int -> Label
closureLabel ns id nargs = constructorLabel ns (concat3 id "@" (toString nargs))

escapeLabel :: !String -> String
escapeLabel s = {#c \\ c <- escape [c \\ c <-: s]}
where
	escape [] = []
	escape [c:cs] | isAlphanum c = [c:escape cs]
	// Symbols allowed in snug but not in symbol names:
	escape ['_':cs] = ['__':escape cs]
	escape ['`':cs] = ['_B':escape cs]
	escape [':':cs] = ['_C':escape cs]
	escape ['.':cs] = ['_D':escape cs]
	escape ['>':cs] = ['_G':escape cs]
	escape ['=':cs] = ['_I':escape cs]
	escape ['<':cs] = ['_L':escape cs]
	escape ['-':cs] = ['_M':escape cs]
	escape ['+':cs] = ['_P':escape cs]
	escape ['\'':cs] = ['_Q':escape cs]
	escape ['~':cs] = ['_T':escape cs]
	// Symbols not allowed in snug but used internally:
	escape ['@':cs] = ['_c':escape cs] // see closureLabel