polycomp 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.toml +1 -0
- package/bin/native/polycomp.exe +0 -0
- package/package.json +1 -1
- package/src/ast.rs +84 -1
- package/src/bundle.rs +173 -46
- package/src/interpreter.rs +466 -115
- package/src/ir.rs +37 -4
- package/src/jit.rs +94 -42
- package/src/lowering.rs +377 -46
- package/src/main.rs +30 -9
- package/src/optimizer.rs +84 -26
- package/src/parser.rs +2637 -487
- package/src/resolver.rs +185 -76
- package/src/runtime.rs +280 -5
package/src/main.rs
CHANGED
|
@@ -32,11 +32,16 @@ impl From<CliMemMode> for MemoryMode {
|
|
|
32
32
|
#[derive(Debug, Clone, Copy, ValueEnum)]
|
|
33
33
|
enum CliLang {
|
|
34
34
|
C,
|
|
35
|
+
Cpp,
|
|
36
|
+
Cc,
|
|
37
|
+
Cs,
|
|
38
|
+
Go,
|
|
35
39
|
Py,
|
|
36
40
|
Python,
|
|
37
41
|
Js,
|
|
38
42
|
Javascript,
|
|
39
43
|
Ts,
|
|
44
|
+
Typescript,
|
|
40
45
|
Rs,
|
|
41
46
|
Rust,
|
|
42
47
|
}
|
|
@@ -45,8 +50,12 @@ impl From<CliLang> for Language {
|
|
|
45
50
|
fn from(value: CliLang) -> Self {
|
|
46
51
|
match value {
|
|
47
52
|
CliLang::C => Language::C,
|
|
53
|
+
CliLang::Cpp | CliLang::Cc => Language::Cpp,
|
|
54
|
+
CliLang::Cs => Language::CSharp,
|
|
55
|
+
CliLang::Go => Language::Go,
|
|
48
56
|
CliLang::Py | CliLang::Python => Language::Python,
|
|
49
|
-
CliLang::Js | CliLang::Javascript
|
|
57
|
+
CliLang::Js | CliLang::Javascript => Language::JavaScript,
|
|
58
|
+
CliLang::Ts | CliLang::Typescript => Language::TypeScript,
|
|
50
59
|
CliLang::Rs | CliLang::Rust => Language::Rust,
|
|
51
60
|
}
|
|
52
61
|
}
|
|
@@ -56,7 +65,7 @@ impl From<CliLang> for Language {
|
|
|
56
65
|
#[command(
|
|
57
66
|
name = "polycomp",
|
|
58
67
|
version,
|
|
59
|
-
about = "PolyComp: Universal
|
|
68
|
+
about = "PolyComp: Universal Multi-Paradigm Compiler & Runtime (C, C++, C#, Go, Rust, Python, JS, TS) with Cranelift JIT & Armored IR"
|
|
60
69
|
)]
|
|
61
70
|
struct Cli {
|
|
62
71
|
#[command(subcommand)]
|
|
@@ -65,7 +74,7 @@ struct Cli {
|
|
|
65
74
|
|
|
66
75
|
#[derive(Debug, Subcommand)]
|
|
67
76
|
enum Commands {
|
|
68
|
-
/// Execute
|
|
77
|
+
/// Execute source code, stdin ('-'), or a .polyir / .polyx container
|
|
69
78
|
Run {
|
|
70
79
|
file: PathBuf,
|
|
71
80
|
#[arg(short, long, default_value = "main")]
|
|
@@ -93,7 +102,7 @@ enum Commands {
|
|
|
93
102
|
#[arg(long)]
|
|
94
103
|
allow_unsigned: bool,
|
|
95
104
|
},
|
|
96
|
-
/// Compile source code
|
|
105
|
+
/// Compile source code into a UTF-8 .polyir or obfuscated .polyx container (use '-o -' for stdout)
|
|
97
106
|
Compile {
|
|
98
107
|
file: PathBuf,
|
|
99
108
|
#[arg(short, long)]
|
|
@@ -183,9 +192,17 @@ fn main() -> ExitCode {
|
|
|
183
192
|
|
|
184
193
|
fn detect_language(src: &str) -> Language {
|
|
185
194
|
let trimmed = src.trim();
|
|
186
|
-
if trimmed.contains("
|
|
195
|
+
if trimmed.contains("package ") || (trimmed.contains("func ") && trimmed.contains('{')) {
|
|
196
|
+
Language::Go
|
|
197
|
+
} else if trimmed.contains("using System") || (trimmed.contains("namespace ") && !trimmed.contains("std::")) || trimmed.contains("Console.Write") {
|
|
198
|
+
Language::CSharp
|
|
199
|
+
} else if trimmed.contains("#include <") || trimmed.contains("std::") || trimmed.contains("cout <<") || trimmed.contains("class ") {
|
|
200
|
+
Language::Cpp
|
|
201
|
+
} else if trimmed.contains("def ") && trimmed.contains(':') {
|
|
187
202
|
Language::Python
|
|
188
|
-
} else if trimmed.contains("
|
|
203
|
+
} else if trimmed.contains(": number") || trimmed.contains(": string") || trimmed.contains(": boolean") || trimmed.contains("interface ") || trimmed.contains("type ") && trimmed.contains('=') {
|
|
204
|
+
Language::TypeScript
|
|
205
|
+
} else if trimmed.contains("function ") || trimmed.contains("console.log") || trimmed.contains("const ") || trimmed.contains("let ") {
|
|
189
206
|
Language::JavaScript
|
|
190
207
|
} else if trimmed.contains("fn ") {
|
|
191
208
|
Language::Rust
|
|
@@ -197,8 +214,12 @@ fn detect_language(src: &str) -> Language {
|
|
|
197
214
|
fn virtual_path_for(lang: Language) -> PathBuf {
|
|
198
215
|
let ext = match lang {
|
|
199
216
|
Language::C => "c",
|
|
217
|
+
Language::Cpp => "cpp",
|
|
218
|
+
Language::CSharp => "cs",
|
|
219
|
+
Language::Go => "go",
|
|
200
220
|
Language::Python => "py",
|
|
201
221
|
Language::JavaScript => "js",
|
|
222
|
+
Language::TypeScript => "ts",
|
|
202
223
|
Language::Rust => "rs",
|
|
203
224
|
};
|
|
204
225
|
PathBuf::from(format!("__inline__.{ext}"))
|
|
@@ -410,7 +431,7 @@ fn run_cli(cli: Cli) -> Result<(), String> {
|
|
|
410
431
|
report.obfuscated,
|
|
411
432
|
report.keyless,
|
|
412
433
|
report.payload_bytes,
|
|
413
|
-
report.signer_pubkey_hex
|
|
434
|
+
report.signer_pubkey_hex
|
|
414
435
|
);
|
|
415
436
|
Ok(())
|
|
416
437
|
}
|
|
@@ -439,7 +460,7 @@ fn run_cli(cli: Cli) -> Result<(), String> {
|
|
|
439
460
|
let (priv_p, pub_p, enc_p) =
|
|
440
461
|
generate_keypair_files(&out_dir).map_err(|e| format!("{e}"))?;
|
|
441
462
|
println!(
|
|
442
|
-
"Generated PolyComp cryptographic keys:\n Signing private key
|
|
463
|
+
"Generated PolyComp cryptographic keys:\n Signing private key: {priv_p}\n Verifying public key: {pub_p}\n Master AEAD key: {enc_p}"
|
|
443
464
|
);
|
|
444
465
|
Ok(())
|
|
445
466
|
}
|
|
@@ -451,7 +472,7 @@ fn run_cli(cli: Cli) -> Result<(), String> {
|
|
|
451
472
|
let report =
|
|
452
473
|
check_project(&file, &entry, trace_imports).map_err(|e| format!("{e}"))?;
|
|
453
474
|
println!(
|
|
454
|
-
"OK: verified {} reachable functions across {} parsed modules
|
|
475
|
+
"OK: verified {} reachable functions across {} parsed modules.",
|
|
455
476
|
report.reachable_functions, report.modules_parsed
|
|
456
477
|
);
|
|
457
478
|
Ok(())
|
package/src/optimizer.rs
CHANGED
|
@@ -15,8 +15,6 @@ pub fn optimize_function(func: &mut IrFunction) {
|
|
|
15
15
|
infer_types(func);
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
/// Fuses `CallLazy(tmp, site, args); Cast(dst, target_ty, tmp)` when `tmp` has a single use,
|
|
19
|
-
/// preserving exact typed return expectations for JIT compilation.
|
|
20
18
|
fn fuse_call_lazy_casts(func: &mut IrFunction) {
|
|
21
19
|
let mut use_counts: FxHashMap<Register, usize> = FxHashMap::default();
|
|
22
20
|
for bb in &func.blocks {
|
|
@@ -184,7 +182,9 @@ fn eval_const_unary(op: IrUnaryOp, c: &Constant) -> Option<Constant> {
|
|
|
184
182
|
(IrUnaryOp::Neg, Constant::Float(f)) => Some(Constant::Float(-f)),
|
|
185
183
|
(IrUnaryOp::Not, c) => const_truthy(c).map(|b| Constant::Bool(!b)),
|
|
186
184
|
(IrUnaryOp::BitNot, Constant::Int(i)) => Some(Constant::Int(!i)),
|
|
187
|
-
(IrUnaryOp::BitsToF64, Constant::Int(i)) =>
|
|
185
|
+
(IrUnaryOp::BitsToF64, Constant::Int(i)) => {
|
|
186
|
+
Some(Constant::Float(f64::from_bits(*i as u64)))
|
|
187
|
+
}
|
|
188
188
|
_ => None,
|
|
189
189
|
}
|
|
190
190
|
}
|
|
@@ -206,8 +206,9 @@ fn const_truthy(c: &Constant) -> Option<bool> {
|
|
|
206
206
|
Constant::Nil => Some(false),
|
|
207
207
|
Constant::Bool(b) => Some(*b),
|
|
208
208
|
Constant::Int(i) => Some(*i != 0),
|
|
209
|
-
Constant::Float(f) => Some(*f != 0.0),
|
|
209
|
+
Constant::Float(f) => Some(*f != 0.0 && !f.is_nan()),
|
|
210
210
|
Constant::String(s) => Some(!s.is_empty()),
|
|
211
|
+
Constant::Regex { .. } => Some(true),
|
|
211
212
|
}
|
|
212
213
|
}
|
|
213
214
|
|
|
@@ -220,6 +221,13 @@ fn eval_const_binop(op: IrBinOp, a: &Constant, b: &Constant) -> Option<Constant>
|
|
|
220
221
|
IrBinOp::Div if *y != 0 => Some(Constant::Int(x.wrapping_div(*y))),
|
|
221
222
|
IrBinOp::FloorDiv if *y != 0 => Some(Constant::Int(x.div_euclid(*y))),
|
|
222
223
|
IrBinOp::Mod if *y != 0 => Some(Constant::Int(x.wrapping_rem(*y))),
|
|
224
|
+
IrBinOp::Pow => {
|
|
225
|
+
if *y >= 0 && *y <= u32::MAX as i64 {
|
|
226
|
+
Some(Constant::Int(x.wrapping_pow(*y as u32)))
|
|
227
|
+
} else {
|
|
228
|
+
Some(Constant::Float((*x as f64).powf(*y as f64)))
|
|
229
|
+
}
|
|
230
|
+
}
|
|
223
231
|
IrBinOp::Eq => Some(Constant::Bool(x == y)),
|
|
224
232
|
IrBinOp::Ne => Some(Constant::Bool(x != y)),
|
|
225
233
|
IrBinOp::Lt => Some(Constant::Bool(x < y)),
|
|
@@ -258,6 +266,7 @@ fn eval_float_binop(op: IrBinOp, x: f64, y: f64) -> Option<Constant> {
|
|
|
258
266
|
IrBinOp::Div if y != 0.0 => Some(Constant::Float(x / y)),
|
|
259
267
|
IrBinOp::FloorDiv if y != 0.0 => Some(Constant::Float((x / y).floor())),
|
|
260
268
|
IrBinOp::Mod if y != 0.0 => Some(Constant::Float(x % y)),
|
|
269
|
+
IrBinOp::Pow => Some(Constant::Float(x.powf(y))),
|
|
261
270
|
IrBinOp::Eq => Some(Constant::Bool(x == y)),
|
|
262
271
|
IrBinOp::Ne => Some(Constant::Bool(x != y)),
|
|
263
272
|
IrBinOp::Lt => Some(Constant::Bool(x < y)),
|
|
@@ -286,6 +295,9 @@ fn prune_unreachable_blocks(func: &mut IrFunction) -> bool {
|
|
|
286
295
|
stack.push(a.0);
|
|
287
296
|
stack.push(b.0);
|
|
288
297
|
}
|
|
298
|
+
IrInstruction::TryBegin(catch_bb, _) => {
|
|
299
|
+
stack.push(catch_bb.0);
|
|
300
|
+
}
|
|
289
301
|
_ => {}
|
|
290
302
|
}
|
|
291
303
|
}
|
|
@@ -318,6 +330,9 @@ fn prune_unreachable_blocks(func: &mut IrFunction) -> bool {
|
|
|
318
330
|
*a = old_to_new[a];
|
|
319
331
|
*b = old_to_new[b];
|
|
320
332
|
}
|
|
333
|
+
IrInstruction::TryBegin(catch_bb, _) => {
|
|
334
|
+
*catch_bb = old_to_new[catch_bb];
|
|
335
|
+
}
|
|
321
336
|
_ => {}
|
|
322
337
|
}
|
|
323
338
|
}
|
|
@@ -347,10 +362,13 @@ fn eliminate_dead_instructions(func: &mut IrFunction) -> bool {
|
|
|
347
362
|
| IrInstruction::UnaryOp(_, d, _)
|
|
348
363
|
| IrInstruction::CreateArray(d, _)
|
|
349
364
|
| IrInstruction::CreateObject(d, _)
|
|
350
|
-
| IrInstruction::ResolveFn(d, _)
|
|
365
|
+
| IrInstruction::ResolveFn(d, _)
|
|
366
|
+
| IrInstruction::LoadFnRef(d, _) => used.contains(d),
|
|
351
367
|
IrInstruction::BinOp(op, d, _, _) => {
|
|
352
|
-
|
|
353
|
-
|
|
368
|
+
matches!(
|
|
369
|
+
op,
|
|
370
|
+
IrBinOp::Div | IrBinOp::FloorDiv | IrBinOp::Mod | IrBinOp::Pow
|
|
371
|
+
) || used.contains(d)
|
|
354
372
|
}
|
|
355
373
|
_ => true,
|
|
356
374
|
});
|
|
@@ -381,6 +399,7 @@ fn infer_types(func: &mut IrFunction) {
|
|
|
381
399
|
Constant::Int(_) => IrType::I64,
|
|
382
400
|
Constant::Float(_) => IrType::F64,
|
|
383
401
|
Constant::String(_) => IrType::Str,
|
|
402
|
+
Constant::Regex { .. } => IrType::Dynamic,
|
|
384
403
|
};
|
|
385
404
|
changed |= update_reg_type(&mut types, *dst, cty);
|
|
386
405
|
}
|
|
@@ -425,27 +444,51 @@ fn infer_types(func: &mut IrFunction) {
|
|
|
425
444
|
(IrType::Str, IrType::Str) => IrType::Str,
|
|
426
445
|
_ => IrType::Dynamic,
|
|
427
446
|
},
|
|
428
|
-
IrBinOp::Sub
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
447
|
+
IrBinOp::Sub
|
|
448
|
+
| IrBinOp::Mul
|
|
449
|
+
| IrBinOp::Div
|
|
450
|
+
| IrBinOp::FloorDiv
|
|
451
|
+
| IrBinOp::Mod
|
|
452
|
+
| IrBinOp::Pow => match (lt, rt) {
|
|
453
|
+
(IrType::I64, IrType::I64) => IrType::I64,
|
|
454
|
+
(IrType::F64, IrType::F64)
|
|
455
|
+
| (IrType::I64, IrType::F64)
|
|
456
|
+
| (IrType::F64, IrType::I64) => IrType::F64,
|
|
457
|
+
_ => IrType::Dynamic,
|
|
458
|
+
},
|
|
437
459
|
};
|
|
438
460
|
changed |= update_reg_type(&mut types, *dst, res_ty);
|
|
439
461
|
}
|
|
440
462
|
IrInstruction::CallBuiltin(dst, b, _) => {
|
|
441
463
|
let bty = match b {
|
|
442
|
-
BuiltinId::Len
|
|
443
|
-
|
|
444
|
-
BuiltinId::
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
464
|
+
BuiltinId::Len
|
|
465
|
+
| BuiltinId::Int
|
|
466
|
+
| BuiltinId::BufLen
|
|
467
|
+
| BuiltinId::ClockMs
|
|
468
|
+
| BuiltinId::Printf => IrType::I64,
|
|
469
|
+
BuiltinId::Float
|
|
470
|
+
| BuiltinId::Sqrt
|
|
471
|
+
| BuiltinId::Pow
|
|
472
|
+
| BuiltinId::Floor
|
|
473
|
+
| BuiltinId::Ceil
|
|
474
|
+
| BuiltinId::Round
|
|
475
|
+
| BuiltinId::Sin
|
|
476
|
+
| BuiltinId::Cos
|
|
477
|
+
| BuiltinId::Log => IrType::F64,
|
|
478
|
+
BuiltinId::Bool
|
|
479
|
+
| BuiltinId::RegexTest
|
|
480
|
+
| BuiltinId::StrContains => IrType::Bool,
|
|
481
|
+
BuiltinId::Str
|
|
482
|
+
| BuiltinId::Sprintf
|
|
483
|
+
| BuiltinId::TypeOf
|
|
484
|
+
| BuiltinId::StrTrim
|
|
485
|
+
| BuiltinId::StrUpper
|
|
486
|
+
| BuiltinId::StrLower
|
|
487
|
+
| BuiltinId::StrReplace
|
|
488
|
+
| BuiltinId::RegexReplace => IrType::Str,
|
|
489
|
+
BuiltinId::Print
|
|
490
|
+
| BuiltinId::Println
|
|
491
|
+
| BuiltinId::Assert => IrType::Void,
|
|
449
492
|
_ => IrType::Dynamic,
|
|
450
493
|
};
|
|
451
494
|
changed |= update_reg_type(&mut types, *dst, bty);
|
|
@@ -537,14 +580,17 @@ fn write_reg(instr: &IrInstruction) -> Option<Register> {
|
|
|
537
580
|
| IrInstruction::CallLazy(d, _, _)
|
|
538
581
|
| IrInstruction::CallFn(d, _, _)
|
|
539
582
|
| IrInstruction::CallIndirect(d, _, _)
|
|
583
|
+
| IrInstruction::CallMethod(d, _, _, _)
|
|
540
584
|
| IrInstruction::CallBuiltin(d, _, _)
|
|
541
585
|
| IrInstruction::ResolveFn(d, _)
|
|
586
|
+
| IrInstruction::LoadFnRef(d, _)
|
|
542
587
|
| IrInstruction::CreateArray(d, _)
|
|
543
588
|
| IrInstruction::GetIndex(d, _, _)
|
|
544
589
|
| IrInstruction::CreateObject(d, _)
|
|
545
590
|
| IrInstruction::GetField(d, _, _)
|
|
546
591
|
| IrInstruction::AllocBuffer(d, _)
|
|
547
|
-
| IrInstruction::LoadByte(d, _, _)
|
|
592
|
+
| IrInstruction::LoadByte(d, _, _)
|
|
593
|
+
| IrInstruction::TryBegin(_, d) => Some(*d),
|
|
548
594
|
_ => None,
|
|
549
595
|
}
|
|
550
596
|
}
|
|
@@ -554,7 +600,8 @@ fn read_regs(instr: &IrInstruction) -> Vec<Register> {
|
|
|
554
600
|
IrInstruction::Move(_, s)
|
|
555
601
|
| IrInstruction::Cast(_, _, s)
|
|
556
602
|
| IrInstruction::UnaryOp(_, _, s)
|
|
557
|
-
| IrInstruction::AllocBuffer(_, s)
|
|
603
|
+
| IrInstruction::AllocBuffer(_, s)
|
|
604
|
+
| IrInstruction::Throw(s) => vec![*s],
|
|
558
605
|
IrInstruction::BinOp(_, _, l, r)
|
|
559
606
|
| IrInstruction::GetIndex(_, l, r)
|
|
560
607
|
| IrInstruction::LoadByte(_, l, r) => vec![*l, *r],
|
|
@@ -567,6 +614,11 @@ fn read_regs(instr: &IrInstruction) -> Vec<Register> {
|
|
|
567
614
|
v.extend(args);
|
|
568
615
|
v
|
|
569
616
|
}
|
|
617
|
+
IrInstruction::CallMethod(_, obj, _, args) => {
|
|
618
|
+
let mut v = vec![*obj];
|
|
619
|
+
v.extend(args);
|
|
620
|
+
v
|
|
621
|
+
}
|
|
570
622
|
IrInstruction::Return(Some(r)) | IrInstruction::BranchIf(r, _, _) => vec![*r],
|
|
571
623
|
IrInstruction::SetField(o, _, v) => vec![*o, *v],
|
|
572
624
|
IrInstruction::GetField(_, o, _) => vec![*o],
|
|
@@ -580,7 +632,8 @@ fn mut_read_regs(instr: &mut IrInstruction) -> Vec<&mut Register> {
|
|
|
580
632
|
IrInstruction::Move(_, s)
|
|
581
633
|
| IrInstruction::Cast(_, _, s)
|
|
582
634
|
| IrInstruction::UnaryOp(_, _, s)
|
|
583
|
-
| IrInstruction::AllocBuffer(_, s)
|
|
635
|
+
| IrInstruction::AllocBuffer(_, s)
|
|
636
|
+
| IrInstruction::Throw(s) => vec![s],
|
|
584
637
|
IrInstruction::BinOp(_, _, l, r)
|
|
585
638
|
| IrInstruction::GetIndex(_, l, r)
|
|
586
639
|
| IrInstruction::LoadByte(_, l, r) => vec![l, r],
|
|
@@ -593,6 +646,11 @@ fn mut_read_regs(instr: &mut IrInstruction) -> Vec<&mut Register> {
|
|
|
593
646
|
v.extend(args.iter_mut());
|
|
594
647
|
v
|
|
595
648
|
}
|
|
649
|
+
IrInstruction::CallMethod(_, obj, _, args) => {
|
|
650
|
+
let mut v = vec![obj];
|
|
651
|
+
v.extend(args.iter_mut());
|
|
652
|
+
v
|
|
653
|
+
}
|
|
596
654
|
IrInstruction::Return(Some(r)) | IrInstruction::BranchIf(r, _, _) => vec![r],
|
|
597
655
|
IrInstruction::SetField(o, _, v) => vec![o, v],
|
|
598
656
|
IrInstruction::GetField(_, o, _) => vec![o],
|