Logo Search packages:      
Sourcecode: yasm version File versions  Download package

unsigned char* yasm_bc_tobytes ( yasm_bytecode bc,
unsigned char *  buf,
unsigned long *  bufsize,
unsigned long *  multiple,
int *  gap,
void *  d,
yasm_output_expr_func  output_expr,
yasm_output_reloc_func  output_reloc 
)

Convert a bytecode into its byte representation.

Parameters:
bc bytecode
buf byte representation destination buffer
bufsize size of buf (in bytes) prior to call; size of the generated data after call
multiple number of times the data should be duplicated when written to the object file [output]
gap if nonzero, indicates the data does not really need to exist in the object file; if nonzero, contents of buf are undefined [output]
d data to pass to each call to output_expr/output_reloc
output_expr function to call to convert expressions into their byte representation
output_reloc function to call to output relocation entries for a single sym
Returns:
Newly allocated buffer that should be used instead of buf for reading the byte representation, or NULL if buf was big enough to hold the entire byte representation.
Note:
Calling twice on the same bytecode may not produce the same results on the second call, as calling this function may result in non-reversible changes to the bytecode.

Definition at line 810 of file bytecode.c.

References yasm_internal_error, and yasm_xmalloc.

{
    /*@only@*/ /*@null@*/ unsigned char *mybuf = NULL;
    unsigned char *origbuf, *destbuf;
    /*@dependent@*/ /*@null@*/ const yasm_intnum *num;
    unsigned long datasize;
    int error = 0;

    if (bc->multiple) {
      num = yasm_expr_get_intnum(&bc->multiple, NULL);
      if (!num)
          yasm_internal_error(
            N_("could not determine multiple in bc_tobytes"));
      *multiple = yasm_intnum_get_uint(num);
      if (*multiple == 0) {
          *bufsize = 0;
          return NULL;
      }
    } else
      *multiple = 1;

    datasize = bc->len / (*multiple);
    *bufsize = datasize;

    /* special case for reserve bytecodes */
    if (bc->callback == &bc_reserve_callback) {
      *gap = 1;
      return NULL;      /* we didn't allocate a buffer */
    }

    *gap = 0;

    if (*bufsize < datasize) {
      mybuf = yasm_xmalloc(sizeof(bc->len));
      origbuf = mybuf;
      destbuf = mybuf;
    } else {
      origbuf = buf;
      destbuf = buf;
    }

    if (!bc->callback)
      yasm_internal_error(N_("got empty bytecode in bc_tobytes"));
    else
      error = bc->callback->tobytes(bc, &destbuf, d, output_expr,
                              output_reloc);

    if (!error && ((unsigned long)(destbuf - origbuf) != datasize))
      yasm_internal_error(
          N_("written length does not match optimized length"));
    return mybuf;
}


Generated by  Doxygen 1.6.0   Back to index