Source

gltut / glload / codegen / _MakeMainSourceFile.lua

  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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
--[[
The function MakeMainSourceFile will generate the main source file. It will generate the definitions declared in the header.

It takes the parameters that MakeMainHeaderFile does, with the addition of enumPrefix, preceedData, and coreData.

preceedData is a list of strings to write after the initial includes.

The coreData can be nil or a table containing the following:

* file: the filename (no .h) of the core data header to load.

If there is no coreData, then the core loading functions will not try to get data from the core stuff.
]]

require "_makeHelpers"
require "_util"

-- Bad form, I know, but necessary.
local glob_HasCore = nil;

local function GetRemovedVersionList(specData)
	local depTable = {};
	
	for i, func in ipairs(specData.funcData.functions) do
		if(func.deprecated) then
			depTable[func.deprecated] = true;
		end
	end
	
	local ret = {};
	
	for version, bool in pairs(depTable) do
		table.insert(ret, version);
	end
	
	table.sort(ret, function(lhs, rhs) return tonumber(lhs) < tonumber(rhs) end);
	
	return ret;
end

local function WriteTestFunc(hFile)
	hFile:write([[
#include <stddef.h>	
	
static int TestPointer(const void *pTest)
{
	ptrdiff_t iTest;
	if(!pTest) return 0;
	iTest = (ptrdiff_t)pTest;
	
	if(iTest == 1 || iTest == 2 || iTest == 3 || iTest == -1) return 0;
	
	return 1;
}

]]);
end

local function GetExtLoaderName(extName, funcPrefix)
	return string.format("%seIntLoad_%s", funcPrefix, extName);
end

local function WriteExtVarClear(hFile, specData, funcPrefix)
	WriteFormatted(hFile, "void %s()\n", Make.GetClearFunctionName(funcPrefix));
	hFile:write("{\n");
	
	for i, extName in ipairs(specData.extensions) do
		WriteFormatted(hFile, "\t%s = 0;\n",
			Make.GetExtensionVarName(extName, funcPrefix));
	end
	
	hFile:write("}\n");
end

local function WriteFuncPointers(hFile, ext, extName, specData, enumPrefix, funcPrefix)
	if(#ext.funcs == 0) then return; end;
	
    local extDefine = string.format("%s_%s", enumPrefix, extName);
    hFile:write(string.format("#ifndef %s\n", extDefine));

	--Write the typedefs.
	for i, func in ipairs(ext.funcs) do
		hFile:write(Make.GetFuncTypedef(func, funcPrefix, specData.typemap));
		hFile:write("\n");
	end
	
	hFile:write("\n");
	
	--Write the function pointers.
	for i, func in ipairs(ext.funcs) do
		if(specData.coreexts[extName]) then
			hFile:write(Make.GetCoreFuncPtr(func, funcPrefix, specData.typemap));
		else
			hFile:write(Make.GetFuncPtr(func, funcPrefix, specData.typemap));
		end
		hFile:write("\n");
	end

    hFile:write(string.format("#endif /*%s*/\n", extDefine));
end

local function WriteFuncLoad(hFile, func, funcPrefix, bIsCoreExt)
	local funcPtrName = nil;
	
	if(bIsCoreExt) then
		funcPtrName = Make.GetCoreFuncPtrNameStr(func, funcPrefix);
	else
		funcPtrName = Make.GetFuncPtrNameStr(func, funcPrefix);
	end
	
	WriteForm(hFile, "\t%s = (%s)gleIntGetProcAddress(\"%s\");\n",
		funcPtrName,
		Make.GetFuncTypedefNameStr(func, funcPrefix),
		Make.GetFuncPtrNameStr(func, funcPrefix)
	);
	
	WriteForm(hFile, "\tif(!TestPointer((const void*)%s)) bIsLoaded = 0;\n", funcPtrName);
end

local function WriteLoaderFunc(hFile, ext, extName, specData, funcPrefix, enumPrefix)
	if(not ext.funcs or #ext.funcs == 0) then return; end;
	
	WriteForm(hFile, "static int %s()\n", GetExtLoaderName(extName, funcPrefix));
	hFile:write("{\n");
	
	hFile:write("\tint bIsLoaded = 1;\n");

	local bIsCoreExt = specData.coreexts[extName] ~= nil;
	
    local extDefine = string.format("%s_%s", enumPrefix, extName);
    hFile:write(string.format("#ifndef %s\n", extDefine));
	
	for i, func in ipairs(ext.funcs) do
		WriteFuncLoad(hFile, func, funcPrefix, bIsCoreExt)
	end
	
    hFile:write(string.format("#endif /*%s*/\n", extDefine));

	hFile:write("\treturn bIsLoaded;\n");
	
	hFile:write("}\n");
end

local function WriteMappingTable(hFile, specData, enumPrefix, funcPrefix)
	WriteForm(hFile, "StrToExtMap %s[] = {\n", Make.GetMapTableName(funcPrefix));
	
	local iNumEntries = 0;
	
	for extName, ext in sortPairs(specData.extdefs, CompLess) do
		local extLoadFuncName = "NULL";
		
		if(ext.funcs and #ext.funcs ~= 0) then
			extLoadFuncName = GetExtLoaderName(extName, funcPrefix);
		end
		
		WriteForm(hFile, "\t{\"%s_%s\", &%s, %s},\n",
			enumPrefix,
			extName,
			Make.GetExtensionVarName(extName, funcPrefix),
			extLoadFuncName
		);
		
		iNumEntries = iNumEntries + 1;
	end
	
	hFile:write("};\n\n");
	
	--Write the variable that stores the size of the mapping table.
	hFile:write(Make.GetMapTableSizeDef(iNumEntries, funcPrefix));
	hFile:write("\n");
end

local function GetStaticCoreRemovedSuffix(removedVersion)
	if(removedVersion) then
		return string.gsub(removedVersion, "(%d+)%.(%d+)", "_Version_%1_%2");
	else
		return "_Base";
	end
end

local function GetStaticCoreVersionSuffix(version)
	return string.gsub(version, "(%d+)%.(%d+)", "_%1_%2");
end

local function GetStaticCoreName(version, funcPrefix, removedVersion)
	return string.format("%seIntLoadCore%s%s", funcPrefix,
		GetStaticCoreVersionSuffix(version),
		GetStaticCoreRemovedSuffix(removedVersion));
end


local bHasCoreStruct = false;

local function WriteCoreFuncLoad(hFile, func, funcPrefix)
	local funcPtrName = nil;
	
	funcPtrName = Make.GetCoreFuncPtrNameStr(func, funcPrefix);
	
	WriteForm(hFile, "\t%s = (%s)gleIntGetProcAddress(\"%s\");\n",
		funcPtrName,
		Make.GetFuncTypedefNameStr(func, funcPrefix),
		Make.GetFuncPtrNameStr(func, funcPrefix)
	);
	
	WriteForm(hFile, "\tif(!TestPointer((const void*)%s))\n", funcPtrName);
	hFile:write("\t{\n");
	WriteForm(hFile, "\t\t%s = (%s)%s.%s;\n", funcPtrName,
		Make.GetFuncTypedefNameStr(func, funcPrefix),
		Make.GetCoreStructVarName(funcPrefix),
		Make.GetCoreStructMemberName(func, funcPrefix));
	WriteForm(hFile, "\t\tif(!%s) bIsLoaded = 0;\n", funcPtrName);
	hFile:write("\t}\n");
end

--If there is a nextVersion, then we should write functions
--that were deprecated <= that version. If there is no next
--version, then we should contain functions that are *not* deprecated ever.
local function CanWriteFunc(func, nextVersion)
	if(nextVersion) then
		--if(thingy) then print(func.name); end;
		if(func.deprecated and tonumber(func.deprecated) <= tonumber(nextVersion)) then
			return true;
		end
	else
		if(not func.deprecated) then
			return true;
		end
	end
	
	return false;
end

local function WriteStaticCoreLoaders(hFile, specData, enumPrefix, funcPrefix, removedVersion,
										nextVersion)
	for coreVersion, core in sortPairs(specData.coredefs, CompLess) do
		if(not removedVersion or (tonumber(removedVersion) > tonumber(coreVersion))) then
			WriteForm(hFile, "static int %s()\n",
				GetStaticCoreName(coreVersion, funcPrefix, removedVersion));
			hFile:write("{\n");
			
			local iVersion = tonumber(coreVersion);
			
			local theNextVersion = nil;
			local initValue = "1";
			if(nextVersion and iVersion < tonumber(nextVersion)) then
				initValue = GetStaticCoreName(coreVersion, funcPrefix, nextVersion) .. "()";
				theNextVersion = nextVersion;
			end

			WriteForm(hFile, "\tint bIsLoaded = %s;\n", initValue);

			local iRemovedVersion = nil;
			if(removedVersion) then iRemovedVersion = tonumber(removedVersion); end;
		
			if(not iRemovedVersion or (tonumber(coreVersion) <= iRemovedVersion)) then
				for i, func in ipairs(core.funcs) do
					if(CanWriteFunc(func, theNextVersion)) then
						WriteCoreFuncLoad(hFile, func, funcPrefix);
					end
				end
			end
			
			for extName, coreext in pairs(specData.coreexts) do
				if(coreVersion == coreext.version and specData.extdefs[extName]) then
					local bDidWrite = false;
					for i, func in ipairs(specData.extdefs[extName].funcs) do
						if(CanWriteFunc(func, theNextVersion)) then
							bDidWrite = true;
							WriteCoreFuncLoad(hFile, func, funcPrefix);
						end
					end
					
					--Set the extension variable for this core extension.
					if(bDidWrite) then
						WriteForm(hFile, "\t%s = 1;\n",
							Make.GetExtensionVarName(extName, funcPrefix));
					end
				end
			end
			
			hFile:write("\treturn bIsLoaded;\n");
			hFile:write("}\n");
			hFile:write("\n");
		end
	end
end

local function WriteCoreLoaderFunc(hFile, specData, funcPrefix, removedVersionList,
									version, profile)
	hFile:write(Make.GetCoreLoaderFuncDef(version, profile, funcPrefix));
	hFile:write("\n");
	hFile:write("{\n");
	
	hFile:write("\tint bIsLoaded = 1;\n");
	
	WriteForm(hFile, "\t%s();\n", Make.GetClearFunctionName(funcPrefix));
	WriteForm(hFile, "\t%s();\n", Make.GetCoreInitFuncName(funcPrefix));
	
	local lastVersion = removedVersionList[#removedVersionList];
	
	local iVersion = tonumber(version);
	for coreVersion, core in sortPairs(specData.coredefs, CompLess) do
		if(tonumber(coreVersion) <= iVersion) then
			local removedVersion = nil;
			if(profile == "core" and iVersion > 3.0 and tonumber(coreVersion) < tonumber(lastVersion)) then
				removedVersion = lastVersion;
			end
			
			WriteForm(hFile, "\tif(!%s()) bIsLoaded = 0;\n",
				GetStaticCoreName(coreVersion, funcPrefix, removedVersion));
		end
	end

	hFile:write("\n");
	hFile:write("\treturn bIsLoaded;\n");
	hFile:write("}\n");
end

local function WriteCoreFuncPtrs(hFile, specData, enumPrefix, funcPrefix)
	for coreVersion, core in sortPairs(specData.coredefs, CompLess) do
		for i, func in ipairs(core.funcs) do
			hFile:write(Make.GetFuncTypedef(func, funcPrefix, specData.typemap));
			hFile:write("\n");
			hFile:write(Make.GetCoreFuncPtr(func, funcPrefix, specData.typemap));
			hFile:write("\n");
		end
	end
	
	hFile:write("\n");
end

local function WriteBaseLoaders(hFile, specData, funcPrefix, baseData)
	WriteForm(hFile, "void %s()\n", Make.GetBaseLoaderFuncName(funcPrefix));
	hFile:write("{\n");
	hFile:write("\tint bIsLoaded = 1; //Unimportant here.\n");
	
	if(bHasCoreStruct) then
		WriteForm(hFile, "\t%s();\n", Make.GetCoreInitFuncName(funcPrefix));
	end

	for i, funcName in ipairs(baseData.funcs) do
		--find the function.
		local func = nil;
		for i, funcTest in ipairs(specData.funcData.functions) do
			if(funcTest.name == funcName) then
				func = funcTest;
				break;
			end
		end
		
		if(func) then
			if(baseData.bFuncsAreCore) then
				WriteCoreFuncLoad(hFile, func, funcPrefix);
			else
				WriteFuncLoad(hFile, func, funcPrefix, false);
			end
		end
	end

	hFile:write("}\n");
end


function MakeMainSourceFile(outFilename, specData, enumPrefix, funcPrefix, VersionFunc,
							preceedData, baseData, coreData)
	if(coreData) then bHasCoreStruct = true; else bHasCoreStruct = false; end;
							
	local hFile = io.open(GetSourcePath() .. outFilename .. ".c", "w");
	if(not hFile) then
		print("Could not open the output file\"" .. GetSourcePath() .. outFilename .. ".c\".\n");
		return;
	end
	
	hFile:write([[#include <stdlib.h>
#include <string.h>
]]);
	WriteFormatted(hFile, "#include \"%s.h\"\n", outFilename);
	if(coreData) then
		WriteFormatted(hFile, "#include \"%s.h\"\n", coreData.file);
	end
	
	hFile:write("\n");
	
	--Write the preceeding data.
	if(preceedData) then
		for i, preceed in ipairs(preceedData) do
			hFile:write(preceed);
			hFile:write("\n");
		end
	end
	
	--Write a function to test function pointers.
	WriteTestFunc(hFile);
	
	--Write the passthru data.
	for i, passthru in ipairs(specData.funcData.passthru) do
		hFile:write(passthru);
		hFile:write("\n");
	end
	hFile:write("\n");

	hFile:write(dofile(GetDataFilePath() .. "getProcAddress.lua"));
	hFile:write("\n");
	
	--Write the extension variables.
	for i, extName in ipairs(specData.extensions) do
		hFile:write(Make.GetExtensionVar(extName, funcPrefix));
		hFile:write("\n");
	end

	hFile:write("\n\n");

	--Write the function that clears the extension variables.
	WriteExtVarClear(hFile, specData, funcPrefix);
	hFile:write("\n\n");
	
	--Write function pointers and the loading function for each extension.
	for extName, ext in sortPairs(specData.extdefs, CompLess) do
		WriteFuncPointers(hFile, ext, extName, specData, enumPrefix, funcPrefix);
		hFile:write("\n");
		WriteLoaderFunc(hFile, ext, extName, specData, funcPrefix, enumPrefix);
	end
	
	--Write the mapping table definition.
	WriteMappingTable(hFile, specData, enumPrefix, funcPrefix);
	
	hFile:write("\n");
	
	--Write the function pointer definitions for all core versions, whether removed or not.
	WriteCoreFuncPtrs(hFile, specData, enumPrefix, funcPrefix);
	
	--Write the core function loaders.
	if(VersionFunc) then
		local removedVersionList = GetRemovedVersionList(specData);
		
		for i, version in ripairs(removedVersionList) do
			WriteStaticCoreLoaders(hFile, specData, enumPrefix, funcPrefix, version,
				removedVersionList[i+1]);
		end

		WriteStaticCoreLoaders(hFile, specData, enumPrefix, funcPrefix, nil, removedVersionList[1]);

		--For each version/profile combo, write loaders for them.
		for version, profile in VersionFunc do
			WriteCoreLoaderFunc(hFile, specData, funcPrefix, removedVersionList, version, profile);
			hFile:write("\n");
		end
	end

	hFile:write("\n");
	
	--Write the base loaders, if any.
	if(baseData) then
		WriteBaseLoaders(hFile, specData, funcPrefix, baseData);
	end
	

	hFile:close();
end