View Javadoc

1   package com.tapina.robe.swi.clib.stdlib;
2   
3   import com.tapina.robe.runtime.CodeBlock;
4   import com.tapina.robe.runtime.DecoderException;
5   import com.tapina.robe.runtime.Environment;
6   import com.tapina.robe.swi.clib.Stub;
7   
8   import java.util.Iterator;
9   import java.util.LinkedList;
10  import java.util.List;
11  
12  /***
13   * This function causes the program to terminate normally, returning status to the host environment.
14   * Status should be zero (EXIT_SUCCESS), or EXIT_FAILURE.
15   */
16  public class Exit extends Stub {
17      private static class ExitHandlerList extends ThreadLocal {
18          protected Object initialValue() {
19              return new LinkedList();
20          }
21          public List getList() {
22              return (List) get();
23          }
24      }
25      private final static ExitHandlerList exitHandlerList = new ExitHandlerList();
26  
27      public void executeStub(Environment environment) {
28          final List exitHandlers = exitHandlerList.getList();
29          for (Iterator iterator = exitHandlers.iterator(); iterator.hasNext();) {
30              CodeBlock codeBlock = (CodeBlock) iterator.next();
31              try {
32                  environment.getCpu().setPC(codeBlock.getAddress() + 8);
33                  codeBlock.execute(environment);
34              } catch (DecoderException e) {
35                  throw new RuntimeException(e);
36              }
37          }
38          System.exit(environment.getCpu().R[0]);
39      }
40  
41      public static void registerExitHandler(CodeBlock atexit) {
42          exitHandlerList.getList().add(atexit);
43      }
44  }