Ticket #5233: 0001-Make-it-possible-to-test-the-generated-assembly.patch

File 0001-Make-it-possible-to-test-the-generated-assembly.patch, 4.5 KB (added by tibbe, 4 years ago)
  • driver/testlib.py

    From 75a726a8a4f41010f515d0119ff5bb3a23b884b0 Mon Sep 17 00:00:00 2001
    From: Johan Tibell <[email protected]>
    Date: Tue, 31 May 2011 11:56:58 +0200
    Subject: [PATCH] Make it possible to test the generated assembly
    
    This test framework feature is inspired by a similar feature in LLVM.
    The programmer writes a bit of Cmm
    
        #include "Cmm.h"
    
        // Large memcpy's should lower to calls.
        callMemcpy
        {
          W_ dst, src;
          prim %memcpy(dst "ptr", src "ptr", 1024, 4) [];
        }
    
    and asserts what the generated assembly should look like, modulo
    register naming.
    
        callMemcpy:
        movq  ; Move arguments into place
        movq
        movl
        movl
        call memcpy
    ---
     driver/testlib.py                                  |   48 +++++++++++++++++++-
     tests/ghc-regress/codeGen/should_gen_asm/all.T     |    1 +
     .../codeGen/should_gen_asm/cgasm001.asm            |    6 +++
     .../codeGen/should_gen_asm/cgasm001.cmm            |    8 +++
     4 files changed, 62 insertions(+), 1 deletions(-)
     create mode 100644 tests/ghc-regress/codeGen/should_gen_asm/all.T
     create mode 100644 tests/ghc-regress/codeGen/should_gen_asm/cgasm001.asm
     create mode 100644 tests/ghc-regress/codeGen/should_gen_asm/cgasm001.cmm
    
    diff --git a/driver/testlib.py b/driver/testlib.py
    index 7909a61..d59806f 100644
    a b def do_compile( name, way, should_fail, top_mod, extra_hc_opts ): 
    762762    # no problems found, this test passed 
    763763    return 'pass' 
    764764 
     765def compile_cmp_asm( name, way, extra_hc_opts ): 
     766    print 'Compile only, extra args = ', extra_hc_opts 
     767    pretest_cleanup(name) 
     768    result = simple_build( name + '.cmm', way, '-keep-s-files ' + extra_hc_opts, 0, '', 0, 0) 
     769 
     770    if result == 'fail': 
     771        return result 
     772 
     773    # the actual stderr should always match the expected, regardless 
     774    # of whether we expected the compilation to fail or not (successful 
     775    # compilations may generate warnings). 
     776 
     777    if getTestOpts().with_namebase == None: 
     778        namebase = name 
     779    else: 
     780        namebase = getTestOpts().with_namebase 
     781 
     782    (platform_specific, expected_asm_file) = platform_wordsize_qualify(namebase, 'asm') 
     783    actual_asm_file = qualify(name, 's') 
     784 
     785    if not compare_outputs('asm', normalise_errmsg, normalise_asm, \ 
     786                           expected_asm_file, actual_asm_file): 
     787        return 'fail' 
     788 
     789    # no problems found, this test passed 
     790    return 'pass' 
     791 
    765792# ----------------------------------------------------------------------------- 
    766793# Compile-and-run tests 
    767794 
    def normalise_output( str ): 
    13681395    str = re.sub('([^\\s])\\.exe', '\\1', str) 
    13691396    return str 
    13701397 
     1398def normalise_asm( str ): 
     1399    lines = str.split('\n') 
     1400    # Only keep instructions and labels not starting with a dot. 
     1401    metadata = re.compile('^[ \t]*\\..*$') 
     1402    out = [] 
     1403    for line in lines: 
     1404      # Drop metadata directives (e.g. ".type") 
     1405      if not metadata.match(line): 
     1406        instr = line.lstrip().split() 
     1407        # Drop empty lines. 
     1408        if not instr: 
     1409          continue 
     1410        # Drop operands, except for call instructions. 
     1411        elif instr[0] == 'call': 
     1412          out.append(instr[0] + ' ' + instr[1]) 
     1413        else: 
     1414          out.append(instr[0]) 
     1415    out = '\n'.join(out) 
     1416    return out 
     1417 
    13711418def if_verbose( n, str ): 
    13721419    if config.verbose >= n: 
    13731420        print str 
    def getStdout(cmd): 
    16051652        return stdout 
    16061653    else: 
    16071654        raise Exception("Need subprocess to get stdout, but don't have it") 
    1608  
  • new file tests/ghc-regress/codeGen/should_gen_asm/all.T

    diff --git a/tests/ghc-regress/codeGen/should_gen_asm/all.T b/tests/ghc-regress/codeGen/should_gen_asm/all.T
    new file mode 100644
    index 0000000..5b4ffee
    - +  
     1test('cgasm001', only_compiler_types(['ghc']), compile_cmp_asm, ['']) 
  • new file tests/ghc-regress/codeGen/should_gen_asm/cgasm001.asm

    diff --git a/tests/ghc-regress/codeGen/should_gen_asm/cgasm001.asm b/tests/ghc-regress/codeGen/should_gen_asm/cgasm001.asm
    new file mode 100644
    index 0000000..d223d71
    - +  
     1callMemcpy: 
     2movq  ; Move arguments into place 
     3movq 
     4movl 
     5movl 
     6call memcpy 
  • new file tests/ghc-regress/codeGen/should_gen_asm/cgasm001.cmm

    diff --git a/tests/ghc-regress/codeGen/should_gen_asm/cgasm001.cmm b/tests/ghc-regress/codeGen/should_gen_asm/cgasm001.cmm
    new file mode 100644
    index 0000000..3cec3a2
    - +  
     1#include "Cmm.h" 
     2 
     3// Large memcpy's should lower to calls. 
     4callMemcpy 
     5{ 
     6  W_ dst, src; 
     7  prim %memcpy(dst "ptr", src "ptr", 1024, 4) []; 
     8}