Skip to content

Commit

Permalink
Merge pull request #44 from kntkymt/feature/optimize-push-pop
Browse files Browse the repository at this point in the history
Optimizerのテスト
  • Loading branch information
kntkymt authored Feb 3, 2024
2 parents 8072016 + 2530ca4 commit ee28a5c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
10 changes: 10 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/CCompiler.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "OptimizerTest"
BuildableName = "OptimizerTest"
BlueprintName = "OptimizerTest"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
Expand Down
4 changes: 4 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ let package = Package(
name: "CCompilerCoreTest",
dependencies: ["CCompilerCore"]
),
.testTarget(
name: "OptimizerTest",
dependencies: ["Optimizer"]
),
.testTarget(
name: "GeneratorTest",
dependencies: ["Generator"]
Expand Down
6 changes: 3 additions & 3 deletions Sources/Generator/AsmRepresentation.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

public enum AsmRepresent {

public enum Instruction {
public enum Instruction: Equatable {
case mov(dst: Register, src: Register)
case movi(dst: Register, immediate: Int64)

Expand Down Expand Up @@ -49,7 +49,7 @@ public enum AsmRepresent {
case globl(label: String)
case dataDecl(kind: DataKind, value: String)

public enum Address {
public enum Address: Equatable {
case register(_ register: Register)
case distance(_ register: Register, _ diff: Int64)
}
Expand Down Expand Up @@ -79,7 +79,7 @@ public enum AsmRepresent {
}
}

public enum Register: String {
public enum Register: String, Equatable {
case x0
case x1
case x2
Expand Down
39 changes: 39 additions & 0 deletions Tests/OptimizerTest/RemoveImmediatePushPopPathTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import XCTest
@testable import Optimizer
import Generator

final class RemoveImmediatePushPopPathTest: XCTestCase {

func testRemoved() {
let instructions: [AsmRepresent.Instruction] = [
.movi(dst: .x0, immediate: 10),
.push(src: .x0),
.pop(dst: .x0),
.ret
]

let result = RemoveImmediatePushPopPath().optimize(instructions: instructions)
let expected: [AsmRepresent.Instruction] = [
instructions[0],
instructions[3]
]

XCTAssertEqual(result, expected)
}

func testNotRemoved() {
let instructions: [AsmRepresent.Instruction] = [
.movi(dst: .x0, immediate: 10),
.push(src: .x0),
.add(dst: .x0, src1: .x1, src2: .x0),
.mov(dst: .x1, src: .x0),
.pop(dst: .x0),
.ret
]

let result = RemoveImmediatePushPopPath().optimize(instructions: instructions)
let expected: [AsmRepresent.Instruction] = instructions

XCTAssertEqual(result, expected)
}
}

0 comments on commit ee28a5c

Please sign in to comment.