History

Rewritten handlers to use lists, added exception type checking

Dmitry Kabak (userdima committed 13 years ago

Commit 62aaff51 parent 0ccfe035 files · +119 −222Browse files

Showing the changes to java (5 of 5 files). Show all

Unified · Split

5 files
  1. java/appl/j2d/asm.b +2 −2
  2. java/appl/j2d/dis.b +11 −14
  3. java/appl/j2d/frame.b +1 −0
  4. java/appl/j2d/java.m +2 −1
  5. java/appl/j2d/xlate.b +103 −205
java/appl/j2d/asm.b+2−2
@@ -220,8 +220,8 @@ asmhandlers() · 219 unmodified lines
220220 {
221221 nex := len class.handlers;
222222 bout.puts("\texceptions " + string nex + "\n");
223− for (i := 0; i < len class.handlers; i++) {
224− h := class.handlers[i];
223+ for (it := class.handlers; it != nil; it = tl it) {
224+ h := hd it;
225225 bout.puts(sprint("\texception\t%d, %d, 0, -1, 0, 0\n", h.start_pc, h.end_pc));
226226 bout.puts(sprint("\texctab\t*, %d\n", h.handler_pc));
227227 }
java/appl/j2d/dis.b+11−14
@@ -277,26 +277,23 @@ disstr(s: string) · 276 unmodified lines
277277 dishandlers()
278278 {
279279 discon(len class.handlers);
280− for (i := 0; i < len class.handlers; i++) {
281− h := class.handlers[i];
282− # FIXME: exception object offset
283− discon(42);
284− discon(h.start_pc);
285− discon(h.end_pc);
286− discon(-1); # type to destroy, nil
287− discon(1); # number of exception handlers
288− disstr("*");
289− discon(h.handler_pc);
290− discon(0);
280+ for (it := class.handlers; it != nil; it = tl it) {
281+ h := hd it;
282+ discon(EXSTR); # exception string offset in fp
283+ discon(h.start_pc); # from start_pc
284+ discon(h.end_pc); # up to, but not including end_pc
285+ discon(-1); # type to destroy, -1 means nothing
286+ discon(1); # number of exception handlers
287+ disstr("*"); # handle any ex, we will later rethrow if not handled
288+ discon(h.handler_pc); # pc to jump to when exception occurs
289+ discon(-1);
291290 }
292− # Don't know what is it, but zero-byte is
293− # expected after handler section
294− discon(0);
295291 }
296292
297293 disout()
298294 {
299295 discon(XMAGIC);
296+ flags := DONTCOMPILE;
300297 discon(DONTCOMPILE | HASEXCEPT); # dis flags
301298 disstackext(); # minimum stack extent size
302299 disninst();
java/appl/j2d/frame.b+1−0
@@ -425,6 +425,7 @@ framedesc(): int · 424 unmodified lines
425425 ln = frameoff / (8*IBY2WD) + (frameoff % (8*IBY2WD) != 0);
426426 map = array [ln] of { * => byte 0 };
427427 #setbit(map, REFSYSEX); # slot for 'ref Sys->Exception'
428+ setbit(map, EXSTR); # slot for exception string
428429 setbit(map, EXOBJ); # slot for exception object
429430 for(i = 0; i < nlocals; i++) {
430431 if(locals[i].dtype == DIS_P)
java/appl/j2d/java.m+2−1
@@ -81,6 +81,7 @@ STR_DISSTR: con IBY2WD; # offset to Dis string in a String · 80 unmodified lines
8181 # miscellaneous
8282
8383 #REFSYSEX: con 24; # 24(fp) is a 'ref Sys->Exception'
84+EXSTR: con 24; # 24(fp) is exception string
8485 EXOBJ: con 28; # 28(fp) is exception object
8586 THISOFF: con 32; # 32(fp) is "this" pointer
8687 REGSIZE: con 32; # 32(fp) is first usable frame cell
@@ -202,7 +203,7 @@ Class: adt { # ClassFile · 115 unmodified lines
202203 methods_count: int;
203204 methods: array of ref Method;
204205 source_file: int;
205− handlers: array of ref Handler;
206+ handlers: list of ref Handler;
206207 };
207208
208209 Addr: adt { # Dis operand
java/appl/j2d/xlate.b+103−205
@@ -1556,24 +1556,24 @@ iinc(val: int, ix: int) · 1555 unmodified lines
15561556 # J.u.i is destination PC (relative from J.pc)
15571557 #
15581558
1559−tldiff(): int
1560−{
1561− tls, tld: int;
1562− ix: int;
1563−
1564− pick jp := J {
1565− Pi =>
1566− ix = jp.i;
1567− * =>
1568− badpick("tldiff");
1569− }
1570−
1571− tld = 0;
1572− tls = trylevel(J.pc);
1573− if(tls > 0)
1574− tld = trylevel(J.pc+ix);
1575− return tls-tld;
1576−}
1559+#tldiff(): int
1560+#{
1561+# tls, tld: int;
1562+# ix: int;
1563+#
1564+# pick jp := J {
1565+# Pi =>
1566+# ix = jp.i;
1567+# * =>
1568+# badpick("tldiff");
1569+# }
1570+#
1571+# tld = 0;
1572+# tls = trylevel(J.pc);
1573+# if(tls > 0)
1574+# tld = trylevel(J.pc+ix);
1575+# return tls-tld;
1576+#}
15771577
15781578 #
15791579 # Java if_acmp<cond>, if_icmp<cond>, if<cond>, ifnonnull, ifnull.
@@ -2105,21 +2105,8 @@ saveehinst(i: ref Inst) · 525 unmodified lines
21052105
21062106 patchehinst()
21072107 {
2108− ehi: ref EHInst;
2109− i: ref Inst;
2110− j: ref Jinst;
2111− jix: int;
2112−
2113− for(ehi = ehinst; ehi != nil; ehi = ehi.next) {
2114− i = ehi.i;
2115− jix = i.d.ival;
2116− j = code.j[jix];
2117− while(j.dis == nil) {
2118− jix += j.size;
2119− j = code.j[jix];
2120− }
2121− i.d.ival = j.dis.pc;
2122− }
2108+ for(ehi := ehinst; ehi != nil; ehi = ehi.next)
2109+ ehi.i.d.ival = instpc(ehi.i.d.ival);
21232110 ehinst = nil;
21242111 }
21252112
@@ -2172,97 +2159,24 @@ trylevel(pc: int): int · 46 unmodified lines
21722159 return level;
21732160 }
21742161
2175−#
2176−# Install an exception handler: call sys.rescue("*", nil).
2177−#
2178−
2179−rescue(): ref Addr
2180−{
2181− print("rescue()\n");
2182− #imframe, i: ref Inst;
2183− #cr: ref Creloc;
2184− #frm, frf: ref Freloc;
2185−#
2186− #cr = getCreloc(RLOADER);
2187− #frm = getFreloc(cr, RSYS, nil, 0);
2188− #frf = getFreloc(cr, "rescue", nil, 0);
2189−#
2190− #imframe = loadermframe(frm, frf);
2191−#
2192− #i = newi(IMOVP);
2193− #addrsind(i.s, Amp, mpstring("*"));
2194− #addrdind(i.d, Afpind, imframe.d.offset, REGSIZE);
2195− #addDreloc(i, PSRC, PSIND);
2196−#
2197− #i = newi(IMOVP);
2198− #addrsind(i.s, Afp, REFSYSEX);
2199− #addrdind(i.d, Afpind, imframe.d.offset, REGSIZE+IBY2WD);
2200−#
2201− #i = newi(ILEA);
2202− #addrsind(i.s, Afp, getreg(DIS_W));
2203− #addrdind(i.d, Afpind, imframe.d.offset, REGRET*IBY2WD);
2204−#
2205− #loadermcall(imframe, frf, frm);
2206− #relreg(imframe.d);
2207−#
2208− #return i.s;
2209− return nil;
2210−}
2211−
2212−#
2213−# Uninstall one or more exception handlers: call sys.unrescue() n times.
2214−#
2215−
2216−unrescue(n: int)
2217−{
2218− print("unrescue()\n");
2219− #imframe: ref Inst;
2220− #cr: ref Creloc;
2221− #frm, frf: ref Freloc;
2222−#
2223− #cr = getCreloc(RLOADER);
2224− #frm = getFreloc(cr, RSYS, nil, 0);
2225− #frf = getFreloc(cr, "unrescue", nil, 0);
2226−#
2227− #while(n > 0) {
2228− # imframe = loadermframe(frm, frf);
2229− # loadermcall(imframe, frf, frm);
2230− # relreg(imframe.d);
2231− # n--;
2232− #}
2233−}
2234−
22352162 #
22362163 # Current handler doesn't catch current exception.
2237−# Call sys.raise(@jex) to propagate (rethrow) the exception.
2164+# Call raise to propagate (rethrow) the exception.
22382165 #
22392166
22402167 rethrow(): ref Inst
22412168 {
22422169 print("rethrow()\n");
2243− #imframe, i: ref Inst;
2244− #cr: ref Creloc;
2245− #frm, frf: ref Freloc;
2246−#
2247− #cr = getCreloc(RLOADER);
2248− #frm = getFreloc(cr, RSYS, nil, 0);
2249− #frf = getFreloc(cr, "raise", nil, 0);
2250−#
2170+ i: ref Inst;
2171+
2172+ # FIXME: is that needed?
22512173 #if(M.access_flags & ACC_SYNCHRONIZED && trylevel(J.pc) == 1)
22522174 # mon_or_throw(Jmonitorexit, M.access_flags, 0);
2253−#
2254− #imframe = loadermframe(frm, frf);
2255−#
2256− #i = newi(IMOVP);
2257− #addrsind(i.s, Amp, 0);
2258− #addrdind(i.d, Afpind, imframe.d.offset, REGSIZE);
2259− #LTIpatch(getFreloc(cr, RJEX, nil, 0), i, PSRC, PSIND);
2260−#
2261− #i = loadermcall(imframe, frf, frm);
2262− #relreg(imframe.d);
2263−#
2264− #return i;
2265− return nil;
2175+
2176+ i = newi(IRAISE);
2177+ addrsind(i.s, Afp, EXSTR);
2178+
2179+ return i;
22662180 }
22672181
22682182 #
@@ -2272,26 +2186,26 @@ rethrow(): ref Inst · 3 unmodified lines
22722186 catch()
22732187 {
22742188 print("catch()\n");
2275− #imframe, i: ref Inst;
2276− #cr: ref Creloc;
2277− #frm, frf: ref Freloc;
2278−#
2279− #cr = getCreloc(RLOADER);
2280− #frm = getFreloc(cr, RMP, nil, 0);
2281− #frf = getFreloc(cr, "culprit", nil, 0);
2282−#
2283− #imframe = loadermframe(frm, frf);
2284−#
2285− #i = newi(IMOVP);
2286− #addrsind(i.s, Afp, REFSYSEX);
2287− #addrdind(i.d, Afpind, imframe.d.offset, REGSIZE);
2288−#
2289− #i = newi(ILEA);
2290− #addrsind(i.s, Afp, EXOBJ); # put in known location
2291− #addrdind(i.d, Afpind, imframe.d.offset, REGRET*IBY2WD);
2292−#
2293− #loadermcall(imframe, frf, frm);
2294− #relreg(imframe.d);
2189+ imframe, i: ref Inst;
2190+ cr: ref Creloc;
2191+ frm, frf: ref Freloc;
2192+
2193+ cr = getCreloc(RLOADER);
2194+ frm = getFreloc(cr, RMP, nil, 0);
2195+ frf = getFreloc(cr, "culprit", nil, 0);
2196+
2197+ imframe = loadermframe(frm, frf);
2198+
2199+ i = newi(IMOVP);
2200+ addrsind(i.s, Afp, EXSTR);
2201+ addrdind(i.d, Afpind, imframe.d.offset, REGSIZE);
2202+
2203+ i = newi(ILEA);
2204+ addrsind(i.s, Afp, EXOBJ); # put in known location
2205+ addrdind(i.d, Afpind, imframe.d.offset, REGRET*IBY2WD);
2206+
2207+ loadermcall(imframe, frf, frm);
2208+ relreg(imframe.d);
22952209 }
22962210
22972211 #
@@ -2301,38 +2215,37 @@ catch() · 3 unmodified lines
23012215 chkhandler(exname: string): ref Addr
23022216 {
23032217 print("chkhandler()\n");
2304− #imframe, i: ref Inst;
2305− #rtc: ref RTClass;
2306− #cr: ref Creloc;
2307− #frm, frf: ref Freloc;
2308−#
2309− #rtc = getRTClass(exname);
2310− #callrtload(rtc, exname);
2311−#
2312− #cr = getCreloc(RLOADER);
2313− #frm = getFreloc(cr, RMP, nil, 0);
2314− #frf = getFreloc(cr, "instanceof", nil, 0);
2315−#
2316− #imframe = loadermframe(frm, frf);
2317−#
2318− #i = newi(IMOVP);
2319− #addrsind(i.s, Afp, EXOBJ);
2320− #addrdind(i.d, Afpind, imframe.d.offset, REGSIZE);
2321−#
2322− #i = newi(IMOVP);
2323− #addrsind(i.s, Amp, 0);
2324− #RTIpatch(getRTReloc(rtc, RADT, nil, 0), i, PSRC, PSIND);
2325− #addrdind(i.d, Afpind, imframe.d.offset, REGSIZE+IBY2WD);
2326−#
2327− #i = newi(ILEA);
2328− #addrsind(i.s, Afp, getreg(DIS_W));
2329− #addrdind(i.d, Afpind, imframe.d.offset, REGRET*IBY2WD);
2330−#
2331− #loadermcall(imframe, frf, frm);
2332− #relreg(imframe.d);
2333−#
2334− #return i.s;
2335− return nil;
2218+ imframe, i: ref Inst;
2219+ rtc: ref RTClass;
2220+ cr: ref Creloc;
2221+ frm, frf: ref Freloc;
2222+
2223+ rtc = getRTClass(exname);
2224+ callrtload(rtc, exname);
2225+
2226+ cr = getCreloc(RLOADER);
2227+ frm = getFreloc(cr, RMP, nil, 0);
2228+ frf = getFreloc(cr, "instanceof", nil, 0);
2229+
2230+ imframe = loadermframe(frm, frf);
2231+
2232+ i = newi(IMOVP);
2233+ addrsind(i.s, Afp, EXOBJ);
2234+ addrdind(i.d, Afpind, imframe.d.offset, REGSIZE);
2235+
2236+ i = newi(IMOVP);
2237+ addrsind(i.s, Amp, 0);
2238+ RTIpatch(getRTReloc(rtc, RADT, nil, 0), i, PSRC, PSIND);
2239+ addrdind(i.d, Afpind, imframe.d.offset, REGSIZE+IBY2WD);
2240+
2241+ i = newi(ILEA);
2242+ addrsind(i.s, Afp, getreg(DIS_W));
2243+ addrdind(i.d, Afpind, imframe.d.offset, REGRET*IBY2WD);
2244+
2245+ loadermcall(imframe, frf, frm);
2246+ relreg(imframe.d);
2247+
2248+ return i.s;
23362249 }
23372250
23382251 #
@@ -2342,21 +2255,13 @@ chkhandler(exname: string): ref Addr · 3 unmodified lines
23422255 trystart(t: ref Try)
23432256 {
23442257 print("trystart()\n");
2345− ibeqw, i: ref Inst;
2258+ ijmp, i: ref Inst;
23462259 a: ref Addr;
23472260 c: ref Catch;
23482261
2349− a = rescue();
2350−
2351− #ibeqw = newi(IBEQW);
2352− #*ibeqw.s = *a;
2353− #relreg(a);
2354− #addrimm(ibeqw.m, 0); # Sys.HANDLER == 0
2355− #addrimm(ibeqw.d, 0);
2356−
2357− # code executed when sys.rescue() "returns" Sys.EXCEPTION
2358−
2359− unrescue(1);
2262+ # always skip this block in normal flow
2263+ ijmp = newi(IJMP);
2264+ addrimm(ijmp.d, 0);
23602265
23612266 catch();
23622267
@@ -2374,14 +2279,16 @@ trystart(t: ref Try) · 11 unmodified lines
23742279 i = newi(IJMP);
23752280 addrimm(i.d, t.any_pc);
23762281 saveehinst(i);
2377− } #else
2378− # i = rethrow();
2282+ } else
2283+ i = rethrow();
23792284
2380− #ibeqw.d.ival = i.pc+1;
2285+ ijmp.d.ival = i.pc + 1;
2286+ t.any_pc = ijmp.pc + 1;
2287+ print("Generated handler block at %d - %d\n", ijmp.pc, i.pc);
23812288
23822289 #
2383− # rtload()'s done in Sys.EXCEPTION branch are not valid
2384− # for normal flow (Sys.HANDLER branch)
2290+ # rtload()'s done in handler branch are not valid
2291+ # for normal flow, as they are not executed
23852292 #
23862293 rtncache = 0;
23872294 }
@@ -2392,17 +2299,12 @@ trystart(t: ref Try) · 4 unmodified lines
23922299
23932300 tryhandling()
23942301 {
2395− #print("tryhandling()\n");
2396− #t: ref Try;
2397−#
2398− #for(t = trylist; t != nil; t = t.next) {
2399− # # > 1 try block can have the same start_pc
2400− # if(t.start_pc == J.pc)
2401− # trystart(t);
2402− # # try block may consist of just 1 bytecode
2403− # if(t.end_pc == J.pc+J.size)
2404− # callunrescue = 1;
2405− #}
2302+ for(t := trylist; t != nil; t = t.next) {
2303+ # FIXME
2304+ # > 1 try block can have the same start_pc
2305+ if(t.start_pc == J.pc)
2306+ trystart(t);
2307+ }
24062308 }
24072309
24082310 #
@@ -2436,7 +2338,7 @@ java2dis() · 27 unmodified lines
24362338 if(J.pc == 0 && M.access_flags & ACC_SYNCHRONIZED)
24372339 mon_or_throw(Jmonitorenter, M.access_flags, 0);
24382340
2439− #tryhandling();
2341+ tryhandling();
24402342
24412343 if(jix+J.size < code.code_length)
24422344 jnext = code.j[jix+J.size];
@@ -2828,16 +2730,12 @@ java2dis() · 385 unmodified lines
28282730 jix += J.size;
28292731 }
28302732 # handle exception handlers
2831− hbase := len class.handlers;
2832− newhandlers := array [len class.handlers + code.nex] of ref Handler;
2833− newhandlers[:] = class.handlers[:];
2834− class.handlers = newhandlers;
2835− for(i := 0; i < code.nex; i++) {
2836− h := code.ex[i];
2733+ for(t := trylist; t != nil; t = t.next) {
28372734 # by this time all the needed code is already translated, so we can
2838− # just convert pc's and append the entries to global handlers section
2839− class.handlers[hbase++] = ref Handler(instpc(h.start_pc), instpc(h.end_pc),
2840− instpc(h.handler_pc), 0);
2735+ # just convert pc's and prepend the entries to global handlers section
2736+ # the final list will be reverted, and that is what we need
2737+ class.handlers = ref Handler(instpc(t.start_pc), instpc(t.end_pc),
2738+ t.any_pc, 0) :: class.handlers;
28412739 }
28422740 J = nil;
28432741 }