From nobody Sat Nov 1 17:45:43 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail header.i=@wdc.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=wdc.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1544644600967795.0228549961379; Wed, 12 Dec 2018 11:56:40 -0800 (PST) Received: from localhost ([::1]:47760 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gXAcV-0002ph-KI for importer@patchew.org; Wed, 12 Dec 2018 14:56:35 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48975) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gXARa-00021n-Px for qemu-devel@nongnu.org; Wed, 12 Dec 2018 14:45:22 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gXARW-0002l3-P2 for qemu-devel@nongnu.org; Wed, 12 Dec 2018 14:45:18 -0500 Received: from esa4.hgst.iphmx.com ([216.71.154.42]:29270) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gXARW-0002jz-BS; Wed, 12 Dec 2018 14:45:14 -0500 Received: from mail-bn3nam01lp2058.outbound.protection.outlook.com (HELO NAM01-BN3-obe.outbound.protection.outlook.com) ([104.47.33.58]) by ob1.hgst.iphmx.com with ESMTP; 13 Dec 2018 03:45:12 +0800 Received: from MWHPR04MB0401.namprd04.prod.outlook.com (10.173.48.18) by MWHPR04MB0512.namprd04.prod.outlook.com (10.173.49.13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1404.19; Wed, 12 Dec 2018 19:45:10 +0000 Received: from MWHPR04MB0401.namprd04.prod.outlook.com ([fe80::9067:fb44:66b2:7604]) by MWHPR04MB0401.namprd04.prod.outlook.com ([fe80::9067:fb44:66b2:7604%3]) with mapi id 15.20.1425.016; Wed, 12 Dec 2018 19:45:10 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1544643915; x=1576179915; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=AhN2PSOZV/z475YMTu0SuDzbnrY0Fun4fLpZN2svjnA=; b=Ru3oObghHMhQkViW/7c4nt7pMjUYaIkqeta2zN1Rg9OV49M3QdWy9Y8I 2zvNPUsjnlx1npVysOlqFKXRa40zIBFp/gJnMQ3lACINS3KTKdvqc6Kk1 14Zexy4zw4nMCTe17wZHkKRe8lRNvXw4H9audqz6Z6bCYZbrPTn1n0T/L Sir9+Ebe3CTCSwL7KB4/2121+n0PMQefs7aWIHk672rPr2ACZrqaH5xOZ 8BCTUDIivhr4KJap9ADM9l/Zn01kT9Of9fxMlOEnrwA8JU+VKSCcHGLu0 vsB66pBerYFGG3fLvpc2uV6VyTyYvqR0tpo54ZYYDFMM79xBAqgvPA7Cp Q==; X-IronPort-AV: E=Sophos;i="5.56,345,1539619200"; d="scan'208";a="96440912" 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=8PZZwg/nQvpn/Sb0igEEylc+b5TLbtVS5erbSlgrAdY=; b=d+8cDYvCSKBl/3ntQ/Mv3GVptLGcAReSstMiS3zDzeDFwd5uGlyQ4maB+oRVLhwBREQOTvy3FuVeZxhTB/jAs4SYKzGN2mEZkqXfZTh3ymIP6ERb35/XlVK21iqYDJeyJaLC8K41vrI6n6rMRYcipEIXF+FXtWRQzshuo+a5+E8= From: Alistair Francis To: "qemu-devel@nongnu.org" , "qemu-riscv@nongnu.org" Thread-Topic: [PATCH v1 15/23] riscv: tcg-target: Add branch and jump instructions Thread-Index: AQHUklMxzxpCc8jMVEexYLHGnmk9sQ== Date: Wed, 12 Dec 2018 19:45:10 +0000 Message-ID: <689fb436a8ead8ffaadc76155b0ed0e74972e689.1544643238.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: BYAPR01CA0025.prod.exchangelabs.com (2603:10b6:a02:80::38) 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.171] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1; MWHPR04MB0512; 6:Isi8JvrChbEgZiX9/7NH6eq8yLU+zfFtcUIzIY8oZxEW9QLlo9YITL90TIpEVaUB3LpOqCUED8+zG6MZToUN6+FncWHUb0htc3Iwz8/aEoc/C9Hw3PD+0R75dqSSHSP36TQlJnH+u7Tdokc1H8OM0bIWTeUlqUpKP5ztlfm+cJwF6ePX6B5VCpP7sGppPmdtQXIGu6e9gD0uloxGgjIqRpjHrpQKPkasrVAfHhQyomTd0qQ1Yoj+5ToVCbdv+FWb4+sqpLZx34seomqItksyopu9nuaeUdLAH1yqaAj5I4IAsrP1doBQ7WlXSAz2koN3QaNyyO3WJwrCapuB0XPnU5Gw2GTLoy5wkS6lNEFN/kKHeTZ7NzKAPafEYEXR3V2dBywajv7YpV1xdopOoK0RrwQCRU5PX1rzt/6kcRbn/J6KOgBiHNnQVXMm65D+qUOCszgl3UyghLCzFWC9WKIUyg==; 5:MnYZ3RiCJzjQZ5CFaAllpxC/Cx0LUN38nMtBsdQ9Gx2D7PY/RzYVUuRVXzN4o/2pulXl3fDF6IT/UnixMeuQZVNap4Q+7hw5FoKSUKBd0TFtSeBkgng5SD6j+3TPpAmWwA09FbqkVlRFjAqVnJhIOXgANOpTllx9F/KT0wxl+fg=; 7:Jejpx37AjeKLTlbPWMjfTmlwqzcJOZwDm0wLla/Mcf+Sd11v/5RKZmAg6C5xkdRGkunSRYZzwKadz0E7DQhiv+C5Z8/B4/LRCr7JPqeKMQwZk/0W6q4v6pH16XH32QAIb4WbZGt8/CB1pxFX9zgoyA== x-ms-office365-filtering-correlation-id: 68883286-66c9-40a5-2c9f-08d6606a5375 x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: BCL:0; PCL:0; RULEID:(2390098)(7020095)(4652040)(8989299)(5600074)(711020)(4618075)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(2017052603328)(7153060)(7193020); SRVR:MWHPR04MB0512; x-ms-traffictypediagnostic: MWHPR04MB0512: wdcipoutbound: EOP-TRUE x-microsoft-antispam-prvs: x-ms-exchange-senderadcheck: 1 x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(8211001083)(3230017)(999002)(6040522)(2401047)(5005006)(8121501046)(3231472)(944501520)(52105112)(3002001)(10201501046)(93006095)(93001095)(6055026)(148016)(149066)(150057)(6041310)(20161123560045)(20161123562045)(20161123558120)(20161123564045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(201708071742011)(7699051)(76991095); SRVR:MWHPR04MB0512; BCL:0; PCL:0; RULEID:; SRVR:MWHPR04MB0512; x-forefront-prvs: 0884AAA693 x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(346002)(39860400002)(396003)(366004)(136003)(376002)(199004)(189003)(316002)(66066001)(97736004)(71200400001)(2501003)(71190400001)(76176011)(486006)(8936002)(6506007)(386003)(102836004)(118296001)(3846002)(81166006)(44832011)(186003)(25786009)(26005)(11346002)(6436002)(6486002)(99286004)(2616005)(105586002)(8676002)(6116002)(81156014)(52116002)(5660300001)(6512007)(478600001)(72206003)(53936002)(68736007)(256004)(50226002)(110136005)(446003)(54906003)(39060400002)(14454004)(36756003)(7736002)(2906002)(476003)(4326008)(86362001)(305945005)(106356001); DIR:OUT; SFP:1102; SCL:1; SRVR:MWHPR04MB0512; H:MWHPR04MB0401.namprd04.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; A:1; MX:1; x-microsoft-antispam-message-info: O21b+fbaEj65yxj91vlLNLoI3AZx1y72Vn6QndvKQePIvn0tIcUhp++61sEaeLzmj3X6Yp4uyFRDCSMX6mA5eXrkJ55Kqi0hh806Z9AysOMFQalL2jH3bbPa1dWQlnIajNeMVC/EflHNzQlKE/GFt2hafcOv5hQL+wDpAKnFdTXIxpKd3Og0Gxhksw4vTHjBP8V1oHAXc0tlSqh3llR20/30eirwfa1CY+p3Pu3dI0lDHkgJWH9lM6igo8e90JwGTNUDIM7kN2CNpU7/Lt4S0KxwS3w6+7u32mxlBTZ6GAIzfP9BGNhLlezx0QBbE9gn spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: wdc.com X-MS-Exchange-CrossTenant-Network-Message-Id: 68883286-66c9-40a5-2c9f-08d6606a5375 X-MS-Exchange-CrossTenant-originalarrivaltime: 12 Dec 2018 19:45:10.4679 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: b61c8803-16f3-4c35-9b17-6f65f441df86 X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR04MB0512 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 216.71.154.42 Subject: [Qemu-devel] [PATCH v1 15/23] 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (found 2 invalid signatures) Content-Type: text/plain; charset="utf-8" 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 586a8917c7..f1ebd86516 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[] =3D { + [TCG_COND_EQ] =3D { OPC_BEQ, false }, + [TCG_COND_NE] =3D { OPC_BNE, false }, + [TCG_COND_LT] =3D { OPC_BLT, false }, + [TCG_COND_GE] =3D { OPC_BGE, false }, + [TCG_COND_LE] =3D { OPC_BGE, true }, + [TCG_COND_GT] =3D { OPC_BLT, true }, + [TCG_COND_LTU] =3D { OPC_BLTU, false }, + [TCG_COND_GEU] =3D { OPC_BGEU, false }, + [TCG_COND_LEU] =3D { OPC_BGEU, true }, + [TCG_COND_GTU] =3D { OPC_BLTU, true } +}; + +static void tcg_out_brcond(TCGContext *s, TCGCond cond, TCGReg arg1, + TCGReg arg2, TCGLabel *l) +{ + RISCVInsn op =3D tcg_brcond_to_riscv[cond].op; + + tcg_debug_assert(op !=3D 0); + + if (tcg_brcond_to_riscv[cond].swap) { + TCGReg t =3D arg1; + arg1 =3D arg2; + arg2 =3D t; + } + + if (l->has_value) { + intptr_t diff =3D tcg_pcrel_diff(s, l->u.value_ptr); + if (diff =3D=3D 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 =3D tcg_pcrel_diff(s, target); + tcg_debug_assert(offset =3D=3D 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 =3D tail ? TCG_REG_ZERO : TCG_REG_RA; + ptrdiff_t offset =3D tcg_pcrel_diff(s, arg); + if (offset =3D=3D 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 =3D=3D 32 || + offset =3D=3D 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 =3D=3D 64) { + /* far jump: 64-bit */ + tcg_target_long imm =3D sextreg((tcg_target_long)arg, 0, 12); + tcg_target_long base =3D (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); +} --=20 2.19.1