summaryrefslogtreecommitdiff
path: root/tests/tst-assembler.c
blob: aa893910a3944b3c045e1ff33e2f480ec0f41efb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <string.h>
#include <libmcpu.h>
#include "../utils/mcpu-asm.h"
#define CHECK(c,m) do{if(!(c)){fprintf(stderr,"FAIL: %s\n",m);return 1;}}while(0)
static const char src[]=
".data\n"
"big:\n"
"  .integer 128, 123456789012345678901234567890\n"
"rv:\n"
"  .real 128, 1.25\n"
"cv:\n"
"  .complex 128, 1.5, -2.5\n"
".text\n"
"  LDI r128@0x00000, -17\n"
"  LDR r128@0x00010, -17.0\n"
"  LDC c128@0x00020, -17.0r0+32.0j0\n"
"  RCONST r128@0x00030, PI\n"
"  IADD r128@0x00040, r128@0x00000, r128@0x00000\n"
"  RADD r128@0x00050, r128@0x00010, r128@0x00010\n"
"  CADD c128@0x00060, c128@0x00020, c128@0x00020\n"
"  LOAD r128@0x00070, big\n"
"  HALT\n";
int
main( void )
{
  cpu_asm_image im;
  char err[256];
  mpu_int128_t ie;
  mpu_real128_t re, rr;
  mpu_wide_real_t cr, ci, er, ei;
  int rc;
 __cpu_init();
 CHECK( cpu_assemble_text( src, &im, err, sizeof( err ) ) == 0, err );
 CHECK( im.code_size > 0, "code produced" );
 CHECK( im.data_size == 64, "DATA NULL guard plus directives size" );
 CHECK( !memcmp( im.data, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", CPU_DATA_NULL_GUARD_SIZE ), "DATA NULL guard bytes" );
 CHECK( __cpu_program_set( im.code, im.code_size ) == 0, "program set" );
 CHECK( __cpu_data_set( im.data, im.data_size ) == 0, "data set" );
 rc = __cpu_run();
 CHECK( rc == 0, "assembled program run" );
 iatoi( ( mpu_int * ) ie, (__mpu_char8_t *)"-17", NB_I128 );
 CHECK( !memcmp( __cpu_reg_ptr( 128, 0 ), ie, NB_I128 ), "LDI" );
 ascii_to_real( ( mpu_real * ) re, (__mpu_char8_t *)"-17.0", NB_R128 );
 CHECK( r_cmp( ( mpu_real * ) __cpu_reg_ptr( 128, 16 ), ( mpu_real * ) re, NB_R128 ) == 0, "LDR" );
 _m_PI( ( mpu_real * ) rr, NB_R128 );
 CHECK( r_cmp( ( mpu_real * ) __cpu_reg_ptr( 128, 48 ), ( mpu_real * ) rr, NB_R128 ) == 0, "RCONST PI" );
 c_real_part( ( mpu_real * ) cr, ( mpu_complex * ) __cpu_reg_ptr( 128, 32 ), NB_R64 );
 c_imaginary( ( mpu_real * ) ci, ( mpu_complex * ) __cpu_reg_ptr( 128, 32 ), NB_R64 );
 ascii_to_real( ( mpu_real * ) er, (__mpu_char8_t *)"-17.0", NB_R64 );
 ascii_to_real( ( mpu_real * ) ei, (__mpu_char8_t *)"32.0", NB_R64 );
 CHECK( r_cmp( ( mpu_real * ) cr, ( mpu_real * ) er, NB_R64 ) == 0, "LDC real component" );
 CHECK( r_cmp( ( mpu_real * ) ci, ( mpu_real * ) ei, NB_R64 ) == 0, "LDC imag component" );
 iatoi( ( mpu_int * ) ie, (__mpu_char8_t *)"123456789012345678901234567890", NB_I128 );
 CHECK( !memcmp( __cpu_reg_ptr( 128, 112 ), ie, NB_I128 ), "LOAD DATA integer" );
 cpu_asm_image_free( &im );
 __cpu_free_context();
 puts( "libcpu test assembler regression: PASS" );
 return( 0 );
 }