summaryrefslogtreecommitdiff
path: root/src/vm/h2inc.ps1
blob: f9729bcffbe0021f0bb4ccf901132896bdcf6753 (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# ==++==
# 
#  Copyright (c) Microsoft. All rights reserved.
#  Licensed under the MIT license. See LICENSE file in the project root for full license information. 
# 
# ==--==

# C to MASM include file translator
# This is replacement for the deprecated h2inc tool that used to be part of VS.

#
# The use of [console]::WriteLine (instead of Write-Output) is intentional.
# PowerShell 2.0 (installed by default on Windows 7) wraps lines written with
# Write-Output at whatever column width is being used by the current terminal,
# even when output is being redirected to a file. We can't have this behavior
# because it will cause the generated file to be malformed.
#

Function ProcessFile($filePath) {

    [console]::WriteLine("// File start: $filePath")

    Get-Content $filePath | ForEach-Object {
        
        if ($_ -match "^\s*#\spragma") {
            # Ignore pragmas
            return
        }
        
        if ($_ -match "^\s*#\s*include\s*`"(.*)`"")
        {
            # Expand includes.
            ProcessFile(Join-Path (Split-Path -Parent $filePath) $Matches[1])
            return
        }
        
        if ($_ -match "^\s*#define\s+(\S+)\s*(.*)")
        {
            # Augment #defines with their MASM equivalent
            $name = $Matches[1]
            $value = $Matches[2]

            # Note that we do not handle multiline constants
            
            # Strip comments from value
            $value = $value -replace "//.*", ""
            $value = $value -replace "/\*.*\*/", ""

            # Strip whitespaces from value
            $value = $value -replace "\s+$", ""

            # ignore #defines with arguments
            if ($name -notmatch "\(") {
                $HEX_NUMBER_PATTERN = "\b0x(\w+)\b"
                $DECIMAL_NUMBER_PATTERN = "(-?\b\d+\b)"
                       
                if ($value -match $HEX_NUMBER_PATTERN -or $value -match $DECIMAL_NUMBER_PATTERN) {
                    $value = $value -replace $HEX_NUMBER_PATTERN, "0`$1h"    # Convert hex constants
                    $value = $value -replace $DECIMAL_NUMBER_PATTERN, "`$1t" # Convert dec constants
                    [console]::WriteLine("$name EQU $value")
                } else {
                    [console]::WriteLine("$name TEXTEQU <$value>")
                }
            }            
        }
        
        [console]::WriteLine("$_")
    }

    [console]::WriteLine("// File end: $filePath")
}

ProcessFile $args[0]