aboutsummaryrefslogtreecommitdiffstats
path: root/src/ortho/llvm/ortho_jit.adb
blob: fdda667d9f7e404fc0896e484f24ce367876bfbf (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
--  LLVM back-end for ortho.
--  Copyright (C) 2014 Tristan Gingold
--
--  GHDL is free software; you can redistribute it and/or modify it under
--  the terms of the GNU General Public License as published by the Free
--  Software Foundation; either version 2, or (at your option) any later
--  version.
--
--  GHDL is distributed in the hope that it will be useful, but WITHOUT ANY
--  WARRANTY; without even the implied warranty of MERCHANTABILITY or
--  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
--  for more details.
--
--  You should have received a copy of the GNU General Public License
--  along with GCC; see the file COPYING.  If not, write to the Free
--  Software Foundation, 59 Temple Place - Suite 330, Boston, MA
--  02111-1307, USA.

--  with GNAT.OS_Lib; use GNAT.OS_Lib;
with Ada.Text_IO; use Ada.Text_IO;

with Ortho_LLVM; use Ortho_LLVM;
with Ortho_LLVM.Jit;

with LLVM.Core; use LLVM.Core;
with LLVM.Target; use LLVM.Target;
--  with LLVM.TargetMachine; use LLVM.TargetMachine;
with LLVM.ExecutionEngine; use LLVM.ExecutionEngine;
with LLVM.Analysis;
--  with Interfaces;
with Interfaces.C; use Interfaces.C;

package body Ortho_Jit is
   --  Snap_Filename : GNAT.OS_Lib.String_Access := null;

   Flag_Dump_Llvm : Boolean := False;

   --  Name of the module.
   Module_Name : String := "ortho" & Ascii.Nul;

   --  procedure DisableLazyCompilation (EE : ExecutionEngineRef;
   --                                    Disable : int);
   --  pragma Import (C, DisableLazyCompilation,
   --        "LLVMDisableLazyCompilation");

   --  Initialize the whole engine.
   procedure Init
   is
      Msg : aliased Cstring;
   begin
      InitializeNativeTarget;
      InitializeNativeAsmPrinter;

      LinkInJIT;

      Module := ModuleCreateWithName (Module_Name'Address);

      -- Now we going to create JIT
      if CreateExecutionEngineForModule
        (Ortho_LLVM.Jit.Engine'Access, Module, Msg'Access) /= 0
      then
         Put_Line (Standard_Error, "cannot create execution engine");
         raise Program_Error;
      end if;

      Target_Data := GetExecutionEngineTargetData (Ortho_LLVM.Jit.Engine);
      SetDataLayout (Module, CopyStringRepOfTargetData (Target_Data));

      Ortho_LLVM.Init;
   end Init;

   procedure Set_Address (Decl : O_Dnode; Addr : Address)
     renames Ortho_LLVM.Jit.Set_Address;

   function Get_Address (Decl : O_Dnode) return Address
     renames Ortho_LLVM.Jit.Get_Address;

   --  procedure InstallLazyFunctionCreator (EE : ExecutionEngineRef;
   --                                        Func : Address);
   --  pragma Import (C, InstallLazyFunctionCreator,
   --                 "LLVMInstallLazyFunctionCreator");

   --  Do link.
   procedure Link (Status : out Boolean)
   is
      use LLVM.Analysis;
      Msg : aliased Cstring;
   begin
      if Flag_Debug then
         Ortho_LLVM.Finish_Debug;
      end if;

      if Flag_Dump_Llvm then
         DumpModule (Module);
      end if;

      --  Verify module.
      if LLVM.Analysis.VerifyModule
        (Module, LLVM.Analysis.PrintMessageAction, Msg'Access) /= 0
      then
         DisposeMessage (Msg);
         Status := False;
         return;
      end if;

      --  FIXME: optim
   end Link;

   procedure Finish
   is
      --  F : ValueRef;
      --  Addr : Address;
      --  pragma Unreferenced (Addr);
   begin
      null;

      --  if No_Lazy then
      --     --  Be sure all functions code has been generated.
      --     F := GetFirstFunction (Module);
      --     while F /= Null_ValueRef loop
      --        if GetFirstBasicBlock (F) /= Null_BasicBlockRef then
      --           --  Only care about defined functions.
      --           Addr := GetPointerToFunction (EE, F);
      --        end if;
      --        F := GetNextFunction (F);
      --     end loop;
      --  end if;
   end Finish;

   function Decode_Option (Option : String) return Boolean
   is
      Opt : constant String (1 .. Option'Length) := Option;
   begin
      if Opt = "--llvm-dump" then
         Flag_Dump_Llvm := True;
         return True;
      end if;
      return False;
   end Decode_Option;

   procedure Disp_Help is
   begin
      null;
   end Disp_Help;

   function Get_Jit_Name return String is
   begin
      return "LLVM";
   end Get_Jit_Name;

end Ortho_Jit;