paul@99 | 1 | /* |
paul@106 | 2 | * Generation of server dispatch and handle functions. |
paul@99 | 3 | * |
paul@214 | 4 | * Copyright (C) 2019, 2022 Paul Boddie <paul@boddie.org.uk> |
paul@99 | 5 | * |
paul@99 | 6 | * This program is free software; you can redistribute it and/or |
paul@99 | 7 | * modify it under the terms of the GNU General Public License as |
paul@99 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@99 | 9 | * the License, or (at your option) any later version. |
paul@99 | 10 | * |
paul@99 | 11 | * This program is distributed in the hope that it will be useful, |
paul@99 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@99 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@99 | 14 | * GNU General Public License for more details. |
paul@99 | 15 | * |
paul@99 | 16 | * You should have received a copy of the GNU General Public License |
paul@99 | 17 | * along with this program; if not, write to the Free Software |
paul@99 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@99 | 19 | * Boston, MA 02110-1301, USA |
paul@99 | 20 | */ |
paul@99 | 21 | |
paul@99 | 22 | #include <stdlib.h> |
paul@99 | 23 | #include "common.h" |
paul@99 | 24 | #include "dispatch.h" |
paul@99 | 25 | #include "templates.h" |
paul@99 | 26 | |
paul@99 | 27 | |
paul@99 | 28 | |
paul@214 | 29 | /* Generate each dispatch possibility within an interface. */ |
paul@214 | 30 | |
paul@214 | 31 | static void write_dispatcher_cases(FILE *fp, struct interface *iface, |
paul@214 | 32 | int compound) |
paul@214 | 33 | { |
paul@214 | 34 | struct signature *sig; |
paul@214 | 35 | char *opcode, *opname, *protocol, *prefix, *ref, *s; |
paul@214 | 36 | |
paul@214 | 37 | for (sig = iface->signatures; sig != NULL; sig = sig->tail) |
paul@214 | 38 | { |
paul@214 | 39 | opname = get_operation_name(iface, sig); |
paul@214 | 40 | opcode = get_opcode_identifier(NULL, opname); |
paul@214 | 41 | prefix = get_operation_wrapper_prefix(sig->attributes); |
paul@214 | 42 | |
paul@214 | 43 | /* Convert to any base interface. */ |
paul@214 | 44 | |
paul@214 | 45 | ref = get_object_conversion(iface, compound); |
paul@214 | 46 | |
paul@214 | 47 | /* Generate a reply if appropriate. */ |
paul@214 | 48 | |
paul@214 | 49 | if (have_attribute(sig->attributes, "completion")) |
paul@214 | 50 | s = dispatch_function_wrapper_case; |
paul@214 | 51 | else |
paul@214 | 52 | s = dispatch_function_reply_wrapper_case; |
paul@214 | 53 | |
paul@214 | 54 | /* Generate the case and invocation. */ |
paul@214 | 55 | |
paul@214 | 56 | fprintf(fp, s, opcode, prefix, opname, ref); |
paul@214 | 57 | |
paul@214 | 58 | /* Free allocated strings. */ |
paul@214 | 59 | |
paul@214 | 60 | free(opcode); |
paul@214 | 61 | free(opname); |
paul@214 | 62 | free(ref); |
paul@214 | 63 | } |
paul@214 | 64 | } |
paul@214 | 65 | |
paul@214 | 66 | /* Dispatch to each interface-level function where the same protocol applies to |
paul@214 | 67 | an entire interface. Dispatch to each operation where an interface does not |
paul@214 | 68 | specify a protocol. */ |
paul@214 | 69 | |
paul@214 | 70 | static void write_dispatcher_interface_cases(FILE *fp, struct interface *iface, |
paul@214 | 71 | int by_protocol) |
paul@214 | 72 | { |
paul@214 | 73 | struct interface_ref *base; |
paul@214 | 74 | char *protocol, *ref; |
paul@214 | 75 | |
paul@214 | 76 | for (base = iface->bases; base != NULL; base = base->tail) |
paul@214 | 77 | { |
paul@214 | 78 | protocol = get_protocol(base->iface->attributes); |
paul@214 | 79 | |
paul@214 | 80 | /* Write cases for each interface, using the protocol to select between the |
paul@214 | 81 | cases. */ |
paul@214 | 82 | |
paul@214 | 83 | if (by_protocol && (protocol != NULL)) |
paul@214 | 84 | { |
paul@214 | 85 | /* Convert to any base interface. */ |
paul@214 | 86 | |
paul@214 | 87 | ref = get_object_conversion(base->iface, 1); |
paul@214 | 88 | fprintf(fp, dispatch_function_interface_case, protocol, base->name, ref); |
paul@214 | 89 | free(ref); |
paul@214 | 90 | } |
paul@214 | 91 | |
paul@214 | 92 | /* Write cases for each operation provided by the interface so that they can |
paul@214 | 93 | be selected. Note that this requires distinct opcodes to be used, so in |
paul@214 | 94 | practice requiring explicit opcodes to be indicated. */ |
paul@214 | 95 | |
paul@214 | 96 | else if (!by_protocol && (protocol == NULL)) |
paul@214 | 97 | write_dispatcher_cases(fp, base->iface, 1); |
paul@214 | 98 | } |
paul@214 | 99 | } |
paul@214 | 100 | |
paul@214 | 101 | /* Return whether any base interface specifies a protocol. */ |
paul@214 | 102 | |
paul@214 | 103 | static int have_interfaces_using_protocols(struct interface *iface) |
paul@214 | 104 | { |
paul@214 | 105 | struct interface_ref *base; |
paul@214 | 106 | |
paul@214 | 107 | for (base = iface->bases; base != NULL; base = base->tail) |
paul@214 | 108 | if (get_protocol(base->iface->attributes) != NULL) |
paul@214 | 109 | return 1; |
paul@214 | 110 | |
paul@214 | 111 | return 0; |
paul@214 | 112 | } |
paul@214 | 113 | |
paul@214 | 114 | |
paul@214 | 115 | |
paul@106 | 116 | /* Generate dispatch function signatures. */ |
paul@99 | 117 | |
paul@110 | 118 | void write_dispatcher_signature(const char *name, enum signature_role role, |
paul@110 | 119 | FILE *fp) |
paul@99 | 120 | { |
paul@116 | 121 | fprintf(fp, dispatch_function_signature, name, name); |
paul@110 | 122 | fputs(get_signature_terminator(role), fp); |
paul@106 | 123 | } |
paul@99 | 124 | |
paul@106 | 125 | /* Generate handler function signatures. */ |
paul@106 | 126 | |
paul@110 | 127 | void write_handler_signature(const char *name, enum signature_role role, |
paul@110 | 128 | FILE *fp) |
paul@106 | 129 | { |
paul@116 | 130 | fprintf(fp, handle_function_signature, name, name); |
paul@110 | 131 | fputs(get_signature_terminator(role), fp); |
paul@99 | 132 | } |
paul@99 | 133 | |
paul@99 | 134 | /* Generate a dispatch function for the different operations. */ |
paul@99 | 135 | |
paul@214 | 136 | void write_dispatcher(FILE *fp, struct interface *iface) |
paul@99 | 137 | { |
paul@214 | 138 | char *protocol = get_protocol(iface->attributes); |
paul@214 | 139 | |
paul@110 | 140 | write_dispatcher_signature(iface->name, DEFINITION_ROLE, fp); |
paul@99 | 141 | |
paul@180 | 142 | /* Declare an error variable to support testing for already-sent messages. */ |
paul@180 | 143 | |
paul@180 | 144 | fputs(" long err;\n\n", fp); |
paul@180 | 145 | |
paul@214 | 146 | /* Without a protocol applying to the entire interface, dispatch using the |
paul@214 | 147 | protocol from the message label. */ |
paul@99 | 148 | |
paul@214 | 149 | if (protocol == NULL) |
paul@214 | 150 | { |
paul@99 | 151 | fputs(" switch (l4_msgtag_label(msg->tag))\n {\n", fp); |
paul@99 | 152 | |
paul@214 | 153 | /* Dispatch using the protocol to base interfaces employing protocols. */ |
paul@99 | 154 | |
paul@214 | 155 | if (have_interfaces_using_protocols(iface)) |
paul@214 | 156 | write_dispatcher_interface_cases(fp, iface, 1); |
paul@214 | 157 | } |
paul@99 | 158 | |
paul@214 | 159 | /* If a protocol applies to the entire interface, dispatch using an operation |
paul@214 | 160 | indicator in the word data. */ |
paul@99 | 161 | |
paul@214 | 162 | else |
paul@214 | 163 | fputs(" switch (ipc_message_get_word(msg, 0))\n {\n", fp); |
paul@99 | 164 | |
paul@214 | 165 | /* Dispatch to operations provided by compound interfaces. */ |
paul@99 | 166 | |
paul@214 | 167 | write_dispatcher_interface_cases(fp, iface, 0); |
paul@146 | 168 | |
paul@214 | 169 | /* Dispatch to each operation defined at this level. */ |
paul@146 | 170 | |
paul@214 | 171 | write_dispatcher_cases(fp, iface, is_compound_interface(iface)); |
paul@99 | 172 | |
paul@214 | 173 | /* Terminate the dispatcher. */ |
paul@99 | 174 | |
paul@214 | 175 | fputs(dispatch_function_default_case, fp); |
paul@214 | 176 | fputs(dispatch_function_dispatcher_epilogue, fp); |
paul@99 | 177 | |
paul@214 | 178 | fputs(END_FUNCTION, fp); |
paul@99 | 179 | } |