{ "cells": [ { "cell_type": "markdown", "id": "b8caa27f", "metadata": {}, "source": [ "# Import Tiger and LLVMTranslate" ] }, { "cell_type": "code", "execution_count": 1, "id": "9cb3bce5", "metadata": {}, "outputs": [], "source": [ "import tc" ] }, { "cell_type": "code", "execution_count": 2, "id": "be38c5fe", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tc.has(\"llvmtranslate\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "9fe09a20", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import tiger_llvmtranslate\n", "tiger_llvmtranslate == tc.llvmtranslate" ] }, { "cell_type": "markdown", "id": "3b3a7285", "metadata": {}, "source": [ "# LLVMTranslate Library" ] }, { "cell_type": "code", "execution_count": 4, "id": "2be2f25d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['translate']" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(filter(lambda e: not e.startswith(\"_\") and not e.startswith(\"tiger\"), dir(tc.llvmtranslate)))" ] }, { "cell_type": "markdown", "id": "39d05040", "metadata": {}, "source": [ "# Tiger Magics and LLVMTranslate" ] }, { "cell_type": "code", "execution_count": 5, "id": "b210db83", "metadata": {}, "outputs": [], "source": [ "%%tiger executor llvm\n", "let\n", " var b := 5\n", "in\n", " print_int(b)\n", "end" ] }, { "cell_type": "code", "execution_count": 6, "id": "cd732dc4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "; ModuleID = 'LRDE Tiger Compiler'\n", "source_filename = \"LRDE Tiger Compiler\"\n" ] } ], "source": [ "llvm = executor.llvm\n", "print(*llvm.splitlines()[0:2], sep=\"\\n\")" ] }, { "cell_type": "code", "execution_count": 7, "id": "defafcde", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] } ], "source": [ "print(executor.result)" ] }, { "cell_type": "markdown", "id": "35825bf6", "metadata": {}, "source": [ "# LLVMTranslate example\n", "In Tiger Magics it run translate and then run clang (see tcsh/python/ti.py)" ] }, { "cell_type": "code", "execution_count": 8, "id": "0ee4cd72", "metadata": {}, "outputs": [], "source": [ "import tempfile\n", "\n", "with tempfile.NamedTemporaryFile() as f:\n", " f.write(b\"let var b := 5 in print_int(b) end\")\n", " f.seek(0)\n", " ast = tc.ti.TiExecutor(f.name).desugar()" ] }, { "cell_type": "code", "execution_count": 9, "id": "29d68680", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "; ModuleID = 'LRDE Tiger Compiler'\n", "source_filename = \"LRDE Tiger Compiler\"\n" ] } ], "source": [ "llvm = tc.llvmtranslate.translate(ast)\n", "print(*llvm.splitlines()[0:2], sep=\"\\n\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.1" } }, "nbformat": 4, "nbformat_minor": 5 }