1//===-- BytecodeBase.td - Base bytecode R/W defs -----------*- tablegen -*-===// 2// 3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4// See https://llvm.org/LICENSE.txt for license information. 5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6// 7//===----------------------------------------------------------------------===// 8// 9// This is the base bytecode reader/writer definition file. 10// 11//===----------------------------------------------------------------------===// 12 13#ifndef BYTECODE_BASE 14#define BYTECODE_BASE 15 16class Bytecode<string parse="", string build="", string print="", string get="", string t=""> { 17 // Template for parsing. 18 // $_reader == dialect bytecode reader 19 // $_resultType == result type of parsed instance 20 // $_var == variable being parsed 21 // If parser is not specified, then the parse of members is used. 22 string cParser = parse; 23 24 // Template for building from parsed. 25 // $_resultType == result type of parsed instance 26 // $_args == args/members comma separated 27 string cBuilder = build; 28 29 // Template for printing. 30 // $_writer == dialect bytecode writer 31 // $_name == parent attribute/type name 32 // $_getter == getter 33 string cPrinter = print; 34 35 // Template for getter from in memory form. 36 // $_attrType == attribute/type 37 // $_member == member instance 38 // $_getMember == get + UpperCamelFromSnake($_member) 39 string cGetter = get; 40 41 // Type built. 42 // Note: if cType is empty, then name of def is used. 43 string cType = t; 44 45 // Predicate guarding parse method as an Attribute/Type could have multiple 46 // parse methods, specify predicates to be orthogonal and cover entire 47 // "print space" to avoid order dependence. 48 // If empty then method is unconditional. 49 // $_val == predicate function to apply on value dyn_casted to cType. 50 string printerPredicate = ""; 51} 52 53class WithParser<string p="", Bytecode t=Bytecode<>> : 54 Bytecode<p, t.cBuilder, t.cPrinter, t.cGetter, t.cType>; 55class WithBuilder<string b="", Bytecode t=Bytecode<>> : 56 Bytecode<t.cParser, b, t.cPrinter, t.cGetter, t.cType>; 57class WithPrinter<string p="", Bytecode t=Bytecode<>> : 58 Bytecode<t.cParser, t.cBuilder, p, t.cGetter, t.cType>; 59class WithType<string ty="", Bytecode t=Bytecode<>> : 60 Bytecode<t.cParser, t.cBuilder, t.cPrinter, t.cGetter, ty>; 61class WithGetter<string g="", Bytecode t=Bytecode<>> : 62 Bytecode<t.cParser, t.cBuilder, t.cPrinter, g, t.cType>; 63 64// Representation of a bytecode element consisting of other bytecode atoms. 65// E.g., it is effectively a struct of bytecode elements. Set the members by 66// define a members dag: `dag members = (attr ...)`. 67class CompositeBytecode<string t = ""> : WithType<t>; 68 69class AttributeKind : 70 WithParser <"succeeded($_reader.readAttribute($_var))", 71 WithBuilder<"$_args", 72 WithPrinter<"$_writer.writeAttribute($_getter)">>>; 73def Attribute : AttributeKind; 74class TypeKind : 75 WithParser <"succeeded($_reader.readType($_var))", 76 WithBuilder<"$_args", 77 WithPrinter<"$_writer.writeType($_getter)">>>; 78def Type : TypeKind; 79 80def VarInt : 81 WithParser <"succeeded($_reader.readVarInt($_var))", 82 WithBuilder<"$_args", 83 WithPrinter<"$_writer.writeVarInt($_getter)", 84 WithType <"uint64_t">>>>; 85def SignedVarInt : 86 WithParser <"succeeded($_reader.readSignedVarInt($_var))", 87 WithBuilder<"$_args", 88 WithPrinter<"$_writer.writeSignedVarInt($_getter)", 89 WithType <"int64_t">>>>; 90def Blob : 91 WithParser <"succeeded($_reader.readBlob($_var))", 92 WithBuilder<"$_args", 93 WithPrinter<"$_writer.writeOwnedBlob($_getter)", 94 WithType <"ArrayRef<char>">>>>; 95def Bool : 96 WithParser <"succeeded($_reader.readBool($_var))", 97 WithBuilder<"$_args", 98 WithPrinter<"$_writer.writeOwnedBool($_getter)", 99 WithType <"bool">>>>; 100class KnownWidthAPInt<string s> : 101 WithParser <"succeeded(readAPIntWithKnownWidth($_reader, " # s # ", $_var))", 102 WithBuilder<"$_args", 103 WithPrinter<"$_writer.writeAPIntWithKnownWidth($_getter)", 104 WithType <"FailureOr<APInt>">>>>; 105class KnownSemanticsAPFloat<string s> : 106 WithParser <"succeeded(readAPFloatWithKnownSemantics($_reader, " # s # ", $_var))", 107 WithBuilder<"$_args", 108 WithPrinter<"$_writer.writeAPFloatWithKnownSemantics($_getter)", 109 WithType <"FailureOr<APFloat>">>>>; 110class ResourceHandle<string s> : 111 WithParser <"succeeded(readResourceHandle<" # s # ">($_reader, $_var))", 112 WithBuilder<"$_args", 113 WithPrinter<"$_writer.writeResourceHandle($_getter)", 114 WithType <"FailureOr<" # s # ">">>>>; 115 116// Helper to define variable that is defined later but not parsed nor printed. 117class LocalVar<string t, string d> : 118 WithParser <"(($_var = " # d # "), true)", 119 WithBuilder<"$_args", 120 WithPrinter<"", 121 WithType <t>>>>; 122 123// Array instances. 124class Array<Bytecode t> { 125 Bytecode elemT = t; 126 127 string cBuilder = "$_args"; 128} 129// - Array elements currently needs a different bytecode type to accommodate 130// for the list print/parsing. 131class List<Bytecode t> : WithGetter<"$_member", t>; 132def SignedVarIntList : List<SignedVarInt>; 133def BoolList : List<Bool>; 134 135// Define dialect attribute or type. 136class DialectAttrOrType<dag d> { 137 // Any members starting with underscore is not fed to create function but 138 // treated as purely local variable. 139 dag members = d; 140 141 // When needing to specify a custom return type. 142 string cType = ""; 143 144 // Any post-processing that needs to be done. 145 code postProcess = ""; 146} 147 148class DialectAttribute<dag d> : DialectAttrOrType<d>, AttributeKind { 149 let cParser = "succeeded($_reader.readAttribute<$_resultType>($_var))"; 150 let cBuilder = "get<$_resultType>(context, $_args)"; 151} 152class DialectType<dag d> : DialectAttrOrType<d>, TypeKind { 153 let cParser = "succeeded($_reader.readType<$_resultType>($_var))"; 154 let cBuilder = "get<$_resultType>(context, $_args)"; 155} 156 157class DialectAttributes<string d> { 158 string dialect = d; 159 list<DialectAttrOrType> elems; 160} 161 162class DialectTypes<string d> { 163 string dialect = d; 164 list<DialectAttrOrType> elems; 165} 166 167def attr; 168def type; 169 170// Marker to indicate a skipped attribute or type in the enum. Could either be 171// reserved for a future value or for marking a previously used value as dead. 172def none; 173def ReservedOrDead : DialectAttrOrType<(none)>; 174 175#endif // BYTECODE_BASE 176 177