paul@100 | 1 | /* |
paul@100 | 2 | * Client code generation. |
paul@100 | 3 | * |
paul@197 | 4 | * Copyright (C) 2019, 2020, 2021 Paul Boddie <paul@boddie.org.uk> |
paul@100 | 5 | * |
paul@100 | 6 | * This program is free software; you can redistribute it and/or |
paul@100 | 7 | * modify it under the terms of the GNU General Public License as |
paul@100 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@100 | 9 | * the License, or (at your option) any later version. |
paul@100 | 10 | * |
paul@100 | 11 | * This program is distributed in the hope that it will be useful, |
paul@100 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@100 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@100 | 14 | * GNU General Public License for more details. |
paul@100 | 15 | * |
paul@100 | 16 | * You should have received a copy of the GNU General Public License |
paul@100 | 17 | * along with this program; if not, write to the Free Software |
paul@100 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@100 | 19 | * Boston, MA 02110-1301, USA |
paul@100 | 20 | */ |
paul@100 | 21 | |
paul@100 | 22 | #include <stdlib.h> |
paul@100 | 23 | #include "client.h" |
paul@126 | 24 | #include "config.h" |
paul@100 | 25 | #include "common.h" |
paul@100 | 26 | #include "declaration.h" |
paul@100 | 27 | #include "message.h" |
paul@100 | 28 | #include "templates.h" |
paul@100 | 29 | |
paul@100 | 30 | |
paul@100 | 31 | |
paul@100 | 32 | /* Generate operation signature details for client use. */ |
paul@100 | 33 | |
paul@110 | 34 | void write_client_signature(struct signature *sig, enum signature_role role, |
paul@107 | 35 | FILE *fp, struct interface *iface) |
paul@100 | 36 | { |
paul@186 | 37 | /* Client interface signatures are oblivious to completions. */ |
paul@186 | 38 | |
paul@186 | 39 | write_interface_signature(sig, role, CLIENT_ROLE, GENERAL_FUNCTION_ROLE, fp, |
paul@186 | 40 | iface); |
paul@100 | 41 | } |
paul@100 | 42 | |
paul@100 | 43 | /* Generate function source code for each operation signature. */ |
paul@100 | 44 | |
paul@100 | 45 | void write_client_function(struct signature *sig, FILE *fp, struct interface *iface) |
paul@100 | 46 | { |
paul@110 | 47 | write_client_signature(sig, DEFINITION_ROLE, fp, iface); |
paul@100 | 48 | write_client_function_body(sig->parameters, fp, iface, sig); |
paul@108 | 49 | fputs(END_FUNCTION, fp); |
paul@100 | 50 | } |
paul@100 | 51 | |
paul@100 | 52 | /* Generate a function body corresponding to an operation for client use. */ |
paul@100 | 53 | |
paul@100 | 54 | void write_client_function_body(struct parameter *param, FILE *fp, |
paul@100 | 55 | struct interface *iface, struct signature *sig) |
paul@100 | 56 | { |
paul@100 | 57 | int input_words, input_items, output_words, output_items; |
paul@100 | 58 | |
paul@100 | 59 | /* With a protocol attribute, the opcode is the attribute value. Otherwise, |
paul@100 | 60 | it is the operation code. */ |
paul@100 | 61 | |
paul@100 | 62 | char *opname = get_operation_name(iface, sig), |
paul@100 | 63 | *protocol = get_protocol(iface->attributes), |
paul@100 | 64 | *opcode = get_opcode_identifier(protocol, opname); |
paul@100 | 65 | |
paul@100 | 66 | /* Generate the prologue. */ |
paul@100 | 67 | |
paul@100 | 68 | fputs(client_function_body_prologue, fp); |
paul@100 | 69 | |
paul@116 | 70 | /* Obtain a suitable endpoint reference. */ |
paul@116 | 71 | |
paul@126 | 72 | if (conf.language == C_LANGUAGE) |
paul@117 | 73 | fprintf(fp, client_function_body_endpoint_conversion_c, L4_CAP_TYPE); |
paul@116 | 74 | |
paul@100 | 75 | /* Count the number of input words, items and output words. */ |
paul@100 | 76 | |
paul@100 | 77 | count_parameters(param, &input_words, &input_items, &output_words, &output_items); |
paul@100 | 78 | |
paul@100 | 79 | /* Adjust the input words where a protocol is used as the opcode. */ |
paul@100 | 80 | |
paul@100 | 81 | if (protocol != NULL) |
paul@100 | 82 | input_words++; |
paul@100 | 83 | |
paul@100 | 84 | /* Generate types parameterised with the qualified operation name. */ |
paul@100 | 85 | |
paul@100 | 86 | write_accessor_declaration(input_words, output_words, opname, fp); |
paul@100 | 87 | |
paul@100 | 88 | /* Initialise the message. */ |
paul@100 | 89 | |
paul@100 | 90 | fprintf(fp, "\n ipc_message_new(&msg);\n"); |
paul@100 | 91 | |
paul@100 | 92 | /* Generate expected output item requirements. */ |
paul@100 | 93 | |
paul@100 | 94 | if (output_items) |
paul@100 | 95 | fprintf(fp, " err = ipc_message_expect(&msg, %d);\n" |
paul@100 | 96 | " if (err)\n" |
paul@100 | 97 | " return err;\n", output_items); |
paul@100 | 98 | |
paul@100 | 99 | /* Populate input parameters in the message. Dereference function parameters |
paul@100 | 100 | acting as "inout" parameters. */ |
paul@100 | 101 | |
paul@100 | 102 | if (input_words) |
paul@100 | 103 | { |
paul@100 | 104 | /* Reserve space for the words before any items, obtaining a pointer to the |
paul@100 | 105 | words. */ |
paul@100 | 106 | |
paul@129 | 107 | write_accessor_initialisation(IN_PARAMETER, CLIENT_ROLE, GENERAL_FUNCTION_ROLE, |
paul@129 | 108 | opname, fp); |
paul@100 | 109 | |
paul@100 | 110 | if (protocol != NULL) |
paul@142 | 111 | fprintf(fp, " in_words->_op = opcode_%s;\n", opname); |
paul@100 | 112 | |
paul@129 | 113 | write_message_access(param, CLIENT_ROLE, GENERAL_FUNCTION_ROLE, IN_PARAMETER, |
paul@129 | 114 | WORD_CLASS, fp); |
paul@100 | 115 | } |
paul@100 | 116 | |
paul@100 | 117 | /* Do the same for items. */ |
paul@100 | 118 | |
paul@100 | 119 | if (input_items) |
paul@100 | 120 | { |
paul@100 | 121 | fputs("\n", fp); |
paul@129 | 122 | write_message_access(param, CLIENT_ROLE, GENERAL_FUNCTION_ROLE, IN_PARAMETER, |
paul@129 | 123 | ITEM_CLASS, fp); |
paul@100 | 124 | } |
paul@100 | 125 | |
paul@100 | 126 | /* Send the request. */ |
paul@100 | 127 | |
paul@115 | 128 | fprintf(fp, client_function_body_call, opcode, "_endp"); |
paul@100 | 129 | |
paul@100 | 130 | /* Retrieve output parameters from the message. */ |
paul@100 | 131 | |
paul@100 | 132 | if (output_words) |
paul@100 | 133 | { |
paul@129 | 134 | write_accessor_initialisation(OUT_PARAMETER, CLIENT_ROLE, GENERAL_FUNCTION_ROLE, |
paul@129 | 135 | opname, fp); |
paul@129 | 136 | write_message_access(param, CLIENT_ROLE, GENERAL_FUNCTION_ROLE, OUT_PARAMETER, |
paul@129 | 137 | WORD_CLASS, fp); |
paul@100 | 138 | } |
paul@100 | 139 | |
paul@100 | 140 | /* Do the same for items. */ |
paul@100 | 141 | |
paul@100 | 142 | if (output_items) |
paul@100 | 143 | { |
paul@100 | 144 | fputs("\n", fp); |
paul@129 | 145 | write_message_access(param, CLIENT_ROLE, GENERAL_FUNCTION_ROLE, OUT_PARAMETER, |
paul@129 | 146 | ITEM_CLASS, fp); |
paul@100 | 147 | } |
paul@100 | 148 | |
paul@197 | 149 | /* Generate the epilogue. */ |
paul@100 | 150 | |
paul@197 | 151 | fputs(client_function_body_epilogue, fp); |
paul@100 | 152 | |
paul@100 | 153 | /* Free allocated strings. */ |
paul@100 | 154 | |
paul@100 | 155 | free(opname); |
paul@100 | 156 | |
paul@100 | 157 | if (protocol == NULL) |
paul@100 | 158 | free(opcode); |
paul@100 | 159 | } |
paul@117 | 160 | |
paul@117 | 161 | /* Populate an interface structure with client functions. */ |
paul@117 | 162 | |
paul@117 | 163 | void write_client_interface(struct interface *iface, FILE *fp) |
paul@117 | 164 | { |
paul@117 | 165 | struct signature *sig; |
paul@117 | 166 | char *opname; |
paul@117 | 167 | |
paul@214 | 168 | if (iface->signatures == NULL) |
paul@214 | 169 | fprintf(fp, client_interface_prologue_empty_c, iface->name, iface->name); |
paul@214 | 170 | else |
paul@117 | 171 | { |
paul@214 | 172 | fprintf(fp, client_interface_prologue_c, iface->name, iface->name); |
paul@117 | 173 | |
paul@214 | 174 | for (sig = iface->signatures; sig != NULL; sig = sig->tail) |
paul@214 | 175 | { |
paul@214 | 176 | opname = get_operation_name(iface, sig); |
paul@117 | 177 | |
paul@214 | 178 | fprintf(fp, client_interface_member_c, sig->operation, opname); |
paul@214 | 179 | |
paul@214 | 180 | free(opname); |
paul@214 | 181 | } |
paul@214 | 182 | |
paul@214 | 183 | fputs(client_interface_epilogue_c, fp); |
paul@117 | 184 | } |
paul@117 | 185 | } |