[Python] automatically converts the designated initializers in the GCC Extension

Source: Internet
Author: User

As a major extension of GCC, designated initializers is one of the main obstacles to porting code platforms. The following is a targeted example. Other types of designated initializers can be processed by modifying the regular expression in the script.

#!/usr/bin/python# assume the offset is in order, or the result could be incorrectimport restru_list = ["sse_op_table1"]leading_space = "    "# we have the form [0x..] = {},def print_without_offset(text):    val = text.split('=', 1);    if val:        print leading_space + val[1].lstrip()    else:        assert 0def print_one_elem(text):    res = re.search(r"{", text)    if not res:        val = re.search(r"(.*) = (.*),(.*)", text)        if val:            print leading_space + "{ " + val.group(2) + " }," + val.group(3)            pass        else:            assert 0    else:        print_without_offset(text)def translate_struct(size):    idx = 0x0           while True:        try:            text = raw_input()        except EOFError:            break                if (text.lstrip().rstrip() == "};"):    # meet the end of struct definition            if (idx != size):                for i in range(idx, (size - 1)):                    print "%s{ NULL }," % (leading_space)                print "%s{ NULL }" % (leading_space)            print text # print "};"            break        result = re.search(r"^(\s*)\[0x([\da-f]+)\] =", text)        if not result:            print text  # probably blank line or comments            continue                cur_idx = int(result.group(2), 16)        assert(cur_idx >= idx)        if (idx != cur_idx):            leading_space = result.group(1)            for i in range(idx, cur_idx):                print "%s{ NULL }," % (leading_space)        # print the current one        print_one_elem(text)        idx = cur_idx + 1while True:    try:        text = raw_input()    except EOFError:        break    for elem in stru_list:        elem = elem + r"\[(\d+)\]"           result = re.search(elem, text)        if result:            #TODO:            print text            translate_struct(int(result.group(1)))            break;

Here, stru_list specifies the name of the struct to be converted.

If you have the following struct (in the. c file), convert it to a non-GCC extension format.

static void *sse_op_table1[256][4] = {    /* 3DNow! extensions */    [0x0e] = { SSE_DUMMY }, /* femms */    [0x0f] = { SSE_DUMMY }, /* pf... */    /* pure SSE operations */    [0x10] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movups, movupd, movss, movsd */    [0x11] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movups, movupd, movss, movsd */    [0x12] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movlps, movlpd, movsldup, movddup */    [0x13] = { SSE_SPECIAL, SSE_SPECIAL },  /* movlps, movlpd */    [0x14] = { gen_helper_punpckldq_xmm, gen_helper_punpcklqdq_xmm },    [0x15] = { gen_helper_punpckhdq_xmm, gen_helper_punpckhqdq_xmm },    [0x16] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL },  /* movhps, movhpd, movshdup */    [0x17] = { SSE_SPECIAL, SSE_SPECIAL },  /* movhps, movhpd */    [0x28] = { SSE_SPECIAL, SSE_SPECIAL },  /* movaps, movapd */    [0x29] = { SSE_SPECIAL, SSE_SPECIAL },  /* movaps, movapd */    [0x2a] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* cvtpi2ps, cvtpi2pd, cvtsi2ss, cvtsi2sd */    [0x2b] = { SSE_SPECIAL, SSE_SPECIAL },  /* movntps, movntpd */    [0x2c] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* cvttps2pi, cvttpd2pi, cvttsd2si, cvttss2si */    [0x2d] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* cvtps2pi, cvtpd2pi, cvtsd2si, cvtss2si */    [0x2e] = { gen_helper_ucomiss, gen_helper_ucomisd },    [0x2f] = { gen_helper_comiss, gen_helper_comisd },    [0x38] = { SSE_SPECIAL, SSE_SPECIAL, NULL, SSE_SPECIAL }, /* SSSE3/SSE4 */    [0x3a] = { SSE_SPECIAL, SSE_SPECIAL }, /* SSSE3/SSE4 */    [0x50] = { SSE_SPECIAL, SSE_SPECIAL }, /* movmskps, movmskpd */    [0x51] = SSE_FOP(sqrt),    [0x52] = { gen_helper_rsqrtps, NULL, gen_helper_rsqrtss, NULL },    [0x53] = { gen_helper_rcpps, NULL, gen_helper_rcpss, NULL },    [0x54] = { gen_helper_pand_xmm, gen_helper_pand_xmm }, /* andps, andpd */    [0x55] = { gen_helper_pandn_xmm, gen_helper_pandn_xmm }, /* andnps, andnpd */    [0x56] = { gen_helper_por_xmm, gen_helper_por_xmm }, /* orps, orpd */    [0x57] = { gen_helper_pxor_xmm, gen_helper_pxor_xmm }, /* xorps, xorpd */    [0x58] = SSE_FOP(add),    [0x59] = SSE_FOP(mul),    [0x5a] = { gen_helper_cvtps2pd, gen_helper_cvtpd2ps,               gen_helper_cvtss2sd, gen_helper_cvtsd2ss },    [0x5b] = { gen_helper_cvtdq2ps, gen_helper_cvtps2dq, gen_helper_cvttps2dq },    [0x5c] = SSE_FOP(sub),    [0x5d] = SSE_FOP(min),    [0x5e] = SSE_FOP(div),    [0x5f] = SSE_FOP(max),    /* MMX ops and their SSE extensions */    [0x60] = MMX_OP2(punpcklbw),    [0x61] = MMX_OP2(punpcklwd),    [0x62] = MMX_OP2(punpckldq),    [0x63] = MMX_OP2(packsswb),    [0x64] = MMX_OP2(pcmpgtb),    [0x65] = MMX_OP2(pcmpgtw),    [0x66] = MMX_OP2(pcmpgtl),    [0x67] = MMX_OP2(packuswb),    [0x68] = MMX_OP2(punpckhbw),    [0x69] = MMX_OP2(punpckhwd),    [0x6a] = MMX_OP2(punpckhdq),    [0x6b] = MMX_OP2(packssdw),    [0x6c] = { NULL, gen_helper_punpcklqdq_xmm },    [0x6d] = { NULL, gen_helper_punpckhqdq_xmm },    [0x6e] = { SSE_SPECIAL, SSE_SPECIAL }, /* movd mm, ea */    [0x6f] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movq, movdqa, , movqdu */    [0x70] = { gen_helper_pshufw_mmx,               gen_helper_pshufd_xmm,               gen_helper_pshufhw_xmm,               gen_helper_pshuflw_xmm },    [0x71] = { SSE_SPECIAL, SSE_SPECIAL }, /* shiftw */    [0x72] = { SSE_SPECIAL, SSE_SPECIAL }, /* shiftd */    [0x73] = { SSE_SPECIAL, SSE_SPECIAL }, /* shiftq */    [0x74] = MMX_OP2(pcmpeqb),    [0x75] = MMX_OP2(pcmpeqw),    [0x76] = MMX_OP2(pcmpeql),    [0x77] = { SSE_DUMMY }, /* emms */    [0x7c] = { NULL, gen_helper_haddpd, NULL, gen_helper_haddps },    [0x7d] = { NULL, gen_helper_hsubpd, NULL, gen_helper_hsubps },    [0x7e] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movd, movd, , movq */    [0x7f] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movq, movdqa, movdqu */    [0xc2] = SSE_FOP(cmpeq),    [0xc4] = { SSE_SPECIAL, SSE_SPECIAL }, /* pinsrw */    [0xc5] = { SSE_SPECIAL, SSE_SPECIAL }, /* pextrw */    [0xc6] = { gen_helper_shufps, gen_helper_shufpd },    [0xd0] = { NULL, gen_helper_addsubpd, NULL, gen_helper_addsubps },    [0xd1] = MMX_OP2(psrlw),    [0xd2] = MMX_OP2(psrld),    [0xd3] = MMX_OP2(psrlq),    [0xd4] = MMX_OP2(paddq),    [0xd5] = MMX_OP2(pmullw),    [0xd6] = { NULL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL },    [0xd7] = { SSE_SPECIAL, SSE_SPECIAL }, /* pmovmskb */    [0xd8] = MMX_OP2(psubusb),    [0xd9] = MMX_OP2(psubusw),    [0xda] = MMX_OP2(pminub),    [0xdb] = MMX_OP2(pand),    [0xdc] = MMX_OP2(paddusb),    [0xdd] = MMX_OP2(paddusw),    [0xde] = MMX_OP2(pmaxub),    [0xdf] = MMX_OP2(pandn),    [0xe0] = MMX_OP2(pavgb),    [0xe1] = MMX_OP2(psraw),    [0xe2] = MMX_OP2(psrad),    [0xe3] = MMX_OP2(pavgw),    [0xe4] = MMX_OP2(pmulhuw),    [0xe5] = MMX_OP2(pmulhw),    [0xe6] = { NULL, gen_helper_cvttpd2dq, gen_helper_cvtdq2pd, gen_helper_cvtpd2dq },    [0xe7] = { SSE_SPECIAL , SSE_SPECIAL },  /* movntq, movntq */    [0xe8] = MMX_OP2(psubsb),    [0xe9] = MMX_OP2(psubsw),    [0xea] = MMX_OP2(pminsw),    [0xeb] = MMX_OP2(por),    [0xec] = MMX_OP2(paddsb),    [0xed] = MMX_OP2(paddsw),    [0xee] = MMX_OP2(pmaxsw),    [0xef] = MMX_OP2(pxor),    [0xf0] = { NULL, NULL, NULL, SSE_SPECIAL }, /* lddqu */    [0xf1] = MMX_OP2(psllw),    [0xf2] = MMX_OP2(pslld),    [0xf3] = MMX_OP2(psllq),    [0xf4] = MMX_OP2(pmuludq),    [0xf5] = MMX_OP2(pmaddwd),    [0xf6] = MMX_OP2(psadbw),    [0xf7] = MMX_OP2(maskmov),    [0xf8] = MMX_OP2(psubb),    [0xf9] = MMX_OP2(psubw),    [0xfa] = MMX_OP2(psubl),    [0xfb] = MMX_OP2(psubq),    [0xfc] = MMX_OP2(paddb),    [0xfd] = MMX_OP2(paddw),    [0xfe] = MMX_OP2(paddl),};

Run the above script

./Main. py <A. C> B. C

The. c file is generated:

static void *sse_op_table1[256][4] = {    /* 3DNow! extensions */    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { SSE_DUMMY }, /* femms */    { SSE_DUMMY }, /* pf... */    /* pure SSE operations */    { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movups, movupd, movss, movsd */    { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movups, movupd, movss, movsd */    { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movlps, movlpd, movsldup, movddup */    { SSE_SPECIAL, SSE_SPECIAL },  /* movlps, movlpd */    { gen_helper_punpckldq_xmm, gen_helper_punpcklqdq_xmm },    { gen_helper_punpckhdq_xmm, gen_helper_punpckhqdq_xmm },    { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL },  /* movhps, movhpd, movshdup */    { SSE_SPECIAL, SSE_SPECIAL },  /* movhps, movhpd */    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { SSE_SPECIAL, SSE_SPECIAL },  /* movaps, movapd */    { SSE_SPECIAL, SSE_SPECIAL },  /* movaps, movapd */    { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* cvtpi2ps, cvtpi2pd, cvtsi2ss, cvtsi2sd */    { SSE_SPECIAL, SSE_SPECIAL },  /* movntps, movntpd */    { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* cvttps2pi, cvttpd2pi, cvttsd2si, cvttss2si */    { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* cvtps2pi, cvtpd2pi, cvtsd2si, cvtss2si */    { gen_helper_ucomiss, gen_helper_ucomisd },    { gen_helper_comiss, gen_helper_comisd },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { SSE_SPECIAL, SSE_SPECIAL, NULL, SSE_SPECIAL }, /* SSSE3/SSE4 */    { NULL },    { SSE_SPECIAL, SSE_SPECIAL }, /* SSSE3/SSE4 */    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { SSE_SPECIAL, SSE_SPECIAL }, /* movmskps, movmskpd */    { SSE_FOP(sqrt) },    { gen_helper_rsqrtps, NULL, gen_helper_rsqrtss, NULL },    { gen_helper_rcpps, NULL, gen_helper_rcpss, NULL },    { gen_helper_pand_xmm, gen_helper_pand_xmm }, /* andps, andpd */    { gen_helper_pandn_xmm, gen_helper_pandn_xmm }, /* andnps, andnpd */    { gen_helper_por_xmm, gen_helper_por_xmm }, /* orps, orpd */    { gen_helper_pxor_xmm, gen_helper_pxor_xmm }, /* xorps, xorpd */    { SSE_FOP(add) },    { SSE_FOP(mul) },    { gen_helper_cvtps2pd, gen_helper_cvtpd2ps,               gen_helper_cvtss2sd, gen_helper_cvtsd2ss },    { gen_helper_cvtdq2ps, gen_helper_cvtps2dq, gen_helper_cvttps2dq },    { SSE_FOP(sub) },    { SSE_FOP(min) },    { SSE_FOP(div) },    { SSE_FOP(max) },    /* MMX ops and their SSE extensions */    { MMX_OP2(punpcklbw) },    { MMX_OP2(punpcklwd) },    { MMX_OP2(punpckldq) },    { MMX_OP2(packsswb) },    { MMX_OP2(pcmpgtb) },    { MMX_OP2(pcmpgtw) },    { MMX_OP2(pcmpgtl) },    { MMX_OP2(packuswb) },    { MMX_OP2(punpckhbw) },    { MMX_OP2(punpckhwd) },    { MMX_OP2(punpckhdq) },    { MMX_OP2(packssdw) },    { NULL, gen_helper_punpcklqdq_xmm },    { NULL, gen_helper_punpckhqdq_xmm },    { SSE_SPECIAL, SSE_SPECIAL }, /* movd mm, ea */    { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movq, movdqa, , movqdu */    { gen_helper_pshufw_mmx,               gen_helper_pshufd_xmm,               gen_helper_pshufhw_xmm,               gen_helper_pshuflw_xmm },    { SSE_SPECIAL, SSE_SPECIAL }, /* shiftw */    { SSE_SPECIAL, SSE_SPECIAL }, /* shiftd */    { SSE_SPECIAL, SSE_SPECIAL }, /* shiftq */    { MMX_OP2(pcmpeqb) },    { MMX_OP2(pcmpeqw) },    { MMX_OP2(pcmpeql) },    { SSE_DUMMY }, /* emms */    { NULL },    { NULL },    { NULL },    { NULL },    { NULL, gen_helper_haddpd, NULL, gen_helper_haddps },    { NULL, gen_helper_hsubpd, NULL, gen_helper_hsubps },    { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movd, movd, , movq */    { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movq, movdqa, movdqu */    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { SSE_FOP(cmpeq) },    { NULL },    { SSE_SPECIAL, SSE_SPECIAL }, /* pinsrw */    { SSE_SPECIAL, SSE_SPECIAL }, /* pextrw */    { gen_helper_shufps, gen_helper_shufpd },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL },    { NULL, gen_helper_addsubpd, NULL, gen_helper_addsubps },    { MMX_OP2(psrlw) },    { MMX_OP2(psrld) },    { MMX_OP2(psrlq) },    { MMX_OP2(paddq) },    { MMX_OP2(pmullw) },    { NULL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL },    { SSE_SPECIAL, SSE_SPECIAL }, /* pmovmskb */    { MMX_OP2(psubusb) },    { MMX_OP2(psubusw) },    { MMX_OP2(pminub) },    { MMX_OP2(pand) },    { MMX_OP2(paddusb) },    { MMX_OP2(paddusw) },    { MMX_OP2(pmaxub) },    { MMX_OP2(pandn) },    { MMX_OP2(pavgb) },    { MMX_OP2(psraw) },    { MMX_OP2(psrad) },    { MMX_OP2(pavgw) },    { MMX_OP2(pmulhuw) },    { MMX_OP2(pmulhw) },    { NULL, gen_helper_cvttpd2dq, gen_helper_cvtdq2pd, gen_helper_cvtpd2dq },    { SSE_SPECIAL , SSE_SPECIAL },  /* movntq, movntq */    { MMX_OP2(psubsb) },    { MMX_OP2(psubsw) },    { MMX_OP2(pminsw) },    { MMX_OP2(por) },    { MMX_OP2(paddsb) },    { MMX_OP2(paddsw) },    { MMX_OP2(pmaxsw) },    { MMX_OP2(pxor) },    { NULL, NULL, NULL, SSE_SPECIAL }, /* lddqu */    { MMX_OP2(psllw) },    { MMX_OP2(pslld) },    { MMX_OP2(psllq) },    { MMX_OP2(pmuludq) },    { MMX_OP2(pmaddwd) },    { MMX_OP2(psadbw) },    { MMX_OP2(maskmov) },    { MMX_OP2(psubb) },    { MMX_OP2(psubw) },    { MMX_OP2(psubl) },    { MMX_OP2(psubq) },    { MMX_OP2(paddb) },    { MMX_OP2(paddw) },    { MMX_OP2(paddl) },    { NULL }};

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.