paul@100 | 1 | /* |
paul@100 | 2 | * Client code generation. |
paul@100 | 3 | * |
paul@100 | 4 | * Copyright (C) 2019 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@110 | 37 | write_interface_signature(sig, role, CLIENT_ROLE, fp, iface); |
paul@100 | 38 | } |
paul@100 | 39 | |
paul@100 | 40 | /* Generate function source code for each operation signature. */ |
paul@100 | 41 | |
paul@100 | 42 | void write_client_function(struct signature *sig, FILE *fp, struct interface *iface) |
paul@100 | 43 | { |
paul@110 | 44 | write_client_signature(sig, DEFINITION_ROLE, fp, iface); |
paul@100 | 45 | write_client_function_body(sig->parameters, fp, iface, sig); |
paul@108 | 46 | fputs(END_FUNCTION, fp); |
paul@100 | 47 | } |
paul@100 | 48 | |
paul@100 | 49 | /* Generate a function body corresponding to an operation for client use. */ |
paul@100 | 50 | |
paul@100 | 51 | void write_client_function_body(struct parameter *param, FILE *fp, |
paul@100 | 52 | struct interface *iface, struct signature *sig) |
paul@100 | 53 | { |
paul@100 | 54 | int input_words, input_items, output_words, output_items; |
paul@100 | 55 | |
paul@100 | 56 | /* With a protocol attribute, the opcode is the attribute value. Otherwise, |
paul@100 | 57 | it is the operation code. */ |
paul@100 | 58 | |
paul@100 | 59 | char *opname = get_operation_name(iface, sig), |
paul@100 | 60 | *protocol = get_protocol(iface->attributes), |
paul@100 | 61 | *opcode = get_opcode_identifier(protocol, opname); |
paul@100 | 62 | |
paul@100 | 63 | /* Generate the prologue. */ |
paul@100 | 64 | |
paul@100 | 65 | fputs(client_function_body_prologue, fp); |
paul@100 | 66 | |
paul@116 | 67 | /* Obtain a suitable endpoint reference. */ |
paul@116 | 68 | |
paul@126 | 69 | if (conf.language == C_LANGUAGE) |
paul@117 | 70 | fprintf(fp, client_function_body_endpoint_conversion_c, L4_CAP_TYPE); |
paul@116 | 71 | |
paul@100 | 72 | /* Count the number of input words, items and output words. */ |
paul@100 | 73 | |
paul@100 | 74 | count_parameters(param, &input_words, &input_items, &output_words, &output_items); |
paul@100 | 75 | |
paul@100 | 76 | /* Adjust the input words where a protocol is used as the opcode. */ |
paul@100 | 77 | |
paul@100 | 78 | if (protocol != NULL) |
paul@100 | 79 | input_words++; |
paul@100 | 80 | |
paul@100 | 81 | /* Generate types parameterised with the qualified operation name. */ |
paul@100 | 82 | |
paul@100 | 83 | write_accessor_declaration(input_words, output_words, opname, fp); |
paul@100 | 84 | |
paul@100 | 85 | /* Initialise the message. */ |
paul@100 | 86 | |
paul@100 | 87 | fprintf(fp, "\n ipc_message_new(&msg);\n"); |
paul@100 | 88 | |
paul@100 | 89 | /* Generate expected output item requirements. */ |
paul@100 | 90 | |
paul@100 | 91 | if (output_items) |
paul@100 | 92 | fprintf(fp, " err = ipc_message_expect(&msg, %d);\n" |
paul@100 | 93 | " if (err)\n" |
paul@100 | 94 | " return err;\n", output_items); |
paul@100 | 95 | |
paul@100 | 96 | /* Populate input parameters in the message. Dereference function parameters |
paul@100 | 97 | acting as "inout" parameters. */ |
paul@100 | 98 | |
paul@100 | 99 | if (input_words) |
paul@100 | 100 | { |
paul@100 | 101 | /* Reserve space for the words before any items, obtaining a pointer to the |
paul@100 | 102 | words. */ |
paul@100 | 103 | |
paul@129 | 104 | write_accessor_initialisation(IN_PARAMETER, CLIENT_ROLE, GENERAL_FUNCTION_ROLE, |
paul@129 | 105 | opname, fp); |
paul@100 | 106 | |
paul@100 | 107 | if (protocol != NULL) |
paul@142 | 108 | fprintf(fp, " in_words->_op = opcode_%s;\n", opname); |
paul@100 | 109 | |
paul@129 | 110 | write_message_access(param, CLIENT_ROLE, GENERAL_FUNCTION_ROLE, IN_PARAMETER, |
paul@129 | 111 | WORD_CLASS, fp); |
paul@100 | 112 | } |
paul@100 | 113 | |
paul@100 | 114 | /* Do the same for items. */ |
paul@100 | 115 | |
paul@100 | 116 | if (input_items) |
paul@100 | 117 | { |
paul@100 | 118 | fputs("\n", fp); |
paul@129 | 119 | write_message_access(param, CLIENT_ROLE, GENERAL_FUNCTION_ROLE, IN_PARAMETER, |
paul@129 | 120 | ITEM_CLASS, fp); |
paul@100 | 121 | } |
paul@100 | 122 | |
paul@100 | 123 | /* Send the request. */ |
paul@100 | 124 | |
paul@115 | 125 | fprintf(fp, client_function_body_call, opcode, "_endp"); |
paul@100 | 126 | |
paul@100 | 127 | /* Retrieve output parameters from the message. */ |
paul@100 | 128 | |
paul@100 | 129 | if (output_words) |
paul@100 | 130 | { |
paul@129 | 131 | write_accessor_initialisation(OUT_PARAMETER, CLIENT_ROLE, GENERAL_FUNCTION_ROLE, |
paul@129 | 132 | opname, fp); |
paul@129 | 133 | write_message_access(param, CLIENT_ROLE, GENERAL_FUNCTION_ROLE, OUT_PARAMETER, |
paul@129 | 134 | WORD_CLASS, fp); |
paul@100 | 135 | } |
paul@100 | 136 | |
paul@100 | 137 | /* Do the same for items. */ |
paul@100 | 138 | |
paul@100 | 139 | if (output_items) |
paul@100 | 140 | { |
paul@100 | 141 | fputs("\n", fp); |
paul@129 | 142 | write_message_access(param, CLIENT_ROLE, GENERAL_FUNCTION_ROLE, OUT_PARAMETER, |
paul@129 | 143 | ITEM_CLASS, fp); |
paul@100 | 144 | } |
paul@100 | 145 | |
paul@100 | 146 | /* Return success. */ |
paul@100 | 147 | |
paul@100 | 148 | fprintf(fp, "\n return L4_EOK;\n"); |
paul@100 | 149 | |
paul@100 | 150 | /* Free allocated strings. */ |
paul@100 | 151 | |
paul@100 | 152 | free(opname); |
paul@100 | 153 | |
paul@100 | 154 | if (protocol == NULL) |
paul@100 | 155 | free(opcode); |
paul@100 | 156 | } |
paul@117 | 157 | |
paul@117 | 158 | /* Populate an interface structure with client functions. */ |
paul@117 | 159 | |
paul@117 | 160 | void write_client_interface(struct interface *iface, FILE *fp) |
paul@117 | 161 | { |
paul@117 | 162 | struct signature *sig; |
paul@117 | 163 | char *opname; |
paul@117 | 164 | |
paul@117 | 165 | fprintf(fp, client_interface_prologue_c, iface->name, iface->name); |
paul@117 | 166 | |
paul@117 | 167 | for (sig = iface->signatures; sig != NULL; sig = sig->tail) |
paul@117 | 168 | { |
paul@117 | 169 | opname = get_operation_name(iface, sig); |
paul@117 | 170 | |
paul@117 | 171 | fprintf(fp, client_interface_member_c, sig->operation, opname); |
paul@117 | 172 | |
paul@117 | 173 | free(opname); |
paul@117 | 174 | } |
paul@117 | 175 | |
paul@121 | 176 | fputs(client_interface_epilogue_c, fp); |
paul@117 | 177 | } |