-
Notifications
You must be signed in to change notification settings - Fork 1
/
vm.ps1
59 lines (48 loc) · 1.22 KB
/
vm.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# change your input and output file path HERE
$inputFilePath = "path/to/SecureEngineCustomVMs_GNU_inline.h"
$outputFilePath = "src/vm.rs"
$inputLines = Get-Content $inputFilePath
$outputLines = @()
$macroName = $null
$assemblyCode = @()
$inAssemblyBlock = $false
$definedMacros = @{}
foreach ($line in $inputLines) {
if ($line -match "//" -or $line -match "/\*") {
continue
}
if ($line -match "#define\s+(VM_\w+)\s+\\") {
$macroName = $matches[1].ToLower()
}
if ($null -ne $macroName) {
if ($line -match "__asm__\s+\((.*)") {
$inAssemblyBlock = $true
}
if ($inAssemblyBlock) {
$cleanedLine = $line -replace '__asm__ \(', '' -replace '\\\"', '`"' -replace '\\\\', '\\' -replace '\\$', ','
$assemblyCode += $cleanedLine
}
if ($line -match "\);") {
$inAssemblyBlock = $false
if (-not $definedMacros.ContainsKey($macroName)) {
$rustMacro = @"
#[macro_export]
macro_rules! $macroName {
() => {
unsafe {
asm!(
$assemblyCode
}
};
}
"@
$outputLines += $rustMacro
$definedMacros[$macroName] = $true
}
$macroName = $null
$assemblyCode = @()
}
}
}
$assemblyCode = $assemblyCode -join "`n "
$outputLines | Out-File $outputFilePath -Encoding utf8