import path from 'path';
import { BuildBaseFileInputOutputModel, LLVM_ARCHITECTURE_CONTENT, } from '../shared/index.js';
import { exec } from './process.js';
import { transform_input_output_file } from './base.js';
export const CALL_CODE = {};
export const FIND_CODE = (input) => {
    const i = input.input.file.path
        ? path.extname(input.input.file.path).replace(/^\./, '')
        : input.input.file.format
            ? input.input.file.format
            : undefined;
    const o = input.output.file.path
        ? path.extname(input.output.file.path).replace(/^\./, '')
        : input.output.file.format
            ? input.output.file.format
            : undefined;
    return CALL_CODE[`/${i}/format`];
};
function define(lang, build, run) {
    const IO = BuildBaseFileInputOutputModel.superRefine(transform_input_output_file(lang, lang));
    CALL_CODE[String(`/${lang}/format`)] = async (source) => {
        const input = IO.parse(source);
        const cmd = build(input);
        await run(cmd);
    };
}
// define('kotlin', buildCommandToFormatKotlin, runFormatKotlinCommand)
// define('swift', buildCommandToFormatSwift, runFormatSwiftCommand)
// define('rust', buildCommandToFormatRust, runFormatRustCommand)
// define('python', buildCommandToFormatPython, runFormatPythonCommand)
// define('ruby', buildCommandToFormatRuby, runFormatRubyCommand)
// define(
//   'assembly',
//   buildCommandToFormatAssembly,
//   runFormatAssemblyCommand,
// )
// define('c', buildCommandToFormatC, runFormatCCommand)
// define('cpp', buildCommandToFormatCpp, runFormatCCommand)
// define('java', buildCommandToFormatJava, runFormatCCommand)
//  CSharp: .cs
//  Java: .java
//  JavaScript: .mjs .js .ts
//  Json: .json
//  Objective-C: .m .mm
//  Proto: .proto .protodevel
//  TableGen: .td
//  TextProto: .textpb .pb.txt .textproto .asciipb
//  Verilog: .sv .svh .v .vh
export async function generateAssemblyFromLlvmIr({ inputPath, outputPath, syntax = 'intel', architecture = 'x86_64', }) {
    const architectureKey = LLVM_ARCHITECTURE_CONTENT[architecture].host;
    return [
        `llc`,
        `--x86-asm-syntax=${syntax}`,
        `-march=${architectureKey}`,
        `-o`,
        `"${outputPath}"`,
        `"${inputPath}"`,
    ];
}
// cargo rustc --release -- --emit asm
// rustc --emit=asm
// rustc --emit=llvm-ir
// cargo rustc -- --emit=llvm-ir
// objdump -S --x86-asm-syntax=intel example.out
// objdump -f assert
// objdump -x assert
// file magic numbers: https://gist.github.com/Qti3e/6341245314bf3513abb080677cd1c93b
// https://github.com/jgm/pandoc/tree/main/test
// https://github.com/SheetJS/test_files
// objdump disassembly
export async function runFormatKotlinCommand(cmd) {
    await exec(cmd.join(' '));
}
export async function runFormatSwiftCommand(cmd) {
    await exec(cmd.join(' '));
}
export async function runFormatRustCommand(cmd) {
    await exec(cmd.join(' '));
}
export async function runFormatPythonCommand(cmd) {
    await exec(cmd.join(' '));
}
export async function runFormatRubyCommand(cmd) {
    await exec(cmd.join(' '));
}
export async function runFormatAssemblyCommand(cmd) {
    await exec(cmd.join(' '));
}
export async function runFormatCCommand(cmd) {
    await exec(cmd.join(' '));
}
export async function runSwiftCommand(command) {
    return await exec(command.join(' '));
}
export async function runClangCommand(command) {
    return await exec(command.join(' '));
}
//# sourceMappingURL=code.js.map