@@ -373,6 +373,244 @@ impl DeadCodeEliminator {
373373 }
374374}
375375
376+ /// Inline optimizer
377+ pub struct InlineOptimizer ;
378+
379+ impl Mutator for InlineOptimizer {
380+ fn mutate_expression ( & mut self , expr : & mut Expression ) {
381+ // First mutate sub-expressions
382+ match expr {
383+ Expression :: MethodCall { receiver, method, arguments } => {
384+ self . mutate_expression ( receiver) ;
385+ for arg in arguments {
386+ self . mutate_expression ( arg) ;
387+ }
388+
389+ // TODO: Implement method inlining based on method size and call frequency
390+ }
391+ Expression :: BinaryOp { left, op, right } => {
392+ self . mutate_expression ( left) ;
393+ self . mutate_expression ( right) ;
394+ }
395+ Expression :: UnaryOp { op, operand } => {
396+ self . mutate_expression ( operand) ;
397+ }
398+ Expression :: ArrayLiteral ( elements) => {
399+ for elem in elements {
400+ self . mutate_expression ( elem) ;
401+ }
402+ }
403+ Expression :: HashLiteral ( pairs) => {
404+ for ( _, value) in pairs {
405+ self . mutate_expression ( value) ;
406+ }
407+ }
408+ Expression :: Block { parameters, body } => {
409+ for stmt in body {
410+ self . mutate_statement ( stmt) ;
411+ }
412+ }
413+ Expression :: SuperCall { arguments } => {
414+ for arg in arguments {
415+ self . mutate_expression ( arg) ;
416+ }
417+ }
418+ _ => { }
419+ }
420+ }
421+
422+ fn mutate_statement ( & mut self , stmt : & mut Statement ) {
423+ // First mutate sub-expressions
424+ match stmt {
425+ Statement :: Expression ( expr) => {
426+ self . mutate_expression ( expr) ;
427+ }
428+ Statement :: Assignment { name, value } => {
429+ self . mutate_expression ( value) ;
430+ }
431+ Statement :: GlobalAssignment { name, value } => {
432+ self . mutate_expression ( value) ;
433+ }
434+ Statement :: InstanceAssignment { name, value } => {
435+ self . mutate_expression ( value) ;
436+ }
437+ Statement :: ClassAssignment { name, value } => {
438+ self . mutate_expression ( value) ;
439+ }
440+ Statement :: If { condition, then_branch, else_branch } => {
441+ self . mutate_expression ( condition) ;
442+ for stmt in then_branch {
443+ self . mutate_statement ( stmt) ;
444+ }
445+ for stmt in else_branch {
446+ self . mutate_statement ( stmt) ;
447+ }
448+ }
449+ Statement :: While { condition, body } => {
450+ self . mutate_expression ( condition) ;
451+ for stmt in body {
452+ self . mutate_statement ( stmt) ;
453+ }
454+ }
455+ Statement :: For { variable, iterator, body } => {
456+ self . mutate_expression ( iterator) ;
457+ for stmt in body {
458+ self . mutate_statement ( stmt) ;
459+ }
460+ }
461+ Statement :: Return ( expr) => {
462+ if let Some ( expr) = expr {
463+ self . mutate_expression ( expr) ;
464+ }
465+ }
466+ Statement :: MethodDefinition { name, parameters, body } => {
467+ for stmt in body {
468+ self . mutate_statement ( stmt) ;
469+ }
470+ }
471+ Statement :: ClassDefinition { name, superclass, body } => {
472+ for stmt in body {
473+ self . mutate_statement ( stmt) ;
474+ }
475+ }
476+ Statement :: ModuleDefinition { name, body } => {
477+ for stmt in body {
478+ self . mutate_statement ( stmt) ;
479+ }
480+ }
481+ Statement :: Require ( expr) => {
482+ self . mutate_expression ( expr) ;
483+ }
484+ Statement :: Load ( expr) => {
485+ self . mutate_expression ( expr) ;
486+ }
487+ _ => { }
488+ }
489+ }
490+ }
491+
492+ /// Loop optimizer
493+ pub struct LoopOptimizer ;
494+
495+ impl Mutator for LoopOptimizer {
496+ fn mutate_statement ( & mut self , stmt : & mut Statement ) {
497+ // First mutate sub-expressions
498+ match stmt {
499+ Statement :: While { condition, body } => {
500+ self . mutate_expression ( condition) ;
501+
502+ // Try to optimize loops
503+ // TODO: Implement loop unrolling and loop invariant code motion
504+
505+ for stmt in body {
506+ self . mutate_statement ( stmt) ;
507+ }
508+ }
509+ Statement :: For { variable, iterator, body } => {
510+ self . mutate_expression ( iterator) ;
511+
512+ // Try to optimize for loops
513+ // TODO: Implement loop unrolling for for loops
514+
515+ for stmt in body {
516+ self . mutate_statement ( stmt) ;
517+ }
518+ }
519+ Statement :: Expression ( expr) => {
520+ self . mutate_expression ( expr) ;
521+ }
522+ Statement :: Assignment { name, value } => {
523+ self . mutate_expression ( value) ;
524+ }
525+ Statement :: GlobalAssignment { name, value } => {
526+ self . mutate_expression ( value) ;
527+ }
528+ Statement :: InstanceAssignment { name, value } => {
529+ self . mutate_expression ( value) ;
530+ }
531+ Statement :: ClassAssignment { name, value } => {
532+ self . mutate_expression ( value) ;
533+ }
534+ Statement :: If { condition, then_branch, else_branch } => {
535+ self . mutate_expression ( condition) ;
536+ for stmt in then_branch {
537+ self . mutate_statement ( stmt) ;
538+ }
539+ for stmt in else_branch {
540+ self . mutate_statement ( stmt) ;
541+ }
542+ }
543+ Statement :: Return ( expr) => {
544+ if let Some ( expr) = expr {
545+ self . mutate_expression ( expr) ;
546+ }
547+ }
548+ Statement :: MethodDefinition { name, parameters, body } => {
549+ for stmt in body {
550+ self . mutate_statement ( stmt) ;
551+ }
552+ }
553+ Statement :: ClassDefinition { name, superclass, body } => {
554+ for stmt in body {
555+ self . mutate_statement ( stmt) ;
556+ }
557+ }
558+ Statement :: ModuleDefinition { name, body } => {
559+ for stmt in body {
560+ self . mutate_statement ( stmt) ;
561+ }
562+ }
563+ Statement :: Require ( expr) => {
564+ self . mutate_expression ( expr) ;
565+ }
566+ Statement :: Load ( expr) => {
567+ self . mutate_expression ( expr) ;
568+ }
569+ _ => { }
570+ }
571+ }
572+
573+ fn mutate_expression ( & mut self , expr : & mut Expression ) {
574+ // First mutate sub-expressions
575+ match expr {
576+ Expression :: MethodCall { receiver, method, arguments } => {
577+ self . mutate_expression ( receiver) ;
578+ for arg in arguments {
579+ self . mutate_expression ( arg) ;
580+ }
581+ }
582+ Expression :: BinaryOp { left, op, right } => {
583+ self . mutate_expression ( left) ;
584+ self . mutate_expression ( right) ;
585+ }
586+ Expression :: UnaryOp { op, operand } => {
587+ self . mutate_expression ( operand) ;
588+ }
589+ Expression :: ArrayLiteral ( elements) => {
590+ for elem in elements {
591+ self . mutate_expression ( elem) ;
592+ }
593+ }
594+ Expression :: HashLiteral ( pairs) => {
595+ for ( _, value) in pairs {
596+ self . mutate_expression ( value) ;
597+ }
598+ }
599+ Expression :: Block { parameters, body } => {
600+ for stmt in body {
601+ self . mutate_statement ( stmt) ;
602+ }
603+ }
604+ Expression :: SuperCall { arguments } => {
605+ for arg in arguments {
606+ self . mutate_expression ( arg) ;
607+ }
608+ }
609+ _ => { }
610+ }
611+ }
612+ }
613+
376614/// Optimize a program
377615pub fn optimize_program ( program : & mut Program ) {
378616 // Apply constant folding
@@ -382,4 +620,12 @@ pub fn optimize_program(program: &mut Program) {
382620 // Apply dead code elimination
383621 let mut dead_code_eliminator = DeadCodeEliminator ;
384622 dead_code_eliminator. mutate_program ( program) ;
623+
624+ // Apply inline optimization
625+ let mut inline_optimizer = InlineOptimizer ;
626+ inline_optimizer. mutate_program ( program) ;
627+
628+ // Apply loop optimization
629+ let mut loop_optimizer = LoopOptimizer ;
630+ loop_optimizer. mutate_program ( program) ;
385631}
0 commit comments