From patchwork Sat Dec 8 00:48:42 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Francis X-Patchwork-Id: 10719081 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 393331750 for ; Sat, 8 Dec 2018 01:03:41 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1A9132DE3B for ; Sat, 8 Dec 2018 01:03:41 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 08B002DE4C; Sat, 8 Dec 2018 01:03:41 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.7 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 6738D2DE3B for ; Sat, 8 Dec 2018 01:03:39 +0000 (UTC) Received: from localhost ([::1]:48618 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gVR1t-0002eg-Hz for patchwork-qemu-devel@patchwork.kernel.org; Fri, 07 Dec 2018 20:03:37 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40143) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gVQnY-00035l-US for qemu-devel@nongnu.org; Fri, 07 Dec 2018 19:48:49 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gVQnV-00015j-Mk for qemu-devel@nongnu.org; Fri, 07 Dec 2018 19:48:48 -0500 Received: from esa5.hgst.iphmx.com ([216.71.153.144]:41973) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gVQnV-00015P-Bj; Fri, 07 Dec 2018 19:48:45 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1544230126; x=1575766126; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=3oGiV26nLPZ2WJrmqvCc4oJo0ocGadG/LzTzxN8xiqg=; b=NHYNNc0LFuN0YBF0CoofZcvBAbX8VIXdcXECzlHE+zRlKBQx0H8F7BHr HGGpSGGxKzjPx7T3jMSK7tV5gTel866p9cWL5APUUNoYyN14uOWF5MVsl /VzOuZVlCXY2pQ8IJhdv+SdSKfAbykaLGB0WySSH6VJ5/Iotu7f1w0oIF tpRijlvttagAIwmn+ixio3UoI2rIwnbBJu8U5zhHPwLUuuoDSk8SwL0tt U0NTqCCh6Z+PZFmYQyBW49w4FeZsuNmbdIxd/Blh3ls74LD5eBHqnHNLy ypreXG+w1DWm9r88jXQklelYecgUexnam41HWSqN42ceOEXpzPglegSC6 w==; X-IronPort-AV: E=Sophos;i="5.56,328,1539619200"; d="scan'208";a="97343888" Received: from mail-sn1nam01lp2054.outbound.protection.outlook.com (HELO NAM01-SN1-obe.outbound.protection.outlook.com) ([104.47.32.54]) by ob1.hgst.iphmx.com with ESMTP; 08 Dec 2018 08:48:45 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sharedspace.onmicrosoft.com; s=selector1-wdc-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=JrAjeZA5O4S488yPwzSnT0UyIvda6GcnOyT/8kS/et4=; b=OrBynks856Aq2Dgv8zCErbEpuiatQZIfoi/PS1ULS6WefUBbxFOpUsVFvnM7Q1kqIORG7gXWtVCaPd4oI3myrqL//R/ZtqPZUGeRCQBRJZeamEFEA5UBR4S7oiV2sGKwrBU7b0YXgMEjkKIK2qVJA2j4Jgo5BJXkxxlOB1ObuFg= Received: from MWHPR04MB0401.namprd04.prod.outlook.com (10.173.48.18) by MWHPR04MB0304.namprd04.prod.outlook.com (10.169.202.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1404.20; Sat, 8 Dec 2018 00:48:42 +0000 Received: from MWHPR04MB0401.namprd04.prod.outlook.com ([fe80::d13d:19d3:fc93:2ec5]) by MWHPR04MB0401.namprd04.prod.outlook.com ([fe80::d13d:19d3:fc93:2ec5%9]) with mapi id 15.20.1404.021; Sat, 8 Dec 2018 00:48:42 +0000 From: Alistair Francis To: "qemu-devel@nongnu.org" , "qemu-riscv@nongnu.org" Thread-Topic: [RFC v3 16/24] riscv: tcg-target: Add branch and jump instructions Thread-Index: AQHUjo/EFc8MKJx8qUSf86VB7dnaHQ== Date: Sat, 8 Dec 2018 00:48:42 +0000 Message-ID: <4369495a6e303d93663e99b3c2239c8d2a1eccba.1544229872.git.alistair.francis@wdc.com> References: In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-mailer: git-send-email 2.19.1 x-clientproxiedby: BYAPR04CA0020.namprd04.prod.outlook.com (2603:10b6:a03:40::33) To MWHPR04MB0401.namprd04.prod.outlook.com (2603:10b6:300:70::18) authentication-results: spf=none (sender IP is ) smtp.mailfrom=Alistair.Francis@wdc.com; x-ms-exchange-messagesentrepresentingtype: 1 x-originating-ip: [199.255.44.250] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1; MWHPR04MB0304; 6:FQArYEsc6TNwQimDr2jVTPD37sZtlZ86/QixfYGMEiRc17n0Dk7XgWa/qUyy2HllDXf3nm7Ej2CWX18496y5NTVN8C7LVcFgiohkFvy5wxzDLMR+CAB4l8a3f04Nr9oDzCaSUmCDY+G0MHeMX5ziFmaFI1cQdS8KCyjS64kZLfrOtm9xhBCCs1/6Q3kxEsYABFtImGKeIWeIxWPhUARexalE3ucK8nuFw4hiof9WQN//8pbKgkdxQMDMnLa7Fk9eXlSIbskNU/g7ZdNlH2ROJZSd+yxJwUC4aKP8I+daoT4F8emfvpUgx+UhEm2DywvnWLeq9dY1WQk7uo6Byq9R0WkPtp1EbaKT9a64meNbydevZEUx8w6AcqzIqiomf2V66M8W/C+6+hPJiqWk07wVKvGEX5jWUfjNZ4w3VDWZnOt5Q9GiX2ZujA7bytFiK0zi/rKh8oss+MVHodsJhx9+pA==; 5:grxSwYgoiSjeoVuwa6IzurObNybabX3AIXqF3mwzTGghtsq0Uvrg58whZm1Ulj5dQ76GzydHKUKaIMgKuXpl4U+avEvB8RYUAtBAr89bU3Ybu3gkIO0LM7pc6ZrAYLA8agd2C7wFRULTDhTJSkbirg2VMkUZ7GfiQPkxJ/7cV0Q=; 7:AtGPhaEU/5iyAzXBcTM1vlmqJeS/PWOtTOWy56KUFZzwNLVLAXs/1YkfslyEAowQoml5vZr48p1m8zUvggWawywErn2V9mtbcy7McGcGZsAbplF2Xzq1SAXbfYHD+fhdzmCcyebTn96DBByPkCc+JA== x-ms-office365-filtering-correlation-id: b31c996e-d273-41e8-7072-08d65ca6e69f x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: BCL:0; PCL:0; RULEID:(2390098)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600074)(711020)(4618075)(2017052603328)(7153060)(7193020); SRVR:MWHPR04MB0304; x-ms-traffictypediagnostic: MWHPR04MB0304: wdcipoutbound: EOP-TRUE x-microsoft-antispam-prvs: x-ms-exchange-senderadcheck: 1 x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(8211001083)(6040522)(2401047)(5005006)(8121501046)(93006095)(93001095)(3231455)(999002)(944501520)(52105112)(3002001)(10201501046)(6055026)(148016)(149066)(150057)(6041310)(20161123564045)(20161123560045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123562045)(20161123558120)(201708071742011)(7699051)(76991095); SRVR:MWHPR04MB0304; BCL:0; PCL:0; RULEID:; SRVR:MWHPR04MB0304; x-forefront-prvs: 0880FB6EC1 x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(346002)(396003)(39860400002)(366004)(376002)(136003)(189003)(199004)(54906003)(110136005)(44832011)(386003)(6506007)(5660300001)(68736007)(2906002)(71190400001)(25786009)(71200400001)(86362001)(316002)(99286004)(14454004)(66066001)(102836004)(2501003)(6512007)(53936002)(4326008)(11346002)(6116002)(478600001)(81166006)(81156014)(3846002)(72206003)(8936002)(105586002)(256004)(118296001)(106356001)(39060400002)(446003)(6436002)(36756003)(6486002)(486006)(76176011)(186003)(2616005)(305945005)(26005)(97736004)(52116002)(476003)(50226002)(7736002)(8676002); DIR:OUT; SFP:1102; SCL:1; SRVR:MWHPR04MB0304; H:MWHPR04MB0401.namprd04.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; MX:1; A:1; x-microsoft-antispam-message-info: 44bU155RgbbdrzS2Bpd9enawxl9TykoS49DCb9MqZTqKA4+gNwSRuALGps+Qxa8wRlvbuDNO2udaVWkg+WjfMbrROJQHwfqwFeCbtwXaSy4YnUnEIsWMCACt9Nqfe7K/mB71Ow8aCIO5bGt5TCS2QEXyIWrI7UuCSj4FoXh36wHxn57M74Yl7WC3KuSpr36KKr7gGqa5LvM3T4/9Aw3NT0pVZvdZPRnsVFxSHBV0rI+RubEpzVFVgksp/3ZmSnDhueA2eVCdf2/RkzQZQJI7JHypONH+D2ms7+2v40rXR+jwQBwieEDUYLgVAAlHtpUUaqJQFw3BDiW2xgxME6ezj30C3vJ0UYY5MfyKSU0uH1k= spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: wdc.com X-MS-Exchange-CrossTenant-Network-Message-Id: b31c996e-d273-41e8-7072-08d65ca6e69f X-MS-Exchange-CrossTenant-originalarrivaltime: 08 Dec 2018 00:48:42.5481 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: b61c8803-16f3-4c35-9b17-6f65f441df86 X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR04MB0304 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 216.71.153.144 Subject: [Qemu-devel] [RFC v3 16/24] riscv: tcg-target: Add branch and jump instructions X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "alistair23@gmail.com" , "richard.henderson@linaro.org" , Alistair Francis Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Signed-off-by: Alistair Francis Signed-off-by: Michael Clark Signed-off-by: Richard Henderson --- tcg/riscv/tcg-target.inc.c | 142 +++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) diff --git a/tcg/riscv/tcg-target.inc.c b/tcg/riscv/tcg-target.inc.c index 10bd007cd4..a00bbe8ff1 100644 --- a/tcg/riscv/tcg-target.inc.c +++ b/tcg/riscv/tcg-target.inc.c @@ -736,3 +736,145 @@ static void tcg_out_addsub2(TCGContext *s, tcg_out_opc_reg(s, opc_add, rh, th, TCG_REG_TMP0); } } + +static const struct { + RISCVInsn op; + bool swap; +} tcg_brcond_to_riscv[] = { + [TCG_COND_EQ] = { OPC_BEQ, false }, + [TCG_COND_NE] = { OPC_BNE, false }, + [TCG_COND_LT] = { OPC_BLT, false }, + [TCG_COND_GE] = { OPC_BGE, false }, + [TCG_COND_LE] = { OPC_BGE, true }, + [TCG_COND_GT] = { OPC_BLT, true }, + [TCG_COND_LTU] = { OPC_BLTU, false }, + [TCG_COND_GEU] = { OPC_BGEU, false }, + [TCG_COND_LEU] = { OPC_BGEU, true }, + [TCG_COND_GTU] = { OPC_BLTU, true } +}; + +static void tcg_out_brcond(TCGContext *s, TCGCond cond, TCGReg arg1, + TCGReg arg2, TCGLabel *l) +{ + RISCVInsn op = tcg_brcond_to_riscv[cond].op; + + tcg_debug_assert(op != 0); + + if (tcg_brcond_to_riscv[cond].swap) { + TCGReg t = arg1; + arg1 = arg2; + arg2 = t; + } + + if (l->has_value) { + intptr_t diff = tcg_pcrel_diff(s, l->u.value_ptr); + if (diff == sextreg(diff, 0, 12)) { + tcg_out_opc_branch(s, op, arg1, arg2, diff); + } else { + /* Invert the conditional branch. */ + tcg_out_opc_branch(s, op ^ (1 << 12), arg1, arg2, 8); + tcg_out_opc_jump(s, OPC_JAL, TCG_REG_ZERO, diff - 4); + } + } else { + tcg_out_reloc(s, s->code_ptr, R_RISCV_BRANCH, l, 0); + tcg_out_opc_branch(s, op, arg1, arg2, 0); + /* NOP to allow patching later */ + tcg_out_opc_imm(s, OPC_ADDI, TCG_REG_ZERO, TCG_REG_ZERO, 0); + } +} + +static void tcg_out_setcond(TCGContext *s, TCGCond cond, TCGReg ret, + TCGReg arg1, TCGReg arg2) +{ + switch (cond) { + case TCG_COND_EQ: + tcg_out_opc_reg(s, OPC_SUB, ret, arg1, arg2); + tcg_out_opc_imm(s, OPC_SLTIU, ret, ret, 1); + break; + case TCG_COND_NE: + tcg_out_opc_reg(s, OPC_SUB, ret, arg1, arg2); + tcg_out_opc_reg(s, OPC_SLTU, ret, TCG_REG_ZERO, ret); + break; + case TCG_COND_LT: + tcg_out_opc_reg(s, OPC_SLT, ret, arg1, arg2); + break; + case TCG_COND_GE: + tcg_out_opc_reg(s, OPC_SLT, ret, arg1, arg2); + tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1); + break; + case TCG_COND_LE: + tcg_out_opc_reg(s, OPC_SLT, ret, arg2, arg1); + tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1); + break; + case TCG_COND_GT: + tcg_out_opc_reg(s, OPC_SLT, ret, arg2, arg1); + break; + case TCG_COND_LTU: + tcg_out_opc_reg(s, OPC_SLTU, ret, arg1, arg2); + break; + case TCG_COND_GEU: + tcg_out_opc_reg(s, OPC_SLTU, ret, arg1, arg2); + tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1); + break; + case TCG_COND_LEU: + tcg_out_opc_reg(s, OPC_SLTU, ret, arg2, arg1); + tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1); + break; + case TCG_COND_GTU: + tcg_out_opc_reg(s, OPC_SLTU, ret, arg2, arg1); + break; + default: + g_assert_not_reached(); + break; + } +} + +static void tcg_out_brcond2(TCGContext *s, TCGCond cond, TCGReg al, TCGReg ah, + TCGReg bl, TCGReg bh, TCGLabel *l) +{ + /* todo */ + g_assert_not_reached(); +} + +static void tcg_out_setcond2(TCGContext *s, TCGCond cond, TCGReg ret, + TCGReg al, TCGReg ah, TCGReg bl, TCGReg bh) +{ + /* todo */ + g_assert_not_reached(); +} + +static inline void tcg_out_goto(TCGContext *s, tcg_insn_unit *target) +{ + ptrdiff_t offset = tcg_pcrel_diff(s, target); + tcg_debug_assert(offset == sextreg(offset, 1, 20) << 1); + tcg_out_opc_jump(s, OPC_JAL, TCG_REG_ZERO, offset); +} + +static void tcg_out_call_int(TCGContext *s, tcg_insn_unit *arg, bool tail) +{ + TCGReg link = tail ? TCG_REG_ZERO : TCG_REG_RA; + ptrdiff_t offset = tcg_pcrel_diff(s, arg); + if (offset == sextreg(offset, 1, 20) << 1) { + /* short jump: -2097150 to 2097152 */ + tcg_out_opc_jump(s, OPC_JAL, link, offset); + } else if (TCG_TARGET_REG_BITS == 32 || + offset == sextreg(offset, 1, 31) << 1) { + /* long jump: -2147483646 to 2147483648 */ + tcg_out_opc_upper(s, OPC_AUIPC, TCG_REG_TMP0, 0); + tcg_out_opc_imm(s, OPC_JALR, link, TCG_REG_TMP0, 0); + reloc_call(s->code_ptr - 2, arg); + } else if (TCG_TARGET_REG_BITS == 64) { + /* far jump: 64-bit */ + tcg_target_long imm = sextreg((tcg_target_long)arg, 0, 12); + tcg_target_long base = (tcg_target_long)arg - imm; + tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP0, base); + tcg_out_opc_imm(s, OPC_JALR, link, TCG_REG_TMP0, imm); + } else { + g_assert_not_reached(); + } +} + +static void tcg_out_call(TCGContext *s, tcg_insn_unit *arg) +{ + tcg_out_call_int(s, arg, false); +}