Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified Assets/screen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 0 additions & 5 deletions Llvm/swiftgen.yml

This file was deleted.

File renamed without changes.
File renamed without changes.
12 changes: 6 additions & 6 deletions Llvm/Package.swift → Lowlevel/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@
import PackageDescription

let package = Package(
name: "Llvm",
name: "Lowlevel",
defaultLocalization: "en",
platforms: [
.macOS(.v13)
],
products: [
.library(
name: "Llvm",
targets: ["Llvm"]
name: "Lowlevel",
targets: ["Lowlevel"]
)
],
targets: [
.target(
name: "Llvm",
name: "Lowlevel",
resources: [
.process("Localizable")
]
),
.testTarget(
name: "LlvmTests",
dependencies: ["Llvm"]
name: "LowlevelTests",
dependencies: ["Lowlevel"]
)
]
)
51 changes: 51 additions & 0 deletions Lowlevel/Sources/Lowlevel/Assembly.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import Foundation

public final class Assembly {

public init() {}

public func generateAssembly(fromSwiftCode swiftCode: String) -> String {
let tempDirectory = FileManager.default.temporaryDirectory
let swiftFileURL = tempDirectory.appendingPathComponent("temp.swift")
let assemblyFileURL = tempDirectory.appendingPathComponent("temp.s")

do {
try swiftCode.write(to: swiftFileURL, atomically: true, encoding: .utf8)

let process = processArgument(
assemblyFileURL: assemblyFileURL,
swiftFileURL: swiftFileURL)

let pipe = Pipe()
process.standardOutput = pipe
process.standardError = pipe

try process.run()
process.waitUntilExit()

let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: .utf8) ?? ""

if process.terminationStatus == 0 {
let assemblyCode = try String(contentsOf: assemblyFileURL, encoding: .utf8)
return assemblyCode
} else {
return L10n.errorAssemblyCode(output)
}
} catch {
return L10n.errorAssemblyCode(error.localizedDescription)
}
}

private func processArgument(assemblyFileURL: URL, swiftFileURL: URL) -> Process {
let process = Process()
process.executableURL = URL(fileURLWithPath: "/usr/bin/swiftc")
process.arguments = [
"-S",
"-o", assemblyFileURL.path,
swiftFileURL.path
]

return process
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,30 @@ import Foundation
// swiftlint:disable explicit_type_interface function_parameter_count identifier_name line_length
// swiftlint:disable nesting type_body_length type_name vertical_whitespace_opening_braces
internal enum L10n {
/// Error generating assembly code: %@
internal static func errorAssemblyCode(_ p1: Any) -> String {
return L10n.tr("Lowlevel", "errorAssemblyCode", String(describing: p1), fallback: "Error generating assembly code: %@")
}
/// Bytecode.strings
///
///
/// Created by João Lucas on 22/06/24.
internal static func errorOutput(_ p1: Any) -> String {
return L10n.tr("Llvm", "errorOutput", String(describing: p1), fallback: "Error Output: %@")
return L10n.tr("Lowlevel", "errorOutput", String(describing: p1), fallback: "Error Output: %@")
}
/// Error reading bytecode
internal static let errorReadingBytecode = L10n.tr("Llvm", "errorReadingBytecode", fallback: "Error reading bytecode")
internal static let errorReadingBytecode = L10n.tr("Lowlevel", "errorReadingBytecode", fallback: "Error reading bytecode")
/// Error reading output file: %@
internal static func errorReadingOutput(_ p1: Any) -> String {
return L10n.tr("Llvm", "errorReadingOutput", String(describing: p1), fallback: "Error reading output file: %@")
return L10n.tr("Lowlevel", "errorReadingOutput", String(describing: p1), fallback: "Error reading output file: %@")
}
/// Error during script execution %@
internal static func errorScriptExecution(_ p1: Any) -> String {
return L10n.tr("Llvm", "errorScriptExecution", String(describing: p1), fallback: "Error during script execution %@")
return L10n.tr("Lowlevel", "errorScriptExecution", String(describing: p1), fallback: "Error during script execution %@")
}
/// Error writing temporary file: %@
internal static func errorWritingTemporary(_ p1: Any) -> String {
return L10n.tr("Llvm", "errorWritingTemporary", String(describing: p1), fallback: "Error writing temporary file: %@")
return L10n.tr("Lowlevel", "errorWritingTemporary", String(describing: p1), fallback: "Error writing temporary file: %@")
}
}
// swiftlint:enable explicit_type_interface function_parameter_count identifier_name line_length
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
"errorScriptExecution" = "Error during script execution %@";
"errorReadingBytecode" = "Error reading bytecode";
"errorReadingOutput" = "Error reading output file: %@";
"errorAssemblyCode" = "Error generating assembly code: %@";
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import XCTest
import SwiftUI
@testable import Llvm
@testable import Lowlevel

final class LlvmTests: XCTestCase {
final class LowlevelTests: XCTestCase {

@State private var swiftCode: String = "let a = 10"
@State private var llvm: String = ""
Expand All @@ -22,4 +22,20 @@ final class LlvmTests: XCTestCase {
XCTFail("Error reading temporary Swift file: \(error.localizedDescription)")
}
}

func testGenerateAssembly() {
let swiftCode = """
func sum(a: Int, b: Int) -> Int {
return a + b
}
"""

let assemblyCode = Assembly().generateAssembly(fromSwiftCode: swiftCode)

XCTAssertTrue(assemblyCode.contains("sum"),
"The generated assembly code must contain the 'sum' function.")
XCTAssertTrue(assemblyCode.contains("ret"),
"The generated assembly code must contain the 'ret' instruction.")
}

}
5 changes: 5 additions & 0 deletions Lowlevel/swiftgen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
strings:
inputs: Sources/Lowlevel/Localizable/en.lproj
outputs:
templateName: structured-swift5
output: Sources/Lowlevel/Generated/L10n.swift
18 changes: 9 additions & 9 deletions SwiftExplorer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
objects = {

/* Begin PBXBuildFile section */
C512A2A62C28D54F00175F0B /* Lowlevel in Frameworks */ = {isa = PBXBuildFile; productRef = C512A2A52C28D54F00175F0B /* Lowlevel */; };
C519A74F2C26ED6B00BF91E3 /* Theme in Frameworks */ = {isa = PBXBuildFile; productRef = C519A74E2C26ED6B00BF91E3 /* Theme */; };
C519A7542C26F16700BF91E3 /* SwiftExplorer.strings in Resources */ = {isa = PBXBuildFile; fileRef = C519A7522C26F16700BF91E3 /* SwiftExplorer.strings */; };
C519A7572C26F1EF00BF91E3 /* L10n.swift in Sources */ = {isa = PBXBuildFile; fileRef = C519A7562C26F1EF00BF91E3 /* L10n.swift */; };
C519A75F2C27034900BF91E3 /* ContentViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C519A75E2C27034900BF91E3 /* ContentViewTests.swift */; };
C519A7622C27044F00BF91E3 /* CommonTest in Frameworks */ = {isa = PBXBuildFile; productRef = C519A7612C27044F00BF91E3 /* CommonTest */; };
C519A7642C27045500BF91E3 /* SnapshotTesting in Frameworks */ = {isa = PBXBuildFile; productRef = C519A7632C27045500BF91E3 /* SnapshotTesting */; };
C55B316F2C28862000542356 /* Llvm in Frameworks */ = {isa = PBXBuildFile; productRef = C55B316E2C28862000542356 /* Llvm */; };
C58379CC2C26A3440068D178 /* SwiftExplorerApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = C58379CB2C26A3440068D178 /* SwiftExplorerApp.swift */; };
C58379CE2C26A3440068D178 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C58379CD2C26A3440068D178 /* ContentView.swift */; };
C58379D02C26A3450068D178 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C58379CF2C26A3450068D178 /* Assets.xcassets */; };
Expand All @@ -34,12 +34,12 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
C512A2A42C28D4E300175F0B /* Lowlevel */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = Lowlevel; sourceTree = "<group>"; };
C519A74D2C26ED3500BF91E3 /* Theme */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = Theme; sourceTree = "<group>"; };
C519A7532C26F16700BF91E3 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/SwiftExplorer.strings; sourceTree = "<group>"; };
C519A7562C26F1EF00BF91E3 /* L10n.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = L10n.swift; sourceTree = "<group>"; };
C519A7592C26F5EE00BF91E3 /* CommonTest */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = CommonTest; sourceTree = "<group>"; };
C519A75E2C27034900BF91E3 /* ContentViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentViewTests.swift; sourceTree = "<group>"; };
C55B316D2C2885A300542356 /* Llvm */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = Llvm; sourceTree = "<group>"; };
C58379C82C26A3440068D178 /* SwiftExplorer.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwiftExplorer.app; sourceTree = BUILT_PRODUCTS_DIR; };
C58379CB2C26A3440068D178 /* SwiftExplorerApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftExplorerApp.swift; sourceTree = "<group>"; };
C58379CD2C26A3440068D178 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
Expand All @@ -57,8 +57,8 @@
buildActionMask = 2147483647;
files = (
C519A74F2C26ED6B00BF91E3 /* Theme in Frameworks */,
C55B316F2C28862000542356 /* Llvm in Frameworks */,
C58379FB2C26B43B0068D178 /* CodeEditor in Frameworks */,
C512A2A62C28D54F00175F0B /* Lowlevel in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -141,7 +141,7 @@
C58379BF2C26A3440068D178 = {
isa = PBXGroup;
children = (
C55B316D2C2885A300542356 /* Llvm */,
C512A2A42C28D4E300175F0B /* Lowlevel */,
C519A7592C26F5EE00BF91E3 /* CommonTest */,
C519A74D2C26ED3500BF91E3 /* Theme */,
C58379CA2C26A3440068D178 /* SwiftExplorer */,
Expand Down Expand Up @@ -218,7 +218,7 @@
packageProductDependencies = (
C58379FA2C26B43B0068D178 /* CodeEditor */,
C519A74E2C26ED6B00BF91E3 /* Theme */,
C55B316E2C28862000542356 /* Llvm */,
C512A2A52C28D54F00175F0B /* Lowlevel */,
);
productName = SwiftExplorer;
productReference = C58379C82C26A3440068D178 /* SwiftExplorer.app */;
Expand Down Expand Up @@ -611,6 +611,10 @@
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
C512A2A52C28D54F00175F0B /* Lowlevel */ = {
isa = XCSwiftPackageProductDependency;
productName = Lowlevel;
};
C519A74E2C26ED6B00BF91E3 /* Theme */ = {
isa = XCSwiftPackageProductDependency;
productName = Theme;
Expand All @@ -624,10 +628,6 @@
package = C519A7602C27042000BF91E3 /* XCRemoteSwiftPackageReference "swift-snapshot-testing" */;
productName = SnapshotTesting;
};
C55B316E2C28862000542356 /* Llvm */ = {
isa = XCSwiftPackageProductDependency;
productName = Llvm;
};
C58379FA2C26B43B0068D178 /* CodeEditor */ = {
isa = XCSwiftPackageProductDependency;
package = C58379F82C26B4110068D178 /* XCRemoteSwiftPackageReference "CodeEditor" */;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "LlvmTests"
BuildableName = "LlvmTests"
BlueprintName = "LlvmTests"
ReferencedContainer = "container:Llvm">
BlueprintIdentifier = "LowlevelTests"
BuildableName = "LowlevelTests"
BlueprintName = "LowlevelTests"
ReferencedContainer = "container:Lowlevel">
</BuildableReference>
</TestableReference>
</Testables>
Expand Down
Loading