From 57117fa687de59234d299d76f406f4e666844f1f Mon Sep 17 00:00:00 2001 From: acqn Date: Thu, 23 Jan 2020 06:39:37 +0800 Subject: [PATCH] Utility functions about compare conditions. --- src/cc65/codeinfo.c | 59 +++++++++++++++++++++++++++++++++++++++++++++ src/cc65/codeinfo.h | 14 ++++++++++- src/cc65/coptcmp.c | 6 +---- 3 files changed, 73 insertions(+), 6 deletions(-) diff --git a/src/cc65/codeinfo.c b/src/cc65/codeinfo.c index a89f6d54c..6bc62757e 100644 --- a/src/cc65/codeinfo.c +++ b/src/cc65/codeinfo.c @@ -64,6 +64,13 @@ static const char CmpSuffixTab [][4] = { "eq", "ne", "gt", "ge", "lt", "le", "ugt", "uge", "ult", "ule" }; +/* Table with the bool transformers */ +static const char BoolTransformerTab [][8] = { + "booleq", "boolne", + "boolgt", "boolge", "boollt", "boolle", + "boolugt", "booluge", "boolult", "boolule" +}; + /* Table listing the function names and code info values for known internally ** used functions. This table should get auto-generated in the future. */ @@ -840,3 +847,55 @@ cmp_t FindTosCmpCond (const char* Name) return CMP_INV; } } + + + +const char* GetBoolTransformer (cmp_t Cond) +/* Get the bool transformer corresponding to the given compare condition */ +{ + if (Cond > CMP_INV && Cond < CMP_END) { + return BoolTransformerTab[Cond]; + } + + /* Not found */ + return 0; +} + + +cmp_t GetNegatedCond (cmp_t Cond) +/* Get the logically opposite compare condition */ +{ + switch (Cond) { + case CMP_EQ: return CMP_NE; + case CMP_NE: return CMP_EQ; + case CMP_GT: return CMP_LE; + case CMP_GE: return CMP_LT; + case CMP_LT: return CMP_GE; + case CMP_LE: return CMP_GT; + case CMP_UGT: return CMP_ULE; + case CMP_UGE: return CMP_ULT; + case CMP_ULT: return CMP_UGE; + case CMP_ULE: return CMP_UGT; + default: return CMP_INV; + } +} + + + +cmp_t GetRevertedCond (cmp_t Cond) +/* Get the compare condition in reverted order of operands */ +{ + switch (Cond) { + case CMP_EQ: return CMP_EQ; + case CMP_NE: return CMP_NE; + case CMP_GT: return CMP_LT; + case CMP_GE: return CMP_LE; + case CMP_LT: return CMP_GT; + case CMP_LE: return CMP_GE; + case CMP_UGT: return CMP_ULT; + case CMP_UGE: return CMP_ULE; + case CMP_ULT: return CMP_UGT; + case CMP_ULE: return CMP_UGE; + default: return CMP_INV; + } +} diff --git a/src/cc65/codeinfo.h b/src/cc65/codeinfo.h index 38e196bcf..3dda3a6bc 100644 --- a/src/cc65/codeinfo.h +++ b/src/cc65/codeinfo.h @@ -115,7 +115,10 @@ typedef enum { CMP_UGT, CMP_UGE, CMP_ULT, - CMP_ULE + CMP_ULE, + + /* End of the enumeration */ + CMP_END } cmp_t; @@ -185,6 +188,15 @@ cmp_t FindTosCmpCond (const char* Name); ** Return the condition code or CMP_INV on failure. */ +const char* GetBoolTransformer (cmp_t Cond); +/* Get the bool transformer corresponding to the given compare condition */ + +cmp_t GetNegatedCond (cmp_t Cond); +/* Get the logically opposite compare condition */ + +cmp_t GetRevertedCond (cmp_t Cond); +/* Get the compare condition in reverted order of operands */ + /* End of codeinfo.h */ diff --git a/src/cc65/coptcmp.c b/src/cc65/coptcmp.c index dbc71bcd5..ca0ba39a8 100644 --- a/src/cc65/coptcmp.c +++ b/src/cc65/coptcmp.c @@ -448,11 +448,7 @@ unsigned OptCmp3 (CodeSeg* S) Delete = 1; break; - case CMP_UGT: - case CMP_UGE: - case CMP_ULT: - case CMP_ULE: - case CMP_INV: + default: /* Leave it alone */ break; }