summaryrefslogtreecommitdiff
path: root/compiler/llvm/cgllvm.pas
blob: 90849816c79c613cce02517b0048fc74afee1f14 (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
{
    Copyright (c) 2010-2013 by Jonas Maebe

    This unit implements the code generator for LLVM

    This program 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 of the License, or
    (at your option) any later version.

    This program 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 this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

 ****************************************************************************
}
unit cgllvm;

{$i fpcdefs.inc}

interface

    uses
       globtype,parabase,
       cgbase,cgutils,cgobj,cghlcpu,
       llvmbase,llvminfo,aasmbase,aasmtai,aasmdata,aasmllvm;

    type
      tcgllvm=class(thlbasecgcpu)
     public
        procedure a_label(list : TAsmList;l : tasmlabel);override;
        procedure a_jmp_always(list: TAsmList; l: tasmlabel); override;
        procedure init_register_allocators;override;
        procedure done_register_allocators;override;
        function  getintregister(list:TAsmList;size:Tcgsize):Tregister;override;
        function  getfpuregister(list:TAsmList;size:Tcgsize):Tregister;override;
      end;

    procedure create_codegen;

implementation

  uses
    globals,verbose,systems,cutils,
    paramgr,fmodule,
    tgobj,rgllvm,cpubase,
    procinfo,cpupi;


{****************************************************************************
                              Assembler code
****************************************************************************}

    procedure tcgllvm.a_label(list: TAsmList; l: tasmlabel);
      begin
        { in llvm, every block must end with a terminator instruction, such as
          a branch -> if the previous instruction is not a terminator instruction,
          add an unconditional branch to the next block (= the one starting with
          this label) }
        if not assigned(list.last) or
           (tai(list.Last).typ<>ait_llvmins) or
           not(taillvm(list.Last).llvmopcode in llvmterminatoropcodes) then
          a_jmp_always(list,l);
        inherited;
      end;


    procedure tcgllvm.a_jmp_always(list: TAsmList; l: tasmlabel);
      begin
        list.concat(taillvm.op_lab(la_br,l));
      end;


    procedure tcgllvm.init_register_allocators;
      begin
        inherited init_register_allocators;
        rg[R_INTREGISTER]:=Trgllvm.create(R_INTREGISTER,R_SUBWHOLE,
          [0],first_int_imreg,[]);
        rg[R_FPUREGISTER]:=Trgllvm.create(R_FPUREGISTER,R_SUBWHOLE,
          [0],first_fpu_imreg,[]);
        rg[R_MMREGISTER]:=Trgllvm.create(R_MMREGISTER,R_SUBWHOLE,
          [0],first_mm_imreg,[]);
        { every temp gets its own "base register" to uniquely identify it }
        rg[R_TEMPREGISTER]:=trgllvm.Create(R_TEMPREGISTER,R_SUBWHOLE,
          [0],1,[]);
      end;


    procedure tcgllvm.done_register_allocators;
      begin
        rg[R_INTREGISTER].free;
        rg[R_FPUREGISTER].free;
        rg[R_MMREGISTER].free;
        rg[R_TEMPREGISTER].free;
        inherited done_register_allocators;
      end;


    function tcgllvm.getintregister(list:TAsmList;size:Tcgsize):Tregister;
      begin
        { all size determinations are based on tdef, subregisters are
          irrelevant }
        result:=rg[R_INTREGISTER].getregister(list,R_SUBWHOLE)
      end;


    function tcgllvm.getfpuregister(list:TAsmList;size:Tcgsize):Tregister;
      begin
        { all size determinations are based on tdef, subregisters are
          irrelevant }
        result:=rg[R_FPUREGISTER].getregister(list,R_SUBWHOLE);
      end;


    procedure create_codegen;
      begin
        cg:=tcgllvm.Create;
      end;

end.