#!/usr/bin/env python
#
# generate_asm.py
#
# This source file is part of the FoundationDB open source project
#
# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


import re
import sys

(platform, source, asm, h) = sys.argv[1:]

functions = {}

func_re = re.compile("^\s*FDB_API_(?:CHANGED|REMOVED)\s*\(\s*([^,]*),\s*([^)]*)\).*")

with open(source, 'r') as srcfile:
    for l in srcfile:
        m = func_re.match(l)
        if m:
            func, ver = m.groups()
            if not func in functions:
                functions[func] = []
            ver = int(ver)
            if not ver in functions[func]:
                functions[func].append(ver)

def write_windows_asm(asmfile, functions):
    asmfile.write(".data\n")
    for f in functions:
        asmfile.write("\textern fdb_api_ptr_%s:qword\n" % f)

    asmfile.write("\n.code\n")

    for f in functions:
        asmfile.write("\n%s proc EXPORT\n" % f)
        asmfile.write("\tmov r11, qword ptr [fdb_api_ptr_%s]\n" % f)
        asmfile.write("\tjmp r11\n")
        asmfile.write("%s endp\n" % f)

    asmfile.write("\nEND\n")

def write_unix_asm(asmfile, functions, prefix):
    asmfile.write(".intel_syntax noprefix\n")

    if platform == "linux":
        asmfile.write("\n.data\n")
        for f in functions:
            asmfile.write("\t.extern fdb_api_ptr_%s\n" % f)

        asmfile.write("\n.text\n")
        for f in functions:
            asmfile.write("\t.global %s\n\t.type %s, @function\n" % (f, f))

    for f in functions:
        asmfile.write("\n.globl %s%s\n" % (prefix, f))
        asmfile.write("%s%s:\n" % (prefix, f))
        asmfile.write("\tmov r11, qword ptr [%sfdb_api_ptr_%s@GOTPCREL+rip]\n" % (prefix, f))
        asmfile.write("\tmov r11, qword ptr [r11]\n")
        asmfile.write("\tjmp r11\n")

with open(asm, 'w') as asmfile, open(h, 'w') as hfile:
    hfile.write("void fdb_api_ptr_unimpl() { fprintf(stderr, \"UNIMPLEMENTED FDB API FUNCTION\\n\"); abort(); }\n\n")
    hfile.write("void fdb_api_ptr_removed() { fprintf(stderr, \"REMOVED FDB API FUNCTION\\n\"); abort(); }\n\n")

    if platform == "linux":
        write_unix_asm(asmfile, functions, '')
    elif platform == "osx":
        write_unix_asm(asmfile, functions, '_')
    elif platform == "windows":
        write_windows_asm(asmfile, functions)

    for f in functions:
        if platform == "windows":
            hfile.write("extern \"C\" ")
        hfile.write("void* fdb_api_ptr_%s = (void*)&fdb_api_ptr_unimpl;\n" % f)
        for v in functions[f]:
            hfile.write("#define %s_v%d_PREV %s_v%d\n" % (f, v, f, v-1))
