From patchwork Thu Sep 19 21:25:04 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 276077 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 028DA2C0105 for ; Fri, 20 Sep 2013 08:15:39 +1000 (EST) Received: from localhost ([::1]:53167 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VMlnq-00089M-46 for incoming@patchwork.ozlabs.org; Thu, 19 Sep 2013 17:30:22 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41184) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VMlk1-00034D-S5 for qemu-devel@nongnu.org; Thu, 19 Sep 2013 17:26:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VMljf-0005Xa-JV for qemu-devel@nongnu.org; Thu, 19 Sep 2013 17:26:25 -0400 Received: from mail-yh0-x22b.google.com ([2607:f8b0:4002:c01::22b]:59034) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VMljf-0005XO-F9 for qemu-devel@nongnu.org; Thu, 19 Sep 2013 17:26:03 -0400 Received: by mail-yh0-f43.google.com with SMTP id b6so4421113yha.16 for ; Thu, 19 Sep 2013 14:26:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=PSqAHg0dyPCs4QPL0yBYWLwRjT26ApNpWA2R/Ob8sQc=; b=RcLjnozW9L2hEY5dE79kH4o3ZMRCJL02BKAV9OXKXU2X2K+w/FJtRoK0n8VaQBLfCy wCNuJyPQOYJ2xieoPif2JpK7Q41QjKqJ1mIeNGT9NL+c1r9LCnunxqjuWHFr2uTdSbLN 7ICcOszWy3yIK2Gd7vTxcpfVDvvUbHLsByzTxqa3PoHG3KdssaPQ785wMnpShcDmgIqA cILD7bddggLzQDi7rd/il5qWhLS/T9/M+IEISiYbCxz4JxFXmuwhWtkem5pn2/2hKqlp NyhbEOpKbSFvp3nh3dJWZlcSD0xMpMS2+W0GI7LB7PJKCTDhEGooOSRLPOU4U/DDtsmA NUwg== X-Received: by 10.236.174.37 with SMTP id w25mr2952324yhl.36.1379625963130; Thu, 19 Sep 2013 14:26:03 -0700 (PDT) Received: from anchor.com (50-194-63-110-static.hfc.comcastbusiness.net. [50.194.63.110]) by mx.google.com with ESMTPSA id e65sm13556947yhc.18.1969.12.31.16.00.00 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Thu, 19 Sep 2013 14:26:02 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Thu, 19 Sep 2013 14:25:04 -0700 Message-Id: <1379625908-27964-13-git-send-email-rth@twiddle.net> X-Mailer: git-send-email 1.8.1.4 In-Reply-To: <1379625908-27964-1-git-send-email-rth@twiddle.net> References: <1379625908-27964-1-git-send-email-rth@twiddle.net> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:4002:c01::22b Cc: blauwirbel@gmail.com, aurelien@aurel32.net Subject: [Qemu-devel] [RFC 12/16] tcg: Tidy temporary allocation X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org In particular, make sure the memory is memset before use. Continues the increased use of TCGTemp pointers instead of integer indices where appropriate. Signed-off-by: Richard Henderson --- tcg/tcg.c | 128 +++++++++++++++++++++++++++++--------------------------------- 1 file changed, 60 insertions(+), 68 deletions(-) diff --git a/tcg/tcg.c b/tcg/tcg.c index 6526305..a69186a 100644 --- a/tcg/tcg.c +++ b/tcg/tcg.c @@ -334,32 +334,45 @@ void tcg_func_start(TCGContext *s) #endif } -static inline void tcg_temp_alloc(TCGContext *s, int n) +static inline int temp_idx(TCGContext *s, TCGTemp *ts) { - if (n > TCG_MAX_TEMPS) - tcg_abort(); + ptrdiff_t n = ts - s->temps; + assert(n >= 0 && n < s->nb_temps); + return n; +} + +static inline TCGTemp *tcg_temp_alloc(TCGContext *s) +{ + int n = s->nb_temps++; + assert(n < TCG_MAX_TEMPS); + return memset(&s->temps[n], 0, sizeof(TCGTemp)); +} + +static inline TCGTemp *tcg_global_alloc(TCGContext *s) +{ + assert(s->nb_globals == s->nb_temps); + s->nb_globals++; + return tcg_temp_alloc(s); } static int tcg_global_reg_new_internal(TCGContext *s, TCGType type, TCGReg reg, const char *name) { TCGTemp *ts; - int idx; if (TCG_TARGET_REG_BITS == 32 && type != TCG_TYPE_I32) { tcg_abort(); } - idx = s->nb_globals; - tcg_temp_alloc(s, s->nb_globals + 1); - ts = &s->temps[s->nb_globals]; + + ts = tcg_global_alloc(s); ts->base_type = type; ts->type = type; ts->fixed_reg = 1; ts->reg = reg; ts->name = name; - s->nb_globals++; tcg_regset_set_reg(s->reserved_regs, reg); - return idx; + + return temp_idx(s, ts); } void tcg_set_frame(TCGContext *s, TCGReg reg, intptr_t start, intptr_t size) @@ -397,109 +410,88 @@ int tcg_global_mem_new_internal(TCGType type, TCGv_ptr base, intptr_t offset, const char *name) { TCGContext *s = &tcg_ctx; - TCGTemp *ts, *base_ts = &s->temps[GET_TCGV_PTR(base)]; - int idx; + TCGTemp *base_ts = &s->temps[GET_TCGV_PTR(base)]; + TCGTemp *ts = tcg_global_alloc(s); + int bigendian = 0; +#ifdef TCG_TARGET_WORDS_BIGENDIAN + bigendian = 1; +#endif - idx = s->nb_globals; -#if TCG_TARGET_REG_BITS == 32 - if (type == TCG_TYPE_I64) { + if (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64) { + TCGTemp *ts2 = tcg_global_alloc(s); char buf[64]; - tcg_temp_alloc(s, s->nb_globals + 2); - ts = &s->temps[s->nb_globals]; - ts->base_type = type; + + ts->base_type = TCG_TYPE_I64; ts->type = TCG_TYPE_I32; - ts->fixed_reg = 0; ts->mem_allocated = 1; ts->mem_base = base_ts; -#ifdef TCG_TARGET_WORDS_BIGENDIAN - ts->mem_offset = offset + 4; -#else - ts->mem_offset = offset; -#endif + ts->mem_offset = offset + bigendian * 4; pstrcpy(buf, sizeof(buf), name); pstrcat(buf, sizeof(buf), "_0"); ts->name = strdup(buf); - ts++; - ts->base_type = type; - ts->type = TCG_TYPE_I32; - ts->fixed_reg = 0; - ts->mem_allocated = 1; - ts->mem_base = base_ts; -#ifdef TCG_TARGET_WORDS_BIGENDIAN - ts->mem_offset = offset; -#else - ts->mem_offset = offset + 4; -#endif + assert(ts2 == ts + 1); + ts2->base_type = TCG_TYPE_I64; + ts2->type = TCG_TYPE_I32; + ts2->mem_allocated = 1; + ts2->mem_base = base_ts; + ts2->mem_offset = offset + (1 - bigendian) * 4; pstrcpy(buf, sizeof(buf), name); pstrcat(buf, sizeof(buf), "_1"); ts->name = strdup(buf); - - s->nb_globals += 2; - } else -#endif - { - tcg_temp_alloc(s, s->nb_globals + 1); - ts = &s->temps[s->nb_globals]; + } else { ts->base_type = type; ts->type = type; - ts->fixed_reg = 0; ts->mem_allocated = 1; ts->mem_base = base_ts; ts->mem_offset = offset; ts->name = name; - s->nb_globals++; } - return idx; + return temp_idx(s, ts); } -static inline int tcg_temp_new_internal(TCGType type, int temp_local) +static int tcg_temp_new_internal(TCGType type, int temp_local) { TCGContext *s = &tcg_ctx; TCGTemp *ts; int idx, k; k = type; - if (temp_local) + if (temp_local) { k += TCG_TYPE_COUNT; + } idx = s->first_free_temp[k]; if (idx != -1) { - /* There is already an available temp with the - right type */ + /* There is already an available temp with the right type. */ ts = &s->temps[idx]; s->first_free_temp[k] = ts->next_free_temp; ts->temp_allocated = 1; + + assert(ts->base_type == type); assert(ts->temp_local == temp_local); } else { - idx = s->nb_temps; -#if TCG_TARGET_REG_BITS == 32 - if (type == TCG_TYPE_I64) { - tcg_temp_alloc(s, s->nb_temps + 2); - ts = &s->temps[s->nb_temps]; - ts->base_type = type; - ts->type = TCG_TYPE_I32; - ts->temp_allocated = 1; - ts->temp_local = temp_local; - ts->name = NULL; - ts++; - ts->base_type = TCG_TYPE_I32; + ts = tcg_temp_alloc(s); + if (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64) { + TCGTemp *ts2 = tcg_temp_alloc(s); + + ts->base_type = TCG_TYPE_I64; ts->type = TCG_TYPE_I32; ts->temp_allocated = 1; ts->temp_local = temp_local; - ts->name = NULL; - s->nb_temps += 2; - } else -#endif - { - tcg_temp_alloc(s, s->nb_temps + 1); - ts = &s->temps[s->nb_temps]; + + assert(ts2 == ts + 1); + ts2->base_type = TCG_TYPE_I64; + ts2->type = TCG_TYPE_I32; + ts2->temp_allocated = 1; + ts2->temp_local = temp_local; + } else { ts->base_type = type; ts->type = type; ts->temp_allocated = 1; ts->temp_local = temp_local; - ts->name = NULL; s->nb_temps++; } + idx = temp_idx(s, ts); } #if defined(CONFIG_DEBUG_TCG)