David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 1 | // Copyright 2018 The Clspv Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #ifndef CLSPV_LIB_CONSTANT_EMITTER_H |
| 16 | #define CLSPV_LIB_CONSTANT_EMITTER_H |
| 17 | |
| 18 | #include "llvm/IR/Constant.h" |
| 19 | #include "llvm/IR/Constants.h" |
| 20 | #include "llvm/IR/DataLayout.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
| 22 | |
| 23 | namespace clspv { |
| 24 | |
| 25 | // A Constant value emitter. Emit the bytes of a constant as if it were |
| 26 | // laid out in memory, converted to hexadecimal. |
| 27 | class ConstantEmitter { |
| 28 | public: |
| 29 | ConstantEmitter(const llvm::DataLayout &DL, llvm::raw_ostream &out) |
| 30 | : Layout(DL), Out(out), Offset(0) {} |
| 31 | |
| 32 | void Emit(llvm::Constant *c); |
| 33 | |
| 34 | private: |
| 35 | // Emit |n| zeroes. |
| 36 | void EmitZeroes(size_t n); |
| 37 | // Emit just enough zeros to align to the given alignment. |
| 38 | void AlignTo(size_t alignment); |
| 39 | |
| 40 | void EmitStruct(llvm::ConstantStruct *c); |
| 41 | // ConstantDataSequential is an LLVM optimization for smallish numeric |
| 42 | // arrays and vectors. |
| 43 | void EmitDataSequential(llvm::ConstantDataSequential *c); |
| 44 | // Aggregate covers both Array and (general) Vector |
| 45 | void EmitAggregate(llvm::ConstantAggregate *c); |
| 46 | void EmitAggregateZero(llvm::ConstantAggregateZero *c); |
| 47 | void EmitInt(llvm::ConstantInt *c); |
| 48 | void EmitFP(llvm::ConstantFP *c); |
| 49 | |
| 50 | // Emit bytes representing the first num_bits bits from the |
| 51 | // array at |data|. |
Diego Novillo | a4c44fa | 2019-04-11 10:56:15 -0400 | [diff] [blame] | 52 | void EmitRaw(unsigned num_bits, const uint64_t *data); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 53 | |
| 54 | // Emit a byte as a hex number to the output stream. |
| 55 | void EmitByte(size_t byte); |
| 56 | |
| 57 | const llvm::DataLayout &Layout; |
| 58 | // The output stream. |
| 59 | llvm::raw_ostream &Out; |
| 60 | // Offset into the memory layout in memory. |
| 61 | size_t Offset; |
| 62 | }; |
| 63 | |
| 64 | } // namespace clspv |
| 65 | |
| 66 | #endif |