c.drv 14.2 KB
Newer Older
1
printer "c"
2

3 4 5
prelude "#include <stdlib.h>"
prelude "#include <stdint.h>"
prelude "#include <stdio.h>"
6
prelude "#include <assert.h>"
7

8
module Ref
9
  syntax type ref "%1"
10 11
  syntax val ref "%1"            prec 0
  syntax val contents "%1"       prec 0
12 13 14
end

module ref.Ref
15
  syntax val (!_) "%1"           prec 0
16
  syntax val (:=) "%1 = %2"      prec 14 13 14
17 18
end

19 20 21 22 23 24
module mach.int.Unsigned

  syntax constant zero_unsigned "0"

end

25 26
module mach.int.Int32

27
  syntax type int32  "int32_t"
28
  syntax literal int32 "%d"
29

30 31 32 33 34 35 36 37 38 39 40
  syntax val (+)     "%1 + %2"   prec 4 4 3
  syntax val (-)     "%1 - %2"   prec 4 4 3
  syntax val (-_)    "-%1"       prec 2 1
  syntax val ( * )   "%1 * %2"   prec 3 3 2
  syntax val (/)     "%1 / %2"   prec 3 3 2
  syntax val (%)     "%1 % %2"   prec 3 3 2
  syntax val (=)     "%1 == %2"  prec 7 7 6
  syntax val (<=)    "%1 <= %2"  prec 6 6 5
  syntax val (<)     "%1 < %2"   prec 6 6 5
  syntax val (>=)    "%1 >= %2"  prec 6 6 5
  syntax val (>)     "%1 > %2"   prec 6 6 5
41 42
end

Raphael Rieu-Helft's avatar
Raphael Rieu-Helft committed
43 44 45 46
module mach.int.UInt32Gen

  syntax type uint32 "uint32_t"

47 48
  syntax val max_uint32 "0xffffffff" prec 0
  syntax val length "32"             prec 0
Raphael Rieu-Helft's avatar
Raphael Rieu-Helft committed
49 50 51 52 53

end

module mach.int.UInt32

54
  syntax literal uint32 "0x%8xU"
55

56 57 58 59 60 61 62 63 64 65 66
  syntax val (+)     "%1 + %2"   prec 4 4 3
  syntax val (-)     "%1 - %2"   prec 4 4 3
  syntax val (-_)    "-%1"       prec 2 1
  syntax val ( * )   "%1 * %2"   prec 3 3 2
  syntax val (/)     "%1 / %2"   prec 3 3 2
  syntax val (%)     "%1 % %2"   prec 3 3 2
  syntax val (=)     "%1 == %2"  prec 7 7 6
  syntax val (<=)    "%1 <= %2"  prec 6 6 5
  syntax val (<)     "%1 < %2"   prec 6 6 5
  syntax val (>=)    "%1 >= %2"  prec 6 6 5
  syntax val (>)     "%1 > %2"   prec 6 6 5
Raphael Rieu-Helft's avatar
Raphael Rieu-Helft committed
67 68 69

end

70
module mach.int.UInt32GMP
71

72 73 74 75 76
  prelude

"
#define LOW_MASK 0x00000000FFFFFFFFULL

77 78 79 80 81
struct __add32_with_carry_result
{ uint32_t __field_0;
  uint32_t __field_1;
};

82
struct __add32_with_carry_result add32_with_carry(uint32_t x, uint32_t y, uint32_t c)
83
{
84
  struct __add32_with_carry_result result;
85
  uint64_t r = (uint64_t)x + (uint64_t)y + (uint64_t) c;
86 87 88
  result.__field_0 = (uint32_t)(r & LOW_MASK);
  result.__field_1 = (uint32_t)(r >> 32);
  return result;
89 90
}

91 92 93 94 95
struct __sub32_with_borrow_result
{ uint32_t __field_0;
  uint32_t __field_1;
};

96
struct __sub32_with_borrow_result sub32_with_borrow(uint32_t x, uint32_t y, uint32_t b)
97
{
98
  struct __sub32_with_borrow_result result;
99
  uint64_t r = (uint64_t)x - (uint64_t)y - (uint64_t) b;
100 101 102
  result.__field_0 = (uint32_t)(r & LOW_MASK);
  result.__field_1 = (uint32_t)(r >> 63);
  return result;
103 104
}

105 106 107 108 109
struct __mul32_double_result
{ uint32_t __field_0;
  uint32_t __field_1;
};

110
struct __mul32_double_result mul32_double(uint32_t x, uint32_t y)
111
{
112
  struct __mul32_double_result result;
113
  uint64_t r = (uint64_t)x * (uint64_t)y;
114 115 116
  result.__field_0 = (uint32_t)(r & LOW_MASK);
  result.__field_1 = (uint32_t)(r >> 32);
  return result;
117 118
}

119 120 121 122 123
struct __add32_3_result
{ uint32_t __field_0;
  uint32_t __field_1;
};

124
struct __add32_3_result add32_3(uint32_t x, uint32_t y, uint32_t z)
125
{
126
  struct __add32_3_result result;
127
  uint64_t r = (uint64_t)x + (uint64_t)y + (uint64_t) z;
128 129 130
  result.__field_0 = (uint32_t)(r & LOW_MASK);
  result.__field_1 = (uint32_t)(r >> 32);
  return result;
131 132
}

133 134 135 136 137
struct __lsld32_result
{ uint32_t __field_0;
  uint32_t __field_1;
};

138
struct __lsld32_result lsld32(uint32_t x, uint32_t cnt)
139
{
140
  struct __lsld32_result result;
141
  uint64_t r = (uint64_t)x << cnt;
142 143 144
  result.__field_0 = (uint32_t)(r & LOW_MASK);
  result.__field_1 = (uint32_t)(r >> 32);
  return result;
145
}
146 147 148 149 150 151 152 153 154
"

  interface
"
struct __add32_with_carry_result
{ uint32_t __field_0;
  uint32_t __field_1;
};

155
struct __add32_with_carry_result add32_with_carry(uint32_t x, uint32_t y, uint32_t c);
156 157 158 159 160 161

struct __sub32_with_borrow_result
{ uint32_t __field_0;
  uint32_t __field_1;
};

162
struct __sub32_with_borrow_result sub32_with_borrow(uint32_t x, uint32_t y, uint32_t b);
163 164 165 166 167 168

struct __mul32_double_result
{ uint32_t __field_0;
  uint32_t __field_1;
};

169
struct __mul32_double_result mul32_double(uint32_t x, uint32_t y);
170 171 172 173 174 175

struct __add32_3_result
{ uint32_t __field_0;
  uint32_t __field_1;
};

176
struct __add32_3_result add32_3(uint32_t x, uint32_t y, uint32_t z);
177 178 179 180 181 182

struct __lsld32_result
{ uint32_t __field_0;
  uint32_t __field_1;
};

183
struct __lsld32_result lsld32(uint32_t x, uint32_t cnt);
184 185
"

186
  syntax literal uint32 "0x%8xU"
187

188 189 190 191 192 193 194 195 196 197 198
  syntax val (+)     "%1 + %2"   prec 4 4 3
  syntax val (-)     "%1 - %2"   prec 4 4 3
  syntax val (-_)    "-%1"       prec 2 1
  syntax val ( * )   "%1 * %2"   prec 3 3 2
  syntax val (/)     "%1 / %2"   prec 3 3 2
  syntax val (%)     "%1 % %2"   prec 3 3 2
  syntax val (=)     "%1 == %2"  prec 7 7 6
  syntax val (<=)    "%1 <= %2"  prec 6 6 5
  syntax val (<)     "%1 < %2"   prec 6 6 5
  syntax val (>=)    "%1 >= %2"  prec 6 6 5
  syntax val (>)     "%1 > %2"   prec 6 6 5
199

Raphael Rieu-Helft's avatar
Raphael Rieu-Helft committed
200 201 202 203 204 205
  syntax val add_with_carry "add32_with_carry"
  syntax val sub_with_borrow "sub32_with_borrow"
  syntax val mul_double "mul32_double"
  syntax val add3 "add32_3"
  syntax val lsld "lsld32"

206 207 208
  syntax val add_mod "%1 + %2" prec 4 4 3
  syntax val sub_mod "%1 - %2" prec 4 4 3
  syntax val mul_mod "%1 * %2" prec 3 3 2
209

210
  syntax val div2by1
211
         "(uint32_t)((((uint64_t)%1) | (((uint64_t)%2) << 32))/(uint64_t)(%3))"
212
         prec 2
213

214 215
  syntax val lsl "%1 << %2" prec 5 5 4
  syntax val lsr "%1 >> %2" prec 5 5 4
216

217
  syntax val is_msb_set "%1 & 0x80000000U" prec 8 8
Raphaël Rieu-Helft's avatar
Raphaël Rieu-Helft committed
218

219
  syntax val count_leading_zeros "__builtin_clz(%1)" prec 1 15
220

221
  syntax val of_int32 "(uint32_t)%1" prec 2 2
222 223 224 225 226 227


end

module mach.int.Int64
  syntax type int64  "int64_t"
228
  syntax literal int64 "%dLL"
229

230 231 232 233 234 235 236 237 238 239 240
  syntax val (+)     "%1 + %2"   prec 4 4 3
  syntax val (-)     "%1 - %2"   prec 4 4 3
  syntax val (-_)    "-%1"       prec 2 1
  syntax val ( * )   "%1 * %2"   prec 3 3 2
  syntax val (/)     "%1 / %2"   prec 3 3 2
  syntax val (%)     "%1 % %2"   prec 3 3 2
  syntax val (=)     "%1 == %2"  prec 7 7 6
  syntax val (<=)    "%1 <= %2"  prec 6 6 5
  syntax val (<)     "%1 < %2"   prec 6 6 5
  syntax val (>=)    "%1 >= %2"  prec 6 6 5
  syntax val (>)     "%1 > %2"   prec 6 6 5
241

Raphael Rieu-Helft's avatar
Raphael Rieu-Helft committed
242 243 244 245 246 247
end

module mach.int.UInt64Gen

  syntax type uint64 "uint64_t"

248 249
  syntax val max_uint64 "0xffffffffffffffff" prec 0
  syntax val length "64" prec 0
Raphael Rieu-Helft's avatar
Raphael Rieu-Helft committed
250 251 252 253 254

end

module mach.int.UInt64

255
  syntax literal uint64 "0x%16xULL"
Raphael Rieu-Helft's avatar
Raphael Rieu-Helft committed
256

257 258 259 260 261 262 263 264 265 266 267
  syntax val (+)     "%1 + %2"   prec 4 4 3
  syntax val (-)     "%1 - %2"   prec 4 4 3
  syntax val (-_)    "-%1"       prec 2 1
  syntax val ( * )   "%1 * %2"   prec 3 3 2
  syntax val (/)     "%1 / %2"   prec 3 3 2
  syntax val (%)     "%1 % %2"   prec 3 3 2
  syntax val (=)     "%1 == %2"  prec 7 7 6
  syntax val (<=)    "%1 <= %2"  prec 6 6 5
  syntax val (<)     "%1 < %2"   prec 6 6 5
  syntax val (>=)    "%1 >= %2"  prec 6 6 5
  syntax val (>)     "%1 > %2"   prec 6 6 5
Raphael Rieu-Helft's avatar
Raphael Rieu-Helft committed
268

269 270
end

271
module mach.int.UInt64GMP
272 273 274 275

  prelude

"
Raphaël Rieu-Helft's avatar
Raphaël Rieu-Helft committed
276 277 278 279 280 281
#include \"gmp.h\"
#include \"gmp-impl.h\"
#include \"longlong.h\"

#undef invert_limb

282 283 284 285 286
struct __add64_with_carry_result
{ uint64_t __field_0;
  uint64_t __field_1;
};

287
struct __add64_with_carry_result
288
add64_with_carry(uint64_t x, uint64_t y, uint64_t c)
289
{
290
  struct __add64_with_carry_result result;
291
  uint64_t r = x + y + c;
292 293 294 295
  result.__field_0 = r;
  if (r == x) result.__field_1 = c;
  else result.__field_1 = (r < x);
  return result;
296 297
}

298 299 300 301 302
struct __add64_double_result
{ uint64_t __field_0;
  uint64_t __field_1;
};

303
struct __add64_double_result
304 305 306 307 308 309 310
add64_double(uint64_t a1, uint64_t a0, uint64_t b1, uint64_t b0)
{
  struct __add64_double_result result;
  add_ssaaaa(result.__field_0, result.__field_1, a1, a0, b1, b0);
  return result;
}

311 312 313 314 315
struct __sub64_with_borrow_result
{ uint64_t __field_0;
  uint64_t __field_1;
};

316
struct __sub64_with_borrow_result
317
sub64_with_borrow(uint64_t x, uint64_t y, uint64_t b)
318
{
319
  struct __sub64_with_borrow_result result;
320
  uint64_t r = x - y - b;
321 322 323 324 325
  result.__field_0 = r;
  if (r > x) result.__field_1 = 1;
  else if (r == x) result.__field_1 = b;
  else result.__field_1 = 0;
  return result;
326 327
}

328 329 330 331 332
struct __sub64_double_result
{ uint64_t __field_0;
  uint64_t __field_1;
};

333
struct __sub64_double_result
334 335 336 337 338 339 340
sub64_double(uint64_t a1, uint64_t a0, uint64_t b1, uint64_t b0)
{
  struct __sub64_double_result result;
  sub_ddmmss(result.__field_0, result.__field_1, a1, a0, b1, b0);
  return result;
}

341 342 343 344 345
struct __mul64_double_result
{ uint64_t __field_0;
  uint64_t __field_1;
};

346
struct __mul64_double_result mul64_double(uint64_t x, uint64_t y)
347
{
348 349 350
  struct __mul64_double_result result;
  umul_ppmm(result.__field_1,result.__field_0,x,y);
  return result;
351 352
}

353
uint64_t div64_2by1(uint64_t ul, uint64_t uh, uint64_t d)
354
{
355 356
  uint64_t q;
  uint64_t _dummy __attribute__((unused));
357
  udiv_qrnnd(q,_dummy,uh,ul,d);
358 359 360
  return q;
}

361 362 363 364 365
struct __add64_3_result
{ uint64_t __field_0;
  uint64_t __field_1;
};

366
struct __add64_3_result add64_3(uint64_t x, uint64_t y, uint64_t z)
367
{
368
  struct __add64_3_result result;
369
  uint64_t r, c1, c2;
370 371 372 373
  r = x + y;
  c1 = r < y;
  r += z;
  c2 = r < z;
374 375 376
  result.__field_1 = c1 + c2;
  result.__field_0 = r;
  return result;
377 378
}

379 380 381 382
struct __lsld64_result
{ uint64_t __field_0;
  uint64_t __field_1;
};
Raphaël Rieu-Helft's avatar
Raphaël Rieu-Helft committed
383

384
struct __lsld64_result lsld64(uint64_t x, uint64_t cnt)
Raphaël Rieu-Helft's avatar
Raphaël Rieu-Helft committed
385
{
386 387 388 389
  struct __lsld64_result result;
  result.__field_1 = x >> (64 - cnt);
  result.__field_0 = x << cnt;
  return result;
Raphaël Rieu-Helft's avatar
Raphaël Rieu-Helft committed
390
}
391
"
392

393 394 395 396 397 398 399 400 401 402 403 404
  interface
"
#include \"gmp.h\"
#include \"gmp-impl.h\"
#include \"longlong.h\"

#undef invert_limb
struct __add64_with_carry_result
{ uint64_t __field_0;
  uint64_t __field_1;
};

405 406 407 408 409 410 411 412 413 414
static struct __add64_with_carry_result
add64_with_carry(uint64_t x, uint64_t y, uint64_t c)
{
  struct __add64_with_carry_result result;
  uint64_t r = x + y + c;
  result.__field_0 = r;
  if (r == x) result.__field_1 = c;
  else result.__field_1 = (r < x);
  return result;
}
415 416 417 418 419 420

struct __add64_double_result
{ uint64_t __field_0;
  uint64_t __field_1;
};

421 422 423 424 425 426 427
static struct __add64_double_result
add64_double(uint64_t a1, uint64_t a0, uint64_t b1, uint64_t b0)
{
  struct __add64_double_result result;
  add_ssaaaa(result.__field_0, result.__field_1, a1, a0, b1, b0);
  return result;
}
428 429 430 431 432 433

struct __sub64_with_borrow_result
{ uint64_t __field_0;
  uint64_t __field_1;
};

434 435 436 437 438 439 440 441 442 443 444
static struct __sub64_with_borrow_result
sub64_with_borrow(uint64_t x, uint64_t y, uint64_t b)
{
  struct __sub64_with_borrow_result result;
  uint64_t r = x - y - b;
  result.__field_0 = r;
  if (r > x) result.__field_1 = 1;
  else if (r == x) result.__field_1 = b;
  else result.__field_1 = 0;
  return result;
}
445 446 447 448 449 450

struct __sub64_double_result
{ uint64_t __field_0;
  uint64_t __field_1;
};

451 452 453 454 455 456 457
static struct __sub64_double_result
sub64_double(uint64_t a1, uint64_t a0, uint64_t b1, uint64_t b0)
{
  struct __sub64_double_result result;
  sub_ddmmss(result.__field_0, result.__field_1, a1, a0, b1, b0);
  return result;
}
458 459 460 461 462 463

struct __mul64_double_result
{ uint64_t __field_0;
  uint64_t __field_1;
};

464 465 466 467 468 469
static struct __mul64_double_result mul64_double(uint64_t x, uint64_t y)
{
  struct __mul64_double_result result;
  umul_ppmm(result.__field_1,result.__field_0,x,y);
  return result;
}
470

471 472 473 474 475 476 477
static uint64_t div64_2by1(uint64_t ul, uint64_t uh, uint64_t d)
{
  uint64_t q;
  uint64_t _dummy __attribute__((unused));
  udiv_qrnnd(q,_dummy,uh,ul,d);
  return q;
}
478 479 480 481 482 483

struct __add64_3_result
{ uint64_t __field_0;
  uint64_t __field_1;
};

484 485 486 487 488 489 490 491 492 493 494 495
static struct __add64_3_result add64_3(uint64_t x, uint64_t y, uint64_t z)
{
  struct __add64_3_result result;
  uint64_t r, c1, c2;
  r = x + y;
  c1 = r < y;
  r += z;
  c2 = r < z;
  result.__field_1 = c1 + c2;
  result.__field_0 = r;
  return result;
}
496 497 498 499 500 501

struct __lsld64_result
{ uint64_t __field_0;
  uint64_t __field_1;
};

502 503 504 505 506 507 508
static struct __lsld64_result lsld64(uint64_t x, uint64_t cnt)
{
  struct __lsld64_result result;
  result.__field_1 = x >> (64 - cnt);
  result.__field_0 = x << cnt;
  return result;
}
509
"
510
  syntax literal uint64 "0x%16xULL"
511

512
  syntax val uint64_max "0xffffffffffffffffULL" prec 0
513

514 515 516 517 518 519 520 521 522 523 524
  syntax val (+)     "%1 + %2"   prec 4 4 3
  syntax val (-)     "%1 - %2"   prec 4 4 3
  syntax val (-_)    "-%1"       prec 2 1
  syntax val ( * )   "%1 * %2"   prec 3 3 2
  syntax val (/)     "%1 / %2"   prec 3 3 2
  syntax val (%)     "%1 % %2"   prec 3 3 2
  syntax val (=)     "%1 == %2"  prec 7 7 6
  syntax val (<=)    "%1 <= %2"  prec 6 6 5
  syntax val (<)     "%1 < %2"   prec 6 6 5
  syntax val (>=)    "%1 >= %2"  prec 6 6 5
  syntax val (>)     "%1 > %2"   prec 6 6 5
525

Raphael Rieu-Helft's avatar
Raphael Rieu-Helft committed
526
  syntax val add_with_carry "add64_with_carry"
527 528 529 530 531 532
  syntax val add_double "add64_double"
  syntax val add_double_nc "add64_double"
  syntax val add_double_gc "add64_double"
  syntax val sub_double "sub64_double"
  syntax val sub_double_nb "sub64_double"
  syntax val sub_double_gb "sub64_double"
Raphael Rieu-Helft's avatar
Raphael Rieu-Helft committed
533 534 535 536 537 538
  syntax val sub_with_borrow "sub64_with_borrow"
  syntax val mul_double "mul64_double"
  syntax val div2by1 "div64_2by1"
  syntax val add3 "add64_3"
  syntax val lsld "lsld64"

539 540 541
  syntax val add_mod "%1 + %2" prec 4 4 3
  syntax val sub_mod "%1 - %2" prec 4 4 3
  syntax val mul_mod "%1 * %2" prec 3 3 2
542

543 544
  syntax val lsl "%1 << %2" prec 5 5 4
  syntax val lsr "%1 >> %2" prec 5 5 4
545

546
  syntax val is_msb_set "%1 & 0x8000000000000000ULL" prec 8 7
547

548
  syntax val count_leading_zeros "__builtin_clzll(%1)" prec 1 15
549

550 551 552
  syntax val of_int32 "(uint64_t)%1" prec 2 2
  syntax val to_int64 "(int64_t)%1" prec 2 2
  syntax val of_int64 "(uint64_t)%1" prec 2 2
553 554 555

end

556
module mach.array.Array32
557

558 559 560 561
  syntax val ([]) "%1[%2]"
  syntax val ([]<-) "%1[%2] = %3"

end
562

563 564
module mach.c.C

565 566
  prelude "#define IGNORE2(x,y) do { (void)(x); (void)(y); } while (0)"
  interface "#define IGNORE2(x,y) do { (void)(x); (void)(y); } while (0)"
567

568
  syntax type ptr "%1 *"
569
  syntax type bool "int" (* ? *)
570

571 572 573
  syntax val malloc "malloc(%1 * sizeof(%v0))" prec 1 3
  syntax val free   "free(%1)" prec 1 15
  syntax val realloc "realloc(%1, %2 * sizeof(%v0))" prec 1 15 3
574

575 576
  syntax val salloc "alloca(%1 * sizeof(%v0))" prec 1 3
  syntax val sfree  "(void)%1" prec 2 2
577

Raphaël Rieu-Helft's avatar
Raphaël Rieu-Helft committed
578
  (* syntax val is_null "(%1) == NULL" *)
579 580
  syntax val is_not_null "%1" prec 0
  syntax val null "NULL" prec 0
581

582
  syntax val incr "%1 + %2" prec 4 4 3
583

584 585
  syntax val get "*%1"        prec 2 2
  syntax val get_ofs "%1[%2]" prec 1 1 15
586

587 588
  syntax val set "*%1 = %2"  prec 14 2 14
  syntax val set_ofs "%1[%2] = %3" prec 14 14 15 14
589

590 591
  syntax val incr_split "%1 + %2" prec 4 4 3
  syntax val decr_split "%1 - %2" prec 4 4 3
592 593
  syntax val join "IGNORE2"
  syntax val join_r "IGNORE2"
594

595
  syntax val c_assert "assert (%1)" prec 1 15
596

597 598
  syntax val print_space "printf(\" \")" prec 1
  syntax val print_newline "printf(\"\\n\")" prec 1
599
  syntax val print_uint32 "printf(\"%#010x\",%1)" prec 1 15
600

601
end