XACC
xasm_listener.hpp
1 /*******************************************************************************
2  * Copyright (c) 2019 UT-Battelle, LLC.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * and Eclipse Distribution License v1.0 which accompanies this
6  * distribution. The Eclipse Public License is available at
7  * http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution
8  *License is available at https://eclipse.org/org/documents/edl-v10.php
9  *
10  * Contributors:
11  * Alexander J. McCaskey - initial API and implementation
12  *******************************************************************************/
13 #ifndef XACC_XASMLISTENER_H
14 #define XACC_XASMLISTENER_H
15 
16 #include "IR.hpp"
17 #include "IRProvider.hpp"
18 #include "xasmBaseListener.h"
19 #include "expression_parsing_util.hpp"
20 #include "InstructionIterator.hpp"
21 #include "xacc.hpp"
22 
23 using namespace xasm;
24 
25 namespace xacc {
26 
27 struct XasmGreaterThan {};
28 struct XasmLessThan {};
31 
32 class XASMListener : public xasmBaseListener {
33 protected:
34  std::shared_ptr<IRProvider> irProvider;
35  std::shared_ptr<CompositeInstruction> function;
36  std::shared_ptr<CompositeInstruction> for_function;
37  std::shared_ptr<CompositeInstruction> if_stmt;
38  std::string bufferName = "";
39  bool inForLoop = false;
40  bool inIfStmt = false;
41 
42  std::vector<std::string> functionBufferNames;
43  std::string currentInstructionName;
44  std::vector<std::size_t> currentBits;
45  std::vector<std::string> currentBufferNames;
46  std::vector<InstructionParameter> currentParameters;
47  std::map<int, std::string> currentBitIdxExpressions;
48 
49  std::map<std::string, int> new_var_to_vector_idx;
50 
51  std::string currentCompositeName;
52  HeterogeneousMap currentOptions;
53 
54  std::shared_ptr<ExpressionParsingUtil> parsingUtil;
55 
56  void for_stmt_update_inst_args(Instruction* inst);
57 
58  std::vector<std::size_t> for_stmt_update_bits(Instruction *inst,
59  const std::string varName,
60  const int value);
61  std::vector<InstructionParameter>
62  for_stmt_update_params(Instruction *inst, const std::string varName,
63  const int value);
64 
65  template <typename T>
66  void createForInstructions(xasmParser::ForstmtContext *ctx,
67  std::vector<InstPtr> &instructions,
68  std::shared_ptr<CompositeInstruction> function) {
69  xacc::warning("[XasmCompiler] createForInstructions called with invalid "
70  "template parameter. Skipping.");
71  return;
72  }
73 
74 public:
75  HeterogeneousMap runtimeOptions;
76 
77  XASMListener();
78 
79  std::vector<std::string> getBufferNames() {return functionBufferNames;}
80 
81  std::shared_ptr<CompositeInstruction> getFunction() { return function; }
82 
83  void enterXacckernel(xasmParser::XacckernelContext * /*ctx*/) override;
84  void enterXacclambda(xasmParser::XacclambdaContext * /*ctx*/) override;
85 
86  void enterInstruction(xasmParser::InstructionContext * /*ctx*/) override;
87  void enterBufferList(xasmParser::BufferListContext * /*ctx*/) override;
88  void enterParamList(xasmParser::ParamListContext * /*ctx*/) override;
89  void exitInstruction(xasmParser::InstructionContext * /*ctx*/) override;
90 
91  void enterComposite_generator(
92  xasmParser::Composite_generatorContext * /*ctx*/) override;
93  void enterOptionsType(xasmParser::OptionsTypeContext * /*ctx*/) override;
94  void exitComposite_generator(
95  xasmParser::Composite_generatorContext * /*ctx*/) override;
96 
97  void enterForstmt(xasmParser::ForstmtContext * /*ctx*/) override;
98  void exitForstmt(xasmParser::ForstmtContext * /*ctx*/) override;
99 
100  void enterIfstmt(xasmParser::IfstmtContext * /*ctx*/) override;
101  void exitIfstmt(xasmParser::IfstmtContext * /*ctx*/) override;
102 };
103 
104 } // namespace xacc
105 
106 #endif
Definition: xasm_listener.hpp:29
Definition: xasm_listener.hpp:28
Definition: Accelerator.hpp:25
Definition: xasm_listener.hpp:27
Definition: heterogeneous.hpp:45
Definition: Instruction.hpp:100
Definition: xasm_listener.hpp:30
Definition: xasm_listener.hpp:32