summaryrefslogtreecommitdiffstats
path: root/src/mac/CFMGlue.pl
blob: f74a3662d3a87e8c8be5b0f2c83035bef46b6b18 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/athena/bin/perl -w

%RESERVEDWORDS = (
		  const  => "const",
		  "*"    => "*",
		  "[]"   => "[]",
		  struct => "struct",
		  enum   => "enum",
		  union  => "union"
		  );

while(<STDIN>)
{
    chop($_);
    $prototype = $_;
    @splitup = split(/\s*\(\s*/, $prototype);
    
    # the return value type and the function name:
    $temp = $splitup[0];
    $temp =~ s/\s*\*\s*/ \* /g;         # add spaces around *
    @funcAndArgs = split(/\s+/, $temp);
    $functionName = $funcAndArgs[$#funcAndArgs];
    
    # Is this function already in the Hash Table?
    if(!exists($FUNCTIONS{$functionName}))
    {
	$FUNCTIONS{$functionName}{prototypeText} = $prototype;
	pop @{funcAndArgs};
	$FUNCTIONS{$functionName}{returnType} = join(' ', @funcAndArgs);
		
	# the arguments:
	@splitup2 = split(/\s*\)\s*/, $splitup[1]);
	@argsAndParams = split(/\s*,\s*/, $splitup2[0]);
		
	for($i = 0, $j = 1; $i <= $#argsAndParams; $i++, $j++)
	{
	    $temp = $argsAndParams[$i];
	    $temp =~ s/\s*\*\s*/ \* /g;         # add spaces around *
	    $temp =~ s/\s*\[\]\s*/ \[\] /g;     # add spaces around []

	    @elements = split(/\s+/, $temp);

            # Is there a parameter name in this argument?
	    $identifierCount = 0;
	    foreach $element (@elements)
	    {
		if(!exists($RESERVEDWORDS{$element})) {
		    $identifierCount++;
		}
	    }
	    
	    if(($identifierCount > 2) or ($identifierCount < 1)) {
		print("************** $argsAndParams ****************");
		die;
	    }

	    if($identifierCount >= 2) {
		$param = $elements[$#elements];
		pop(@elements);
		if($param eq "[]") {
		    $param = $elements[$#elements];
		    pop(@elements);
		    push(@elements, '*');
		}
		$type = join(' ', @elements);
	    } else {
		$type = $argsAndParams[$i];
		$param = "param" . $j;
	    }
	    $FUNCTIONS{$functionName}{typeList}[$i] = $type;
	    $FUNCTIONS{$functionName}{paramList}[$i] = $param;
	}
    }
}

foreach $function (keys(%FUNCTIONS))
{
    # the variables we will be playing with:
    $name      = $function;
    $retType   = $FUNCTIONS{$function}{returnType};
    $prototype = $FUNCTIONS{$function}{prototypeText};
    @args      = @{ $FUNCTIONS{$function}{typeList} };
    @params    = @{ $FUNCTIONS{$function}{paramList} };
    

    # Now Generate the ProcInfo Macro:
    # --------------------------------
    print("/**** $name ****/\n");
    print("/* $prototype */\n\n");
    
    print("enum {\n");
    print("  $name" . "_ProcInfo = kThinkCStackBased\n");
    if($retType ne "void") {
	print("  | RESULT_SIZE(SIZE_CODE(sizeof($retType)))\n");
    }
    for($i = 0, $j = 1; $i <= $#args; $i++, $j++)
    {
	$arg = $args[$i];
	print("  | STACK_ROUTINE_PARAMETER($j, SIZE_CODE(sizeof($arg)))\n");
    }    
    print("};\n\n");
    
    # Now Generate the ProcPtr Typedef
    # --------------------------------
    print("typedef ");
    print("$retType ");
    print("(*$name" . "_ProcPtrType)(");
    
    for($i = 0; $i<=$#args; $i++) {
	    $arg = $args[$i];
	    print("$arg");
	    if ($i ne $#args) {
	    	print (", ");
	    }
    }
    print(");\n");
  
    
    # Now Generate the Static 68K Function Declaration:
    # -------------------------------------------------
    print("$retType $name (\n");
    for($i = 0; $i <= $#args; $i++)
    {
	for($j = 0; $j <= length($retType); $j++) {	
	    print(" ");
	}
	print($args[$i] . ' ' . $params[$i]);
	if($i >= $#args) {
	    print(")\n");
	} else {
	    print(",\n");
	}
    } 
    print("{\n");
    print("  static $name" . "_ProcPtrType $name" . "_ProcPtr = kUnresolvedCFragSymbolAddress;\n\n");

    print("  // if this symbol has not been setup yet...\n");
    print("  if((Ptr) $name" . "_ProcPtr == (Ptr) kUnresolvedCFragSymbolAddress)\n");
    print("    Find_Symbol((Ptr *) &" . $name . "_ProcPtr, ");
    print("\"\\p" . $name . "\", $name" . "_ProcInfo);\n");
    print("  if((Ptr) $name" . "_ProcPtr != (Ptr) kUnresolvedCFragSymbolAddress)\n");
    if($retType ne "void") {
	print("    return $name" . "_ProcPtr(");
    } else {
	print("    $name" . "_ProcPtr(");
    }	    
    for($i = 0; $i <= $#args; $i++)
    {
	print($params[$i]);
	if($i >= $#args) {
	    print(");\n");
	} else {
	    print(", ");
	}
    } 
    
    print("}\n\n\n");
}