Changeset 6809 for trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs
- Timestamp:
- 09/20/11 17:45:14 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs
r6770 r6809 247 247 248 248 ILGenerator il = testFun.GetILGenerator(); 249 CompileInstructions(il, state );249 CompileInstructions(il, state, dataset); 250 250 il.Emit(System.Reflection.Emit.OpCodes.Conv_R8); 251 251 il.Emit(System.Reflection.Emit.OpCodes.Ret); … … 257 257 } 258 258 259 private void CompileInstructions(ILGenerator il, InterpreterState state ) {259 private void CompileInstructions(ILGenerator il, InterpreterState state, Dataset ds) { 260 260 Instruction currentInstr = state.NextInstruction(); 261 261 int nArgs = currentInstr.nArguments; … … 264 264 case OpCodes.Add: { 265 265 if (nArgs > 0) { 266 CompileInstructions(il, state );266 CompileInstructions(il, state, ds); 267 267 } 268 268 for (int i = 1; i < nArgs; i++) { 269 CompileInstructions(il, state );269 CompileInstructions(il, state, ds); 270 270 il.Emit(System.Reflection.Emit.OpCodes.Add); 271 271 } … … 274 274 case OpCodes.Sub: { 275 275 if (nArgs == 1) { 276 CompileInstructions(il, state );276 CompileInstructions(il, state, ds); 277 277 il.Emit(System.Reflection.Emit.OpCodes.Neg); 278 278 return; 279 279 } 280 280 if (nArgs > 0) { 281 CompileInstructions(il, state );281 CompileInstructions(il, state, ds); 282 282 } 283 283 for (int i = 1; i < nArgs; i++) { 284 CompileInstructions(il, state );284 CompileInstructions(il, state, ds); 285 285 il.Emit(System.Reflection.Emit.OpCodes.Sub); 286 286 } … … 289 289 case OpCodes.Mul: { 290 290 if (nArgs > 0) { 291 CompileInstructions(il, state );291 CompileInstructions(il, state, ds); 292 292 } 293 293 for (int i = 1; i < nArgs; i++) { 294 CompileInstructions(il, state );294 CompileInstructions(il, state, ds); 295 295 il.Emit(System.Reflection.Emit.OpCodes.Mul); 296 296 } … … 300 300 if (nArgs == 1) { 301 301 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); 302 CompileInstructions(il, state );302 CompileInstructions(il, state, ds); 303 303 il.Emit(System.Reflection.Emit.OpCodes.Div); 304 304 return; 305 305 } 306 306 if (nArgs > 0) { 307 CompileInstructions(il, state );307 CompileInstructions(il, state, ds); 308 308 } 309 309 for (int i = 1; i < nArgs; i++) { 310 CompileInstructions(il, state );310 CompileInstructions(il, state, ds); 311 311 il.Emit(System.Reflection.Emit.OpCodes.Div); 312 312 } … … 314 314 } 315 315 case OpCodes.Average: { 316 CompileInstructions(il, state );316 CompileInstructions(il, state, ds); 317 317 for (int i = 1; i < nArgs; i++) { 318 CompileInstructions(il, state );318 CompileInstructions(il, state, ds); 319 319 il.Emit(System.Reflection.Emit.OpCodes.Add); 320 320 } … … 324 324 } 325 325 case OpCodes.Cos: { 326 CompileInstructions(il, state );326 CompileInstructions(il, state, ds); 327 327 il.Emit(System.Reflection.Emit.OpCodes.Call, cos); 328 328 return; 329 329 } 330 330 case OpCodes.Sin: { 331 CompileInstructions(il, state );331 CompileInstructions(il, state, ds); 332 332 il.Emit(System.Reflection.Emit.OpCodes.Call, sin); 333 333 return; 334 334 } 335 335 case OpCodes.Tan: { 336 CompileInstructions(il, state );336 CompileInstructions(il, state, ds); 337 337 il.Emit(System.Reflection.Emit.OpCodes.Call, tan); 338 338 return; 339 339 } 340 340 case OpCodes.Power: { 341 CompileInstructions(il, state );342 CompileInstructions(il, state );341 CompileInstructions(il, state, ds); 342 CompileInstructions(il, state, ds); 343 343 il.Emit(System.Reflection.Emit.OpCodes.Call, round); 344 344 il.Emit(System.Reflection.Emit.OpCodes.Call, power); … … 346 346 } 347 347 case OpCodes.Root: { 348 CompileInstructions(il, state );348 CompileInstructions(il, state, ds); 349 349 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); // 1 / round(...) 350 CompileInstructions(il, state );350 CompileInstructions(il, state, ds); 351 351 il.Emit(System.Reflection.Emit.OpCodes.Call, round); 352 352 il.Emit(System.Reflection.Emit.OpCodes.Div); … … 355 355 } 356 356 case OpCodes.Exp: { 357 CompileInstructions(il, state );357 CompileInstructions(il, state, ds); 358 358 il.Emit(System.Reflection.Emit.OpCodes.Call, exp); 359 359 return; 360 360 } 361 361 case OpCodes.Log: { 362 CompileInstructions(il, state );362 CompileInstructions(il, state, ds); 363 363 il.Emit(System.Reflection.Emit.OpCodes.Call, log); 364 364 return; … … 367 367 Label end = il.DefineLabel(); 368 368 Label c1 = il.DefineLabel(); 369 CompileInstructions(il, state );369 CompileInstructions(il, state, ds); 370 370 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0); // > 0 371 371 il.Emit(System.Reflection.Emit.OpCodes.Cgt); 372 372 il.Emit(System.Reflection.Emit.OpCodes.Brfalse, c1); 373 CompileInstructions(il, state );373 CompileInstructions(il, state, ds); 374 374 il.Emit(System.Reflection.Emit.OpCodes.Br, end); 375 375 il.MarkLabel(c1); 376 CompileInstructions(il, state );376 CompileInstructions(il, state, ds); 377 377 il.MarkLabel(end); 378 378 return; … … 381 381 Label falseBranch = il.DefineLabel(); 382 382 Label end = il.DefineLabel(); 383 CompileInstructions(il, state );383 CompileInstructions(il, state, ds); 384 384 for (int i = 1; i < nArgs; i++) { 385 385 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0); // > 0 386 386 il.Emit(System.Reflection.Emit.OpCodes.Cgt); 387 387 il.Emit(System.Reflection.Emit.OpCodes.Brfalse, falseBranch); 388 CompileInstructions(il, state );388 CompileInstructions(il, state, ds); 389 389 } 390 390 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0); // > 0 … … 403 403 Label end = il.DefineLabel(); 404 404 Label resultBranch = il.DefineLabel(); 405 CompileInstructions(il, state );405 CompileInstructions(il, state, ds); 406 406 for (int i = 1; i < nArgs; i++) { 407 407 Label nextArgBranch = il.DefineLabel(); … … 413 413 il.MarkLabel(nextArgBranch); 414 414 il.Emit(System.Reflection.Emit.OpCodes.Pop); 415 CompileInstructions(il, state );415 CompileInstructions(il, state, ds); 416 416 } 417 417 il.MarkLabel(resultBranch); … … 428 428 } 429 429 case OpCodes.NOT: { 430 CompileInstructions(il, state );430 CompileInstructions(il, state, ds); 431 431 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0); // > 0 432 432 il.Emit(System.Reflection.Emit.OpCodes.Cgt); … … 439 439 } 440 440 case OpCodes.GT: { 441 CompileInstructions(il, state); 442 CompileInstructions(il, state); 441 CompileInstructions(il, state, ds); 442 CompileInstructions(il, state, ds); 443 443 444 il.Emit(System.Reflection.Emit.OpCodes.Cgt); // 1 (>) / 0 (otherwise) 444 445 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 2.0); // * 2 … … 449 450 } 450 451 case OpCodes.LT: { 451 CompileInstructions(il, state );452 CompileInstructions(il, state );452 CompileInstructions(il, state, ds); 453 CompileInstructions(il, state, ds); 453 454 il.Emit(System.Reflection.Emit.OpCodes.Clt); 454 455 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 2.0); // * 2 … … 459 460 } 460 461 case OpCodes.TimeLag: { 461 throw new NotImplementedException(); 462 LaggedTreeNode laggedTreeNode = (LaggedTreeNode)currentInstr.dynamicNode; 463 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row -= lag 464 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, laggedTreeNode.Lag); 465 il.Emit(System.Reflection.Emit.OpCodes.Add); 466 il.Emit(System.Reflection.Emit.OpCodes.Starg, 0); 467 CompileInstructions(il, state, ds); 468 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row += lag 469 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, laggedTreeNode.Lag); 470 il.Emit(System.Reflection.Emit.OpCodes.Sub); 471 il.Emit(System.Reflection.Emit.OpCodes.Starg, 0); 472 return; 462 473 } 463 474 case OpCodes.Integral: { 464 throw new NotImplementedException(); 475 int savedPc = state.ProgramCounter; 476 LaggedTreeNode laggedTreeNode = (LaggedTreeNode)currentInstr.dynamicNode; 477 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row -= lag 478 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, laggedTreeNode.Lag); 479 il.Emit(System.Reflection.Emit.OpCodes.Add); 480 il.Emit(System.Reflection.Emit.OpCodes.Starg, 0); 481 CompileInstructions(il, state, ds); 482 for (int l = laggedTreeNode.Lag; l < 0; l++) { 483 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row += lag 484 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_1); 485 il.Emit(System.Reflection.Emit.OpCodes.Add); 486 il.Emit(System.Reflection.Emit.OpCodes.Starg, 0); 487 state.ProgramCounter = savedPc; 488 CompileInstructions(il, state, ds); 489 il.Emit(System.Reflection.Emit.OpCodes.Add); 490 } 491 return; 465 492 } 466 493 … … 470 497 // y' = 1/8h (f_i + 2f_i-1, -2 f_i-3 - f_i-4) 471 498 case OpCodes.Derivative: { 472 throw new NotImplementedException(); 499 int savedPc = state.ProgramCounter; 500 CompileInstructions(il, state, ds); 501 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row -- 502 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_M1); 503 il.Emit(System.Reflection.Emit.OpCodes.Add); 504 il.Emit(System.Reflection.Emit.OpCodes.Starg, 0); 505 state.ProgramCounter = savedPc; 506 CompileInstructions(il, state, ds); 507 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 2.0); // f_0 + 2 * f_1 508 il.Emit(System.Reflection.Emit.OpCodes.Mul); 509 il.Emit(System.Reflection.Emit.OpCodes.Add); 510 511 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row -=2 512 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_2); 513 il.Emit(System.Reflection.Emit.OpCodes.Sub); 514 il.Emit(System.Reflection.Emit.OpCodes.Starg, 0); 515 state.ProgramCounter = savedPc; 516 CompileInstructions(il, state, ds); 517 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 2.0); // f_0 + 2 * f_1 - 2 * f_3 518 il.Emit(System.Reflection.Emit.OpCodes.Mul); 519 il.Emit(System.Reflection.Emit.OpCodes.Sub); 520 521 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row -- 522 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_M1); 523 il.Emit(System.Reflection.Emit.OpCodes.Add); 524 il.Emit(System.Reflection.Emit.OpCodes.Starg, 0); 525 state.ProgramCounter = savedPc; 526 CompileInstructions(il, state, ds); 527 il.Emit(System.Reflection.Emit.OpCodes.Sub); // f_0 + 2 * f_1 - 2 * f_3 - f_4 528 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 8.0); // / 8 529 il.Emit(System.Reflection.Emit.OpCodes.Div); 530 531 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row +=4 532 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_4); 533 il.Emit(System.Reflection.Emit.OpCodes.Add); 534 il.Emit(System.Reflection.Emit.OpCodes.Starg, 0); 535 return; 473 536 } 474 537 case OpCodes.Call: { … … 479 542 } 480 543 case OpCodes.Variable: { 544 var nanResult = il.DefineLabel(); 545 var normalResult = il.DefineLabel(); 481 546 VariableTreeNode varNode = (VariableTreeNode)currentInstr.dynamicNode; 482 547 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); // load columns array … … 484 549 il.Emit(System.Reflection.Emit.OpCodes.Ldelem_Ref); 485 550 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // sampleIndex 551 il.Emit(System.Reflection.Emit.OpCodes.Dup); 552 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0); 553 il.Emit(System.Reflection.Emit.OpCodes.Blt, nanResult); 554 il.Emit(System.Reflection.Emit.OpCodes.Dup); 555 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, ds.Rows); 556 il.Emit(System.Reflection.Emit.OpCodes.Bge, nanResult); 486 557 il.Emit(System.Reflection.Emit.OpCodes.Call, listGetValue); 487 558 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, varNode.Weight); // load weight 488 559 il.Emit(System.Reflection.Emit.OpCodes.Mul); 560 il.Emit(System.Reflection.Emit.OpCodes.Br, normalResult); 561 il.MarkLabel(nanResult); 562 il.Emit(System.Reflection.Emit.OpCodes.Pop); // sample index 563 il.Emit(System.Reflection.Emit.OpCodes.Pop); // column reference 564 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, double.NaN); 565 il.MarkLabel(normalResult); 489 566 return; 490 567 } 491 568 case OpCodes.LagVariable: { 569 var nanResult = il.DefineLabel(); 570 var normalResult = il.DefineLabel(); 492 571 LaggedVariableTreeNode varNode = (LaggedVariableTreeNode)currentInstr.dynamicNode; 493 572 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); // load columns array … … 497 576 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // sampleIndex 498 577 il.Emit(System.Reflection.Emit.OpCodes.Add); // row = sampleIndex + sampleOffset 578 il.Emit(System.Reflection.Emit.OpCodes.Dup); 579 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0); 580 il.Emit(System.Reflection.Emit.OpCodes.Blt, nanResult); 581 il.Emit(System.Reflection.Emit.OpCodes.Dup); 582 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, ds.Rows); 583 il.Emit(System.Reflection.Emit.OpCodes.Bge, nanResult); 499 584 il.Emit(System.Reflection.Emit.OpCodes.Call, listGetValue); 500 585 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, varNode.Weight); // load weight 501 586 il.Emit(System.Reflection.Emit.OpCodes.Mul); 587 il.Emit(System.Reflection.Emit.OpCodes.Br, normalResult); 588 il.MarkLabel(nanResult); 589 il.Emit(System.Reflection.Emit.OpCodes.Pop); // sample index 590 il.Emit(System.Reflection.Emit.OpCodes.Pop); // column reference 591 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, double.NaN); 592 il.MarkLabel(normalResult); 502 593 return; 503 594 }
Note: See TracChangeset
for help on using the changeset viewer.