#!/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 automatically generated -- Do not edit */

#include <sc/swi.h>

#if defined(FAST_SEMIHOSTING)
#define SWI(x) .word 0xe6000010 | ((x)<<5) 
#else
#define SWI(x) swi (x)
#endif

#define _syscall0(t,name)               \\
    .global %(FILE)s_##name ;                \\
    .type   %(FILE)s_##name,"function";      \\
%(FILE)s_##name: ;                           \\
        SWI(SWI_##name);                \\
        bx  lr;                         \\
    .size   %(FILE)s_##name,.-%(FILE)s_##name;
 
#define _syscall1 _syscall0
#define _syscall2 _syscall0
#define _syscall3 _syscall0
#define _syscall4 _syscall0
#define _syscall5 _syscall0
#define _syscall6 _syscall0
#define _syscall7 _syscall0
#define _syscall8 _syscall0
#define _syscall9 _syscall0
#define _syscall10 _syscall0
"""

footer = """
"""

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

if __name__ == "__main__":
  prefix = sys.argv[1]
  calls = parse_syscalls(sys.argv[2])
  out = open(sys.argv[3], "w")
  out.write(header % {"FILE":prefix})
  #i = 0
  for call in calls:
    s = "_syscall%d(%s, %s);" % (len(call[2]), call[0], call[1])
    out.write(s + '\n')
  out.write(footer)
  out.close()
