From f86d46c72eeff2c3fe96d8c2103e824db5b578a5 Mon Sep 17 00:00:00 2001 From: Christoph Reimann Date: Tue, 13 Jul 2010 19:52:58 +0200 Subject: fix: unserialize & serialize, automatic padding, _aux() functions tested with switch replacement for xproto's CreateWindow.value_list --- src/c_client.py | 692 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 412 insertions(+), 280 deletions(-) diff --git a/src/c_client.py b/src/c_client.py index bd7d4c5..469a7f5 100755 --- a/src/c_client.py +++ b/src/c_client.py @@ -274,10 +274,13 @@ def _c_type_setup(self, name, postfix): self.c_cookie_type = _t(name + ('cookie',)) self.c_aux_name = _n(name + ('aux',)) + self.c_aux_checked_name = _n(name + ('aux', 'checked')) + self.c_aux_unchecked_name = _n(name + ('aux', 'unchecked')) self.c_serialize_name = _n(name + ('serialize',)) self.c_unserialize_name = _n(name + ('unserialize',)) # whether a request or reply has a switch field + self.need_aux = False self.need_serialize = False if self.is_switch: self.need_serialize = True @@ -292,6 +295,10 @@ def _c_type_setup(self, name, postfix): first_field_after_varsized = None for field in self.fields: + # information about a fields anchestors + if self.is_reply or hasattr(self, 'in_reply'): + field.type.in_reply = True + _c_type_setup(field.type, field.field_type, ()) if field.type.is_list: _c_type_setup(field.type.member, field.field_type, ()) @@ -304,6 +311,7 @@ def _c_type_setup(self, name, postfix): if field.type.is_switch: field.c_pointer = '*' field.c_field_const_type = 'const ' + field.c_field_type + self.need_aux = True field.c_iterator_type = _t(field.field_type + ('iterator',)) # xcb_fieldtype_iterator_t field.c_iterator_name = _n(name + (field.field_name, 'iterator')) # xcb_container_field_iterator @@ -333,15 +341,19 @@ def _c_type_setup(self, name, postfix): # special: switch C structs get pointer fields for variable-sized members _c_complex(self) # declare switch (un)packing functions -# _c_switch_aux(self) _c_accessors(self, name, name) -# _c_iterator(self, name) + if self.need_serialize: - _c_serialize(self) + if not hasattr(self, 'in_reply'): + _c_serialize(self) + _c_unserialize(self) + +# _c_type_setup() def get_request_fields(self): param_fields = [] wire_fields = [] + for field in self.fields: if field.visible: # the field should appear as a parameter in the function call @@ -351,44 +363,138 @@ def get_request_fields(self): # need to set the field up in the xcb_out structure wire_fields.append(field) # fields like 'pad0' are skipped! + return (param_fields, wire_fields) +# get_request_fields() -def get_variable_size_fields(self): - var_fields = [] - for field in self.fields: - if not field.type.fixed_size(): - var_fields.append(field) - return var_fields +def unserialize_fields(complex_type, code_lines=[], space='', prefix='', bitcase=False): + prefix_str = prefix + need_padding = False + + if prefix != '': + prefix_str += "->" + if hasattr(complex_type, 'type'): + self = complex_type.type + complex_name = complex_type.name + else: + self = complex_type + complex_name = '_aux' + + if self.is_switch: + switch_expr = _c_accessor_get_expr(self.expr) + need_padding = True + for b in self.bitcases: + bitcase_expr = _c_accessor_get_expr(b.type.expr, prefix) + code_lines.append(' if(%s & %s) {' % (switch_expr, bitcase_expr)) + unserialize_fields(b.type, code_lines, space="%s " % space, + prefix="%s%s" % (prefix_str, complex_name), bitcase=True) + code_lines.append(' }') + + else: + for field in self.fields: + if not ((field.wire and not field.auto) or field.visible): + continue + + length = "sizeof(%s)" % field.c_field_type + + # 1. fields with fixed size + if field.type.fixed_size(): + need_padding = True + value = ' _aux->%s = (%s) *xcb_tmp;' % (field.c_field_name, field.type.c_type) + # FIXME - lists + + # 2. fields with variable size + else: + #if need_padding: + # unserialize: always calculate padding before variable sized fields + code_lines.append('%s if (0 != xcb_block_len) {' % space) + code_lines.append('%s xcb_block_len += -xcb_block_len & 3;' % space) + code_lines.append('%s xcb_tmp += xcb_block_len;' % space) + code_lines.append('%s xcb_buffer_len += xcb_block_len;' % space) + code_lines.append('%s xcb_block_len = 0;' % space) + code_lines.append('%s }' % space) + + # FIXME + print("unserialize not yet implemented for variable size fields like %s" % field.c_field_name) + + value = ' xcb_parts[xcb_parts_idx].iov_base = (char *) %s%s;' % (prefix_str, field.c_field_name) + if field.type.is_list: + # FIXME - list with variable-sized elements + if field.type.size is None: + errmsg = '%s: warning: list object with variable-sized members not supported for field %s\n' + sys.stderr.write(errmsg % (self.c_type, field.c_field_name)) + length = '%s * sizeof(%s)' % (_c_accessor_get_expr(field.type.expr, prefix), field.type.member.c_wiretype) + elif field.type.is_switch: + # switch is handled by this function as a special case + unserialize_fields(field.type, code_lines, space, prefix="%s%s" % (prefix_str, field.c_field_name)) + else: + # FIXME - variable sized field that is not a list + errmsg = '%s: warning: non-list object of variable size not supported for field %s\n' + sys.stderr.write(errmsg % (self.c_type, field.c_field_name)) + length = '%s * sizeof(%s)' % ('Uh oh', field.type.c_wiretype) + + # save serialization C code + if value is not None: + if field.type.fixed_size(): + # field appears in the request structure + code_lines.append('%s /* %s.%s */' % (space, self.c_type, field.c_field_name)) + else: + code_lines.append('%s /* %s */' % (space, field.c_field_name)) -def unserialize_field(complex_type): + # _aux->XXX = + code_lines.append('%s%s' % (space, value)) + if field.type.fixed_size(): + code_lines.append('%s xcb_block_len += %s;' % (space, length)) + code_lines.append('%s xcb_tmp += %s;' % (space, length)) + else: + # take account of padding + code_lines.append('%s xcb_block_len = %s;' % (space, length)) + code_lines.append('%s xcb_block_len += -xcb_block_len & 3;' % space) + code_lines.append('%s xcb_tmp += xcb_block_len;' % space) + code_lines.append('%s xcb_buffer_len += xcb_block_len;' % space) + code_lines.append('%s xcb_block_len = 0;' % space) + + if not bitcase: + code_lines.append('%s xcb_block_len += -xcb_block_len & 3;' % space) + #code_lines.append('%s xcb_tmp += xcb_block_len;' % space) + code_lines.append('%s xcb_buffer_len += xcb_block_len;' % space) + +# unserialize_fields() -def serialize_fields(complex_type, new_block=True, code_lines=[], temp_vars=set(), - space='', prefix='', serialize_fixed_size_fields=False): +def serialize_fields(complex_type, code_lines=[], temp_vars=set(), + space='', prefix='', serialize_fixed_size_fields=False, + bitcase=False): """ helper routine to build up iovec arrays that will later be copied into a single buffer - self - encapsulating Type object - count - current index for xcb_parts array + complex_type - encapsulating Type/Field code_lines, temp_vars - containers for generated code & variable declarations space - extra space to be inserted before any statement prefix - prefix to be used for struct members, needed for switch/bitcase mapping + bitcase - flags whether fields are bitcase members """ + # count -> no. of entries in xcb_parts array count = 0 - need_padding = False + # flag to mark wether explicit padding needs to be inserted + need_padding = False + prefix_str = prefix + if prefix != '': + prefix_str += "->" + if hasattr(complex_type, 'type'): self = complex_type.type complex_name = complex_type.name else: self = complex_type - complex_name = 'aux' + complex_name = '_aux' def end_block(count): "end a block => insert padding" - if count > 0 and new_block: + if need_padding and not bitcase: code_lines.append(' /* padding */') - code_lines.append(' xcb_parts[xcb_parts_idx].iov_base = 0;') + code_lines.append(' xcb_parts[xcb_parts_idx].iov_base = xcb_pad0;') code_lines.append(' xcb_pad = -xcb_block_len & 3;') code_lines.append(' xcb_parts[xcb_parts_idx].iov_len = xcb_pad;') code_lines.append(' xcb_parts_idx++;') @@ -397,105 +503,133 @@ def serialize_fields(complex_type, new_block=True, code_lines=[], temp_vars=set( code_lines.append('') count += 1 return count - - def start_block(count): - """starting a new block implies adding padding for the previous block, if any, - and storing and resetting counters""" - if new_block: - count = end_block(count) - # reset counters - code_lines.append(' xcb_block_len = 0;') - return count + # end_block() + + def insert_padding(count): + code_lines.append('%s /* implicit padding */' % space) + code_lines.append('%s xcb_pad = -xcb_block_len & 3;' % space) + code_lines.append('%s if (0 != xcb_pad) {' % space) + code_lines.append('%s xcb_parts[xcb_parts_idx].iov_base = xcb_pad0;' % space) + code_lines.append('%s xcb_parts[xcb_parts_idx].iov_len = xcb_pad;' % space) + code_lines.append('%s xcb_buffer_len += xcb_block_len + xcb_pad;' % space) + code_lines.append('%s xcb_pad = 0;' % space) + code_lines.append('%s xcb_parts_idx++;' % space) + code_lines.append('%s }' % space) + return count + 1 + # insert_padding() - count += start_block(count) - value = None # special case - if self.is_switch, all fields need to be serialized conditionally if self.is_switch: switch_expr = _c_accessor_get_expr(self.expr) - for bitcase in self.bitcases: - bitcase_expr = _c_accessor_get_expr(bitcase.type.expr, prefix) + need_padding = True + + for b in self.bitcases: + bitcase_expr = _c_accessor_get_expr(b.type.expr, prefix) code_lines.append(' if(%s & %s) {' % (switch_expr, bitcase_expr)) - count += serialize_fields(bitcase.type, False, code_lines, temp_vars, '%s ' % space, - prefix="%s%s->" % (prefix, complex_name), - serialize_fixed_size_fields=True) + + count += serialize_fields(b.type, code_lines, temp_vars, '%s ' % space, + prefix="%s%s" % (prefix_str, complex_name), + serialize_fixed_size_fields=True, bitcase=True) code_lines.append(' }') + else: for field in self.fields: + value = None + + # sort out invisible fields + if not ((field.wire and not field.auto) or field.visible): + continue + # else + length = "sizeof(%s)" % field.c_field_type + # 1. fields with fixed size - if (field.wire and not field.auto) or field.visible: - length = "sizeof(%s)" % field.c_field_type - # note: this code is currently used only for testing - if field.type.fixed_size() and serialize_fixed_size_fields: - value = ' xcb_parts[xcb_parts_idx].iov_base = (char *) ' - if field.type.is_expr: - # need to register a temporary variable for the expression - if field.type.c_type is None: - raise Exception("type for field '%s' (expression '%s') unkown" % - (field.field_name, _c_accessor_get_expr(field.type.expr))) - temp_vars.add(' %s xcb_expr_%s = %s;' % (field.type.c_type, field.field_name, - _c_accessor_get_expr(field.type.expr, prefix))) - value += "&xcb_expr_%s;" % field.field_name - elif field.type.is_pad: - if field.type.nmemb == 1: - temp_vars.add(' unsigned int xcb_pad = 0;') - value += "&xcb_pad;" - else: - value = ' memset(xcb_parts[xcb_parts_idx].iov_base, 0, %d);' % field.type.nmemb - length += "*%d" % field.type.nmemb + # fixed size fields are not always handled here, + # dependent on serialize_fixed_size_fields + if field.type.fixed_size() and serialize_fixed_size_fields: + value = ' xcb_parts[xcb_parts_idx].iov_base = (char *) ' + need_padding = True + + if field.type.is_expr: + # need to register a temporary variable for the expression + if field.type.c_type is None: + raise Exception("type for field '%s' (expression '%s') unkown" % + (field.field_name, _c_accessor_get_expr(field.type.expr))) + temp_vars.add(' %s xcb_expr_%s = %s;' % (field.type.c_type, field.field_name, + _c_accessor_get_expr(field.type.expr, prefix))) + value += "&xcb_expr_%s;" % field.field_name + + elif field.type.is_pad: + if field.type.nmemb == 1: + temp_vars.add(' unsigned int xcb_pad = 0;') + temp_vars.add(' char xcb_pad0[3] = {0, 0, 0};') + value += "&xcb_pad;" else: - if field.type.nmemb == 1: - value += "&%s%s;" % (prefix, field.c_field_name) - else: - if field.type.is_container: - # FIXME - call serialize function - pass - #count = serialize_field(field.type, count) - else: - value += '%s%s;' % (prefix, field.c_field_name) - length = '%d' % field.type.nmemb - - # 2. fields with variable size - elif not field.type.fixed_size(): - if serialize_fixed_size_fields: - code_lines.append('%s xcb_parts[xcb_parts_idx].iov_base = 0;' % space) - code_lines.append('%s xcb_pad = -xcb_unpadded & 3;' % space) - code_lines.append('%s xcb_parts[xcb_parts_idx].iov_len = xcb_pad;' % space) - code_lines.append('%s xcb_block_len += xcb_pad;' % space) - code_lines.append('%s xcb_parts_idx++;' % space) - count += 1 - # field must appear in the request API - value = ' xcb_parts[xcb_parts_idx].iov_base = (char *) %s%s;' % (prefix, field.c_field_name) - if field.type.is_list: - # FIXME - list with variable-sized elements - if field.type.size is None: - errmsg = '%s: warning: list object with variable-sized members not supported for field %s\n' - sys.stderr.write(errmsg % (self.c_type, field.c_field_name)) - length = '%s * sizeof(%s)' % (_c_accessor_get_expr(field.type.expr), field.type.member.c_wiretype) - elif field.type.is_switch: - # switch is handled by this function as a special case - count += serialize_fields(field.type, True, code_lines, temp_vars, space, - prefix="%s%s->" % (prefix, field.c_field_name)) + value = ' memset(xcb_parts[xcb_parts_idx].iov_base, 0, %d);' % field.type.nmemb + length += "*%d" % field.type.nmemb + + else: + # non-list type with fixed size + if field.type.nmemb == 1: + value += "&%s%s;" % (prefix_str, field.c_field_name) + # list with nmemb (fixed size) elements else: - # FIXME - variable sized field that is not a list - errmsg = '%s: warning: non-list object of variable size not supported for field %s\n' - sys.stderr.write(errmsg % (self.c_type, field.c_field_name)) - length = '%s * sizeof(%s)' % ('Uh oh', field.type.c_wiretype) + value += '%s%s;' % (prefix_str, field.c_field_name) + length = '%d' % field.type.nmemb - # save serialization C code - if value is not None: - if field.type.fixed_size(): - # field appears in the request structure - code_lines.append('%s /* %s.%s */' % (space, self.c_type, field.c_field_name)) - else: - code_lines.append('%s /* %s */' % (space, field.c_field_name)) - code_lines.append('%s%s' % (space, value)) - code_lines.append('%s xcb_parts[xcb_parts_idx].iov_len = %s;' % (space, length)) - if field.type.fixed_size(): - code_lines.append('%s %s += %s;' % (space, 'xcb_block_len', length)) + # 2. fields with variable size + elif not field.type.fixed_size(): + # always calculate padding before variable sized fields + count = insert_padding(count) + code_lines.append('%s xcb_block_len = 0;' % space) + + value = ' xcb_parts[xcb_parts_idx].iov_base = (char *) %s%s;' % (prefix_str, field.c_field_name) + + if field.type.is_list: + # FIXME - list of variable length with variable size elements + if field.type.size is None: + errmsg = '%s: warning: list object with variable-sized members not supported for field %s\n' + sys.stderr.write(errmsg % (self.c_type, field.c_field_name)) + length = 'undefined' + # list of variable length with fixed size elements else: - code_lines.append('%s xcb_unpadded = xcb_parts[xcb_parts_idx].iov_len;' % space) - code_lines.append('%s xcb_parts_idx++;' % space) - count += 1 + length = '%s * sizeof(%s)' % (_c_accessor_get_expr(field.type.expr, prefix), + field.type.member.c_wiretype) + + elif field.type.is_switch: + # switch is handled at the beginning of this function as a special case + count += serialize_fields(field.type, code_lines, temp_vars, space, + prefix="%s%s" % (prefix_str, field.c_field_name)) + + else: + # FIXME - variable sized field that is not a list + errmsg = '%s: warning: non-list object of variable size not supported for field %s\n' + sys.stderr.write(errmsg % (self.c_type, field.c_field_name)) + length = '%s * sizeof(%s)' % ('Uh oh', field.type.c_wiretype) + + # 3. save serialization C code + if value is not None: + # insert a comment so one can easily trace back to the XML + if field.type.fixed_size(): + # field belongs to some anchestor structure + code_lines.append('%s /* %s.%s */' % (space, self.c_type, field.c_field_name)) + else: + code_lines.append('%s /* %s */' % (space, field.c_field_name)) + + # set xcb_parts[].iov_base and xcb_parts[].iov_len + code_lines.append('%s%s' % (space, value)) + code_lines.append('%s xcb_parts[xcb_parts_idx].iov_len = %s;' % (space, length)) + # increase xcb_parts index + code_lines.append('%s xcb_parts_idx++;' % space) + count += 1 + + # record required memory + if field.type.fixed_size(): + code_lines.append('%s xcb_block_len += %s;' % (space, length)) + else: + # FIXME + count = insert_padding(count) + # raise Exception("obsolete - should not be reached") + # code_lines.append('%s xcb_unpadded = xcb_parts[xcb_parts_idx].iov_len;' % space) count = end_block(count) if count > 0: @@ -503,10 +637,9 @@ def serialize_fields(complex_type, new_block=True, code_lines=[], temp_vars=set( temp_vars.add(' unsigned int xcb_block_len = 0;') if need_padding: temp_vars.add(' unsigned int xcb_pad = 0;') + temp_vars.add(' char xcb_pad0[3] = {0, 0, 0};') return count - -#count = 2 -#count, need_padding = evaluate_field(self, count) +# serialize_fields() def _c_switch_aux_params(self): # get the fields referenced by the switch expression @@ -525,30 +658,37 @@ def _c_switch_aux_params(self): return [] else: return get_expr_fields(expr.lhs) + get_expr_fields(expr.rhs) + # get_expr_fields() # resolve the field names with the parent structure(s) - expr_fields = dict.fromkeys(get_expr_fields(self.expr)) + unresolved_fields = get_expr_fields(self.expr) + expr_fields = dict.fromkeys(unresolved_fields) for p in reversed(self.parent): - # FIXME: c_field_name - fields = dict((f.field_name, f) for f in p.fields) - for f in expr_fields: - if f in fields: - expr_fields[f] = fields[f] + parent_fields = dict((f.field_name, f) for f in p.fields) + if len(unresolved_fields) == 0: + break + for f in parent_fields.keys(): + if f in unresolved_fields: + expr_fields[f] = parent_fields[f] + unresolved_fields.remove(f) + if None in expr_fields.values(): raise Exception("could not resolve all fields for %s" % self.name) + params = [] for name, field in expr_fields.iteritems(): params.append((field, name)) + return params +# _c_switch_aux_params() -def get_serialize_params(self, buffer_var='_buffer', aux_var='_aux'): +def get_serialize_params(self, buffer_var='_buffer', aux_var='_aux', unserialize=False): param_fields, wire_fields = get_request_fields(self) if self.is_switch: switch_params = _c_switch_aux_params(self) param_fields += [s[0] for s in switch_params] # _serialize function parameters - # buffer argument params = [('void', '**', buffer_var)] # parameter fields if any if self.is_switch: @@ -557,12 +697,16 @@ def get_serialize_params(self, buffer_var='_buffer', aux_var='_aux'): pointerspec = p[0].c_pointer params.append((typespec, pointerspec, p[0].c_field_name)) # aux argument - structure to be serialized - params.append(('const %s' % self.c_type, '*', aux_var)) + if not unserialize: + params.append(('const %s' % self.c_type, '*', aux_var)) + else: + params.append(('%s' % self.c_type, '*', aux_var)) if not self.is_switch: for p in param_fields: if not p.type.fixed_size(): params.append((p.c_field_const_type, p.c_pointer, p.c_field_name)) return (param_fields, wire_fields, params) +# get_serialize_params() def _c_serialize(self): _h_setlevel(1) @@ -582,24 +726,20 @@ def _c_serialize(self): # if self.is_switch, treat all fields as if they are variable sized if not field.type.fixed_size() or self.is_switch: variable_size_fields += 1 - # determine + # determine maxtypelen for p in params: maxtypelen = max(maxtypelen, len(p[0]) + len(p[1])) -# typespec = "void" -# pointerspec = '**' -# spacing = ' '*(maxtypelen-len(typespec)-len(pointerspec)) -# codelines = ['%s (%s%s %sbuffer,' % (self.c_serialize_name, typespec, spacing, pointerspec)] # write to .c/.h for p in range(len(params)): line = "" typespec, pointerspec, field_name = params[p] indent = ' '*(len(self.c_serialize_name)+2) - spacing = ' '*(maxtypelen-len(typespec)-len(pointerspec)) # p==0: function declaration if 0==p: line = "%s (" % self.c_serialize_name indent = '' + spacing = ' '*(maxtypelen-len(typespec)-len(pointerspec)) line += "%s%s%s %s%s /**< */" % (indent, typespec, spacing, pointerspec, field_name) if p < len(params)-1: _hc("%s," % line) @@ -607,24 +747,17 @@ def _c_serialize(self): _h("%s);" % line) _c("%s)" % line) -# typespec = p.c_field_const_type -# pointerspec = p.c_pointer -# spacing = ' '*(maxtypelen-len(typespec)-len(pointerspec)) -# codelines.append('%s %s%s %s%s,' % (' '*len(self.c_serialize_name), -# typespec, spacing, pointerspec, p.c_field_name)) - -# for l in range(len(codelines)-1): -# _hc(codelines[l]) -# last_param = codelines[-1].rstrip(',') -# _h("%s);", last_param) -# _c("%s)", last_param) - _c('{') - _c(' %s *xcb_out = *_buffer;', self.c_type) + if not self.is_switch: + _c(' %s *xcb_out = *_buffer;', self.c_type) + _c(' unsigned int xcb_buffer_len = sizeof(%s);', self.c_type) + else: + _c(' char *xcb_out = *_buffer;') + _c(' unsigned int xcb_buffer_len = 0;') if variable_size_fields > 0: code_lines = [] temp_vars = set() - count = serialize_fields(self, True, code_lines, temp_vars, + count = serialize_fields(self, code_lines, temp_vars, serialize_fixed_size_fields=False) # update variable size fields variable_size_fields = count @@ -633,55 +766,16 @@ def _c_serialize(self): _c(t) _c(' char *xcb_tmp;') _c(' unsigned int i;') - _c(' unsigned int xcb_buffer_len = sizeof(%s);', self.c_type) - _c(' unsigned int xcb_out_pad = -xcb_buffer_len & 3;') - _c(' /* add size of padding */') - _c(' xcb_buffer_len += xcb_out_pad;') + if not self.is_switch: + _c(' unsigned int xcb_out_pad = -xcb_buffer_len & 3;') + _c(' /* add size of padding */') + _c(' xcb_buffer_len += xcb_out_pad;') + _c('') if variable_size_fields > 0: for l in code_lines: _c(l) - # switch needs special treatment -# if self.is_switch: -# switch_expr = _c_accessor_get_expr(self.expr) -# code_lines = [] -# temp_vars = set() -# print "switch:" -# count, need_padding = serialize_fields(self, 0, code_lines, temp_vars, '', 'aux->') -# for i in code_lines: -# _c(i) -# for bitcase in self.bitcases: -# bitcase_expr = _c_accessor_get_expr(bitcase.type.expr, "_aux") -# _c(' if(%s & %s) {' % (switch_expr, bitcase_expr)) -# count, need_padding = evaluate_field(bitcase.type, count, '%s ' % space, -# "%s%s->" % (prefix, "_aux")) -# _c(' }') - # eeeeeee - # if there are fields with variable size, collect them into xcb_parts array -# elif variable_size_fields > 0: -# _c(' /* collect fields with variable size and update buffer length */') -# count = 0 -# for field in param_fields[1:]: -# if not field.type.fixed_size(): -# if field.type.is_list: -# _c(' xcb_parts[%d].iov_base = (char *) %s;', count, field.c_field_name) -# _c(' xcb_parts[%d].iov_len = %s * sizeof(%s);', count, -# _c_accessor_get_expr(field.type.expr), field.type.member.c_wiretype) -# elif field.type.is_switch: - # FIXME -# pass - # else: -# # FIXME -# _c(' xcb_tmp = NULL;') -# var_fields = get_variable_size_fields(field.type) -# var_fields = reduce(lambda x,y: "%s, %s" % (x,y), [v.c_field_name for v in var_fields]) -# _c(' xcb_parts[%d].iov_len = %s(&xcb_tmp, %s);', count, field.type.c_serialize_name, field.c_field_n#ame) -# _c(' xcb_parts[%d].iov_len = %s * sizeof(%s);', count, 'Uh oh', field.type.c_wiretype) -# _c(' xcb_buffer_len += xcb_parts[%s].iov_len;', count) -# _c(' xcb_buffer_len += -xcb_parts[%d].iov_len & 3;', count) -# count += 1 -# _c('') # variable sized fields have been collected, now # allocate memory and copy everything into a continuous memory area @@ -694,69 +788,85 @@ def _c_serialize(self): # fill in struct members if not self.is_switch: -# for field in wire_fields: -# if field.type.fixed_size(): -# if field.type.is_expr: -# _c(' xcb_out->%s = %s;', field.c_field_name, _c_accessor_get_expr(field.type.expr)) -# elif field.type.is_pad: -# if field.type.nmemb == 1: -# _c(' xcb_out->%s = 0;', field.c_field_name) -# else: -# _c(' memset(xcb_out->%s, 0, %d);', field.c_field_name, field.type.nmemb) -# else: -# if field.type.nmemb == 1: -# _c(' xcb_out->%s = %s;', field.c_field_name, field.c_field_name) -# else: -# _c(' memcpy(xcb_out->%s, %s, %d);', field.c_field_name, field.c_field_name, field.type.nmemb) -# _c('') if len(wire_fields)>0: - _c(' *xcb_out = *aux;') + _c(' *xcb_out = *_aux;') # copy variable size fields into the buffer if variable_size_fields > 0: # xcb_out padding - _c(' xcb_tmp = (char*)++xcb_out;') - _c(' xcb_tmp += xcb_out_pad;') + if not self.is_switch: + _c(' xcb_tmp = (char*)++xcb_out;') + _c(' xcb_tmp += xcb_out_pad;') + else: + _c(' xcb_tmp = xcb_out;') + # variable sized fields _c(' for(i=0; i0 else aux_param - _c('%s (%s', self.c_aux_name, param0) - if len(params) == 0: - param0 += ';' - _h('%s (%s', self.c_aux_name, param0) - if len(params) > 0: - for p in params[1:]: - _hc('%s%s %s, /**< */', ' '*(len(self.c_aux_name)+2), p[0], p[1]) - _c('%sconst %s *aux /**< */)', ' '*(len(self.c_aux_name)+2), self.c_type) - _h('%sconst %s *aux /**< */);', ' '*(len(self.c_aux_name)+2), self.c_type) - - params = reduce(lambda x,y: "%s, %s" % (x,y), [p[1] for p in params]) + # _unserialize() returns the buffer size as well + _hc('int') + + + variable_size_fields = 0 + # maximum space required for type definition of function arguments + maxtypelen = 0 + param_fields, wire_fields, params = get_serialize_params(self, unserialize=True) + + # determine N(variable_fields) + for field in param_fields: + # if self.is_switch, treat all fields as if they are variable sized + if not field.type.fixed_size() or self.is_switch: + variable_size_fields += 1 + # determine maxtypelen + for p in params: + maxtypelen = max(maxtypelen, len(p[0]) + len(p[1])) + + # write to .c/.h + for p in range(len(params)): + line = "" + typespec, pointerspec, field_name = params[p] + indent = ' '*(len(self.c_unserialize_name)+2) + # p==0: function declaration + if 0==p: + line = "%s (" % self.c_unserialize_name + indent = '' + pointerspec = '*' + spacing = ' '*(maxtypelen-len(typespec)-len(pointerspec)) + line += "%s%s%s %s%s /**< */" % (indent, typespec, spacing, pointerspec, field_name) + if p < len(params)-1: + _hc("%s," % line) + else: + _h("%s);" % line) + _c("%s)" % line) + _c('{') - _c(' char *buffer = NULL;') - _c(' %s(&buffer, %s, aux);', self.c_serialize_name, params) - _c(' // FIXME: call request function') - _c(' free(*buffer);') + _c(' char *xcb_tmp = _buffer;') + _c(' unsigned int xcb_buffer_len = 0;') + _c(' unsigned int xcb_block_len = 0;') + _c('') + code_lines = [] + unserialize_fields(self, code_lines) + for l in code_lines: + _c(l) + _c('') + _c(' return xcb_buffer_len;') _c('}') +# _c_unserialize() def _c_iterator_get_end(field, accum): ''' @@ -905,13 +1015,20 @@ def _c_accessor_get_expr(expr, prefix=''): c_name = _n(enum_name + (constant_name,)).upper() return c_name elif expr.op == 'sumof': -# print prefix -# print expr.lenfield_name -# print dir(expr) -# print expr.lenfield_parent -# print expr.parent -# raise "" - return 'xcb_sumof()' + # 1. locate the referenced list object + list_obj = expr.lenfield_type + field = None + for f in expr.lenfield_parent.fields: + if f.field_name == expr.lenfield_name: + field = f + break + if field is None: + raise Exception("list field '%s' referenced by sumof not found" % expr.lenfield_name) + if prefix != '': + prefix = "%s->" % prefix + list_name = "%s%s" % (prefix, field.c_field_name) + c_length_func = "%s(%s%s)" % (field.c_length_name, prefix, field.c_field_name) + return 'xcb_sumof(%s, %s)' % (list_name, c_length_func) elif expr.op != None: return '(' + _c_accessor_get_expr(expr.lhs, prefix) + ' ' + expr.op + ' ' + _c_accessor_get_expr(expr.rhs, prefix) + ')' elif expr.bitfield: @@ -1143,7 +1260,6 @@ def _c_complex(self): spacing = ' ' * (maxtypelen - (len(field.c_field_type) - 1)) _h(' %s%s *%s%s; /**< */', field.c_field_type, spacing, field.c_field_name, field.c_subscript) -# print "c_complex", [f.c_field_name for f in struct_fields] _h('} %s;', self.c_type) def c_struct(self, name): @@ -1163,7 +1279,7 @@ def c_union(self, name): _c_complex(self) _c_iterator(self, name) -def _c_request_helper(self, name, cookie_type, void, regular): +def _c_request_helper(self, name, cookie_type, void, regular, aux=False): ''' Declares a request function. ''' @@ -1196,15 +1312,16 @@ def _c_request_helper(self, name, cookie_type, void, regular): func_ext_global = '&' + _ns.c_ext_global_name if _ns.is_ext else '0' # What our function name is - func_name = self.c_request_name + func_name = self.c_request_name if not aux else self.c_aux_name if checked: - func_name = self.c_checked_name + func_name = self.c_checked_name if not aux else self.c_aux_checked_name if unchecked: - func_name = self.c_unchecked_name + func_name = self.c_unchecked_name if not aux else self.c_aux_unchecked_name param_fields = [] wire_fields = [] maxtypelen = len('xcb_connection_t') + serial_fields = [] for field in self.fields: if field.visible: @@ -1213,9 +1330,13 @@ def _c_request_helper(self, name, cookie_type, void, regular): if field.wire and not field.auto: # We need to set the field up in the structure wire_fields.append(field) + if field.type.need_serialize: + serial_fields.append(field) for field in param_fields: - c_field_const_type = field.c_field_const_type if not field.type.need_serialize else "const void" + c_field_const_type = field.c_field_const_type + if field.type.need_serialize and not aux: + c_field_const_type = "const void" if len(c_field_const_type) > maxtypelen: maxtypelen = len(c_field_const_type) @@ -1249,7 +1370,9 @@ def _c_request_helper(self, name, cookie_type, void, regular): _hc(' ** @param xcb_connection_t%s *c', spacing) for field in param_fields: - c_field_const_type = field.c_field_const_type if not field.type.need_serialize else "const void" + c_field_const_type = field.c_field_const_type + if field.type.need_serialize and not aux: + c_field_const_type = "const void" spacing = ' ' * (maxtypelen - len(c_field_const_type)) _hc(' ** @param %s%s %s%s', c_field_const_type, spacing, field.c_pointer, field.c_field_name) @@ -1269,22 +1392,26 @@ def _c_request_helper(self, name, cookie_type, void, regular): count = len(param_fields) for field in param_fields: count = count - 1 - c_field_const_type = field.c_field_const_type if not field.type.need_serialize else "const void" + c_field_const_type = field.c_field_const_type + if field.type.need_serialize and not aux: + c_field_const_type = "const void" spacing = ' ' * (maxtypelen - len(c_field_const_type)) comma = ',' if count else ');' - _h('%s%s%s %s%s /**< */%s', func_spacing, c_field_const_type, spacing, field.c_pointer, field.c_field_name, comma) + _h('%s%s%s %s%s /**< */%s', func_spacing, c_field_const_type, + spacing, field.c_pointer, field.c_field_name, comma) comma = ',' if count else ')' - _c('%s%s%s %s%s /**< */%s', func_spacing, c_field_const_type, spacing, field.c_pointer, field.c_field_name, comma) + _c('%s%s%s %s%s /**< */%s', func_spacing, c_field_const_type, + spacing, field.c_pointer, field.c_field_name, comma) count = 2 for field in param_fields: if not field.type.fixed_size(): count = count + 2 + if field.type.need_serialize: + # _serialize() keeps track of padding automatically + count -= 1 _c('{') -# _c(' unsigned int xcb_unpadded = 0;') -# for v in temp_vars: -# _c(v) _c(' static const xcb_protocol_request_t xcb_req = {') _c(' /* count */ %d,', count) _c(' /* ext */ %s,', func_ext_global) @@ -1294,33 +1421,12 @@ def _c_request_helper(self, name, cookie_type, void, regular): _c(' ') _c(' struct iovec xcb_parts[%d];', count + 2) -# _c(' unsigned int xcb_parts_idx = 2;') _c(' %s xcb_ret;', func_cookie) _c(' %s xcb_out;', self.c_type) + for idx, f in enumerate(serial_fields): + _c(' %s xcb_aux%d;', f.type.c_type, idx) _c(' ') - _c(' printf("in function %s\\n");' % func_name) - - # _c(' unsigned int xcb_buffer_len;') - # _c(' char *xcb_buffer = NULL;') - # _c(' xcb_buffer_len = %s( ', self.c_serialize_name) - # args = '&xcb_buffer' - # get_serialize_params(self) - # for field in param_fields: - # args += ", %s" % field.c_field_name - # _c(' %s);', args) - # _c(' xcb_parts[2].iov_base = xcb_buffer;') - # _c(' xcb_parts[2].iov_len = xcb_buffer_len;') - # _c(' xcb_parts[3].iov_base = 0;') - # _c(' xcb_parts[3].iov_len = -xcb_parts[2].iov_len & 3;') - # _c(' printf("%s done\\n");', self.c_serialize_name) - - -# for line in code_lines: -# _c(line) - # end xcb_parts -# _c(' xcb_parts[xcb_parts_idx].iov_base = 0;') -# _c(' xcb_parts[xcb_parts_idx].iov_len = -xcb_unpadded & 3;') - + _c(' printf("in function %s\\n");' % func_name) # fixed size fields for field in wire_fields: @@ -1344,27 +1450,46 @@ def _c_request_helper(self, name, cookie_type, void, regular): _c(' xcb_parts[3].iov_base = 0;') _c(' xcb_parts[3].iov_len = -xcb_parts[2].iov_len & 3;') + # calls in order to free dyn. all. memory + free_calls = [] count = 4 for field in param_fields: if not field.type.fixed_size(): _c(' xcb_parts[%d].iov_base = (char *) %s;', count, field.c_field_name) + if field.type.need_serialize: + idx = serial_fields.index(field) + serialize_args = get_serialize_params(field.type, + field.c_field_name, + '&xcb_aux%d' % idx, + )[2] + serialize_args = reduce(lambda x,y: "%s, %s" % (x,y), [a[2] for a in serialize_args]) + _c(' xcb_parts[%d].iov_len = ', count) + if aux: + _c(' %s (%s);', field.type.c_serialize_name, serialize_args) + free_calls.append(' free(xcb_parts[%d].iov_base);' % count) + else: + _c(' %s (%s);', field.type.c_unserialize_name, serialize_args) if field.type.is_list: - _c(' xcb_parts[%d].iov_len = %s * sizeof(%s);', count, _c_accessor_get_expr(field.type.expr), field.type.member.c_wiretype) - elif field.type.need_serialize: + _c(' xcb_parts[%d].iov_len = %s * sizeof(%s);', count, + _c_accessor_get_expr(field.type.expr), field.type.member.c_wiretype) + elif not field.type.need_serialize: + # FIXME - _serialize() + _c(' xcb_parts[%d].iov_len = %s * sizeof(%s);', + count, 'Uh oh', field.type.c_wiretype) + + count += 1 + if not field.type.need_serialize: + # the _serialize() function keeps track of padding automatically _c(' xcb_parts[%d].iov_base = 0;', count) - _c(' //%s ();', field.type.c_serialize_name) - serialize_args = get_serialize_params(field.type, - '&xcb_parts[%d].iov_base' % count, - field.c_field_name)[2] - _c(' // %s', reduce(lambda x,y: "%s, %s" % (x,y), [a[2] for a in serialize_args])) - else: - _c(' xcb_parts[%d].iov_len = %s * sizeof(%s);', count, 'Uh oh', field.type.c_wiretype) - _c(' xcb_parts[%d].iov_base = 0;', count + 1) - _c(' xcb_parts[%d].iov_len = -xcb_parts[%d].iov_len & 3;', count + 1, count) - count = count + 2 + _c(' xcb_parts[%d].iov_len = -xcb_parts[%d].iov_len & 3;', count, count-1) + count += 1 _c(' ') _c(' xcb_ret.sequence = xcb_send_request(c, %s, xcb_parts + 2, &xcb_req);', func_flags) + + # free dyn. all. data, if any + for f in free_calls: + _c(f) _c(' return xcb_ret;') _c('}') @@ -1458,6 +1583,9 @@ def c_request(self, name): # Request prototypes _c_request_helper(self, name, self.c_cookie_type, False, True) _c_request_helper(self, name, self.c_cookie_type, False, False) + if self.need_aux: + _c_request_helper(self, name, self.c_cookie_type, False, True, True) + _c_request_helper(self, name, self.c_cookie_type, False, False, True) # Reply accessors _c_accessors(self.reply, name + ('reply',), name) _c_reply(self, name) @@ -1465,6 +1593,10 @@ def c_request(self, name): # Request prototypes _c_request_helper(self, name, 'xcb_void_cookie_t', True, False) _c_request_helper(self, name, 'xcb_void_cookie_t', True, True) + if self.need_aux: + _c_request_helper(self, name, 'xcb_void_cookie_t', True, False, True) + _c_request_helper(self, name, 'xcb_void_cookie_t', True, True, True) + def c_event(self, name): ''' -- cgit v1.2.3