Make it easier to add syscalls for another architecture.

Much of the per-architecture duplication can be removed, so let's do so
before we add the 64-bit architectures.

Change-Id: Ieb796503c8e5353ea38c3bab768bb9a690c9a767
diff --git a/libc/tools/gensyscalls.py b/libc/tools/gensyscalls.py
index 4894f2d..ea60eec 100755
--- a/libc/tools/gensyscalls.py
+++ b/libc/tools/gensyscalls.py
@@ -15,9 +15,6 @@
 # temp directory where we store all intermediate files
 bionic_temp = "/tmp/bionic_gensyscalls/"
 
-# all architectures, update as you see fit
-all_archs = [ "arm", "mips", "x86" ]
-
 def make_dir( path ):
     path = os.path.abspath(path)
     if not os.path.exists(path):
@@ -217,10 +214,7 @@
         result += x86_return % t
         return result
 
-    def x86_genstub_cid(self, fname, numparams, idname, cid):
-        # We'll ignore numparams here because in reality, if there is a
-        # dispatch call (like a socketcall syscall) there are actually
-        # only 2 arguments to the syscall and 2 regs we have to save:
+    def x86_genstub_socketcall(self, fname, idname, socketcall_id):
         #   %ebx <--- Argument 1 - The call id of the needed vectored
         #                          syscall (socket, bind, recv, etc)
         #   %ecx <--- Argument 2 - Pointer to the rest of the arguments
@@ -238,7 +232,7 @@
         stack_bias += 4
 
         # set the call id (%ebx)
-        result += "    mov     $%d, %%ebx" % (cid) + "\n"
+        result += "    mov     $%d, %%ebx" % (socketcall_id) + "\n"
 
         # set the pointer to the rest of the args into %ecx
         result += "    mov     %esp, %ecx" + "\n"
@@ -281,23 +275,23 @@
             syscall_func   = t["func"]
             syscall_params = t["params"]
             syscall_name   = t["name"]
+            __NR_name = make__NR_name(t["name"])
 
-            if t["common"] >= 0 or t["armid"] >= 0:
+            if t.has_key("arm"):
                 num_regs = count_arm_param_registers(syscall_params)
-                t["asm-arm"] = self.arm_eabi_genstub(syscall_func, num_regs, make__NR_name(syscall_name))
+                t["asm-arm"] = self.arm_eabi_genstub(syscall_func, num_regs, __NR_name)
 
-            if t["common"] >= 0 or t["x86id"] >= 0:
+            if t.has_key("x86"):
                 num_regs = count_generic_param_registers(syscall_params)
-                if t["cid"] >= 0:
-                    t["asm-x86"] = self.x86_genstub_cid(syscall_func, num_regs, make__NR_name(syscall_name), t["cid"])
+                if t["socketcall_id"] >= 0:
+                    t["asm-x86"] = self.x86_genstub_socketcall(syscall_func, __NR_name, t["socketcall_id"])
                 else:
-                    t["asm-x86"] = self.x86_genstub(syscall_func, num_regs, make__NR_name(syscall_name))
-            elif t["cid"] >= 0:
-                E("cid for dispatch syscalls is only supported for x86 in "
-                  "'%s'" % syscall_name)
+                    t["asm-x86"] = self.x86_genstub(syscall_func, num_regs, __NR_name)
+            elif t["socketcall_id"] >= 0:
+                E("socketcall_id for dispatch syscalls is only supported for x86 in '%s'" % t)
                 return
 
-            if t["common"] >= 0 or t["mipsid"] >= 0:
+            if t.has_key("mips"):
                 t["asm-mips"] = self.mips_genstub(syscall_func, make__NR_name(syscall_name))
 
 
@@ -336,60 +330,39 @@
         self.other_files.append(glibc_syscalls_h_path)
 
 
-    # now dump the contents of syscalls.mk
+    # Write the contents of syscalls.mk.
     def gen_arch_syscalls_mk(self, arch):
         path = "arch-%s/syscalls.mk" % arch
-        D( "generating "+path )
-        fp = create_file( path )
-        fp.write( "# auto-generated by gensyscalls.py, do not touch\n" )
-        fp.write( "syscall_src := \n" )
-        arch_test = {
-            "arm": lambda x: x.has_key("asm-arm"),
-            "x86": lambda x: x.has_key("asm-x86"),
-            "mips": lambda x: x.has_key("asm-mips")
-        }
-
+        D("generating " + path)
+        fp = create_file(path)
+        fp.write("# Auto-generated by gensyscalls.py. Do not edit.\n")
+        fp.write("syscall_src :=\n")
         for sc in self.syscalls:
-            if arch_test[arch](sc):
-                fp.write("syscall_src += arch-%s/syscalls/%s.S\n" %
-                         (arch, sc["func"]))
+            if sc.has_key("asm-%s" % arch):
+                fp.write("syscall_src += arch-%s/syscalls/%s.S\n" % (arch, sc["func"]))
         fp.close()
-        self.other_files.append( path )
+        self.other_files.append(path)
 
 
-    # now generate each syscall stub
+    # Write each syscall stub.
     def gen_syscall_stubs(self):
         for sc in self.syscalls:
-            if sc.has_key("asm-arm") and 'arm' in all_archs:
-                fname = "arch-arm/syscalls/%s.S" % sc["func"]
-                D2( ">>> generating "+fname )
-                fp = create_file( fname )
-                fp.write(sc["asm-arm"])
-                fp.close()
-                self.new_stubs.append( fname )
+            for arch in all_arches:
+                if sc.has_key("asm-%s" % arch):
+                    filename = "arch-%s/syscalls/%s.S" % (arch, sc["func"])
+                    D2(">>> generating " + filename)
+                    fp = create_file(filename)
+                    fp.write(sc["asm-%s" % arch])
+                    fp.close()
+                    self.new_stubs.append(filename)
 
-            if sc.has_key("asm-x86") and 'x86' in all_archs:
-                fname = "arch-x86/syscalls/%s.S" % sc["func"]
-                D2( ">>> generating "+fname )
-                fp = create_file( fname )
-                fp.write(sc["asm-x86"])
-                fp.close()
-                self.new_stubs.append( fname )
 
-            if sc.has_key("asm-mips") and 'mips' in all_archs:
-                fname = "arch-mips/syscalls/%s.S" % sc["func"]
-                D2( ">>> generating "+fname )
-                fp = create_file( fname )
-                fp.write(sc["asm-mips"])
-                fp.close()
-                self.new_stubs.append( fname )
-
-    def  regenerate(self):
+    def regenerate(self):
         D( "scanning for existing architecture-specific stub files" )
 
         bionic_libc_root_len = len(bionic_libc_root)
 
-        for arch in all_archs:
+        for arch in all_arches:
             arch_path = bionic_libc_root + "arch-" + arch
             D( "scanning " + arch_path )
             files = glob.glob( arch_path + "/syscalls/*.S" )
@@ -405,7 +378,7 @@
         D( "re-generating stubs and support files" )
 
         self.gen_glibc_syscalls_h()
-        for arch in all_archs:
+        for arch in all_arches:
             self.gen_arch_syscalls_mk(arch)
         self.gen_syscall_stubs()