From patchwork Sat Aug 27 21:27:45 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Torvalds X-Patchwork-Id: 1105282 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter2.kernel.org (8.14.4/8.14.4) with ESMTP id p7RLQf4m032290 for ; Sat, 27 Aug 2011 21:27:50 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751628Ab1H0V1t (ORCPT ); Sat, 27 Aug 2011 17:27:49 -0400 Received: from mail-pz0-f42.google.com ([209.85.210.42]:60269 "EHLO mail-pz0-f42.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751554Ab1H0V1t (ORCPT ); Sat, 27 Aug 2011 17:27:49 -0400 Received: by mail-pz0-f42.google.com with SMTP id 37so6219152pzk.1 for ; Sat, 27 Aug 2011 14:27:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=sender:date:from:x-x-sender:to:cc:subject:in-reply-to:message-id :references:user-agent:mime-version:content-type; bh=tNg4ngpBBsfNVNDcpr/UD6XQz8Zr8sVmOfK+UI+Ne1Q=; b=FsIU7hOSWyLpZN2eaFE0Fb5kZRSWISfI7dfscoi9M6CmMUahnfu1xMixU9Kxalixsm bE5oFwxyKZL6EfpF7MPEGin7AQoFpPnBWEvR+yA9Cm/gxDGQ0h/ZKPqJqhXCygthwoAS fJ0C6NHgqe9AQKsIqH+mpeQJJncPu67Hxo0Wo= Received: by 10.143.98.14 with SMTP id a14mr1355951wfm.198.1314480469119; Sat, 27 Aug 2011 14:27:49 -0700 (PDT) Received: from [192.168.1.87] (c-24-22-0-219.hsd1.or.comcast.net. [24.22.0.219]) by mx.google.com with ESMTPS id e3sm9040718pbi.7.2011.08.27.14.27.47 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 27 Aug 2011 14:27:47 -0700 (PDT) Date: Sat, 27 Aug 2011 14:27:45 -0700 (PDT) From: Linus Torvalds X-X-Sender: torvalds@i5.linux-foundation.org To: Sparse Mailing-list , Christopher Li , Josh Triplett cc: Pekka Enberg , Jeff Garzik Subject: [PATCH 2/3] Make 'linearize_switch()' helper function In-Reply-To: Message-ID: References: User-Agent: Alpine 2.02 (LFD 1266 2009-07-14) MIME-Version: 1.0 Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter2.kernel.org [140.211.167.43]); Sat, 27 Aug 2011 21:27:50 +0000 (UTC) From: Linus Torvalds Date: Sat, 27 Aug 2011 11:41:35 -0700 Subject: [PATCH 2/3] Make 'linearize_switch()' helper function Rather than do it in that huge 'linearize_statement()' function, split out the switch generation case. Avoids one level of indentation, and makes for simpler and more straightforward functions. Signed-off-by: Linus Torvalds --- linearize.c | 126 ++++++++++++++++++++++++++++++---------------------------- 1 files changed, 65 insertions(+), 61 deletions(-) diff --git a/linearize.c b/linearize.c index 2decd5300859..e48854755b1e 100644 --- a/linearize.c +++ b/linearize.c @@ -1843,6 +1843,69 @@ static pseudo_t linearize_declaration(struct entrypoint *ep, struct statement *s return VOID; } +static pseudo_t linearize_switch(struct entrypoint *ep, struct statement *stmt) +{ + struct symbol *sym; + struct instruction *switch_ins; + struct basic_block *switch_end = alloc_basic_block(ep, stmt->pos); + struct basic_block *active, *default_case; + struct multijmp *jmp; + pseudo_t pseudo; + + pseudo = linearize_expression(ep, stmt->switch_expression); + + active = ep->active; + if (!bb_reachable(active)) + return VOID; + + switch_ins = alloc_instruction(OP_SWITCH, 0); + use_pseudo(switch_ins, pseudo, &switch_ins->cond); + add_one_insn(ep, switch_ins); + finish_block(ep); + + default_case = NULL; + FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) { + struct statement *case_stmt = sym->stmt; + struct basic_block *bb_case = get_bound_block(ep, sym); + + if (!case_stmt->case_expression) { + default_case = bb_case; + continue; + } else { + int begin, end; + + begin = end = case_stmt->case_expression->value; + if (case_stmt->case_to) + end = case_stmt->case_to->value; + if (begin > end) + jmp = alloc_multijmp(bb_case, end, begin); + else + jmp = alloc_multijmp(bb_case, begin, end); + + } + add_multijmp(&switch_ins->multijmp_list, jmp); + add_bb(&bb_case->parents, active); + add_bb(&active->children, bb_case); + } END_FOR_EACH_PTR(sym); + + bind_label(stmt->switch_break, switch_end, stmt->pos); + + /* And linearize the actual statement */ + linearize_statement(ep, stmt->switch_statement); + set_activeblock(ep, switch_end); + + if (!default_case) + default_case = switch_end; + + jmp = alloc_multijmp(default_case, 1, 0); + add_multijmp(&switch_ins->multijmp_list, jmp); + add_bb(&default_case->parents, active); + add_bb(&active->children, default_case); + sort_switch_cases(switch_ins); + + return VOID; +} + static pseudo_t linearize_iterator(struct entrypoint *ep, struct statement *stmt) { struct statement *pre_statement = stmt->iterator_pre_statement; @@ -2030,67 +2093,8 @@ pseudo_t linearize_statement(struct entrypoint *ep, struct statement *stmt) break; } - case STMT_SWITCH: { - struct symbol *sym; - struct instruction *switch_ins; - struct basic_block *switch_end = alloc_basic_block(ep, stmt->pos); - struct basic_block *active, *default_case; - struct multijmp *jmp; - pseudo_t pseudo; - - pseudo = linearize_expression(ep, stmt->switch_expression); - - active = ep->active; - if (!bb_reachable(active)) - break; - - switch_ins = alloc_instruction(OP_SWITCH, 0); - use_pseudo(switch_ins, pseudo, &switch_ins->cond); - add_one_insn(ep, switch_ins); - finish_block(ep); - - default_case = NULL; - FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) { - struct statement *case_stmt = sym->stmt; - struct basic_block *bb_case = get_bound_block(ep, sym); - - if (!case_stmt->case_expression) { - default_case = bb_case; - continue; - } else { - int begin, end; - - begin = end = case_stmt->case_expression->value; - if (case_stmt->case_to) - end = case_stmt->case_to->value; - if (begin > end) - jmp = alloc_multijmp(bb_case, end, begin); - else - jmp = alloc_multijmp(bb_case, begin, end); - - } - add_multijmp(&switch_ins->multijmp_list, jmp); - add_bb(&bb_case->parents, active); - add_bb(&active->children, bb_case); - } END_FOR_EACH_PTR(sym); - - bind_label(stmt->switch_break, switch_end, stmt->pos); - - /* And linearize the actual statement */ - linearize_statement(ep, stmt->switch_statement); - set_activeblock(ep, switch_end); - - if (!default_case) - default_case = switch_end; - - jmp = alloc_multijmp(default_case, 1, 0); - add_multijmp(&switch_ins->multijmp_list, jmp); - add_bb(&default_case->parents, active); - add_bb(&active->children, default_case); - sort_switch_cases(switch_ins); - - break; - } + case STMT_SWITCH: + return linearize_switch(ep, stmt); case STMT_ITERATOR: return linearize_iterator(ep, stmt);