anchor.diff:          print >>f, "#define false (0)"
   
       print >>f
  -    if has_personality:
  -        print >>f, "static int export(char *prefix, long extra_arg, long personality) {"
  -    else:
  -        print >>f, "static int export(char *prefix, long extra_arg) {"
  -    if len(functions) > 0:
  -        print >>f, "    char buf[HAL_NAME_LEN + 1];"
  +
  +###################################  funct()  #######################################
  +
  +    print >>f, "static int funct(const void *arg, const hal_funct_args_t *fa)\n{"
  +    print >>f, "long period __attribute__((unused)) = fa_period(fa);\n"
  +
  +    print >>f, "    // the following accessors are available here:"
  +    print >>f, "    // fa_period(fa) - formerly 'long period'"
  +    print >>f, "    // fa_thread_start_time(fa): _actual_ start time of thread invocation"
  +    print >>f, "    // fa_start_time(fa): _actual_ start time of function invocation"
  +    print >>f, "    // fa_thread_name(fa): name of the calling thread (char *)"
  +    print >>f, "    // fa_funct_name(fa): name of the this called function (char *)"
  +    print >>f, "    return 0;\n}\n"
  +
  +
  +
  +###########################  export_halobjs()  ######################################################
  +
  +    print >>f, "static int export_halobjs(struct inst_data *ip, int owner_id, const char *name)\n{"
  +
  +    print >>f, "    char buf[HAL_NAME_LEN + 1];"
       print >>f, "    int r = 0;"
  -    if has_array:
  -        print >>f, "    int j = 0;"
  -    print >>f, "    int sz = sizeof(struct __comp_state) + __comp_get_data_size();"
  -    print >>f, "    struct __comp_state *inst = hal_malloc(sz);"
  -    print >>f, "    memset(inst, 0, sz);"
  +    print >>f, "    int j __attribute__((unused)) = 0;"
  +    print >>f, "    int z __attribute__((unused)) = 0;"
  +
       if has_data:
  -        print >>f, "    inst->_data = (char*)inst + sizeof(struct __comp_state);"
  -    if has_personality:
  -        print >>f, "    inst->_personality = personality;"
  -    if options.get("extra_setup"):
  -        print >>f, "    r = extra_setup(inst, prefix, extra_arg);"
  -	print >>f, "    if(r != 0) return r;"
  -        # the extra_setup() function may have changed the personality
  -        if has_personality:
  -            print >>f, "    personality = inst->_personality;"
  -    for name, type, array, dir, value, personality in pins:
  -        if personality:
  -            print >>f, "if(%s) {" % personality
  +        print >>f, "    ip->_data = (char*)ip + sizeof(struct inst_data);"
  +
  +    for name, type, array, dir, value in pins:
           if array:
  -            if isinstance(array, tuple): array = array[1]
  -            print >>f, "    for(j=0; j < (%s); j++) {" % array
  -            print >>f, "        r = hal_pin_%s_newf(%s, &(inst->%s[j]), comp_id," % (
  -                type, dirmap[dir], to_c(name))
  -            print >>f, "            \"%%s%s\", prefix, j);" % to_hal("." + name)
  +            print >>f, "    z = %s;" % array
  +            print >>f, "    if(z > maxpins)\n       z = maxpins;"
  +            print >>f, "    for(j=0; j < z; j++)\n        {"
  +            print >>f, "        r = hal_pin_%s_newf(%s, &(ip->%s[j]), owner_id," % (type, dirmap[dir], to_c(name))
  +            print >>f, "            \"%%s%s\", name, j);" % to_hal("." + name)
               print >>f, "        if(r != 0) return r;"
               if value is not None:
  -                print >>f, "    *(inst->%s[j]) = %s;" % (to_c(name), value)
  -            print >>f, "    }"
  -        else:
  -            print >>f, "    r = hal_pin_%s_newf(%s, &(inst->%s), comp_id," % (
  -                type, dirmap[dir], to_c(name))
  -            print >>f, "        \"%%s%s\", prefix);" % to_hal("." + name)
  -            print >>f, "    if(r != 0) return r;"
  +                print >>f, "    *(ip->%s[j]) = %s;" % (to_c(name), value)
  +            print >>f, "        }\n"
  +        else:
  +            print >>f, "    r = hal_pin_%s_newf(%s, &(ip->%s), owner_id," % (type, dirmap[dir], to_c(name))
  +            print >>f, "            \"%%s%s\", name);" % to_hal("." + name)
  +            print >>f, "    if(r != 0) return r;\n"
               if value is not None:
  -                print >>f, "    *(inst->%s) = %s;" % (to_c(name), value)
  -        if personality:
  -            print >>f, "}"
  -
  -    for name, type, array, dir, value, personality in params:
  -        if personality:
  -            print >>f, "if(%s) {" % personality
  +                print >>f, "    *(ip->%s) = %s;" % (to_c(name), value)
  +
  +    for name, type, array, dir, value in params:
           if array:
  -            if isinstance(array, tuple): array = array[1]
  -            print >>f, "    for(j=0; j < %s; j++) {" % array
  -            print >>f, "        r = hal_param_%s_newf(%s, &(inst->%s[j]), comp_id," % (
  -                type, dirmap[dir], to_c(name))
  -            print >>f, "            \"%%s%s\", prefix, j);" % to_hal("." + name)
  +            print >>f, "    z = %s;" % array
  +            print >>f, "    if(z > maxpins)\n       z = maxpins;"
  +            print >>f, "    for(j=0; j < z; j++)\n        {"
  +            print >>f, "        r = hal_param_%s_newf(%s, &(ip->%s[j]), owner_id," % (type, dirmap[dir], to_c(name))
  +            print >>f, "            \"%%s%s\", name, j);" % to_hal("." + name)
               print >>f, "        if(r != 0) return r;"
               if value is not None:
  -                print >>f, "    inst->%s[j] = %s;" % (to_c(name), value)
  -            print >>f, "    }"
  -        else:
  -            print >>f, "    r = hal_param_%s_newf(%s, &(inst->%s), comp_id," % (
  -                type, dirmap[dir], to_c(name))
  -            print >>f, "        \"%%s%s\", prefix);" % to_hal("." + name)
  +                print >>f, "    ip->%s[j] = %s;" % (to_c(name), value)
  +            print >>f, "        }\n"
  +        else:
  +            print >>f, "    r = hal_param_%s_newf(%s, &(ip->%s), owner_id," % (type, dirmap[dir], to_c(name))
  +            print >>f, "            \"%%s%s\", name);" % to_hal("." + name)
               if value is not None:
  -                print >>f, "    inst->%s = %s;" % (to_c(name), value)
  -            print >>f, "    if(r != 0) return r;"
  -        if personality:
  -            print >>f, "}"
  +                print >>f, "    ip->%s = %s;" % (to_c(name), value)
  +            print >>f, "    if(r != 0) return r;\n"
   
       for type, name, array, value in variables:
           if value is None: continue
           if array:
  -            print >>f, "    for(j=0; j < %s; j++) {" % array
  -            print >>f, "        inst->%s[j] = %s;" % (name, value)
  -            print >>f, "    }"
  -        else:
  -            print >>f, "    inst->%s = %s;" % (name, value)
  -
  +            print >>f, "    z = %s;"  % array
  +            print >>f, "    if(z > maxpins)\n       z = maxpins;"
  +            print >>f, "    for(j=0; j < z; j++)\n       {"
  +            print >>f, "        ip->%s[j] = %s;" % (name, value)
  +            print >>f, "        }\n"
  +        else:
  +            print >>f, "    ip->%s = %s;" % (name, value)
  +
  +# TODO
  +#     need to insert the funct name and make required number of calls
  +#     use 'functions' to get the names
  +# for name, fp in functions:
  +#        if names.has_key(name):
  +#            Error("Duplicate item name: %s" % name)
  +#        print >>f, "static int %s(void *arg, const hal_funct_args_t *fa);\n" % to_c(name)
       for name, fp in functions:
  -        print >>f, "    rtapi_snprintf(buf, sizeof(buf), \"%%s%s\", prefix);"\
  -            % to_hal("." + name)
  -        print >>f, "    r = hal_export_funct(buf, (void(*)(void *inst, long))%s, inst, %s, 0, comp_id);" % (
  -            to_c(name), int(fp))
  -        print >>f, "    if(r != 0) return r;"
  -    print >>f, "    if(__comp_last_inst) __comp_last_inst->_next = inst;"
  -    print >>f, "    __comp_last_inst = inst;"
  -    print >>f, "    if(!__comp_first_inst) __comp_first_inst = inst;"
  +        print >>f, "    // exporting an extended thread function:"
  +        print >>f, "    hal_export_xfunct_args_t %s_xf = " % to_c(name)
  +        print >>f, "        {"
  +        print >>f, "        .type = FS_XTHREADFUNC,"
  +        print >>f, "        .funct.x = %s," % to_c(name)
  +        print >>f, "        .arg = ip,"
  +        print >>f, "        .uses_fp = %d," % int(fp)
  +        print >>f, "        .reentrant = 0,"
  +        print >>f, "        .owner_id = owner_id"
  +        print >>f, "        };\n"
  +
  +#        print >>f, "    %s_xf.uses_fp = %d;" % ( to_c(name), int(fp))
  +        strng =  "    rtapi_snprintf(buf, sizeof(buf),\"%s."
  +        if (name == "" or name == "_" or name == " ") :
  +            strng += "funct\", name);"
  +        else :
  +            strng += "%s.funct\", name);" % (to_hal(name))
  +        print >>f, strng
  +
  +        print >>f, "    r = hal_export_xfunctf(&%s_xf, buf, name);" % to_c(name)
  +        print >>f, "    if(r != 0)\n        return r;"
  +
       print >>f, "    return 0;"
       print >>f, "}"
   
  -    if options.get("count_function"):
  -        print >>f, "static int get_count(void);"
  -
  -    if options.get("rtapi_app", 1):
  -        if options.get("constructable") and not options.get("singleton"):
  -            print >>f, "static int export_1(char *prefix, char *argstr) {"
  -            print >>f, "    int arg = simple_strtol(argstr, NULL, 0);"
  -            print >>f, "    return export(prefix, arg);"
  -            print >>f, "}"
  -        if not options.get("singleton") and not options.get("count_function") :
  -            print >>f, "static int default_count=%s, count=0;" \
  -                % options.get("default_count", 1)
  -            print >>f, "char *names[16] = {0,};"
  -            if not options.get("userspace"):
  -                print >>f, "RTAPI_MP_INT(count, \"number of %s\");" % comp_name
  -                print >>f, "RTAPI_MP_ARRAY_STRING(names, 16, \"names of %s\");" % comp_name
  -
  -        if has_personality:
  -            init1 = str(int(options.get('default_personality', 0)))
  -            init = ",".join([init1] * 16)
  -            print >>f, "static int personality[16] = {%s};" % init
  -            print >>f, "RTAPI_MP_ARRAY_INT(personality, 16, \"personality of each %s\");" % comp_name
  -        print >>f, "int rtapi_app_main(void) {"
  -        print >>f, "    int r = 0;"
  -        if not options.get("singleton"):
  -            print >>f, "    int i;"
  -        if options.get("count_function"):
  -            print >>f, "    int count = get_count();"
  -
  -        print >>f, "    comp_id = hal_init(\"%s\");" % comp_name
  -        print >>f, "    if(comp_id < 0) return comp_id;"
  -
  -        if options.get("singleton"):
  -            if has_personality:
  -                print >>f, "    r = export(\"%s\", 0, personality[0]);" % \
  -                        to_hal(removeprefix(comp_name, "hal_"))
  -            else:
  -                print >>f, "    r = export(\"%s\", 0);" % \
  -                        to_hal(removeprefix(comp_name, "hal_"))
  -        elif options.get("count_function"):
  -            print >>f, "    for(i=0; i<count; i++) {"
  -            print >>f, "        char buf[HAL_NAME_LEN + 1];"
  -            print >>f, "        rtapi_snprintf(buf, sizeof(buf), " \
  -                                        "\"%s.%%d\", i);" % \
  -                    to_hal(removeprefix(comp_name, "hal_"))
  -            if has_personality:
  -                print >>f, "        r = export(buf, i, personality[i%16]);"
  -            else:
  -                print >>f, "        r = export(buf, i);"
  -	    print >>f, "    }"
  -	else:
  -            print >>f, "    if(count && names[0]) {"
  -            print >>f, "        rtapi_print_msg(RTAPI_MSG_ERR," \
  -                            "\"count= and names= are mutually exclusive\\n\");"
  -            print >>f, "        return -EINVAL;"
  -            print >>f, "    }"
  -            print >>f, "    if(!count && !names[0]) count = default_count;"
  -            print >>f, "    if(count) {"
  -            print >>f, "        for(i=0; i<count; i++) {"
  -            print >>f, "            char buf[HAL_NAME_LEN + 1];"
  -            print >>f, "            rtapi_snprintf(buf, sizeof(buf), " \
  -                                        "\"%s.%%d\", i);" % \
  -                    to_hal(removeprefix(comp_name, "hal_"))
  -            if has_personality:
  -                print >>f, "        r = export(buf, i, personality[i%16]);"
  -            else:
  -                print >>f, "        r = export(buf, i);"
  -            print >>f, "            if(r != 0) break;"
  -            print >>f, "       }"
  -            print >>f, "    } else {"
  -            print >>f, "        for(i=0; names[i]; i++) {"
  -            if has_personality:
  -                print >>f, "        r = export(names[i], i, personality[i%16]);"
  -            else:
  -                print >>f, "        r = export(names[i], i);"
  -            print >>f, "            if(r != 0) break;"
  -            print >>f, "       }"
  -            print >>f, "    }"
  -
  -        if options.get("constructable") and not options.get("singleton"):
  -            print >>f, "    hal_set_constructor(comp_id, export_1);"
  -        print >>f, "    if(r) {"
  -	if options.get("extra_cleanup"):
  -            print >>f, "    extra_cleanup();"
  -        print >>f, "        hal_exit(comp_id);"
  -        print >>f, "    } else {"
  -        print >>f, "        hal_ready(comp_id);"
  -        print >>f, "    }"
  -        print >>f, "    return r;"
  -        print >>f, "}"
  -
  -        print >>f
  -        print >>f, "void rtapi_app_exit(void) {"
  -	if options.get("extra_cleanup"):
  -            print >>f, "    extra_cleanup();"
  -        print >>f, "    hal_exit(comp_id);"
  -        print >>f, "}"
  -
  -    if options.get("userspace"):
  -        print >>f, "static void user_mainloop(void);"
  -        if options.get("userinit"):
  -            print >>f, "static void userinit(int argc, char **argv);"
  -        print >>f, "int argc=0; char **argv=0;"
  -        print >>f, "int main(int argc_, char **argv_) {"
  -        print >>f, "    argc = argc_; argv = argv_;"
  -        print >>f
  -        if options.get("userinit", 0):
  -            print >>f, "    userinit(argc, argv);"
  -        print >>f
  -        print >>f, "    if(rtapi_app_main() < 0) return 1;"
  -        print >>f, "    user_mainloop();"
  -        print >>f, "    rtapi_app_exit();"
  -        print >>f, "    return 0;"
  -        print >>f, "}"
  +
  +
  +###########################  instantiate() ###############################################################
  +
  +    print >>f, "\n// constructor - init all HAL pins, params, funct etc here"
  +    print >>f, "static int instantiate(const char *name, const int argc, const char**argv)\n{"
  +    print >>f, "struct inst_data *ip;"
  +    print >>f, "int r;"
  +    if options.get("extra_inst_setup"):
  +            print >>f, "int k;"
  +    print >>f, "\n// allocate a named instance, and some HAL memory for the instance data"
  +    print >>f, "int inst_id = hal_inst_create(name, comp_id, sizeof(struct inst_data), (void **)&ip);\n"
  +
  +    print >>f, "    if (inst_id < 0)\n        return -1;\n"
  +
  +    print >>f, "// here ip is guaranteed to point to a blob of HAL memory of size sizeof(struct inst_data)."
  +    print >>f, "    hal_print_msg(RTAPI_MSG_ERR,\"%s inst=%s argc=%d\",__FUNCTION__, name, argc);\n"
  +    print >>f, "// Debug print of params and values"
  +    for name, mptype, value in instanceparams:
  +        if (mptype == 'int'):
  +            strg = "    hal_print_msg(RTAPI_MSG_ERR,\"%s: int instance param: %s=%d\",__FUNCTION__,"
  +            strg += "\"%s\", %s);" % (to_c(name), to_c(name))
  +            print >>f, strg
  +        else:
  +            strg = "    hal_print_msg(RTAPI_MSG_ERR,\"%s: string instance param: %s=%s\",__FUNCTION__,"
  +            strg += "\"%s\", %s);" % (to_c(name), to_c(name))
  +            print >>f, strg
  +
  +    print >>f, "\n// These pins - params - functs will be owned by the instance, and can be separately exited with delinst"
  +#    To get param updates have to test in C code not this script, for iprefix
  +    print >>f, "    if(strlen(iprefix))"
  +    print >>f, "        r = export_halobjs(ip, inst_id, iprefix);"
  +    print >>f, "    else"
  +    print >>f, "        r = export_halobjs(ip, inst_id, name);"
  +    if options.get("extra_inst_setup"):
  +            print >>f, "    // if the extra_inst_setup returns non zero will abort module creation"
  +            print >>f, "    k = extra_inst_setup(ip, name, argc, argv);\n"
  +            print >>f, "    if(k != 0)"
  +            print >>f, "        return k;\n"
  +
  +    print >>f, "    if(r == 0)"
  +    print >>f, "        hal_print_msg(RTAPI_MSG_ERR,\"%s - instance %s creation SUCCESSFUL\",__FUNCTION__, name);"
  +    print >>f, "    else"
  +    print >>f, "        hal_print_msg(RTAPI_MSG_ERR,\"%s - instance %s creation ABORTED\",__FUNCTION__, name);"
  +    print >>f, "    return r;\n}"
  +
  +##############################  rtapi_app_main  ######################################################
  +
  +    print >>f, "\nint rtapi_app_main(void)\n{"
  +    print >>f, "// Debug print of params and values"
  +    for name, mptype, value in instanceparams:
  +        if (mptype == 'int'):
  +            strg = "    hal_print_msg(RTAPI_MSG_ERR,\"%s: int instance param: %s=%d\",__FUNCTION__,"
  +            strg += "\"%s\", %s);" % (to_c(name), to_c(name))
  +            print >>f, strg
  +        else:
  +            strg = "    hal_print_msg(RTAPI_MSG_ERR,\"%s: string instance param: %s=%s\",__FUNCTION__,"
  +            strg += "\"%s\", %s);" % (to_c(name), to_c(name))
  +            print >>f, strg
  +
  +    print >>f, "    // to use default destructor, use NULL instead of delete"
  +    print >>f, "    comp_id = hal_xinit(TYPE_RT, 0, 0, instantiate, delete, compname);"
  +    print >>f, "    if (comp_id < 0)"
  +    print >>f, "        return -1;\n"
  +
  +    print >>f, "    // exporting an extended thread function:"
  +    print >>f, "    hal_export_xfunct_args_t xtf = "
  +    print >>f, "        {"
  +    print >>f, "        .type = FS_XTHREADFUNC,"
  +    print >>f, "        .funct.x = (void *) funct,"
  +    print >>f, "        .arg = \"x-instance-data\","
  +    print >>f, "        .uses_fp = 0,"
  +    print >>f, "        .reentrant = 0,"
  +    print >>f, "        .owner_id = comp_id"
  +    print >>f, "        };\n"
  +
  +    print >>f, "    if (hal_export_xfunctf(&xtf,\"%s.funct\", compname))"
  +    print >>f, "        return -1;"
  +
  +    print >>f, "    hal_ready(comp_id);\n"
  +
  +
  +
  +    print >>f, "    return 0;\n}\n"
  +
  +###############################  rtapi_app_exit()  #####################################################
  +
  +    print >>f, "void rtapi_app_exit(void)\n{"
  +    if options.get("extra_inst_cleanup"):
  +            print >>f, "    extra_inst_cleanup();"
  +    print >>f, "    hal_exit(comp_id);"
  +    print >>f, "}\n"
  +
  +
  +#########################   delete()  ####################################################################
  +
  +    print >>f, "// custom destructor - normally not needed"
  +    print >>f, "// pins, params, and functs are automatically deallocated regardless if a"
  +    print >>f, "// destructor is used or not (see below)"
  +    print >>f, "//"
  +    print >>f, "// some objects like vtables, rings, threads are not owned by a component"
  +    print >>f, "// interaction with such objects may require a custom destructor for"
  +    print >>f, "// cleanup actions"
  +    print >>f, "// NB: if a customer destructor is used, it is called"
  +    print >>f, "// - after the instance's functs have been removed from their respective threads"
  +    print >>f, "//   (so a thread funct call cannot interact with the destructor any more)"
  +    print >>f, "// - any pins and params of this instance are still intact when the destructor is"
  +    print >>f, "//   called, and they are automatically destroyed by the HAL library once the"
  +    print >>f, "//   destructor returns"
  +    print >>f, "static int delete(const char *name, void *inst, const int inst_size)\n{\n"
  +
  +    print >>f, "    hal_print_msg(RTAPI_MSG_ERR,\"%s inst=%s size=%d %p\\n\", __FUNCTION__, name, inst_size, inst);"
  +    print >>f, "// Debug print of params and values"
  +    for name, mptype, value in instanceparams:
  +        if (mptype == 'int'):
  +            strg = "    hal_print_msg(RTAPI_MSG_ERR,\"%s: int instance param: %s=%d\",__FUNCTION__,"
  +            strg += "\"%s\", %s);" % (to_c(name), to_c(name))
  +            print >>f, strg
  +        else:
  +            strg = "    hal_print_msg(RTAPI_MSG_ERR,\"%s: string instance param: %s=%s\",__FUNCTION__,"
  +            strg += "\"%s\", %s);" % (to_c(name), to_c(name))
  +            print >>f, strg
  +
  +    print >>f, "    return 0;\n"
  +    print >>f, "}\n"
  +
  +######################  preliminary defines before user FUNCTION(_) ######################################
  +
  +    print >>f
  + #   print >>f, "struct inst_data *ip;\n"
   
       print >>f
       if not options.get("no_convenience_defines"):
           print >>f, "#undef FUNCTION"
  -        print >>f, "#define FUNCTION(name) static void name(struct __comp_state *__comp_inst, long period)"
  -        print >>f, "#undef EXTRA_SETUP"
  -        print >>f, "#define EXTRA_SETUP() static int extra_setup(struct __comp_state *__comp_inst, char *prefix, long extra_arg)"
  -        print >>f, "#undef EXTRA_CLEANUP"
  -        print >>f, "#define EXTRA_CLEANUP() static void extra_cleanup(void)"
  +        print >>f, "#define FUNCTION(name) static int name(void *arg, const hal_funct_args_t *fa)"
  +        if options.get("extra_inst_setup"):
  +            print >>f, "// if the extra_inst_setup returns non zero it will abort the module creation"
  +            print >>f, "#undef EXTRA_INST_SETUP"
  +            print >>f, "#define EXTRA_INST_SETUP() static int extra_inst_setup(struct inst_data *ip, const char *name, int argc, const char**argv)"
  +        if options.get("extra_inst_cleanup"):
  +            print >>f, "#undef EXTRA_INST_CLEANUP"
  +            print >>f, "#define EXTRA_INST_CLEANUP() static void extra_inst_cleanup(void)"
           print >>f, "#undef fperiod"
           print >>f, "#define fperiod (period * 1e-9)"
  -        for name, type, array, dir, value, personality in pins:
  +        for name, type, array, dir, value in pins:
               print >>f, "#undef %s" % to_c(name)
               if array:
                   if dir == 'in':
  -                    print >>f, "#define %s(i) (0+*(__comp_inst->%s[i]))" % (to_c(name), to_c(name))
  +                    print >>f, "#define %s(i) (0+*(ip->%s[i]))" % (to_c(name), to_c(name))
                   else:
  -                    print >>f, "#define %s(i) (*(__comp_inst->%s[i]))" % (to_c(name), to_c(name))
  +                    print >>f, "#define %s(i) (*(ip->%s[i]))" % (to_c(name), to_c(name))
               else:
                   if dir == 'in':
  -                    print >>f, "#define %s (0+*__comp_inst->%s)" % (to_c(name), to_c(name))
  +                    print >>f, "#define %s (0+*ip->%s)" % (to_c(name), to_c(name))
                   else:
  -                    print >>f, "#define %s (*__comp_inst->%s)" % (to_c(name), to_c(name))
  -        for name, type, array, dir, value, personality in params:
  +                    print >>f, "#define %s (*ip->%s)" % (to_c(name), to_c(name))
  +        for name, type, array, dir, value in params:
               print >>f, "#undef %s" % to_c(name)
               if array:
  -                print >>f, "#define %s(i) (__comp_inst->%s[i])" % (to_c(name), to_c(name))
  +                print >>f, "#define %s(i) (ip->%s[i])" % (to_c(name), to_c(name))
               else:
  -                print >>f, "#define %s (__comp_inst->%s)" % (to_c(name), to_c(name))
  +                print >>f, "#define %s (ip->%s)" % (to_c(name), to_c(name))
   
           for type, name, array, value in variables:
               name = name.replace("*", "")
               print >>f, "#undef %s" % name
  -            print >>f, "#define %s (__comp_inst->%s)" % (name, name)
  +            print >>f, "#define %s (ip->%s)" % (name, name)
   
           if has_data:
               print >>f, "#undef data"
  -            print >>f, "#define data (*(%s*)(__comp_inst->_data))" % options['data']
  -        if has_personality:
  -            print >>f, "#undef personality"
  -            print >>f, "#define personality (__comp_inst->_personality)"
  -
  -        if options.get("userspace"):
  -            print >>f, "#undef FOR_ALL_INSTS"
  -            print >>f, "#define FOR_ALL_INSTS() for(__comp_inst = __comp_first_inst; __comp_inst; __comp_inst = __comp_inst->_next)"
  +            print >>f, "#define data (*(%s*)(ip->_data))" % options['data']
  +
       print >>f
       print >>f
  +
  +#########################  Epilogue - FUNCTION(_) printed in direct from file ################
   
   def epilogue(f):
       data = options.get('data')
       print >>f
  -    if data:
  -        print >>f, "static int __comp_get_data_size(void) { return sizeof(%s); }" % data
  -    else:
  -        print >>f, "static int __comp_get_data_size(void) { return 0; }"
   
   INSTALL, COMPILE, PREPROCESS, DOCUMENT, INSTALLDOC, VIEWDOC, MODINC = range(7)
   modename = ("install", "compile", "preprocess", "document", "installdoc", "viewdoc", "print-modinc")
  
positive.code:     token NUMBER: "0x[0-9a-fA-F]+|[+-]?[0-9]+"
      token STRING: "\"(\\.|[^\\\"])*\""
      token HEADER: "<.*?>"
      token POP: "[-()+*/]|&&|\\|\\||personality|==|&|!=|<|<=|>|>="
      token TSTRING: "\"\"\"(\\.|\\\n|[^\\\"]|\"(?!\"\")|\n)*\"\"\""
  
      rule File: ComponentDeclaration Declaration* "$" {{ return True }}
      rule ComponentDeclaration:
          "component" NAME OptString";" {{ comp(NAME, OptString); }}
      rule Declaration:
          "pin" PINDIRECTION TYPE HALNAME OptArray OptSAssign OptPersonality OptString ";"  {{ pin(HALNAME, TYPE, OptArray, PINDIRECTION, OptString, OptSAssign, OptPersonality) }}
        | "param" PARAMDIRECTION TYPE HALNAME OptArray OptSAssign OptPersonality OptString ";" {{ param(HALNAME, TYPE, OptArray, PARAMDIRECTION, OptString, OptSAssign, OptPersonality) }}
        | "function" NAME OptFP OptString ";"       {{ function(NAME, OptFP, OptString) }}
        | "variable" NAME STARREDNAME OptSimpleArray OptAssign ";" {{ variable(NAME, STARREDNAME, OptSimpleArray, OptAssign) }}
        | "option" NAME OptValue ";"   {{ option(NAME, OptValue) }}
        | "see_also" String ";"   {{ see_also(String) }}
        | "notes" String ";"   {{ notes(String) }}
  
negative.code: def to_hal(name):
      name = re.sub("#+", lambda m: "%%0%dd" % len(m.group(0)), name)
      return name.replace("_", "-").rstrip("-").rstrip(".")
  
  def to_c(name):
      name = re.sub("[-._]*#+", "", name)
      name = name.replace("#", "").replace(".", "_").replace("-", "_")
      return re.sub("_+", "_", name)
  
  def to_noquotes(name):
      name = name.replace("\"", "")
      return name
  
  
  ##################### Start ########################################
  
  def prologue(f):
  
      if options.get("userspace"):
          raise SystemExit, "instcomp does not support userspace components"
  
      if not functions :
          raise SystemExit, """\
                          Component code must declare a function. \
                          For single functions the default  \
                          function _; declaration is fine. \
                          Multiple functions must be uniquely named."""
  
      if options.get("singleton"):
          print " 'option singleton' has no effect in a instantiated component"
          print " each new instance is a stand alone unit anyway."
          print " If your code relies upon only one possible instance being created,"
          print " you should write for and compile with the comp tool, not instcomp."
  
      print >> f, "\n/* Autogenerated by %s on %s -- do not edit */\n" % (
          sys.argv[0], time.asctime())
  
  
      print >> f, """\
  #include "rtapi.h"
  #ifdef RTAPI
  