Skip to content

Commit

Permalink
Refactor IR printing
Browse files Browse the repository at this point in the history
  • Loading branch information
kyouko-taiga committed Jul 11, 2022
1 parent 5f18bbb commit 3163475
Show file tree
Hide file tree
Showing 17 changed files with 243 additions and 244 deletions.
113 changes: 84 additions & 29 deletions Sources/Compiler/AST/DeclLocator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public struct DeclLocator: Hashable {

case `extension`(target: DeclLocator)

case function(name: String, labels: [String])
case function(name: String, labels: [String], notation: OperatorNotation?)

case methodImpl(MethodImplDecl.Introducer)

Expand Down Expand Up @@ -54,12 +54,12 @@ public struct DeclLocator: Hashable {

switch ast[declID].introducer.value {
case .memberwiseInit, .`init`:
self = .function(name: "init", labels: labels)
self = .function(name: "init", labels: labels, notation: nil)
case .deinit:
self = .function(name: "deinit", labels: [])
self = .function(name: "deinit", labels: [], notation: nil)
case .fun:
if let name = ast[declID].identifier?.value {
self = .function(name: name, labels: labels)
self = .function(name: name, labels: labels, notation: ast[declID].notation?.value)
} else {
self = .lambda(declID)
}
Expand All @@ -78,6 +78,59 @@ public struct DeclLocator: Hashable {
}
}

/// A mangled description of this component.
public var mangled: String {
switch self {
case .conformance(let target, let trait):
return "C\(target)\(trait.mangled)"

case .extension(let target):
return "E\(target)"

case .function(let name, let labels, let notation):
let labels = labels.map({ $0.mangled }).joined()
if let n = notation {
return "O\(String(describing: n).mangled)\(name.mangled)\(labels.count)\(labels)a"
} else {
return "F\(name.mangled)\(labels.count)\(labels)a"
}

case .methodImpl(let introducer):
switch introducer {
case .let : return "Il"
case .inout: return "Ii"
case .sink : return "Is"
}

case .module(let name):
return "M\(name.mangled)"

case .namespace(let name):
return "N\(name.mangled)"

case .lambda(let discriminator):
return "L\(discriminator.rawValue)"

case .product(let name):
return "P\(name.mangled)"

case .subscript(let name, let labels):
let ls = labels.map({ $0.mangled }).joined()
return "S\(name.mangled)\(labels.count)\(ls)"

case .subscriptImpl(let introducer):
switch introducer {
case .let : return "Il"
case .inout : return "Ii"
case .sink : return "Is"
case .assign: return "Ia"
}

case .trait(let name):
return "N\(name.mangled)"
}
}

}

/// The constituents of the locator.
Expand Down Expand Up @@ -114,13 +167,17 @@ public struct DeclLocator: Hashable {
}

/// The locator's value encoded as a string.
public var mangled: String { components.descriptions(joinedBy: "") }
public var mangled: String {
components.lazy.map({ $0.mangled }).joined()
}

}

extension DeclLocator: CustomStringConvertible {

public var description: String { mangled }
public var description: String {
components.descriptions(joinedBy: ".")
}

}

Expand All @@ -129,48 +186,46 @@ extension DeclLocator.Component: CustomStringConvertible {
public var description: String {
switch self {
case .conformance(let target, let trait):
return "C\(target)\(trait.mangled)"
return "(\(target)::\(trait)"

case .extension(let target):
return "E\(target)"
return target.description

case .function(let name, let labels):
let ls = labels.map({ $0.mangled }).joined()
return "F\(name.mangled)\(labels.count)\(ls)a"
case .function(let name, let labels, let notation):
let n = notation.map(String.init(describing:)) ?? ""
if labels.isEmpty {
return n + name
} else {
return n + name + "(" + labels.lazy.map({ "\($0):" }).joined() + ")"
}

case .methodImpl(let introducer):
switch introducer {
case .let : return "Il"
case .inout: return "Ii"
case .sink : return "Is"
}
return String(describing: introducer)

case .module(let name):
return "M\(name.mangled)"
return name

case .namespace(let name):
return "N\(name.mangled)"
return name

case .lambda(let discriminator):
return "L\(discriminator.rawValue)"
return String(describing: discriminator.rawValue)

case .product(let name):
return "P\(name.mangled)"
return name.description

case .subscript(let name, let labels):
let ls = labels.map({ $0.mangled }).joined()
return "S\(name.mangled)\(labels.count)\(ls)"
if labels.isEmpty {
return name
} else {
return name + "[" + labels.lazy.map({ "\($0):" }).joined() + "]"
}

case .subscriptImpl(let introducer):
switch introducer {
case .let : return "Il"
case .inout : return "Ii"
case .sink : return "Is"
case .assign: return "Ia"
}
return String(describing: introducer)

case .trait(let name):
return "N\(name.mangled)"
return name
}
}

Expand Down
42 changes: 18 additions & 24 deletions Sources/Compiler/IR/Emitter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public struct Emitter {
let decl = program.ast[name].decl
let declType = program.declTypes[decl]!

let alloc = module.insert(AllocStackInst(objectType: declType), at: insertionPoint!)
let alloc = module.insert(AllocStackInst(declType), at: insertionPoint!)
locals[decl] = .inst(alloc)

if let source = initializer {
Expand All @@ -185,10 +185,10 @@ public struct Emitter {
at: insertionPoint!)
} else {
let member = module.insert(
BorrowMemberInst(value: source, path: path, type: .address(declType)),
BorrowMemberInst(type: .address(declType), value: source, path: path),
at: insertionPoint!)
let object = module.insert(
LoadInst(source: .inst(member), type: .object(declType)),
LoadInst(type: .object(declType), source: .inst(member)),
at: insertionPoint!)
_ = module.insert(
StoreInst(object: .inst(object), target: .inst(alloc)),
Expand All @@ -208,7 +208,7 @@ public struct Emitter {
} else {
let value = emitR(expr: initializer, into: &module)
let alloc = module.insert(
AllocStackInst(objectType: program.exprTypes[initializer]!),
AllocStackInst(program.exprTypes[initializer]!),
at: insertionPoint!)
_ = module.insert(
StoreInst(object: value, target: .inst(alloc)),
Expand All @@ -224,7 +224,7 @@ public struct Emitter {
locals[decl] = source
} else {
let member = module.insert(
BorrowMemberInst(value: source, path: path, type: .address(declType)),
BorrowMemberInst(type: .address(declType), value: source, path: path),
at: insertionPoint!)
locals[decl] = .inst(member)
}
Expand Down Expand Up @@ -264,7 +264,7 @@ public struct Emitter {
bitPattern: BitPattern(pattern: program.ast[expr].value ? 1 : 0, width: 1))

let result = module.insert(
RecordInst(type: .object(.product(boolType)), operands: [.constant(.integer(boolValue))]),
RecordInst(objectType: .product(boolType), operands: [.constant(.integer(boolValue))]),
at: insertionPoint!)
return .inst(result)
}
Expand All @@ -278,9 +278,7 @@ public struct Emitter {
// If the expression is supposed to return a value, allocate storage for it.
var resultStorage: InstID?
if let type = program.exprTypes[expr], type != .unit {
resultStorage = module.insert(
AllocStackInst(objectType: type),
at: insertionPoint!)
resultStorage = module.insert(AllocStackInst(type), at: insertionPoint!)
}

// Emit the condition(s).
Expand All @@ -296,10 +294,10 @@ public struct Emitter {
// Evaluate the condition in the current block.
var condition = emitL(expr: itemExpr, into: &module)
condition = .inst(module.insert(
BorrowMemberInst(value: condition, path: [0], type: .address(.builtin(.i(1)))),
BorrowMemberInst(type: .address(.builtin(.i(1))), value: condition, path: [0]),
at: insertionPoint!))
condition = .inst(module.insert(
LoadInst(source: condition, type: .object(.builtin(.i(1)))),
LoadInst(type: .object(.builtin(.i(1))), source: condition),
at: insertionPoint!))

module.insert(
Expand Down Expand Up @@ -352,7 +350,7 @@ public struct Emitter {
insertionPoint = InsertionPoint(endOf: continuation)
if let source = resultStorage {
let load = module.insert(
LoadInst(source: .inst(source), type: LoweredType(lowering: program.exprTypes[expr]!)),
LoadInst(type: LoweredType(lowering: program.exprTypes[expr]!), source: .inst(source)),
at: insertionPoint!)
return .inst(load)
} else {
Expand Down Expand Up @@ -390,7 +388,7 @@ public struct Emitter {
emitR(expr: argument.value.value, into: &module)
})
let record = module.insert(
RecordInst(type: .object(program.exprTypes[expr]!), operands: operands),
RecordInst(objectType: program.exprTypes[expr]!, operands: operands),
at: insertionPoint!)
return .inst(record)
} else {
Expand Down Expand Up @@ -446,7 +444,7 @@ public struct Emitter {
}

let i = module.insert(
CallInst(callee: callee, operands: operands, type: .object(program.exprTypes[expr]!)),
CallInst(type: .object(program.exprTypes[expr]!), callee: callee, operands: operands),
at: insertionPoint!)
return .inst(i)
}
Expand All @@ -462,7 +460,7 @@ public struct Emitter {
let bits = BitPattern(fromDecimal: program.ast[expr].value)!.resized(to: 64)
let value = IntegerConstant(bitPattern: bits)
let result = module.insert(
RecordInst(type: .object(.product(type)), operands: [.constant(.integer(value))]),
RecordInst(objectType: .product(type), operands: [.constant(.integer(value))]),
at: insertionPoint!)
return .inst(result)

Expand All @@ -477,7 +475,7 @@ public struct Emitter {
) -> Operand {
let source = emitL(expr: expr, into: &module)
let load = module.insert(
LoadInst(source: source, type: .object(program.exprTypes[expr]!)),
LoadInst(type: .object(program.exprTypes[expr]!), source: source),
at: insertionPoint!)
return .inst(load)
}
Expand All @@ -500,12 +498,8 @@ public struct Emitter {

default:
let value = emitR(expr: expr, into: &module)
let alloc = module.insert(
AllocStackInst(objectType: program.exprTypes[expr]!),
at: insertionPoint!)
_ = module.insert(
StoreInst(object: value, target: .inst(alloc)),
at: insertionPoint!)
let alloc = module.insert(AllocStackInst(program.exprTypes[expr]!), at: insertionPoint!)
_ = module.insert(StoreInst(object: value, target: .inst(alloc)), at: insertionPoint!)
return .inst(alloc)
}
}
Expand Down Expand Up @@ -547,9 +541,9 @@ public struct Emitter {
let layout = TypeLayout(module.type(of: receiver).astType)
let member = module.insert(
BorrowMemberInst(
type: .address(exprType),
value: receiver,
path: [layout.offset(of: NodeID(unsafeRawValue: declID.rawValue), ast: program.ast)],
type: .address(exprType)),
path: [layout.offset(of: NodeID(unsafeRawValue: declID.rawValue), ast: program.ast)]),
at: insertionPoint!)
return .inst(member)

Expand Down
37 changes: 0 additions & 37 deletions Sources/Compiler/IR/IRPrinter.swift

This file was deleted.

Loading

0 comments on commit 3163475

Please sign in to comment.