/*
 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,
 *               2018, 2019, 2020
 *   Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#import "OFArray.h"
#import "OFXMLAttribute.h"

#import "GlueGenerator.h"

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

#import "copyright.h"

@implementation GlueGenerator
- (instancetype)initWithLibrary: (OFXMLElement *)library
		   outputStream: (OFStream *)outputStream
{
	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];
		_outputStream = [outputStream retain];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

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

	[super dealloc];
}

- (void)generate
{
	[_outputStream writeString: COPYRIGHT];
	[_outputStream writeString:
	    @"/* This file is automatically generated from library.xml */\n"
	    @"\n"
	    @"#include \"config.h\"\n"
	    @"\n"];

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

	[_outputStream 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"
	    @"#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"
	    @"    \"\tlwz\t%r13, 44(%r12)\\n\"\n"
	    @"    \"\tblr\\n\"\n"
	    @");\n"
	    @"#endif\n"];

	for (OFXMLElement *function in
	    [_library elementsForName: @"function"]) {
		OFString *name =
		    [function attributeForName: @"name"].stringValue;
		OFString *returnType =
		    [function attributeForName: @"return-type"].stringValue;
		OFArray OF_GENERIC(OFXMLElement *) *arguments =
		    [function elementsForName: @"argument"];
		size_t argumentIndex;

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

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

		if (arguments.count > 0)
			[_outputStream writeString: @" PPC_PARAMS("];
		else
			[_outputStream 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];
		}

		[_outputStream 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];
		}

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

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

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

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

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

			[_outputStream writeString: argName];
		}

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