summaryrefslogtreecommitdiff
path: root/lib/CodeGen/CodeGenModule.h
blob: dfa1a31080650f16df6bd52dac6d2f61086d21db (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
//===--- CodeGenModule.h - Per-Module state for LLVM CodeGen ----*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This is the internal per-translation-unit state used for llvm translation.
//
//===----------------------------------------------------------------------===//

#ifndef FLANG_CODEGEN_CODEGENMODULE_H
#define FLANG_CODEGEN_CODEGENMODULE_H

#include "CodeGenTypes.h"
#include "flang/AST/Decl.h"
#include "flang/Basic/LangOptions.h"
#include "flang/Frontend/CodeGenOptions.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/IR/CallingConv.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/ValueHandle.h"
#include "llvm/Transforms/Utils/BlackList.h"

namespace llvm {
  class Module;
  class Constant;
  class ConstantInt;
  class ConstantArray;
  class Function;
  class GlobalValue;
  class DataLayout;
  class FunctionType;
  class LLVMContext;
}

namespace flang {
  class ASTContext;
  class Decl;
  class Expr;
  class Stmt;
  class NamedDecl;
  class ValueDecl;
  class VarDecl;
  class LangOptions;
  class DiagnosticsEngine;
  class TargetCodeGenInfo;

namespace CodeGen {

  class CGIORuntime;
  class CGSystemRuntime;

struct CodeGenTypeCache {
  /// void
  llvm::Type *VoidTy;

  /// i1, i8, i16, i32, and i64
  llvm::IntegerType *Int1Ty, *Int8Ty, *Int16Ty, *Int32Ty, *Int64Ty;
  /// float, double
  llvm::Type *FloatTy, *DoubleTy;

  /// int
  llvm::IntegerType *IntTy;

  /// intptr_t, size_t, and ptrdiff_t, which we assume are the same size.
  union {
    llvm::IntegerType *IntPtrTy;
    llvm::IntegerType *SizeTy;
    llvm::IntegerType *PtrDiffTy;
  };

  /// void* in address space 0
  union {
    llvm::PointerType *VoidPtrTy;
    llvm::PointerType *Int8PtrTy;
  };

  /// void** in address space 0
  union {
    llvm::PointerType *VoidPtrPtrTy;
    llvm::PointerType *Int8PtrPtrTy;
  };

  /// The width of a pointer into the generic address space.
  unsigned char PointerWidthInBits;

  /// The size and alignment of a pointer into the generic address
  /// space.
  union {
    unsigned char PointerAlignInBytes;
    unsigned char PointerSizeInBytes;
    unsigned char SizeSizeInBytes;     // sizeof(size_t)
  };

  llvm::CallingConv::ID RuntimeCC;
  llvm::CallingConv::ID getRuntimeCC() const {
    return RuntimeCC;
  }
};

/// CodeGenModule - This class organizes the cross-function state that is used
/// while generating LLVM code.
class CodeGenModule : public CodeGenTypeCache {
  CodeGenModule(const CodeGenModule &) LLVM_DELETED_FUNCTION;
  void operator=(const CodeGenModule &) LLVM_DELETED_FUNCTION;

  ASTContext &Context;
  const LangOptions &LangOpts;
  const CodeGenOptions &CodeGenOpts;
  llvm::Module &TheModule;
  DiagnosticsEngine &Diags;
  const llvm::DataLayout &TheDataLayout;
  //const TargetInfo &Target;
  llvm::LLVMContext &VMContext;

  CodeGenTypes Types;
  LibflangABI RuntimeABI;
  const TargetCodeGenInfo *TheTargetCodeGenInfo;
  CGIORuntime *IORuntime;
  CGSystemRuntime *SystemRuntime;

  /// RuntimeFunctions - contains all the runtime functions
  /// used in this module.
  llvm::StringMap<CGFunction> RuntimeFunctions;

  llvm::DenseMap<const FunctionDecl*, CGFunction> Functions;



public:
  CodeGenModule(ASTContext &C, const CodeGenOptions &CodeGenOpts,
                llvm::Module &M, const llvm::DataLayout &TD,
                DiagnosticsEngine &Diags);

  ~CodeGenModule();

  ASTContext &getContext() const { return Context; }

  llvm::Module &getModule() const { return TheModule; }

  llvm::LLVMContext &getLLVMContext() const { return VMContext; }

  const llvm::DataLayout &getDataLayout() const {
    return TheDataLayout;
  }

  CodeGenTypes &getTypes() { return Types; }

  bool hasIORuntime() const {
    return IORuntime != nullptr;
  }

  /// getIORuntime() - Return a reference to the configured
  /// IO runtime.
  CGIORuntime &getIORuntime() {
    return *IORuntime;
  }

  bool hasSystemRuntime() const {
    return SystemRuntime != nullptr;
  }

  /// getSystemRuntime() - Return a reference to the configured
  /// system runtime.
  CGSystemRuntime &getSystemRuntime() {
    return *SystemRuntime;
  }

  /// getTargetCodeGenInfo - Retun a reference to the configured
  /// target code gen information.
  const TargetCodeGenInfo &getTargetCodeGenInfo();

  /// Release - Finalize LLVM code generation.
  void Release();

  void EmitTopLevelDecl(const Decl *Declaration);

  void EmitMainProgramDecl(const MainProgramDecl *Program);

  void EmitFunctionDecl(const FunctionDecl *Function);

  llvm::GlobalVariable *EmitGlobalVariable(StringRef FuncName, const VarDecl *Var,
                                           llvm::Constant *Initializer = nullptr);

  llvm::GlobalVariable *EmitGlobalVariable(StringRef FuncName, StringRef VarName,
                                           llvm::Type *Type, llvm::Constant *Initializer);

  llvm::Value *EmitConstantArray(llvm::Constant *Array);

  llvm::Value *EmitCommonBlock(const CommonBlockDecl *CB,
                               llvm::Type *Type,
                               llvm::Constant *Initializer = nullptr);

  llvm::Value *GetCFunction(StringRef Name,
                            ArrayRef<llvm::Type*> ArgTypes,
                            llvm::Type *ReturnType = nullptr);

  llvm::Value *GetRuntimeFunction(StringRef Name,
                                  ArrayRef<llvm::Type*> ArgTypes,
                                  llvm::Type *ReturnType = nullptr);

  CGFunction GetRuntimeFunction(StringRef Name,
                                ArrayRef<CGType> ArgTypes,
                                CGType ReturnType = CGType(),
                                FortranABI *ABI = nullptr);

  CGFunction GetRuntimeFunction1(StringRef Name,
                                 CGType ArgType,
                                 CGType ReturnType = CGType(),
                                 FortranABI *ABI = nullptr) {
    return GetRuntimeFunction(Name, ArgType,
                              ReturnType, ABI);
  }

  CGFunction GetRuntimeFunction2(StringRef Name,
                                 CGType A1, CGType A2,
                                 CGType ReturnType = CGType(),
                                 FortranABI *ABI = nullptr) {
    CGType ArgTypes[] = { A1, A2 };
    return GetRuntimeFunction(Name, llvm::makeArrayRef(ArgTypes, 2),
                              ReturnType, ABI);
  }

  CGFunction GetRuntimeFunction3(StringRef Name,
                                 CGType A1, CGType A2, CGType A3,
                                 CGType ReturnType = CGType(),
                                 FortranABI *ABI = nullptr) {
    CGType ArgTypes[] = { A1, A2, A3 };
    return GetRuntimeFunction(Name, llvm::makeArrayRef(ArgTypes, 3),
                              ReturnType, ABI);
  }

  CGFunction GetRuntimeFunction4(StringRef Name,
                                 CGType A1, CGType A2, CGType A3,
                                 CGType A4, CGType ReturnType = CGType(),
                                 FortranABI *ABI = nullptr) {
    CGType ArgTypes[] = { A1, A2, A3, A4 };
    return GetRuntimeFunction(Name, llvm::makeArrayRef(ArgTypes, 4),
                              ReturnType, ABI);
  }

  CGFunction GetRuntimeFunction5(StringRef Name,
                                 CGType A1, CGType A2, CGType A3,
                                 CGType A4, CGType A5, CGType ReturnType = CGType(),
                                 FortranABI *ABI = nullptr) {
    CGType ArgTypes[] = { A1, A2, A3, A4, A5 };
    return GetRuntimeFunction(Name, llvm::makeArrayRef(ArgTypes, 5),
                              ReturnType, ABI);
  }

  CGFunction GetFunction(const FunctionDecl *Function);

};

}  // end namespace CodeGen
}  // end namespace flang

#endif