#!/usr/bin/python

import sys, re


header = """/*
 *               Copyright (C) 2005, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  and  are protected by Federal copyright law. They may not be disclosed
 *  to  third  parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 */

/*
 * This file was automatically generated.  Do not modify!
 */

.data
.global SWI_jumptable

SWI_jumptable:
"""
middle = """
.global SWI_argtable

SWI_argtable:
"""

footer = """
    .end
"""

def parse_syscalls(name):
  f = open(name)
  calls = []
  for l in f.readlines():
    l = l.strip()
    if l != "":
      result = re.match('([a-zA-Z0-9_]*\s*\**)([^(]*)\((.*)\)', l)
      ret = result.group(1)
      func = result.group(2)
      params = []
      for p in result.group(3).split(','):
        result = re.match('([a-zA-Z0-9_]*\s*\**)(.*)', p.strip())
        params.append((result.group(1), result.group(2)))
      calls.append((ret, func, params));
  return calls


def numArgs(n):
  if n > 4:
    return n - 4
  else:
    return 0

if __name__ == "__main__":
  out = open(sys.argv[-1], "w")
  out.write(header)
  calls = []
  for f in sys.argv[1:-1]:
    calls = calls + parse_syscalls(f)
  for call in calls:
    out.write("    .word ios%s\n" % call[1])
  out.write(middle)
  for call in calls:
    out.write("    .word %d /* ios%s */\n" % (numArgs(len(call[2])), call[1]))
  out.write(footer)
  out.close()

