[PATCH] D55758: [TableGen] : Extend !if semantics through new language feature !ifs

Javed Absar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 17 01:54:20 PST 2018


javed.absar created this revision.
javed.absar added reviewers: nhaehnle, greened, Eugene.Zelenko.

Currently, some TableGen expressions are forced to use multiple embedded '!if' which makes the the expression cumbersome. Below is an example (from an actual td file):

  list<SubRegIndex> ret = !if(!eq(size, 2), ret2,
                                                !if(!eq(size, 3), ret3,
                                                    !if(!eq(size, 4), ret4,
                                                         !if(!eq(size, 8), ret8, ret16)))); 

This patch will allow such expession to be more easily written as:

list<SubRegIndex> ret = !ifs(!eq(size, 2) : ret2,

  !eq(size, 3) : ret3,
  !eq(size, 4) : ret4,
  !eq(size, 8) : ret8,
  default: ret16);  

The default can appear in the begining, end or anywhere within the !ifs.

The grammer of !ifs is :

  SimpleValue ::= IFS '(' [Value ':' Value,]+ DEFAULT ':' Value ')'
  
   

We could call 'ifs' 'switch' although it is slightly different from C language switch as the case value is not a constant.
Some of the tests below are ported from !if as those check are required of !ifs as well.

``!ifs(cond1: val1, cond2 : val2, ..., default : valn)``

  Instead of embedding !if inside !if which can get cumbersome, one can use !ifs.
  
  !ifs returns 'val1' if the result of 'int' or 'bit' operator 'cond1' is nonzero.
  
  Otherwise, checks 'cond2'. If 'cond2' is nonzero, returns 'val2', and so on.
  
  If all conditions are zero, the default value 'valn' is returned.
  
  The default value can appear anywhere, but there must be
  
  one and only one default value. Below is an example to convert an integer 'x'
  
  into string form:
  
   
  
  string S =  !ifs(!lt(x,0) : "Negative",
  
                   !eq(x,0) : "zero",
  
                   !eq(x,1) : "one",
  
                   default  : "MoreThanOne");


https://reviews.llvm.org/D55758

Files:
  docs/TableGen/LangIntro.rst
  include/llvm/TableGen/Record.h
  lib/TableGen/Record.cpp
  lib/TableGen/TGLexer.cpp
  lib/TableGen/TGLexer.h
  lib/TableGen/TGParser.cpp
  lib/TableGen/TGParser.h
  test/TableGen/ifs-empty-list-arg.td
  test/TableGen/ifs-type.td
  test/TableGen/ifs-usage.td
  test/TableGen/ifs.td
  test/TableGen/ifsbit.td

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D55758.178433.patch
Type: text/x-patch
Size: 23129 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181217/ec7fec27/attachment.bin>


More information about the llvm-commits mailing list