ObjFW  Diff

Differences From Artifact [a45c4e65ce]:

To Artifact [be3132a03b]:


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
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







-
+
+


















+
-
+











+
-
+






+
-
-
+
+
+
+
+
+
+



+
+



-
+
-

-
+










-
+
+







#import "OFInvalidFormatException.h"
#import "OFUnsupportedVersionException.h"

#import "copyright.h"

@implementation GlueGenerator
- (instancetype)initWithLibrary: (OFXMLElement *)library
		   outputStream: (OFStream *)outputStream
			 header: (OFStream *)header
		 implementation: (OFStream *)impl
{
	self = [super init];

	@try {
		OFXMLAttribute *version;

		if (![library.name isEqual: @"amiga-library"] ||
		    library.namespace != nil)
			@throw [OFInvalidFormatException exception];

		if ((version = [library attributeForName: @"version"]) == nil)
			@throw [OFInvalidFormatException exception];

		if (![version.stringValue isEqual: @"1.0"])
			@throw [OFUnsupportedVersionException
			    exceptionWithVersion: version.stringValue];

		_library = [library retain];
		_header = [header retain];
		_outputStream = [outputStream retain];
		_impl = [impl retain];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[_library release];
	[_header release];
	[_outputStream release];
	[_impl release];

	[super dealloc];
}

- (void)generate
{
	[_header writeString: COPYRIGHT];
	[_outputStream writeString: COPYRIGHT];
	[_outputStream writeString:
	[_impl writeString: COPYRIGHT];

	[_header writeString:
	    @"/* This file is automatically generated from library.xml */\n"
	    @"\n"];

	[_impl writeString:
	    @"/* This file is automatically generated from library.xml */\n"
	    @"\n"
	    @"#include \"config.h\"\n"
	    @"\n"
	    @"#import \"amiga-glue.h\"\n"
	    @"\n"];

	for (OFXMLElement *include in [_library elementsForName: @"include"])
		[_outputStream writeFormat: @"#import \"%@\"\n",
		[_header writeFormat: @"#import \"%@\"\n", include.stringValue];
					    include.stringValue];

	[_outputStream writeString:
	[_header writeString:
	    @"\n"
	    @"#ifdef OF_AMIGAOS_M68K\n"
	    @"# define PPC_PARAMS(...) (void)\n"
	    @"# define M68K_ARG(type, name, reg)\t\t\\\n"
	    @"\tregister type reg##name __asm__(#reg);\t\\\n"
	    @"\ttype name = reg##name;\n"
	    @"#else\n"
	    @"# define PPC_PARAMS(...) (__VA_ARGS__)\n"
	    @"# define M68K_ARG(...)\n"
	    @"#endif\n"
	    @"\n"
	    @"\n"];
	[_impl writeString:
	    @"#ifdef OF_MORPHOS\n"
	    @"/* All __saveds functions in this file need to use the SysV "
	    @"ABI */\n"
	    @"__asm__ (\n"
	    @"    \".section .text\\n\"\n"
	    @"    \".align 2\\n\"\n"
	    @"    \"__restore_r13:\\n\"\n"
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

159
160
161

162
163

164
165

166
167
168
169
170
171
172
173

174
175

176
177
178

179
180
181
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
159
160
161
162
163
164
165
166
167
168
169
170
171

172
173
174
175
176
177
178
179
180


181
182
183
184
185

186
187
188

189
190

191
192

193
194
195
196
197
198
199
200

201
202

203
204
205

206
207
208
209







+
+
+
+
+
+
-
-
-
-
+
+
+
+

-
-
-
-
+
+
+
+
+
+
+








-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+


+
+
-
+








-
-
+
+



-
+


-
+

-
+

-
+







-
+

-
+


-
+



		OFArray OF_GENERIC(OFXMLElement *) *arguments =
		    [function elementsForName: @"argument"];
		size_t argumentIndex;

		if (returnType == nil)
			returnType = @"void";

		[_header writeFormat:
		    @"extern %@%@glue_%@",
		    returnType,
		    (![returnType hasSuffix: @"*"] ? @" " : @""),
		    name];

		[_outputStream writeFormat: @"\n"
					    @"%@ __saveds\n"
					    @"glue_%@",
					    returnType, name];
		[_impl writeFormat: @"\n"
				    @"%@ __saveds\n"
				    @"glue_%@",
				    returnType, name];

		if (arguments.count > 0)
			[_outputStream writeString: @" PPC_PARAMS("];
		else
			[_outputStream writeString: @"(void"];
		if (arguments.count > 0) {
			[_header writeString: @" PPC_PARAMS("];
			[_impl writeString: @" PPC_PARAMS("];
		} else {
			[_header writeString: @"(void"];
			[_impl writeString: @"(void"];
		}

		argumentIndex = 0;
		for (OFXMLElement *argument in arguments) {
			OFString *argName =
			    [argument attributeForName: @"name"].stringValue;
			OFString *argType =
			    [argument attributeForName: @"type"].stringValue;

			if (argumentIndex++ > 0)
				[_outputStream writeString: @", "];

			[_outputStream writeString: argType];
			if (![argType hasSuffix: @"*"])
				[_outputStream writeString: @" "];
			[_outputStream writeString: argName];
			if (argumentIndex++ > 0) {
				[_header writeString: @", "];
				[_impl writeString: @", "];
			}

			[_header writeString: argType];
			[_impl writeString: argType];
			if (![argType hasSuffix: @"*"]) {
				[_header writeString: @" "];
				[_impl writeString: @" "];
			}
			[_header writeString: argName];
			[_impl writeString: argName];
		}

		[_header writeString: @");\n"];

		[_outputStream writeString: @")\n{\n"];
		[_impl writeString: @")\n{\n"];
		for (OFXMLElement *argument in arguments) {
			OFString *argName =
			    [argument attributeForName: @"name"].stringValue;
			OFString *argType =
			    [argument attributeForName: @"type"].stringValue;
			OFString *m68kReg = [argument
			    attributeForName: @"m68k-reg"].stringValue;

			[_outputStream writeFormat: @"\tM68K_ARG(%@, %@, %@)\n",
						    argType, argName, m68kReg];
			[_impl writeFormat: @"\tM68K_ARG(%@, %@, %@)\n",
					    argType, argName, m68kReg];
		}

		if (arguments.count > 0)
			[_outputStream writeString: @"\n"];
			[_impl writeString: @"\n"];

		if (![returnType isEqual: @"void"])
			[_outputStream writeString: @"\treturn "];
			[_impl writeString: @"\treturn "];
		else
			[_outputStream writeString: @"\t"];
			[_impl writeString: @"\t"];

		[_outputStream writeFormat: @"%@(", name];
		[_impl writeFormat: @"%@(", name];

		argumentIndex = 0;
		for (OFXMLElement *argument in arguments) {
			OFString *argName =
			    [argument attributeForName: @"name"].stringValue;

			if (argumentIndex++ > 0)
				[_outputStream writeString: @", "];
				[_impl writeString: @", "];

			[_outputStream writeString: argName];
			[_impl writeString: argName];
		}

		[_outputStream writeString: @");\n}\n"];
		[_impl writeString: @");\n}\n"];
	}
}
@end