|
2 | 2 | import cllvm |
3 | 3 | #endif |
4 | 4 |
|
5 | | -/// Enumerates the attributes of LLVM functions. |
| 5 | +/// Enumerates the attributes of LLVM functions and function parameters. |
6 | 6 | public enum FunctionAttribute: String { |
7 | 7 | /// This attribute indicates that, when emitting the prologue and epilogue, |
8 | 8 | /// the backend should forcibly align the stack pointer. |
@@ -149,15 +149,6 @@ public enum FunctionAttribute: String { |
149 | 149 | /// unwind table entry be produced for this function even if we can show |
150 | 150 | /// that no exceptions passes by it. |
151 | 151 | case uwtable |
152 | | - |
153 | | - /// ID of the attribute. |
154 | | - internal var kindID: UInt32 { |
155 | | - return LLVMGetEnumAttributeKindForName(rawValue, rawValue.count) |
156 | | - } |
157 | | -} |
158 | | - |
159 | | -/// Enumerates the parameter attributes of LLVM functions. |
160 | | -public enum ParameterAttribute: String { |
161 | 152 | /// This indicates to the code generator that the parameter or return value |
162 | 153 | /// should be zero-extended to the extent required by the target’s ABI by the |
163 | 154 | /// caller (for a parameter) or the callee (for a return value). |
@@ -215,48 +206,55 @@ public enum ParameterAttribute: String { |
215 | 206 | } |
216 | 207 | } |
217 | 208 |
|
218 | | -extension Function { |
219 | | - /// Adds an attribute to the function. |
220 | | - /// |
221 | | - /// - parameter attr: The attribute to add. |
222 | | - /// - parameter value: The optional value of the attribute. |
223 | | - public func addAttribute(_ attr: FunctionAttribute, value: UInt64 = 0) { |
224 | | - let ctx = LLVMGetModuleContext(LLVMGetGlobalParent(llvm)) |
225 | | - let attrRef = LLVMCreateEnumAttribute(ctx, attr.kindID, value) |
226 | | - LLVMAddAttributeAtIndex(llvm, 0, attrRef) |
| 209 | +/// Represents the possible indices of function attributes. |
| 210 | +public enum AttributeIndex: ExpressibleByIntegerLiteral, RawRepresentable { |
| 211 | + /// Represents the function itself. |
| 212 | + case function |
| 213 | + /// Represents the function's return value. |
| 214 | + case returnValue |
| 215 | + /// Represents the function's i-th argument. |
| 216 | + case argument(Int) |
| 217 | + |
| 218 | + public init(integerLiteral value: UInt32) { |
| 219 | + switch value { |
| 220 | + case ~0: self = .function |
| 221 | + case 0: self = .returnValue |
| 222 | + default: self = .argument(Int(value) - 1) |
| 223 | + } |
227 | 224 | } |
228 | 225 |
|
229 | | - /// Adds an attribute to the given function's parameter. |
| 226 | + public init?(rawValue: RawValue) { |
| 227 | + self.init(integerLiteral: rawValue) |
| 228 | + } |
| 229 | + |
| 230 | + public var rawValue: UInt32 { |
| 231 | + switch self { |
| 232 | + case .function: return ~0 |
| 233 | + case .returnValue: return 0 |
| 234 | + case .argument(let i): return UInt32(i) + 1 |
| 235 | + } |
| 236 | + } |
| 237 | +} |
| 238 | + |
| 239 | +extension Function { |
| 240 | + /// Adds an attribute to the function, its return value or its parameters. |
230 | 241 | /// |
231 | 242 | /// - parameter attr: The attribute to add. |
232 | 243 | /// - parameter value: The optional value of the attribute. |
233 | | - /// - parameterIndex: The index of the parameter to which add the |
234 | | - /// attribute, starting from 0. |
235 | | - public func addParameterAttribute(_ attr: ParameterAttribute, value: UInt64 = 0, |
236 | | - for parameterIndex: LLVMAttributeIndex) { |
| 244 | + /// - parameter index: The index representing the function, its return value |
| 245 | + /// or one of its parameters. |
| 246 | + public func addAttribute(_ attr: FunctionAttribute, value: UInt64 = 0, to index: AttributeIndex) { |
237 | 247 | let ctx = LLVMGetModuleContext(LLVMGetGlobalParent(llvm)) |
238 | 248 | let attrRef = LLVMCreateEnumAttribute(ctx, attr.kindID, value) |
239 | | - LLVMAddAttributeAtIndex(llvm, parameterIndex + 1, attrRef) |
240 | | - } |
241 | | - |
242 | | - /// Removes an attribute from the function. |
243 | | - /// |
244 | | - /// - parameter attr: The attribute to remove. |
245 | | - public func removeAttribute(_ attr: FunctionAttribute, value: UInt64 = 0) { |
246 | | - LLVMRemoveEnumAttributeAtIndex(llvm, 0, attr.kindID) |
| 249 | + LLVMAddAttributeAtIndex(llvm, index.rawValue, attrRef) |
247 | 250 | } |
248 | 251 |
|
249 | 252 | /// Removes an attribute from the function. |
250 | 253 | /// |
251 | | - /// - parameter attr: The attribute to add. |
252 | | - |
253 | | - /// Removes an attribute from the given function's parameter. |
254 | | - /// |
255 | 254 | /// - parameter attr: The attribute to remove. |
256 | | - /// - parameter parameterIndex: The index of the parameter to which add the |
257 | | - /// attribute, starting from 0. |
258 | | - public func removeParameterAttribute(_ attr: ParameterAttribute, |
259 | | - for parameterIndex: LLVMAttributeIndex) { |
260 | | - LLVMRemoveEnumAttributeAtIndex(llvm, parameterIndex + 1, attr.kindID) |
| 255 | + /// - parameter index: The index representing the function, its return value |
| 256 | + /// or one of its parameters. |
| 257 | + public func removeAttribute(_ attr: FunctionAttribute, value: UInt64 = 0, from index: AttributeIndex) { |
| 258 | + LLVMRemoveEnumAttributeAtIndex(llvm, index.rawValue, attr.kindID) |
261 | 259 | } |
262 | 260 | } |
0 commit comments