summaryrefslogtreecommitdiff
path: root/tiger-compiler/tcsh/python/ti.py
blob: a7df47952cab1f43c948dc98d24e4242c8d20af1 (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#! /usr/bin/env python3

# Tiger interpreter.

# Execute a Tiger program, using the Tiger Compiler through
# TCSH/Python and a supporting back-end environment (HAVM, Nolimips,
# or an IA-32 environment).
#
# This script needs a complete, dynamically-compiled Tiger compiler
# (with all its modules) to work.  The Tree back-end needs a `havm'
# binary to be available in the PATH; the MIPS back-end, a `nolimips'
# binary; and the IA-32 back-end, an IA-32 execution environment.

from __future__ import annotations
from collections.abc import Callable
from typing import List, Optional, Tuple, TypeVar, Union

import optparse
import tempfile
import subprocess

import os
import sys
import re

import tc


# Subclass to redefine error method (for exit code to be 1).
class TiOptionParser(optparse.OptionParser):
    def error(self, msg: str) -> None:
        self.print_usage(sys.stderr)
        self.exit(1, "%s: error: %s\n" % (self.get_prog_name(), msg))


T = TypeVar("T")


def wrap_step(
    modules: List[str] = [],
    require: Optional[str] = None,
    backend: Optional[tc.BackendType] = None,
) -> Callable[[Callable[[TiExecutor], T]], Callable[[TiExecutor], Optional[T]]]:
    """
    Decorator for steps of TiExecutor, execute the require step if needed
    """

    def check_wrap_step(
        f: Callable[[TiExecutor], T]
    ) -> Callable[[TiExecutor], Optional[T]]:
        def new_f(self: TiExecutor) -> Optional[T]:
            if self.debug:
                print(f"CALL  {f.__name__} [{self.backend}]")
            if backend and self.backend != backend:
                self.backend = backend
                if hasattr(self, "__step"):
                    delattr(self, "__step")
                    self._rm_asm()
                    self._rm_llvm()
                    self.data = TiExecutor.TiExecution()
            if require and not hasattr(self, require):
                raise RuntimeError(require + " step must exist")
            if self.data.error:
                return None
            hasstep = hasattr(self, "__step")
            if require and (not hasstep or hasstep and not self.__step == require):
                getattr(self, require)()
                if self.data.error:
                    return None
            for mod in modules:
                if not tc.has(mod):
                    msg = f"Module {mod} is not available to execute {f.__name__} step\nCheck your install of tc"
                    self.data.error = (-1, msg)
                    print(msg, file=sys.stderr)
                    if self.exit_on_error:
                        sys.exit(1)
                    return None
            if self.debug:
                print(f"ENTER {f.__name__} [{self.backend}]")
            res = f(self)
            if self.debug:
                print(f"EXIT  {f.__name__} [{self.backend}]")
            self.__step = f.__name__
            return res

        new_f.__doc__ = f"Ast {f.__name__} step"
        if require:
            new_f.__doc__ += f"\nRequire {require} step, execute it otherwise"
        if modules and len(modules):
            new_f.__doc__ += "\nRequire {} tc modules".format(", ".join(modules))
        new_f.__name__ = f.__name__
        return new_f

    return check_wrap_step


class TiExecutor:
    class TiExecution:
        ast: Optional[tc.ast.ChunkList] = None
        error: Optional[Tuple[int, str]] = None
        fragments: Optional[tc.tree.Fragments] = None
        lir_fragments: Optional[tc.assem.Fragments] = None
        target: Optional[Union[tc.target.Ia32Target, tc.target.MipsTarget]] = None
        tempmap: Optional[tc.temp.TempMap] = None
        result: Optional[str] = None
        llvm: Optional[str] = None

        def __str__(self) -> str:
            res = ""

            def truncate(msg: str) -> str:
                return msg if not msg or len(msg) < 100 else msg[:100] + "..."

            for e in dir(self):
                if not e.startswith("_"):
                    res += e + ": " + truncate(repr(getattr(self, e))) + "\n"
            return res

    def __init__(
        self,
        filename: str,
        backend: tc.BackendType = tc.BackendType.mips,
        exit_on_error: bool = True,
        get_result: bool = False,
        rename: bool = True,
        desugar: bool = True,
        object_enabled: bool = True,
        debug: bool = False,
    ):
        self.filename = filename
        self.backend = tc.BackendType(backend)
        self.exit_on_error = exit_on_error
        self.get = get_result
        self.rename_enabled = rename
        self.desugar_enabled = desugar
        self.object_enabled = object_enabled
        self.debug = debug
        self.data = TiExecutor.TiExecution()

    def error_message(self) -> str:
        if not self.data.error:
            return ""
        status, message = self.data.error
        statusMessage = tc.misc.error.error_type_message()[status]
        if re.match(r"(.*/)?tmp\w{8}\.tig$", self.filename):
            if message.startswith(self.filename + ":"):
                message = message.replace(self.filename + ":", "")
            message = " " + message
        else:
            message = "\n" + message
        return str(status) + " " + statusMessage + ":" + message

    def throw_error(self, e: Exception) -> None:
        self.__step = "error"
        self.data.error = e.args
        status, message = e.args
        if self.exit_on_error:
            print(message, file=sys.stderr)
            sys.exit(status)
        else:
            print(self.error_message(), end="", file=sys.stderr)

    def _rm_attribute_file(self, arg: str) -> None:
        if hasattr(self, arg):
            os.unlink(getattr(self, arg))
            delattr(self, arg)

    def _rm_attribute_dir(self, arg: str) -> None:
        if hasattr(self, arg):
            os.rmdir(getattr(self, arg))
            delattr(self, arg)

    def _rm_attribute_temp(self, arg: str) -> None:
        if hasattr(self, arg):
            os.unlink(getattr(self, arg).name)
            delattr(self, arg)

    def _run_cmd(self, *cmd: str) -> Optional[str]:
        if self.get:
            proc = subprocess.run(cmd, capture_output=True)
            self.data.result = proc.stdout.decode("utf-8")
        else:
            os.system(" ".join(cmd))
            self.data.result = None
        return self.data.result

    @wrap_step(["misc", "parse"])
    def parse(self) -> Optional[tc.ast.ChunkList]:
        lib = tc.misc.file_library()
        try:
            self.data.ast = tc.parse.parse("builtin", self.filename, lib)
        except Exception as e:
            return self.throw_error(e)
        return self.data.ast

    @wrap_step(["bind"], "parse")
    def bind(self) -> Optional[tc.ast.ChunkList]:
        try:
            if self.object_enabled and tc.has("object"):
                tc.object.bind(self.data.ast).exit_on_error()
            else:
                tc.bind.bind(self.data.ast).exit_on_error()
        except Exception as e:
            return self.throw_error(e)
        return self.data.ast

    @wrap_step([], "bind")
    def rename(self) -> Optional[tc.ast.ChunkList]:
        if (
            self.rename_enabled
            and tc.has("bind")
            and not self.object_enabled
            and not tc.has("object")
        ):
            tc.bind.rename(self.data.ast)
        return self.data.ast

    @wrap_step(["type"], "rename")
    def type(self) -> Optional[tc.ast.ChunkList]:
        try:
            if self.object_enabled and tc.has("object"):
                tc.object.types_check(self.data.ast).exit_on_error()
            else:
                tc.type.types_check(self.data.ast).exit_on_error()
        except Exception as e:
            return self.throw_error(e)
        return self.data.ast

    @wrap_step([], "type")
    def object_desugar(self) -> Optional[tc.ast.ChunkList]:
        if self.object_enabled and tc.has("object"):
            class_names = tc.object.rename(self.data.ast)
            self.data.ast = tc.object.desugar(self.data.ast, class_names)
        return self.data.ast

    @wrap_step([], "object_desugar")
    def desugar(self) -> Optional[tc.ast.ChunkList]:
        if self.desugar_enabled and tc.has("desugar"):
            self.data.ast = tc.desugar.desugar(self.data.ast, True, True)
        return self.data.ast

    @wrap_step(["llvmtranslate"], "desugar")
    def llvm_file(self) -> str:
        self.data.llvm = tc.llvmtranslate.translate(self.data.ast)

        self._rm_llvm()
        self.llvm_temp_dir = tempfile.mkdtemp()
        # Dump assembly code output into a temporary file.
        self.llvm_output = os.path.join(self.llvm_temp_dir, "llvm.ll")
        with open(self.llvm_output, "w") as f:
            f.write(str(self.data.llvm))
        return self.llvm_output

    def _rm_llvm(self) -> None:
        self._rm_attribute_file("llvm_output")
        self._rm_attribute_file("llvm_binary")
        self._rm_attribute_dir("llvm_temp_dir")

    @wrap_step([], "llvm_file")
    def llvm_bin(self) -> str:
        self._rm_attribute_file("llvm_binary")
        self.llvm_binary = os.path.join(self.llvm_temp_dir, "bin")
        os.system(f"clang -m32 {self.llvm_output} -o {self.llvm_binary}")
        return self.llvm_binary

    @wrap_step([], "llvm_bin", tc.BackendType.llvm)
    def llvm(self) -> Optional[str]:
        self._run_cmd(self.llvm_binary)
        self._rm_llvm()
        return self.data.result

    def frontend_run(self) -> None:
        """Run parse, bind and type depending of TC step"""
        self.parse()

        self.bind()
        self.rename()
        self.type()
        self.object_desugar()
        self.desugar()
        return None

    def backend_exec(self) -> Optional[str]:
        """execute backends: llvm, hir, lir, mips and ia32"""
        self.frontend_run()
        if self.backend == tc.BackendType.llvm:
            return self.llvm()
        return None

    def backend_run(self) -> None:
        self.get = False
        self.backend_exec()

    def backend_get(self) -> Optional[str]:
        self.get = True
        return self.backend_exec()


def process_file(
    filename: str, backend: tc.BackendType = tc.BackendType.mips, **kwargs
) -> None:
    executor = TiExecutor(filename, backend=backend, **kwargs)
    executor.backend_run()


if __name__ == "__main__":
    # Parser creation.
    parser = TiOptionParser(
        """%prog [options] file.tig
    Execute a Tiger program, using a given back-end."""
    )
    parser.add_option(
        "-b",
        "--back-end",
        metavar="BACKEND",
        dest="backend",
        default=tc.BackendType.mips,
        help="use BACKEND as back-end.  Can be either "
        f"`{tc.BackendType.llvm.value}' (LLVM), "
        f"`{tc.BackendType.mips.value}' (MIPS assembly language) "
        "[default: %default]",
    )
    parser.add_option(
        "-d",
        "--debug",
        action="store_true",
        dest="debug",
        default=False,
        help="print debug call trace",
    )

    # Options parsing.
    (options, args) = parser.parse_args()

    # Invalid argument.
    me = os.path.basename(sys.argv[0])
    error = False
    if len(args) != 1:
        print(f"{me}: not enough arguments")
        error = True

    if options.backend not in [e.value for e in tc.BackendType]:
        print(f"{me}: select a valid backend")
        error = True

    if error:
        parser.print_help()
        sys.exit(1)

    # Get filename from arguments.
    filename = args[-1]
    process_file(filename, **vars(options))

# Local Variables:
# mode: python
# End: