00001 /* -*- coding:utf-8 -*- 00002 Copyright (c) 2009, Fixstars Corporation 00003 All rights reserved. 00004 00005 Redistribution and use in source and binary forms, with or without modification, 00006 are permitted provided that the following conditions are met: 00007 00008 * Redistributions of source code must retain the above copyright notice, 00009 this list of conditions and the following disclaimer. 00010 00011 * Redistributions in binary form must reproduce the above copyright notice, 00012 this list of conditions and the following disclaimer in the documentation 00013 and/or other materials provided with the distribution. 00014 00015 * Neither the name of Fixstars Corporation nor the names of its contributors 00016 may be used to endorse or promote products derived from this software 00017 without specific prior written permission. 00018 00019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00020 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00021 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00022 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 00023 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00024 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00025 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 00026 AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00027 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 00028 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 */ 00030 00037 #include "ctrump/analyzer/peel.h" 00038 #include "ctrump/ast/ast.h" 00039 00040 struct ctrump_expr * 00041 ctrump_peel_bitequality_cast_expr(struct ctrump_expr *e, 00042 const struct ctrump_abi *abi) 00043 { 00044 struct ctrump_texpr *cast_to, *cast_from; 00045 struct ctrump_expr *c; 00046 00047 tailcall: 00048 switch (e->code) { 00049 case CTRUMP_EXPR_IMPLICIT_CAST: 00050 cast_to = e->u.implicit_cast.cast_to; 00051 c = e->u.implicit_cast.expr; 00052 cast_from = c->type; 00053 if (!ctrump_is_bitconvertion_on_type_conversion(cast_to, 00054 cast_from, 00055 abi)) { 00056 return e; 00057 } 00058 e = c; 00059 goto tailcall; 00060 00061 case CTRUMP_EXPR_CAST: 00062 cast_to = e->u.cast.cast_to; 00063 c = e->u.cast.expr; 00064 cast_from = c->type; 00065 if (!ctrump_is_bitconvertion_on_type_conversion(cast_to, 00066 cast_from, 00067 abi)) { 00068 return e; 00069 } 00070 e = c; 00071 goto tailcall; 00072 00073 case CTRUMP_EXPR_PAREN: 00074 e = e->u.paren.expr; 00075 goto tailcall; 00076 00077 default: 00078 return e; 00079 } 00080 } 00081 00082 struct ctrump_expr * 00083 ctrump_peel_cast_to_pointer_from_array(struct ctrump_expr *e) 00084 { 00085 while (1) { 00086 struct ctrump_expr *from; 00087 00088 tailcall: 00089 switch (e->code) { 00090 case CTRUMP_EXPR_IMPLICIT_CAST: 00091 from = e->u.implicit_cast.expr; 00092 break; 00093 00094 case CTRUMP_EXPR_CAST: 00095 from = e->u.cast.expr; 00096 break; 00097 00098 case CTRUMP_EXPR_PAREN: 00099 e = e->u.paren.expr; 00100 goto tailcall; 00101 00102 default: 00103 return e; 00104 } 00105 00106 if (from->type->code == CTRUMP_TYPE_ARRAY) { 00107 e = from; 00108 } else { 00109 return e; 00110 } 00111 } 00112 } 00113 00114 struct ctrump_expr * 00115 ctrump_peel_paren(struct ctrump_expr *e) 00116 { 00117 while (e->code == CTRUMP_EXPR_PAREN) { 00118 e = e->u.paren.expr; 00119 } 00120 00121 return e; 00122 } 00123 00124 struct ctrump_expr * 00125 ctrump_peel_sign(int *is_sign_ret, struct ctrump_expr *e) 00126 { 00127 int is_sign = 0; 00128 while (1) { 00129 switch (e->code) { 00130 case CTRUMP_EXPR_PAREN: 00131 e = e->u.paren.expr; 00132 break; 00133 00134 case CTRUMP_EXPR_UNA_POS: 00135 e = e->u.unary.expr; 00136 break; 00137 00138 case CTRUMP_EXPR_UNA_NEG: 00139 is_sign = !is_sign; 00140 break; 00141 00142 default: 00143 *is_sign_ret = is_sign; 00144 return e; 00145 } 00146 } 00147 }