paul@100 | 1 | /* |
paul@100 | 2 | * Structure definition. |
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 "common.h" |
paul@100 | 24 | #include "declaration.h" |
paul@100 | 25 | #include "structure.h" |
paul@121 | 26 | #include "templates.h" |
paul@100 | 27 | |
paul@100 | 28 | /* Generate an enumeration of operator codes. */ |
paul@100 | 29 | |
paul@100 | 30 | void write_opcode_definition(struct interface *iface, FILE *fp) |
paul@100 | 31 | { |
paul@121 | 32 | fprintf(fp, opcode_enumeration_prologue, iface->name); |
paul@100 | 33 | write_opcodes(iface->signatures, fp, iface->name); |
paul@121 | 34 | fputs(opcode_enumeration_epilogue, fp); |
paul@100 | 35 | } |
paul@100 | 36 | |
paul@100 | 37 | /* Generate operator code enumeration entries. */ |
paul@100 | 38 | |
paul@100 | 39 | void write_opcodes(struct signature *sig, FILE *fp, const char *name) |
paul@100 | 40 | { |
paul@100 | 41 | char *opcode; |
paul@100 | 42 | |
paul@100 | 43 | if (sig == NULL) |
paul@100 | 44 | return; |
paul@100 | 45 | |
paul@121 | 46 | fprintf(fp, opcode_identifier, name, sig->operation); |
paul@100 | 47 | |
paul@100 | 48 | /* Obtain any explicit opcode from the signature attributes. */ |
paul@100 | 49 | |
paul@100 | 50 | opcode = get_opcode(sig->attributes); |
paul@100 | 51 | if (opcode != NULL) |
paul@100 | 52 | fprintf(fp, " = %s", opcode); |
paul@100 | 53 | |
paul@100 | 54 | fputs(",\n", fp); |
paul@100 | 55 | |
paul@100 | 56 | write_opcodes(sig->tail, fp, name); |
paul@100 | 57 | } |
paul@100 | 58 | |
paul@100 | 59 | /* Generate structures for messages corresponding to the signatures. */ |
paul@100 | 60 | |
paul@100 | 61 | void write_structures(struct signature *sig, FILE *fp, struct interface *iface) |
paul@100 | 62 | { |
paul@100 | 63 | if (sig == NULL) |
paul@100 | 64 | return; |
paul@100 | 65 | |
paul@100 | 66 | write_structure(sig, fp, iface); |
paul@100 | 67 | write_structures(sig->tail, fp, iface); |
paul@100 | 68 | } |
paul@100 | 69 | |
paul@100 | 70 | /* Generate message structures for a signature. */ |
paul@100 | 71 | |
paul@100 | 72 | void write_structure(struct signature *sig, FILE *fp, struct interface *iface) |
paul@100 | 73 | { |
paul@100 | 74 | struct parameter *param = sig->parameters; |
paul@100 | 75 | char *opname = get_operation_name(iface, sig), |
paul@208 | 76 | *protocol = get_protocol(iface->attributes), |
paul@208 | 77 | *opcode_type; |
paul@100 | 78 | int input_words, input_items, output_words, output_items; |
paul@100 | 79 | |
paul@100 | 80 | count_parameters(param, &input_words, &input_items, &output_words, &output_items); |
paul@100 | 81 | |
paul@144 | 82 | /* Adjust the input words where a protocol is used as the opcode. */ |
paul@144 | 83 | |
paul@144 | 84 | if (protocol != NULL) |
paul@144 | 85 | input_words++; |
paul@144 | 86 | |
paul@144 | 87 | /* Generate the structures for populating and interpreting messages. */ |
paul@144 | 88 | |
paul@100 | 89 | if (input_words) |
paul@100 | 90 | { |
paul@121 | 91 | fprintf(fp, structure_prologue, "in", opname); |
paul@100 | 92 | if (protocol != NULL) |
paul@207 | 93 | { |
paul@208 | 94 | opcode_type = get_opcode_type(sig->attributes); |
paul@208 | 95 | |
paul@208 | 96 | if (opcode_type != NULL) |
paul@208 | 97 | fprintf(fp, structure_opcode_member, opcode_type); |
paul@208 | 98 | else |
paul@208 | 99 | fprintf(fp, structure_opcode_member, L4_OPCODE_TYPE); |
paul@207 | 100 | } |
paul@100 | 101 | write_declarations(param, IN_PARAMETER, WORD_CLASS, fp); |
paul@121 | 102 | fputs(structure_epilogue, fp); |
paul@100 | 103 | } |
paul@100 | 104 | |
paul@100 | 105 | if (output_words) |
paul@100 | 106 | { |
paul@121 | 107 | fprintf(fp, structure_prologue, "out", opname); |
paul@100 | 108 | write_declarations(param, OUT_PARAMETER, WORD_CLASS, fp); |
paul@121 | 109 | fputs(structure_epilogue, fp); |
paul@100 | 110 | } |
paul@100 | 111 | |
paul@100 | 112 | /* Free allocated strings. */ |
paul@100 | 113 | |
paul@100 | 114 | free(opname); |
paul@100 | 115 | } |