summaryrefslogtreecommitdiff
path: root/lib/CodeGen/CGStmt.cpp
blob: 5af6b012155933438f932e96a9cfa5454866e822 (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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
//===--- CGStmt.cpp - Emit LLVM Code from Statements ----------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This contains code to emit Stmt nodes as LLVM code.
//
//===----------------------------------------------------------------------===//

#include "CodeGenFunction.h"
#include "CodeGenModule.h"
#include "CGIORuntime.h"
#include "flang/AST/StmtVisitor.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/InlineAsm.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/Support/CallSite.h"

namespace flang {
namespace CodeGen {

class StmtEmmitter : public ConstStmtVisitor<StmtEmmitter> {
  CodeGenFunction &CGF;
public:

  StmtEmmitter(CodeGenFunction &cgf) : CGF(cgf) {}

  void VisitCompoundStmt(const CompoundStmt *S) {
    for(auto I : S->getBody())
      CGF.EmitStmt(I);
  }
  void VisitBlockStmt(const BlockStmt *S) {
    for(auto I : S->getStatements())
      CGF.EmitStmt(I);
  }
  void VisitGotoStmt(const GotoStmt *S) {
    CGF.EmitGotoStmt(S);
  }
  void VisitAssignStmt(const AssignStmt *S) {
    CGF.EmitAssignStmt(S);
  }
  void VisitAssignedGotoStmt(const AssignedGotoStmt *S) {
    CGF.EmitAssignedGotoStmt(S);
  }
  void VisitComputedGotoStmt(const ComputedGotoStmt *S) {
    CGF.EmitComputedGotoStmt(S);
  }
  void VisitIfStmt(const IfStmt *S) {
    CGF.EmitIfStmt(S);
  }
  void VisitDoStmt(const DoStmt *S) {
    CGF.EmitDoStmt(S);
  }
  void VisitDoWhileStmt(const DoWhileStmt *S) {
    CGF.EmitDoWhileStmt(S);
  }
  void VisitCycleStmt(const CycleStmt *S) {
    CGF.EmitCycleStmt(S);
  }
  void VisitExitStmt(const ExitStmt *S) {
    CGF.EmitExitStmt(S);
  }
  void VisitSelectCaseStmt(const SelectCaseStmt *S) {
    CGF.EmitSelectCaseStmt(S);
  }
  void VisitWhereStmt(const WhereStmt *S) {
    CGF.EmitWhereStmt(S);
  }
  void VisitStopStmt(const StopStmt *S) {
    CGF.EmitStopStmt(S);
  }
  void VisitReturnStmt(const ReturnStmt *S) {
    CGF.EmitReturnStmt(S);
  }
  void VisitCallStmt(const CallStmt *S) {
    CGF.EmitCallStmt(S);
  }
  void VisitAssignmentStmt(const AssignmentStmt *S) {
    CGF.EmitAssignmentStmt(S);
  }
  void VisitWriteStmt(const WriteStmt *S) {
    CGF.getModule().getIORuntime().EmitWriteStmt(CGF, S);
  }
  void VisitPrintStmt(const PrintStmt *S) {
    CGF.getModule().getIORuntime().EmitPrintStmt(CGF, S);
  }
};

void CodeGenFunction::EmitStmt(const Stmt *S) {
  StmtEmmitter SV(*this);
  if(S->getStmtLabel())
    EmitStmtLabel(S);
  SV.Visit(S);
}

void CodeGenFunction::EmitBlock(llvm::BasicBlock *BB) {
  auto CurBB = Builder.GetInsertBlock();
  EmitBranch(BB);
  // Place the block after the current block, if possible, or else at
  // the end of the function.
  if (CurBB && CurBB->getParent())
    CurFn->getBasicBlockList().insertAfter(CurBB, BB);
  else
    CurFn->getBasicBlockList().push_back(BB);
  Builder.SetInsertPoint(BB);
}

void CodeGenFunction::EmitBranch(llvm::BasicBlock *Target) {
  // Emit a branch from the current block to the target one if this
  // was a real block.  If this was just a fall-through block after a
  // terminator, don't emit it.
  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();

  if (!CurBB || CurBB->getTerminator()) {
    // If there is no insert point or the previous block is already
    // terminated, don't touch it.
  } else {
    // Otherwise, create a fall-through branch.
    Builder.CreateBr(Target);
  }

  Builder.ClearInsertionPoint();
}

void CodeGenFunction::EmitBranchOnLogicalExpr(const Expr *Condition,
                                              llvm::BasicBlock *ThenBB,
                                              llvm::BasicBlock *ElseBB) {
  auto CV = EmitLogicalConditionExpr(Condition);
  Builder.CreateCondBr(CV, ThenBB, ElseBB);
}

void CodeGenFunction::EmitStmtLabel(const Stmt *S) {
  if(!S->isStmtLabelUsedAsGotoTarget())
    return;
  if(S->isStmtLabelUsedAsAssignTarget())
    AssignedGotoTargets.push_back(S);

  EmitBlock(GetGotoTarget(S));
}

llvm::BasicBlock *CodeGenFunction::GetGotoTarget(const Stmt *S) {
  auto Result = GotoTargets.find(S);
  if(Result != GotoTargets.end())
    return Result->second;
  auto Block = createBasicBlock("");
  GotoTargets.insert(std::make_pair(S, Block));
  return Block;
}

void CodeGenFunction::EmitGotoStmt(const GotoStmt *S) {
  auto Dest = GetGotoTarget(S->getDestination().Statement);

  Builder.CreateBr(Dest);
  EmitBlock(createBasicBlock("goto-continue"));
}

void CodeGenFunction::EmitAssignStmt(const AssignStmt *S) {
  auto Val = EmitScalarExpr(S->getAddress().Statement->getStmtLabel());
  // FIXME: verify that destination type can actually hold the value of a statement label
  EmitAssignment(EmitLValue(S->getDestination()),
                   EmitScalarToScalarConversion(
                     Val, S->getDestination()->getType()));
}

void CodeGenFunction::EmitAssignedGotoStmt(const AssignedGotoStmt *S) {
  if(!AssignedGotoVarPtr)
    AssignedGotoVarPtr = CreateTempAlloca(CGM.Int64Ty, "assigned-goto-val");
  auto Val = Builder.CreateZExtOrTrunc(EmitScalarExpr(S->getDestination()),
                                       CGM.Int64Ty);
  Builder.CreateStore(Val, AssignedGotoVarPtr);
  if(!AssignedGotoDispatchBlock)
    AssignedGotoDispatchBlock = createBasicBlock("assigned-goto-dispatch");
  Builder.CreateBr(AssignedGotoDispatchBlock);
  EmitBlock(createBasicBlock("assigned-goto-after"));
}

void CodeGenFunction::EmitAssignedGotoDispatcher() {
  assert(AssignedGotoDispatchBlock);
  EmitBlock(AssignedGotoDispatchBlock);
  auto DefaultCase = createBasicBlock("assigned-goto-dispatch-failed");
  auto VarVal = Builder.CreateLoad(AssignedGotoVarPtr);
  auto Switch = Builder.CreateSwitch(VarVal, DefaultCase,
                                     AssignedGotoTargets.size());
  for(size_t I = 0; I < AssignedGotoTargets.size(); ++I) {
    auto Target = AssignedGotoTargets[I];
    auto Dest = GotoTargets[Target];
    auto Val = llvm::ConstantInt::get(VarVal->getType(),
                                      cast<IntegerConstantExpr>(Target->getStmtLabel())->getValue());
    Switch->addCase(cast<llvm::ConstantInt>(Val), Dest);
  }
  EmitBlock(DefaultCase);
  // FiXME raise error
  Builder.CreateUnreachable();
}

void CodeGenFunction::EmitComputedGotoStmt(const ComputedGotoStmt *S) {
  auto Operand = EmitScalarExpr(S->getExpression());
  auto Type = Operand->getType();
  auto DefaultCase = createBasicBlock("computed-goto-continue");
  auto Targets = S->getTargets();
  auto Switch = Builder.CreateSwitch(Operand, DefaultCase,
                                     Targets.size());
  for(size_t I = 0; I < Targets.size(); ++I) {
    auto Dest = GetGotoTarget(Targets[I].Statement);
    auto Val = llvm::ConstantInt::get(Type, int(I) + 1);
    Switch->addCase(cast<llvm::ConstantInt>(Val), Dest);
  }
  EmitBlock(DefaultCase);
}

void CodeGenFunction::EmitIfStmt(const IfStmt *S) {
  auto ElseStmt = S->getElseStmt();
  auto ThenArm = createBasicBlock("if-then");
  auto MergeBlock = createBasicBlock("end-if");
  auto ElseArm = ElseStmt? createBasicBlock("if-else") :
                           MergeBlock;

  EmitBranchOnLogicalExpr(S->getCondition(), ThenArm, ElseArm);
  EmitBlock(ThenArm);
  EmitStmt(S->getThenStmt());
  EmitBranch(MergeBlock);
  if(ElseStmt) {
    EmitBlock(ElseArm);
    EmitStmt(S->getElseStmt());
  }
  EmitBlock(MergeBlock);
}

class LoopScope {
public:
  CodeGenFunction *CGF;
  const LoopScope *Previous;
  const Stmt *Loop;
  llvm::BasicBlock *ContinueTarget;
  llvm::BasicBlock *BreakTarget;

  LoopScope(CodeGenFunction *cgf,
            const Stmt *S,
            llvm::BasicBlock *ContinueBB,
            llvm::BasicBlock *BreakBB)
    : CGF(cgf), Previous(CGF->CurLoopScope), Loop(S),
      ContinueTarget(ContinueBB), BreakTarget(BreakBB) {
      CGF->CurLoopScope = this;
    }
  ~LoopScope() {
    CGF->CurLoopScope = Previous;
  }
  const LoopScope *getScope(const Stmt *S) const {
    if(Loop == S) return this;
    return Previous->getScope(S);
  }
};

void CodeGenFunction::EmitDoStmt(const DoStmt *S) {
  // Init
  auto VarPtr = GetVarPtr(cast<VarExpr>(S->getDoVar())->getVarDecl());
  auto InitValue = EmitScalarExpr(S->getInitialParameter());
  Builder.CreateStore(InitValue, VarPtr);  
  auto EndValue = EmitScalarExpr(S->getTerminalParameter());
  llvm::Value *IncValue;
  bool UseIterationCount = true;
  if(S->getIncrementationParameter())
    IncValue = EmitScalarExpr(S->getIncrementationParameter());
  else {
    IncValue = GetConstantOne(S->getDoVar()->getType());
    if(S->getDoVar()->getType()->isIntegerType())
      UseIterationCount = false;
  }

  auto Loop = createBasicBlock("do");
  auto LoopBody = createBasicBlock("loop");
  auto LoopIncrement = createBasicBlock("loop-inc");
  auto EndLoop = createBasicBlock("end-do");

  // IterationCount = MAX( INT( (m2 - m1 + m3)/m3), 0)
  llvm::Value *IterationCountVar;
  auto Zero = llvm::ConstantInt::get(CGM.SizeTy, 0);
  if(UseIterationCount) {
    IterationCountVar = CreateTempAlloca(CGM.SizeTy,
                                         "iteration-count");
    auto Val = EmitScalarBinaryExpr(BinaryExpr::Minus,
                                    EndValue, InitValue);
    Val = EmitScalarBinaryExpr(BinaryExpr::Plus,
                               Val, IncValue);
    Val = EmitScalarBinaryExpr(BinaryExpr::Divide,
                               Val, IncValue);
    if(Val->getType()->isFloatingPointTy())
      Val = Builder.CreateFPToSI(Val, CGM.SizeTy);
    else
      Val = Builder.CreateSExtOrTrunc(Val, CGM.SizeTy);
    Val = Builder.CreateSelect(Builder.CreateICmpSGE(Val, Zero),
                               Val, Zero, "max");
    Builder.CreateStore(Val, IterationCountVar);
  } else {
    // DO i = -1, -5 => IterationCount is 0 => don't run
    auto Cond = EmitScalarRelationalExpr(
                  BinaryExpr::GreaterThanEqual,
                  EndValue, InitValue);
    Builder.CreateCondBr(Cond, Loop, EndLoop);
  }

  LoopScope Scope(this, S, LoopIncrement, EndLoop);

  EmitBlock(Loop);
  // Check condition
  llvm::Value *Cond;
  if(UseIterationCount)
    Cond = Builder.CreateICmpNE(Builder.CreateLoad(IterationCountVar),
                                Zero);
  else
    Cond = EmitScalarRelationalExpr(
             BinaryExpr::LessThanEqual,
             Builder.CreateLoad(VarPtr), EndValue);
  Builder.CreateCondBr(Cond, LoopBody, EndLoop);

  EmitBlock(LoopBody);
  EmitStmt(S->getBody());

  EmitBlock(LoopIncrement);
  // increment the do variable
  llvm::Value *CurVal = Builder.CreateLoad(VarPtr);
  CurVal = EmitScalarBinaryExpr(BinaryExpr::Plus,
                                CurVal, IncValue);
  Builder.CreateStore(CurVal, VarPtr);
  // decrement loop counter if its used.
  if(UseIterationCount) {
    Builder.CreateStore(Builder.CreateSub(
                        Builder.CreateLoad(IterationCountVar),
                        llvm::ConstantInt::get(CGM.SizeTy, 1)), IterationCountVar);
  }

  EmitBranch(Loop);
  EmitBlock(EndLoop);
}

void CodeGenFunction::EmitDoWhileStmt(const DoWhileStmt *S) {
  auto Loop = createBasicBlock("do-while");
  auto LoopBody = createBasicBlock("loop");
  auto EndLoop = createBasicBlock("end-do-while");

  LoopScope Scope(this, S, Loop, EndLoop);

  EmitBlock(Loop);
  EmitBranchOnLogicalExpr(S->getCondition(), LoopBody, EndLoop);
  EmitBlock(LoopBody);
  EmitStmt(S->getBody());
  EmitBranch(Loop);
  EmitBlock(EndLoop);
}

void CodeGenFunction::EmitCycleStmt(const CycleStmt *S) {
  EmitBranch(CurLoopScope->getScope(S->getLoop())->ContinueTarget);
  EmitBlock(createBasicBlock("after-cycle"));
}

void CodeGenFunction::EmitExitStmt(const ExitStmt *S) {
  EmitBranch(CurLoopScope->getScope(S->getLoop())->BreakTarget);
  EmitBlock(createBasicBlock("after-exit"));
}

struct IntegerCaseStmtEmitter {
  static llvm::Value *EmitRelationalExpr(CodeGenFunction &CGF, BinaryExpr::Operator Op,
                                        llvm::Value *LHS, llvm::Value *RHS) {
    return CGF.EmitScalarRelationalExpr(Op, LHS, RHS);
  }
  static llvm::Value *EmitExpr(CodeGenFunction &CGF, const Expr *E) {
    return CGF.EmitScalarExpr(E);
  }
};

struct LogicalCaseStmtEmitter : IntegerCaseStmtEmitter {
  static llvm::Value *EmitRelationalExpr(CodeGenFunction &CGF, BinaryExpr::Operator Op,
                                        llvm::Value *LHS, llvm::Value *RHS) {
    // NB: there are no ranges for logical values.
    return CGF.EmitScalarRelationalExpr(BinaryExpr::Eqv, LHS, RHS);
  }
};

struct CharCaseStmtEmitter {
  static llvm::Value *EmitRelationalExpr(CodeGenFunction &CGF, BinaryExpr::Operator Op,
                                         CharacterValueTy LHS, CharacterValueTy RHS) {
    return CGF.EmitCharacterRelationalExpr(Op, LHS, RHS);
  }
  static CharacterValueTy EmitExpr(CodeGenFunction &CGF, const Expr *E) {
    return CGF.EmitCharacterExpr(E);
  }
};

template<typename F, typename T>
static
void EmitCaseStmt(CodeGenFunction &CGF,
                  CGBuilderTy &Builder,
                  T Operand, const CaseStmt *S,
                  llvm::BasicBlock *MatchBlock,
                  llvm::BasicBlock *NextCaseBlock) {
  auto Values = S->getValues();
  for(size_t I = 0, End = Values.size(); I < End; ++I) {
    auto E = Values[I];
    bool IsLast = (I+1) >= End;
    auto FalseBlock = IsLast? NextCaseBlock : CGF.createBasicBlock("case-test-next-value");

    llvm::Value *Condition;
    if(auto Range = dyn_cast<RangeExpr>(E)) {
      if(Range->hasFirstExpr())
        Condition = F::EmitRelationalExpr(CGF, BinaryExpr::LessThanEqual,
                                          F::EmitExpr(CGF, Range->getFirstExpr()), Operand);
      if(Range->hasSecondExpr()) {
        auto C = F::EmitRelationalExpr(CGF, BinaryExpr::LessThanEqual,
                                       Operand, F::EmitExpr(CGF, Range->getSecondExpr()));
        if(Range->hasFirstExpr())
          Condition = Builder.CreateAnd(Condition, C);
        else Condition = C;
      }
    } else
      Condition = F::EmitRelationalExpr(CGF, BinaryExpr::Equal,
                                        Operand, F::EmitExpr(CGF, E));

    Builder.CreateCondBr(Condition, MatchBlock, FalseBlock);
    if(!IsLast) CGF.EmitBlock(FalseBlock);
  }
}

template<typename F, typename T>
static void EmitCases(CodeGenFunction &CGF,
                      CGBuilderTy &Builder,
                      T Operand, const SelectCaseStmt *S,
                      llvm::BasicBlock *DefaultBlock,
                      llvm::BasicBlock *ContinueBlock) {
  for(auto Case = S->getFirstCase(); Case; Case = Case->getNextCase()) {
    auto MatchBlock = CGF.createBasicBlock("case-match");
    auto NextCaseBlock = Case->isLastCase()? DefaultBlock : CGF.createBasicBlock("case");
    EmitCaseStmt<F>(CGF, Builder, Operand,
                    Case, MatchBlock, NextCaseBlock);
    CGF.EmitBlock(MatchBlock);
    CGF.EmitStmt(Case->getBody());
    CGF.EmitBranch(ContinueBlock);
    if(!Case->isLastCase())
      CGF.EmitBlock(NextCaseBlock);
  }
}

void CodeGenFunction::EmitSelectCaseStmt(const SelectCaseStmt *S) {
  // FIXME: many integer selects can be emitted into a switch.

  auto E = S->getOperand();

  auto ContinueBlock = createBasicBlock("after-select-case");
  auto DefaultBlock  = S->hasDefaultCase()? createBasicBlock("case-default") :
                                            ContinueBlock;

  if(E->getType()->isIntegerType()) {
    auto Val = EmitScalarExpr(E);
    EmitCases<IntegerCaseStmtEmitter>(*this, Builder, Val, S, DefaultBlock, ContinueBlock);
  } else if(E->getType()->isLogicalType()) {
    auto Val = EmitScalarExpr(E);
    EmitCases<LogicalCaseStmtEmitter>(*this, Builder, Val, S, DefaultBlock, ContinueBlock);
  } else {
    auto Val = EmitCharacterExpr(E);
    EmitCases<CharCaseStmtEmitter>(*this, Builder, Val, S, DefaultBlock, ContinueBlock);
  }

  if(S->hasDefaultCase()) {
    EmitBlock(DefaultBlock);
    EmitStmt(S->getDefaultCase()->getBody());
    EmitBranch(ContinueBlock);
  }
  EmitBlock(ContinueBlock);
}

void CodeGenFunction::EmitStopStmt(const StopStmt *S) {
  EmitRuntimeCall(CGM.GetRuntimeFunction("stop", ArrayRef<llvm::Type*>()));
}

void CodeGenFunction::EmitReturnStmt(const ReturnStmt *S) {
  Builder.CreateBr(ReturnBlock);
}

void CodeGenFunction::EmitCallStmt(const CallStmt *S) {
  CallArgList ArgList;
  EmitCall(S->getFunction(), ArgList, S->getArguments(), true);
}

void CodeGenFunction::EmitAssignmentStmt(const AssignmentStmt *S) {
  auto RHS = S->getRHS();
  auto RHSType = RHS->getType();

  if(S->getLHS()->getType()->isArrayType()) {
    EmitArrayAssignment(S->getLHS(), S->getRHS());
    return;
  }
  auto Destination = EmitLValue(S->getLHS());

  if(RHSType->isIntegerType() || RHSType->isRealType()) {
    auto Value = EmitScalarExpr(RHS);
    Builder.CreateStore(Value, Destination.getPointer());
  } else if(RHSType->isLogicalType()) {
    auto Value = EmitLogicalValueExpr(RHS);
    Builder.CreateStore(Value, Destination.getPointer());
  } else if(RHSType->isComplexType()) {
    auto Value = EmitComplexExpr(RHS);
    EmitComplexStore(Value, Destination.getPointer());
  } else if(RHSType->isCharacterType())
    EmitCharacterAssignment(S->getLHS(), S->getRHS());
  else if(RHSType->isRecordType())
    EmitAggregateAssignment(S->getLHS(), S->getRHS());
}

void CodeGenFunction::EmitAssignment(LValueTy LHS, RValueTy RHS) {
  if(RHS.isScalar())
    Builder.CreateStore(RHS.asScalar(), LHS.getPointer());
  else if(RHS.isComplex())
    EmitComplexStore(RHS.asComplex(), LHS.getPointer());
  else if(RHS.isCharacter())
    EmitCharacterAssignment(GetCharacterValueFromPtr(LHS.getPointer(), LHS.getType()),
                            RHS.asCharacter());
}

}
} // end namespace flang